Debian Codegraph @

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

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\TreeBuilder;
4
5 use Wikimedia\RemexHtml\Tokenizer\Attributes;
6
7 /**
8 * The "in row" insertion mode
9 */
10 class InRow extends InsertionMode {
11 private const TABLE_ROW_CONTEXT = [
12 'tr' => true,
13 'template' => true,
14 'html' => true,
15 ];
16
17 /** @inheritDoc */
18 public function characters( $text, $start, $length, $sourceStart, $sourceLength ) {
19 $this->dispatcher->inTable->characters( $text, $start, $length,
20 $sourceStart, $sourceLength );
21 }
22
23 /** @inheritDoc */
24 public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) {
25 $builder = $this->builder;
26 $stack = $builder->stack;
27 $dispatcher = $this->dispatcher;
28
29 switch ( $name ) {
30 case 'th':
31 case 'td':
32 $builder->clearStackBack( self::TABLE_ROW_CONTEXT, $sourceStart );
33 $builder->insertElement( $name, $attrs, false, $sourceStart, $sourceLength );
34 $dispatcher->switchMode( Dispatcher::IN_CELL );
35 $builder->afe->insertMarker();
36 break;
37
38 case 'caption':
39 case 'col':
40 case 'colgroup':
41 case 'tbody':
42 case 'tfoot':
43 case 'thead':
44 case 'tr':
45 if ( !$stack->isInTableScope( 'tr' ) ) {
46 $builder->error( "<$name> should close the tr but it is not in scope",
47 $sourceStart );
48 // Ignore
49 return;
50 }
51 $builder->clearStackBack( self::TABLE_ROW_CONTEXT, $sourceStart );
52 $builder->pop( $sourceStart, 0 );
53 $dispatcher->switchMode( Dispatcher::IN_TABLE_BODY )
54 ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
55 break;
56
57 default:
58 $dispatcher->inTable->startTag( $name, $attrs, $selfClose,
59 $sourceStart, $sourceLength );
60 }
61 }
62
63 /** @inheritDoc */
64 public function endTag( $name, $sourceStart, $sourceLength ) {
65 $builder = $this->builder;
66 $stack = $builder->stack;
67 $dispatcher = $this->dispatcher;
68
69 switch ( $name ) {
70 case 'tr':
71 if ( !$stack->isInTableScope( 'tr' ) ) {
72 $builder->error( '</tr> found but no tr element in scope', $sourceStart );
73 // Ignore
74 return;
75 }
76 $builder->clearStackBack( self::TABLE_ROW_CONTEXT, $sourceStart );
77 $builder->pop( $sourceStart, $sourceLength );
78 $dispatcher->switchMode( Dispatcher::IN_TABLE_BODY );
79 break;
80
81 case 'table':
82 if ( !$stack->isInTableScope( 'tr' ) ) {
83 $builder->error( "</table> should close the tr but it is not in scope",
84 $sourceStart );
85 // Ignore
86 return;
87 }
88 $builder->clearStackBack( self::TABLE_ROW_CONTEXT, $sourceStart );
89 $builder->pop( $sourceStart, 0 );
90 $dispatcher->switchMode( Dispatcher::IN_TABLE_BODY )
91 ->endTag( $name, $sourceStart, $sourceLength );
92 break;
93
94 case 'tbody':
95 case 'tfoot':
96 case 'thead':
97 if ( !$stack->isInTableScope( $name ) ) {
98 $builder->error( "</$name> encountered but there is no $name element in scope",
99 $sourceStart );
100 return;
101 }
102 if ( !$stack->isInTableScope( 'tr' ) ) {
103 return;
104 }
105 $builder->clearStackBack( self::TABLE_ROW_CONTEXT, $sourceStart );
106 $builder->pop( $sourceStart, 0 );
107 $dispatcher->switchMode( Dispatcher::IN_TABLE_BODY )
108 ->endTag( $name, $sourceStart, $sourceLength );
109 break;
110
111 case 'body':
112 case 'caption':
113 case 'col':
114 case 'colgroup':
115 case 'html':
116 case 'td':
117 case 'th':
118 $builder->error( "</$name> encountered in row mode, ignoring", $sourceStart );
119 return;
120
121 default:
122 $dispatcher->inTable->endTag( $name, $sourceStart, $sourceLength );
123 }
124 }
125
126 /** @inheritDoc */
127 public function endDocument( $pos ) {
128 $this->dispatcher->inTable->endDocument( $pos );
129 }
130 }