Debian Codegraph @

vendor/wikimedia/remex-html/src/TreeBuilder/InSelect.php

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