Debian Codegraph @

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

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\TreeBuilder;
4
5 use Wikimedia\RemexHtml\Tokenizer\Attributes;
6
7 /**
8 * The "in cell" insertion mode
9 */
10 class InCell extends InsertionMode {
11 /** @inheritDoc */
12 public function characters( $text, $start, $length, $sourceStart, $sourceLength ) {
13 $this->dispatcher->inBody->characters( $text, $start, $length,
14 $sourceStart, $sourceLength );
15 }
16
17 /** @inheritDoc */
18 public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) {
19 switch ( $name ) {
20 case 'caption':
21 case 'col':
22 case 'colgroup':
23 case 'tbody':
24 case 'td':
25 case 'tfoot':
26 case 'th':
27 case 'thead':
28 case 'tr':
29 if ( !$this->builder->stack->isInTableScope( 'td' )
30 && !$this->builder->stack->isInTableScope( 'th' )
31 ) {
32 $this->builder->error( "<$name> tag should close the cell but none is in scope",
33 $sourceStart );
34 return;
35 }
36 $this->closeTheCell( $sourceStart )
37 ->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength );
38 break;
39
40 default:
41 $this->dispatcher->inBody->startTag(
42 $name, $attrs, $selfClose, $sourceStart, $sourceLength );
43 }
44 }
45
46 /** @inheritDoc */
47 public function endTag( $name, $sourceStart, $sourceLength ) {
48 $builder = $this->builder;
49 $stack = $builder->stack;
50 $dispatcher = $this->dispatcher;
51
52 switch ( $name ) {
53 case 'td':
54 case 'th':
55 if ( !$stack->isInTableScope( $name ) ) {
56 $builder->error( "</$name> encountered but there is no $name in scope, ignoring",
57 $sourceStart );
58 return;
59 }
60 $builder->generateImpliedEndTags( false, $sourceStart );
61 if ( $stack->current->htmlName !== $name ) {
62 $builder->error( "</$name> encountered when there are tags open " .
63 "which can't be closed automatically", $sourceStart );
64 }
65 $builder->popAllUpToName( $name, $sourceStart, $sourceLength );
66 $builder->afe->clearToMarker();
67 $dispatcher->switchMode( Dispatcher::IN_ROW );
68 break;
69
70 case 'body':
71 case 'caption':
72 case 'col':
73 case 'colgroup':
74 case 'html':
75 $builder->error( "unexpected </$name> in cell, ignoring", $sourceStart );
76 return;
77
78 case 'table':
79 case 'tbody':
80 case 'tfoot':
81 case 'thead':
82 case 'tr':
83 if ( !$stack->isInTableScope( $name ) ) {
84 $builder->error( "</$name> encountered but there is no $name in scope, ignoring",
85 $sourceStart );
86 return;
87 }
88 $this->closeTheCell( $sourceStart )
89 ->endTag( $name, $sourceStart, $sourceLength );
90 break;
91
92 default:
93 $dispatcher->inBody->endTag( $name, $sourceStart, $sourceLength );
94 }
95 }
96
97 /** @inheritDoc */
98 public function endDocument( $pos ) {
99 $this->dispatcher->inBody->endDocument( $pos );
100 }
101
102 private function closeTheCell( int $sourceStart ): InsertionMode {
103 $tdth = [ 'td' => true, 'th' => true ];
104 $builder = $this->builder;
105 $stack = $builder->stack;
106 $builder->generateImpliedEndTags( false, $sourceStart );
107 if ( !isset( $tdth[$stack->current->htmlName] ) ) {
108 $builder->error( "closing the cell but there are tags open " .
109 "which can't be closed automatically", $sourceStart );
110 }
111 $builder->afe->clearToMarker();
112 return $this->dispatcher->switchMode( Dispatcher::IN_ROW );
113 }
114 }