Debian Codegraph @

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

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\TreeBuilder;
4
5 use Wikimedia\RemexHtml\HTMLData;
6 use Wikimedia\RemexHtml\Tokenizer\Attributes;
7 use Wikimedia\RemexHtml\Tokenizer\TokenHandler;
8 use Wikimedia\RemexHtml\Tokenizer\Tokenizer;
9
10 /**
11 * This is the approximate equivalent of the "tree construction dispatcher" in
12 * the spec. It receives token events and distributes them to the appropriate
13 * insertion mode class. It also implements some things specific to the
14 * dispatcher state:
15 * - "Reset the insertion mode appropriately"
16 * - The stack of template insertion modes
17 * - The "original insertion mode"
18 */
19 class Dispatcher implements TokenHandler {
20 /**
21 * The insertion mode indexes
22 */
23 public const INITIAL = 1;
24 public const BEFORE_HTML = 2;
25 public const BEFORE_HEAD = 3;
26 public const IN_HEAD = 4;
27 public const IN_HEAD_NOSCRIPT = 5;
28 public const AFTER_HEAD = 6;
29 public const IN_BODY = 7;
30 public const TEXT = 8;
31 public const IN_TABLE = 9;
32 public const IN_TABLE_TEXT = 10;
33 public const IN_CAPTION = 11;
34 public const IN_COLUMN_GROUP = 12;
35 public const IN_TABLE_BODY = 13;
36 public const IN_ROW = 14;
37 public const IN_CELL = 15;
38 public const IN_SELECT = 16;
39 public const IN_SELECT_IN_TABLE = 17;
40 public const IN_TEMPLATE = 18;
41 public const AFTER_BODY = 19;
42 public const IN_FRAMESET = 20;
43 public const AFTER_FRAMESET = 21;
44 public const AFTER_AFTER_BODY = 22;
45 public const AFTER_AFTER_FRAMESET = 23;
46 public const IN_FOREIGN_CONTENT = 24;
47 public const IN_PRE = 25;
48 public const IN_TEXTAREA = 26;
49
50 /**
51 * The handler class for each insertion mode
52 */
53 protected const HANDLER_CLASSES = [
54 self::INITIAL => Initial::class,
55 self::BEFORE_HTML => BeforeHtml::class,
56 self::BEFORE_HEAD => BeforeHead::class,
57 self::IN_HEAD => InHead::class,
58 self::IN_HEAD_NOSCRIPT => InHeadNoscript::class,
59 self::AFTER_HEAD => AfterHead::class,
60 self::IN_BODY => InBody::class,
61 self::TEXT => Text::class,
62 self::IN_TABLE => InTable::class,
63 self::IN_TABLE_TEXT => InTableText::class,
64 self::IN_CAPTION => InCaption::class,
65 self::IN_COLUMN_GROUP => InColumnGroup::class,
66 self::IN_TABLE_BODY => InTableBody::class,
67 self::IN_ROW => InRow::class,
68 self::IN_CELL => InCell::class,
69 self::IN_SELECT => InSelect::class,
70 self::IN_SELECT_IN_TABLE => InSelectInTable::class,
71 self::IN_TEMPLATE => InTemplate::class,
72 self::AFTER_BODY => AfterBody::class,
73 self::IN_FRAMESET => InFrameset::class,
74 self::AFTER_FRAMESET => AfterFrameset::class,
75 self::AFTER_AFTER_BODY => AfterAfterBody::class,
76 self::AFTER_AFTER_FRAMESET => AfterAfterFrameset::class,
77 self::IN_FOREIGN_CONTENT => InForeignContent::class,
78 self::IN_PRE => InPre::class,
79 self::IN_TEXTAREA => InTextarea::class,
80 ];
81
82 // Public shortcuts for "using the rules for" actions
83
84 /** @var InHead */
85 public $inHead;
86 /** @var InBody */
87 public $inBody;
88 /** @var InTable */
89 public $inTable;
90 /** @var InSelect */
91 public $inSelect;
92 /** @var InTemplate */
93 public $inTemplate;
94 /** @var InForeignContent */
95 public $inForeign;
96
97 protected TreeBuilder $builder;
98
99 /**
100 * The InsertionMode object for the current insertion mode in HTML content
101 *
102 * @var InsertionMode
103 */
104 protected $handler;
105
106 /**
107 * An array mapping insertion mode indexes to InsertionMode objects
108 *
109 * @var InsertionMode[]
110 */
111 protected $dispatchTable;
112
113 /**
114 * The insertion mode index
115 *
116 * @var int
117 */
118 protected $mode;
119
120 /**
121 * The "original insertion mode" index
122 *
123 * @var ?int
124 */
125 protected $originalMode;
126
127 /**
128 * The insertion mode sets this to true to acknowledge the tag's
129 * self-closing flag.
130 *
131 * @var bool|null
132 */
133 public $ack;
134
135 /**
136 * The stack of template insertion modes
137 */
138 public TemplateModeStack $templateModeStack;
139
140 /**
141 * @param TreeBuilder $builder
142 */
143 public function __construct( TreeBuilder $builder ) {
144 $this->builder = $builder;
145 $this->templateModeStack = new TemplateModeStack;
146 }
147
148 /**
149 * Switch the insertion mode, and return the new handler
150 *
151 * @param int $mode
152 * @return InsertionMode
153 */
154 public function switchMode( $mode ) {
155 $this->mode = $mode;
156 $this->handler = $this->dispatchTable[$mode];
157 return $this->handler;
158 }
159
160 /**
161 * Let the original insertion mode be the current insertion mode, and
162 * switch the insertion mode to some new value. Return the new handler.
163 *
164 * @param int $mode
165 * @return InsertionMode
166 */
167 public function switchAndSave( $mode ) {
168 $this->originalMode = $this->mode;
169 $this->mode = $mode;
170 $this->handler = $this->dispatchTable[$mode];
171 return $this->handler;
172 }
173
174 /**
175 * Switch the insertion mode to the original insertion mode and return the
176 * new handler.
177 *
178 * @return InsertionMode
179 */
180 public function restoreMode() {
181 if ( $this->originalMode === null ) {
182 throw new TreeBuilderError( "original insertion mode is not set" );
183 }
184 $mode = $this->mode = $this->originalMode;
185 $this->originalMode = null;
186 $this->handler = $this->dispatchTable[$mode];
187 return $this->handler;
188 }
189
190 /**
191 * Get the handler for the current insertion mode in HTML content.
192 * This is used by the "in foreign" handler to execute the HTML insertion
193 * mode. It does not necessarily correspond to the handler currently being
194 * executed.
195 *
196 * @return InsertionMode
197 */
198 public function getHandler() {
199 return $this->handler;
200 }
201
202 /**
203 * True if we are in a table mode, for the purposes of switching to
204 * IN_SELECT_IN_TABLE as opposed to IN_SELECT.
205 *
206 * @return bool
207 */
208 public function isInTableMode() {
209 static $tableModes = [
210 self::IN_TABLE => true,
211 self::IN_CAPTION => true,
212 self::IN_TABLE_BODY => true,
213 self::IN_ROW => true,
214 self::IN_CELL => true ];
215 return isset( $tableModes[$this->mode] );
216 }
217
218 /**
219 * If the insertion mode is "in table text", flush the pending table text.
220 * This is a facility allowing users to insert into the DOM more cleanly.
221 */
222 public function flushTableText() {
223 if ( $this->mode === self::IN_TABLE_TEXT
224 && $this->handler instanceof InTableText
225 ) {
226 $this->handler->flush();
227 }
228 }
229
230 /**
231 * Reset the insertion mode appropriately, and return the new handler.
232 *
233 * @return InsertionMode
234 */
235 public function reset() {
236 return $this->switchMode( $this->getAppropriateMode() );
237 }
238
239 /**
240 * Get the insertion mode index which is switched to when we reset the
241 * insertion mode appropriately.
242 *
243 * @return int
244 */
245 protected function getAppropriateMode() {
246 $builder = $this->builder;
247 $stack = $builder->stack;
248 $last = false;
249 for ( $idx = $stack->length() - 1; $idx >= 0; $idx-- ) {
250 $node = $stack->item( $idx );
251 if ( $idx === 0 ) {
252 $last = true;
253 if ( $builder->isFragment ) {
254 $node = $builder->fragmentContext;
255 }
256 }
257
258 switch ( $node->htmlName ) {
259 case 'select':
260 if ( $last ) {
261 return self::IN_SELECT;
262 }
263 for ( $ancestorIdx = $idx - 1; $ancestorIdx >= 1; $ancestorIdx-- ) {
264 $ancestor = $stack->item( $ancestorIdx );
265 if ( $ancestor->htmlName === 'template' ) {
266 return self::IN_SELECT;
267 } elseif ( $ancestor->htmlName === 'table' ) {
268 return self::IN_SELECT_IN_TABLE;
269 }
270 }
271 return self::IN_SELECT;
272
273 case 'td':
274 case 'th':
275 if ( !$last ) {
276 return self::IN_CELL;
277 }
278 break;
279
280 case 'tr':
281 return self::IN_ROW;
282
283 case 'tbody':
284 case 'thead':
285 case 'tfoot':
286 return self::IN_TABLE_BODY;
287
288 case 'caption':
289 return self::IN_CAPTION;
290
291 case 'colgroup':
292 return self::IN_COLUMN_GROUP;
293
294 case 'table':
295 return self::IN_TABLE;
296
297 case 'template':
298 return $this->templateModeStack->current;
299
300 case 'head':
301 if ( $last ) {
302 return self::IN_BODY;
303 } else {
304 return self::IN_HEAD;
305 }
306
307 case 'body':
308 return self::IN_BODY;
309
310 case 'frameset':
311 return self::IN_FRAMESET;
312
313 case 'html':
314 if ( $builder->headElement === null ) {
315 return self::BEFORE_HEAD;
316 } else {
317 return self::AFTER_HEAD;
318 }
319 }
320 }
321
322 return self::IN_BODY;
323 }
324
325 /**
326 * If the stack of open elements is empty, return null, otherwise return
327 * the adjusted current node.
328 * @return Element|null
329 */
330 protected function dispatcherCurrentNode() {
331 $current = $this->builder->stack->current;
332 if ( $current && $current->stackIndex === 0 && $this->builder->isFragment ) {
333 return $this->builder->fragmentContext;
334 } else {
335 return $current;
336 }
337 }
338
339 /** @inheritDoc */
340 public function startDocument( Tokenizer $tokenizer, $namespace, $name ) {
341 $this->dispatchTable = [];
342 foreach ( self::HANDLER_CLASSES as $mode => $class ) {
343 $this->dispatchTable[$mode] = new $class( $this->builder, $this );
344 }
345
346 $this->inHead = $this->dispatchTable[self::IN_HEAD];
347 $this->inBody = $this->dispatchTable[self::IN_BODY];
348 $this->inTable = $this->dispatchTable[self::IN_TABLE];
349 $this->inSelect = $this->dispatchTable[self::IN_SELECT];
350 $this->inTemplate = $this->dispatchTable[self::IN_TEMPLATE];
351 $this->inForeign = $this->dispatchTable[self::IN_FOREIGN_CONTENT];
352
353 $this->switchMode( self::INITIAL );
354
355 $this->builder->startDocument( $tokenizer, $namespace, $name );
356 if ( $namespace !== null ) {
357 if ( $namespace === HTMLData::NS_HTML && $name === 'template' ) {
358 $this->templateModeStack->push( self::IN_TEMPLATE );
359 }
360 $this->reset();
361 }
362 }
363
364 /**
365 * @inheritDoc
366 * @suppress PhanTypeMismatchPropertyProbablyReal Clears references to null
367 */
368 public function endDocument( $pos ) {
369 $this->handler->endDocument( $pos );
370
371 // All references to insertion modes must be explicitly released, since
372 // they have a circular reference back to $this
373 $this->dispatchTable = null;
374 $this->handler = null;
375 $this->inHead = null;
376 $this->inBody = null;
377 $this->inTable = null;
378 $this->inSelect = null;
379 $this->inTemplate = null;
380 $this->inForeign = null;
381 }
382
383 /** @inheritDoc */
384 public function error( $text, $pos ) {
385 $this->builder->error( $text, $pos );
386 }
387
388 /** @inheritDoc */
389 public function characters( $text, $start, $length, $sourceStart, $sourceLength ) {
390 $current = $this->dispatcherCurrentNode();
391 if ( !$current
392 || $current->namespace === HTMLData::NS_HTML
393 || $current->isMathmlTextIntegration()
394 || $current->isHtmlIntegration()
395 ) {
396 $this->handler->characters( $text, $start, $length, $sourceStart, $sourceLength );
397 } else {
398 $this->inForeign->characters(
399 $text, $start, $length, $sourceStart, $sourceLength );
400 }
401 }
402
403 /** @inheritDoc */
404 public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) {
405 $this->ack = false;
406 $current = $this->dispatcherCurrentNode();
407 if ( !$current
408 || $current->namespace === HTMLData::NS_HTML
409 || ( $current->isMathmlTextIntegration()
410 && $name !== 'mglyph'
411 && $name !== 'malignmark'
412 )
413 || ( $name === 'svg'
414 && $current->namespace === HTMLData::NS_MATHML
415 && $current->name === 'annotation-xml'
416 )
417 || $current->isHtmlIntegration()
418 ) {
419 $this->handler->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
420 } else {
421 $this->inForeign->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
422 }
423 if ( $selfClose && !$this->ack ) {
424 $this->builder->error( "unacknowledged self-closing tag", $sourceStart );
425 }
426 }
427
428 /** @inheritDoc */
429 public function endTag( $name, $sourceStart, $sourceLength ) {
430 $current = $this->dispatcherCurrentNode();
431 if ( !$current || $current->namespace === HTMLData::NS_HTML ) {
432 $this->handler->endTag( $name, $sourceStart, $sourceLength );
433 } else {
434 $this->inForeign->endTag( $name, $sourceStart, $sourceLength );
435 }
436 }
437
438 /** @inheritDoc */
439 public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) {
440 $current = $this->dispatcherCurrentNode();
441 if ( !$current || $current->namespace === HTMLData::NS_HTML ) {
442 $this->handler->doctype( $name, $public, $system, $quirks,
443 $sourceStart, $sourceLength );
444 } else {
445 $this->inForeign->doctype( $name, $public, $system, $quirks,
446 $sourceStart, $sourceLength );
447 }
448 }
449
450 /** @inheritDoc */
451 public function comment( $text, $sourceStart, $sourceLength ) {
452 $current = $this->dispatcherCurrentNode();
453 if ( !$current || $current->namespace === HTMLData::NS_HTML ) {
454 $this->handler->comment( $text, $sourceStart, $sourceLength );
455 } else {
456 $this->inForeign->comment( $text, $sourceStart, $sourceLength );
457 }
458 }
459 }