Debian Codegraph @

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

raw
1 <?php
2 namespace Wikimedia\RemexHtml\TreeBuilder;
3
4 use Wikimedia\RemexHtml\HTMLData;
5
6 /**
7 * An implementation of the "stack of open elements" which, unlike CachingStack,
8 * iterates through the stack in order to answer queries about which elements
9 * are in scope. This is presumably faster for best case input.
10 */
11 class SimpleStack extends Stack {
12 /** @var array<int,Element> */
13 private $elements;
14
15 /**
16 * A 2-d array giving the element types which break a scope region for the
17 * default scope, i.e. the one for phrases of the form "has an X element
18 * in scope".
19 */
20 private const DEFAULT_SCOPE = [
21 HTMLData::NS_HTML => [
22 'applet' => true,
23 'caption' => true,
24 'html' => true,
25 'table' => true,
26 'td' => true,
27 'th' => true,
28 'marquee' => true,
29 'object' => true,
30 'template' => true,
31 ],
32 HTMLData::NS_MATHML => [
33 'mi' => true,
34 'mo' => true,
35 'mn' => true,
36 'ms' => true,
37 'mtext' => true,
38 'annotation-xml' => true,
39 ],
40 HTMLData::NS_SVG => [
41 'foreignObject' => true,
42 'desc' => true,
43 'title' => true,
44 ],
45 ];
46
47 /**
48 * The element types which break the table scope.
49 */
50 private const TABLE_SCOPE = [
51 HTMLData::NS_HTML => [
52 'html' => true,
53 'table' => true,
54 'template' => true,
55 ]
56 ];
57
58 /**
59 * The element types which break the list scope. This is lazy-initialised.
60 * @var array<string,array<string,bool>>
61 */
62 private static $listScope;
63
64 /**
65 * The element types which break the button scope. This is lazy-initialised.
66 * @var array<string,array<string,bool>>
67 */
68 private static $buttonScope;
69
70 /** @inheritDoc */
71 public function push( Element $elt ) {
72 $n = count( $this->elements );
73 $this->elements[$n] = $elt;
74 $this->current = $elt;
75 $elt->stackIndex = $n;
76 }
77
78 /** @inheritDoc */
79 public function pop() {
80 $elt = array_pop( $this->elements );
81 $elt->stackIndex = null;
82 $n = count( $this->elements );
83 $this->current = $n ? $this->elements[$n - 1] : null;
84 return $elt;
85 }
86
87 /** @inheritDoc */
88 public function replace( Element $oldElt, Element $elt ) {
89 $idx = $oldElt->stackIndex;
90 $this->elements[$idx] = $elt;
91 $oldElt->stackIndex = null;
92 $elt->stackIndex = $idx;
93 if ( $idx === count( $this->elements ) - 1 ) {
94 $this->current = $elt;
95 }
96 }
97
98 /** @inheritDoc */
99 public function remove( Element $elt ) {
100 $eltIndex = $elt->stackIndex;
101 $n = count( $this->elements );
102 for ( $i = $eltIndex + 1; $i < $n; $i++ ) {
103 $this->elements[$i]->stackIndex--;
104 }
105 $elt->stackIndex = null;
106 }
107
108 /** @inheritDoc */
109 public function isInScope( $name ) {
110 return $this->isInSpecificScope( $name, self::DEFAULT_SCOPE );
111 }
112
113 /** @inheritDoc */
114 public function isElementInScope( Element $elt ) {
115 for ( $i = count( $this->elements ) - 1; $i >= 0; $i-- ) {
116 $node = $this->elements[$i];
117 if ( $node === $elt ) {
118 return true;
119 }
120 if ( isset( self::DEFAULT_SCOPE[$node->namespace][$node->name] ) ) {
121 return false;
122 }
123 }
124 return false;
125 }
126
127 /** @inheritDoc */
128 public function isOneOfSetInScope( $names ) {
129 for ( $i = count( $this->elements ) - 1; $i >= 0; $i-- ) {
130 $node = $this->elements[$i];
131 if ( $node->namespace === HTMLData::NS_HTML && isset( $names[$node->name] ) ) {
132 return true;
133 }
134 if ( isset( self::DEFAULT_SCOPE[$node->namespace][$node->name] ) ) {
135 return false;
136 }
137 }
138 return false;
139 }
140
141 /** @inheritDoc */
142 public function isInListScope( $name ) {
143 if ( self::$listScope === null ) {
144 self::$listScope = self::DEFAULT_SCOPE;
145 self::$listScope[HTMLData::NS_HTML] += [
146 'ol' => true,
147 'li' => true
148 ];
149 }
150 return $this->isInSpecificScope( $name, self::$listScope );
151 }
152
153 /** @inheritDoc */
154 public function isInButtonScope( $name ) {
155 if ( self::$buttonScope === null ) {
156 self::$buttonScope = self::DEFAULT_SCOPE;
157 self::$buttonScope[HTMLData::NS_HTML]['button'] = true;
158 }
159 return $this->isInSpecificScope( $name, self::$buttonScope );
160 }
161
162 /** @inheritDoc */
163 public function isInTableScope( $name ) {
164 return $this->isInSpecificScope( $name, self::TABLE_SCOPE );
165 }
166
167 /** @inheritDoc */
168 public function isInSelectScope( $name ) {
169 for ( $i = count( $this->elements ) - 1; $i >= 0; $i-- ) {
170 $node = $this->elements[$i];
171 if ( $node->namespace === HTMLData::NS_HTML && $node->name === $name ) {
172 return true;
173 }
174 if ( $node->namespace !== HTMLData::NS_HTML ) {
175 return false;
176 }
177 if ( $node->name !== 'optgroup' && $node->name !== 'option' ) {
178 return false;
179 }
180 }
181 return false;
182 }
183
184 private function isInSpecificScope( string $name, array $set ): bool {
185 for ( $i = count( $this->elements ) - 1; $i >= 0; $i-- ) {
186 $node = $this->elements[$i];
187 if ( $node->namespace === HTMLData::NS_HTML && $node->name === $name ) {
188 return true;
189 }
190 if ( isset( $set[$node->namespace][$node->name] ) ) {
191 return false;
192 }
193 }
194 return false;
195 }
196
197 /** @inheritDoc */
198 public function item( $idx ) {
199 return $this->elements[$idx];
200 }
201
202 /** @inheritDoc */
203 public function length() {
204 return count( $this->elements );
205 }
206
207 /** @inheritDoc */
208 public function hasTemplate() {
209 foreach ( $this->elements as $elt ) {
210 if ( $elt->namespace === HTMLData::NS_HTML && $elt->name === 'template' ) {
211 return true;
212 }
213 }
214 return false;
215 }
216 }