Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 82 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| InSelect | |
0.00% |
0 / 82 |
|
0.00% |
0 / 4 |
930 | |
0.00% |
0 / 1 |
| characters | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| startTag | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
272 | |||
| endTag | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
156 | |||
| 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 select" insertion mode |
| 10 | */ |
| 11 | class InSelect extends InsertionMode { |
| 12 | /** @inheritDoc */ |
| 13 | public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 14 | $this->stripNulls( |
| 15 | function ( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 16 | $this->builder->insertCharacters( $text, $start, $length, |
| 17 | $sourceStart, $sourceLength ); |
| 18 | }, |
| 19 | $text, $start, $length, $sourceStart, $sourceLength ); |
| 20 | } |
| 21 | |
| 22 | /** @inheritDoc */ |
| 23 | public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 24 | $builder = $this->builder; |
| 25 | $stack = $builder->stack; |
| 26 | $dispatcher = $this->dispatcher; |
| 27 | |
| 28 | switch ( $name ) { |
| 29 | case 'html': |
| 30 | $dispatcher->inBody->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 31 | break; |
| 32 | |
| 33 | case 'option': |
| 34 | if ( $stack->current->htmlName === 'option' ) { |
| 35 | $builder->pop( $sourceStart, 0 ); |
| 36 | } |
| 37 | $builder->insertElement( 'option', $attrs, false, $sourceStart, $sourceLength ); |
| 38 | break; |
| 39 | |
| 40 | case 'optgroup': |
| 41 | if ( $stack->current->htmlName === 'option' ) { |
| 42 | $builder->pop( $sourceStart, 0 ); |
| 43 | } |
| 44 | if ( $stack->current->htmlName === 'optgroup' ) { |
| 45 | $builder->pop( $sourceStart, 0 ); |
| 46 | } |
| 47 | $builder->insertElement( 'optgroup', $attrs, false, $sourceStart, $sourceLength ); |
| 48 | break; |
| 49 | |
| 50 | case 'select': |
| 51 | if ( !$stack->isInSelectScope( 'select' ) ) { |
| 52 | $builder->error( "<select> found in select mode but no select element is in " . |
| 53 | "scope, ignoring", $sourceStart ); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $builder->error( "<select> found inside a select element", $sourceStart ); |
| 58 | $builder->popAllUpToName( 'select', $sourceStart, $sourceLength ); |
| 59 | $dispatcher->reset(); |
| 60 | break; |
| 61 | |
| 62 | case 'input': |
| 63 | case 'keygen': |
| 64 | case 'textarea': |
| 65 | $builder->error( "<$name> found inside a select element", $sourceStart ); |
| 66 | if ( !$stack->isInSelectScope( 'select' ) ) { |
| 67 | // Ignore |
| 68 | return; |
| 69 | } |
| 70 | $builder->popAllUpToName( 'select', $sourceStart, 0 ); |
| 71 | $dispatcher->reset() |
| 72 | ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 73 | break; |
| 74 | |
| 75 | case 'script': |
| 76 | case 'template': |
| 77 | $dispatcher->inHead->startTag( $name, $attrs, $selfClose, |
| 78 | $sourceStart, $sourceLength ); |
| 79 | break; |
| 80 | |
| 81 | default: |
| 82 | $builder->error( "<$name> found inside a select element, ignoring", $sourceStart ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** @inheritDoc */ |
| 87 | public function endTag( $name, $sourceStart, $sourceLength ) { |
| 88 | $builder = $this->builder; |
| 89 | $stack = $builder->stack; |
| 90 | $dispatcher = $this->dispatcher; |
| 91 | |
| 92 | switch ( $name ) { |
| 93 | case 'optgroup': |
| 94 | if ( $stack->current->htmlName === 'option' ) { |
| 95 | $penultimate = $stack->item( $stack->length() - 2 ); |
| 96 | if ( $penultimate && $penultimate->htmlName === 'optgroup' ) { |
| 97 | $builder->pop( $sourceStart, 0 ); |
| 98 | } |
| 99 | } |
| 100 | if ( $stack->current->htmlName !== 'optgroup' ) { |
| 101 | $builder->error( "unexpected </optgroup>, ignoring", $sourceStart ); |
| 102 | return; |
| 103 | } |
| 104 | $builder->pop( $sourceStart, $sourceLength ); |
| 105 | break; |
| 106 | |
| 107 | case 'option': |
| 108 | if ( $stack->current->htmlName !== 'option' ) { |
| 109 | $builder->error( "unexpected </option>, ignoring", $sourceStart ); |
| 110 | return; |
| 111 | } |
| 112 | $builder->pop( $sourceStart, $sourceLength ); |
| 113 | break; |
| 114 | |
| 115 | case 'select': |
| 116 | if ( !$stack->isInSelectScope( 'select' ) ) { |
| 117 | $builder->error( "</select> found but the select element is " . |
| 118 | "not in scope", $sourceStart ); |
| 119 | return; |
| 120 | } |
| 121 | $builder->popAllUpToName( 'select', $sourceStart, $sourceLength ); |
| 122 | $dispatcher->reset(); |
| 123 | break; |
| 124 | |
| 125 | case 'template': |
| 126 | $dispatcher->inHead->endTag( $name, $sourceStart, $sourceLength ); |
| 127 | break; |
| 128 | |
| 129 | default: |
| 130 | $builder->error( "unexpected </$name> in select, ignoring", $sourceStart ); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** @inheritDoc */ |
| 136 | public function endDocument( $pos ) { |
| 137 | $this->dispatcher->inBody->endDocument( $pos ); |
| 138 | } |
| 139 | } |