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