| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\TreeBuilder; |
| 4 |
|
| 5 |
use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 6 |
use Wikimedia\RemexHtml\Tokenizer\PlainAttributes; |
| 7 |
|
| 8 |
/** |
| 9 |
* The "in table body" insertion mode |
| 10 |
*/ |
| 11 |
class InTableBody extends InsertionMode { |
| 12 |
private const TABLE_BODY_CONTEXT = [ |
| 13 |
'tbody' => true, |
| 14 |
'tfoot' => true, |
| 15 |
'thead' => true, |
| 16 |
'template' => true, |
| 17 |
'html' => true |
| 18 |
]; |
| 19 |
|
| 20 |
/** @inheritDoc */ |
| 21 |
public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 22 |
$this->dispatcher->inTable->characters( $text, $start, $length, |
| 23 |
$sourceStart, $sourceLength ); |
| 24 |
} |
| 25 |
|
| 26 |
/** @inheritDoc */ |
| 27 |
public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 28 |
$builder = $this->builder; |
| 29 |
$stack = $builder->stack; |
| 30 |
$dispatcher = $this->dispatcher; |
| 31 |
|
| 32 |
switch ( $name ) { |
| 33 |
case 'tr': |
| 34 |
$builder->clearStackBack( self::TABLE_BODY_CONTEXT, $sourceStart ); |
| 35 |
$builder->insertElement( $name, $attrs, false, $sourceStart, $sourceLength ); |
| 36 |
$dispatcher->switchMode( Dispatcher::IN_ROW ); |
| 37 |
break; |
| 38 |
|
| 39 |
case 'th': |
| 40 |
case 'td': |
| 41 |
$builder->error( "<$name> encountered in table body (not row) mode", $sourceStart ); |
| 42 |
$builder->clearStackBack( self::TABLE_BODY_CONTEXT, $sourceStart ); |
| 43 |
$builder->insertElement( 'tr', new PlainAttributes, false, $sourceStart, 0 ); |
| 44 |
$dispatcher->switchMode( Dispatcher::IN_ROW ) |
| 45 |
->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 46 |
break; |
| 47 |
|
| 48 |
case 'caption': |
| 49 |
case 'col': |
| 50 |
case 'colgroup': |
| 51 |
case 'tbody': |
| 52 |
case 'tfoot': |
| 53 |
case 'thead': |
| 54 |
if ( !$stack->isInTableScope( 'tbody' ) |
| 55 |
&& !$stack->isInTableScope( 'thead' ) |
| 56 |
&& !$stack->isInTableScope( 'tfoot' ) |
| 57 |
) { |
| 58 |
$builder->error( "<$name> encountered in table body mode " . |
| 59 |
"when there is no tbody/thead/tfoot in scope", $sourceStart ); |
| 60 |
return; |
| 61 |
} |
| 62 |
$builder->clearStackBack( self::TABLE_BODY_CONTEXT, $sourceStart ); |
| 63 |
$builder->pop( $sourceStart, 0 ); |
| 64 |
$dispatcher->switchMode( Dispatcher::IN_TABLE ) |
| 65 |
->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 66 |
break; |
| 67 |
|
| 68 |
default: |
| 69 |
$dispatcher->inTable->startTag( $name, $attrs, $selfClose, |
| 70 |
$sourceStart, $sourceLength ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** @inheritDoc */ |
| 75 |
public function endTag( $name, $sourceStart, $sourceLength ) { |
| 76 |
$builder = $this->builder; |
| 77 |
$stack = $builder->stack; |
| 78 |
$dispatcher = $this->dispatcher; |
| 79 |
|
| 80 |
switch ( $name ) { |
| 81 |
case 'tbody': |
| 82 |
case 'tfoot': |
| 83 |
case 'thead': |
| 84 |
if ( !$stack->isInTableScope( $name ) ) { |
| 85 |
$builder->error( "</$name> found but no $name in scope", $sourceStart ); |
| 86 |
return; |
| 87 |
} |
| 88 |
$builder->clearStackBack( self::TABLE_BODY_CONTEXT, $sourceStart ); |
| 89 |
$builder->pop( $sourceStart, $sourceLength ); |
| 90 |
$dispatcher->switchMode( Dispatcher::IN_TABLE ); |
| 91 |
break; |
| 92 |
|
| 93 |
case 'body': |
| 94 |
case 'caption': |
| 95 |
case 'col': |
| 96 |
case 'colgroup': |
| 97 |
case 'html': |
| 98 |
case 'td': |
| 99 |
case 'th': |
| 100 |
case 'tr': |
| 101 |
$builder->error( "</$name> found in table body mode, ignoring", $sourceStart ); |
| 102 |
return; |
| 103 |
|
| 104 |
default: |
| 105 |
$dispatcher->inTable->endTag( $name, $sourceStart, $sourceLength ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** @inheritDoc */ |
| 110 |
public function endDocument( $pos ) { |
| 111 |
$this->dispatcher->inTable->endDocument( $pos ); |
| 112 |
} |
| 113 |
} |