Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 67 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| InCell | |
0.00% |
0 / 67 |
|
0.00% |
0 / 5 |
1190 | |
0.00% |
0 / 1 |
| characters | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| startTag | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
182 | |||
| endTag | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
306 | |||
| endDocument | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| closeTheCell | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| 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 cell" insertion mode |
| 10 | */ |
| 11 | class InCell extends InsertionMode { |
| 12 | private const EXPLICT_CLOSE_TAG = [ |
| 13 | 'td' => true, |
| 14 | 'th' => true, |
| 15 | ]; |
| 16 | |
| 17 | /** @inheritDoc */ |
| 18 | public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 19 | $this->dispatcher->inBody->characters( $text, $start, $length, |
| 20 | $sourceStart, $sourceLength ); |
| 21 | } |
| 22 | |
| 23 | /** @inheritDoc */ |
| 24 | public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 25 | switch ( $name ) { |
| 26 | case 'caption': |
| 27 | case 'col': |
| 28 | case 'colgroup': |
| 29 | case 'tbody': |
| 30 | case 'td': |
| 31 | case 'tfoot': |
| 32 | case 'th': |
| 33 | case 'thead': |
| 34 | case 'tr': |
| 35 | if ( !$this->builder->stack->isInTableScope( 'td' ) |
| 36 | && !$this->builder->stack->isInTableScope( 'th' ) |
| 37 | ) { |
| 38 | $this->builder->error( "<$name> tag should close the cell but none is in scope", |
| 39 | $sourceStart ); |
| 40 | return; |
| 41 | } |
| 42 | $this->closeTheCell( $sourceStart ) |
| 43 | ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 44 | break; |
| 45 | |
| 46 | default: |
| 47 | $this->dispatcher->inBody->startTag( |
| 48 | $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** @inheritDoc */ |
| 53 | public function endTag( $name, $sourceStart, $sourceLength ) { |
| 54 | $builder = $this->builder; |
| 55 | $stack = $builder->stack; |
| 56 | $dispatcher = $this->dispatcher; |
| 57 | |
| 58 | switch ( $name ) { |
| 59 | case 'td': |
| 60 | case 'th': |
| 61 | if ( !$stack->isInTableScope( $name ) ) { |
| 62 | $builder->error( "</$name> encountered but there is no $name in scope, ignoring", |
| 63 | $sourceStart ); |
| 64 | return; |
| 65 | } |
| 66 | $builder->generateImpliedEndTags( false, $sourceStart ); |
| 67 | if ( $stack->current->htmlName !== $name ) { |
| 68 | $builder->error( "</$name> encountered when there are tags open " . |
| 69 | "which can't be closed automatically", $sourceStart ); |
| 70 | } |
| 71 | $builder->popAllUpToName( $name, $sourceStart, $sourceLength ); |
| 72 | $builder->afe->clearToMarker(); |
| 73 | $dispatcher->switchMode( Dispatcher::IN_ROW ); |
| 74 | break; |
| 75 | |
| 76 | case 'body': |
| 77 | case 'caption': |
| 78 | case 'col': |
| 79 | case 'colgroup': |
| 80 | case 'html': |
| 81 | $builder->error( "unexpected </$name> in cell, ignoring", $sourceStart ); |
| 82 | return; |
| 83 | |
| 84 | case 'table': |
| 85 | case 'tbody': |
| 86 | case 'tfoot': |
| 87 | case 'thead': |
| 88 | case 'tr': |
| 89 | if ( !$stack->isInTableScope( $name ) ) { |
| 90 | $builder->error( "</$name> encountered but there is no $name in scope, ignoring", |
| 91 | $sourceStart ); |
| 92 | return; |
| 93 | } |
| 94 | $this->closeTheCell( $sourceStart ) |
| 95 | ->endTag( $name, $sourceStart, $sourceLength ); |
| 96 | break; |
| 97 | |
| 98 | default: |
| 99 | $dispatcher->inBody->endTag( $name, $sourceStart, $sourceLength ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** @inheritDoc */ |
| 104 | public function endDocument( $pos ) { |
| 105 | $this->dispatcher->inBody->endDocument( $pos ); |
| 106 | } |
| 107 | |
| 108 | private function closeTheCell( int $sourceStart ): InsertionMode { |
| 109 | $builder = $this->builder; |
| 110 | $stack = $builder->stack; |
| 111 | $builder->generateImpliedEndTags( false, $sourceStart ); |
| 112 | if ( !isset( self::EXPLICT_CLOSE_TAG[$stack->current->htmlName] ) ) { |
| 113 | $builder->error( "closing the cell but there are tags open " . |
| 114 | "which can't be closed automatically", $sourceStart ); |
| 115 | } |
| 116 | $builder->afe->clearToMarker(); |
| 117 | return $this->dispatcher->switchMode( Dispatcher::IN_ROW ); |
| 118 | } |
| 119 | } |