Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 76 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| InRow | |
0.00% |
0 / 76 |
|
0.00% |
0 / 4 |
1056 | |
0.00% |
0 / 1 |
| characters | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| startTag | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
156 | |||
| endTag | |
0.00% |
0 / 45 |
|
0.00% |
0 / 1 |
342 | |||
| 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 | |
| 8 | /** |
| 9 | * The "in row" insertion mode |
| 10 | */ |
| 11 | class InRow extends InsertionMode { |
| 12 | private const TABLE_ROW_CONTEXT = [ |
| 13 | 'tr' => true, |
| 14 | 'template' => true, |
| 15 | 'html' => true, |
| 16 | ]; |
| 17 | |
| 18 | /** @inheritDoc */ |
| 19 | public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 20 | $this->dispatcher->inTable->characters( $text, $start, $length, |
| 21 | $sourceStart, $sourceLength ); |
| 22 | } |
| 23 | |
| 24 | /** @inheritDoc */ |
| 25 | public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 26 | $builder = $this->builder; |
| 27 | $stack = $builder->stack; |
| 28 | $dispatcher = $this->dispatcher; |
| 29 | |
| 30 | switch ( $name ) { |
| 31 | case 'th': |
| 32 | case 'td': |
| 33 | $builder->clearStackBack( self::TABLE_ROW_CONTEXT, $sourceStart ); |
| 34 | $builder->insertElement( $name, $attrs, false, $sourceStart, $sourceLength ); |
| 35 | $dispatcher->switchMode( Dispatcher::IN_CELL ); |
| 36 | $builder->afe->insertMarker(); |
| 37 | break; |
| 38 | |
| 39 | case 'caption': |
| 40 | case 'col': |
| 41 | case 'colgroup': |
| 42 | case 'tbody': |
| 43 | case 'tfoot': |
| 44 | case 'thead': |
| 45 | case 'tr': |
| 46 | if ( !$stack->isInTableScope( 'tr' ) ) { |
| 47 | $builder->error( "<$name> should close the tr but it is not in scope", |
| 48 | $sourceStart ); |
| 49 | // Ignore |
| 50 | return; |
| 51 | } |
| 52 | $builder->clearStackBack( self::TABLE_ROW_CONTEXT, $sourceStart ); |
| 53 | $builder->pop( $sourceStart, 0 ); |
| 54 | $dispatcher->switchMode( Dispatcher::IN_TABLE_BODY ) |
| 55 | ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 56 | break; |
| 57 | |
| 58 | default: |
| 59 | $dispatcher->inTable->startTag( $name, $attrs, $selfClose, |
| 60 | $sourceStart, $sourceLength ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** @inheritDoc */ |
| 65 | public function endTag( $name, $sourceStart, $sourceLength ) { |
| 66 | $builder = $this->builder; |
| 67 | $stack = $builder->stack; |
| 68 | $dispatcher = $this->dispatcher; |
| 69 | |
| 70 | switch ( $name ) { |
| 71 | case 'tr': |
| 72 | if ( !$stack->isInTableScope( 'tr' ) ) { |
| 73 | $builder->error( '</tr> found but no tr element in scope', $sourceStart ); |
| 74 | // Ignore |
| 75 | return; |
| 76 | } |
| 77 | $builder->clearStackBack( self::TABLE_ROW_CONTEXT, $sourceStart ); |
| 78 | $builder->pop( $sourceStart, $sourceLength ); |
| 79 | $dispatcher->switchMode( Dispatcher::IN_TABLE_BODY ); |
| 80 | break; |
| 81 | |
| 82 | case 'table': |
| 83 | if ( !$stack->isInTableScope( 'tr' ) ) { |
| 84 | $builder->error( "</table> should close the tr but it is not in scope", |
| 85 | $sourceStart ); |
| 86 | // Ignore |
| 87 | return; |
| 88 | } |
| 89 | $builder->clearStackBack( self::TABLE_ROW_CONTEXT, $sourceStart ); |
| 90 | $builder->pop( $sourceStart, 0 ); |
| 91 | $dispatcher->switchMode( Dispatcher::IN_TABLE_BODY ) |
| 92 | ->endTag( $name, $sourceStart, $sourceLength ); |
| 93 | break; |
| 94 | |
| 95 | case 'tbody': |
| 96 | case 'tfoot': |
| 97 | case 'thead': |
| 98 | if ( !$stack->isInTableScope( $name ) ) { |
| 99 | $builder->error( "</$name> encountered but there is no $name element in scope", |
| 100 | $sourceStart ); |
| 101 | return; |
| 102 | } |
| 103 | if ( !$stack->isInTableScope( 'tr' ) ) { |
| 104 | return; |
| 105 | } |
| 106 | $builder->clearStackBack( self::TABLE_ROW_CONTEXT, $sourceStart ); |
| 107 | $builder->pop( $sourceStart, 0 ); |
| 108 | $dispatcher->switchMode( Dispatcher::IN_TABLE_BODY ) |
| 109 | ->endTag( $name, $sourceStart, $sourceLength ); |
| 110 | break; |
| 111 | |
| 112 | case 'body': |
| 113 | case 'caption': |
| 114 | case 'col': |
| 115 | case 'colgroup': |
| 116 | case 'html': |
| 117 | case 'td': |
| 118 | case 'th': |
| 119 | $builder->error( "</$name> encountered in row mode, ignoring", $sourceStart ); |
| 120 | return; |
| 121 | |
| 122 | default: |
| 123 | $dispatcher->inTable->endTag( $name, $sourceStart, $sourceLength ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** @inheritDoc */ |
| 128 | public function endDocument( $pos ) { |
| 129 | $this->dispatcher->inTable->endDocument( $pos ); |
| 130 | } |
| 131 | } |