Debian Codegraph @

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

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\TreeBuilder;
4
5 use Wikimedia\RemexHtml\PropGuard;
6 use Wikimedia\RemexHtml\Tokenizer\Attributes;
7
8 abstract class InsertionMode {
9 use PropGuard;
10
11 protected TreeBuilder $builder;
12 protected Dispatcher $dispatcher;
13
14 public function __construct( TreeBuilder $builder, Dispatcher $dispatcher ) {
15 $this->builder = $builder;
16 $this->dispatcher = $dispatcher;
17 }
18
19 /**
20 * A valid DOCTYPE token was found.
21 *
22 * @param string $name The doctype name, usually "html"
23 * @param string $public The PUBLIC identifier
24 * @param string $system The SYSTEM identifier
25 * @param bool $quirks What the spec calls the "force-quirks flag"
26 * @param int $sourceStart The input position
27 * @param int $sourceLength The length of the input which is consumed
28 */
29 public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) {
30 $this->builder->error( "unexpected doctype", $sourceStart );
31 }
32
33 /**
34 * @param string $text The text of the comment
35 * @param int $sourceStart The input position
36 * @param int $sourceLength The length of the input which is consumed
37 */
38 public function comment( $text, $sourceStart, $sourceLength ) {
39 $this->builder->comment( null, $text, $sourceStart, $sourceLength );
40 }
41
42 /**
43 * A parse error
44 *
45 * @param string $text An error message explaining in English what the
46 * author did wrong, and what the parser intends to do about the
47 * situation.
48 * @param int $pos The input position at which the error occurred
49 */
50 public function error( $text, $pos ) {
51 $this->builder->error( $text, $pos );
52 }
53
54 /**
55 * @param bool $isStartOfToken
56 * @param string $mask Match mask
57 * @param string $text The text to insert is a substring of this string,
58 * with the start and length of the substring given by $start and
59 * $length. We do it this way to avoid unnecessary copying.
60 * @param int $start The start of the substring
61 * @param int $length The length of the substring
62 * @param int $sourceStart The input position
63 * @param int $sourceLength The length of the input which is consumed
64 * @return array
65 */
66 protected function splitInitialMatch( $isStartOfToken, $mask, $text, $start, $length,
67 $sourceStart, $sourceLength
68 ) {
69 $matchLength = strspn( $text, $mask, $start, $length );
70 if ( $isStartOfToken && $matchLength ) {
71 // Do some extra work to figure out a plausible start position if
72 // the text node started with <![CDATA[
73 // FIXME: make this optional?
74 $sourceText = $this->builder->tokenizer->getPreprocessedText();
75 $isCdata = substr_compare( $sourceText, '<![CDATA[', $sourceStart, $sourceLength ) === 0;
76 $cdataLength = $isCdata ? strlen( '<![CDATA[' ) : 0;
77 } else {
78 $cdataLength = 0;
79 }
80
81 return [
82 [
83 $start,
84 $matchLength,
85 $sourceStart,
86 $matchLength + $cdataLength,
87 ], [
88 $start + $matchLength,
89 $length - $matchLength,
90 $sourceStart + $matchLength + $cdataLength,
91 $sourceLength - $matchLength - $cdataLength
92 ]
93 ];
94 }
95
96 /**
97 * @param bool $inBody
98 * @param string $text The text to insert is a substring of this string,
99 * with the start and length of the substring given by $start and
100 * $length. We do it this way to avoid unnecessary copying.
101 * @param int $start The start of the substring
102 * @param int $length The length of the substring
103 * @param int $sourceStart The input position
104 * @param int $sourceLength The length of the input which is consumed
105 */
106 protected function handleFramesetWhitespace( $inBody, $text, $start, $length,
107 $sourceStart, $sourceLength
108 ) {
109 $isStartOfToken = true;
110 $builder = $this->builder;
111
112 do {
113 [ $part1, $part2 ] = $this->splitInitialMatch(
114 $isStartOfToken, "\t\n\f\r ", $text, $start, $length, $sourceStart, $sourceLength );
115 $isStartOfToken = false;
116
117 [ $start, $length, $sourceStart, $sourceLength ] = $part1;
118 if ( $length ) {
119 if ( $inBody ) {
120 $this->dispatcher->inBody->characters( $text, $start, $length,
121 $sourceStart, $sourceLength );
122 } else {
123 $builder->insertCharacters( $text, $start, $length, $sourceStart, $sourceLength );
124 }
125 }
126
127 [ $start, $length, $sourceStart, $sourceLength ] = $part2;
128 if ( $length ) {
129 $builder->error( "unexpected non-whitespace character", $sourceStart );
130 $start++;
131 $length--;
132 $sourceStart++;
133 $sourceLength--;
134 }
135 } while ( $length > 0 );
136 }
137
138 /**
139 * @param callable $callback
140 * @param string $text The text to insert is a substring of this string,
141 * with the start and length of the substring given by $start and
142 * $length. We do it this way to avoid unnecessary copying.
143 * @param int $start The start of the substring
144 * @param int $length The length of the substring
145 * @param int $sourceStart The input position
146 * @param int $sourceLength The length of the input which is consumed
147 */
148 protected function stripNulls( $callback, $text, $start, $length, $sourceStart, $sourceLength ) {
149 $errorOffset = $sourceStart - $start;
150 while ( $length > 0 ) {
151 $validLength = strcspn( $text, "\0", $start, $length );
152 if ( $validLength ) {
153 $callback( $text, $start, $validLength, $sourceStart, $sourceLength );
154 $start += $validLength;
155 $length -= $validLength;
156 }
157 if ( $length <= 0 ) {
158 break;
159 }
160 $this->error( 'unexpected null character', $start + $errorOffset );
161 $start++;
162 $length--;
163 }
164 }
165
166 /**
167 * Insert characters.
168 *
169 * @param string $text The text to insert is a substring of this string,
170 * with the start and length of the substring given by $start and
171 * $length. We do it this way to avoid unnecessary copying.
172 * @param int $start The start of the substring
173 * @param int $length The length of the substring
174 * @param int $sourceStart The input position
175 * @param int $sourceLength The length of the input which is consumed
176 */
177 abstract public function characters( $text, $start, $length, $sourceStart, $sourceLength );
178
179 /**
180 * @param string $name The tag name being ended
181 * @param Attributes $attrs
182 * @param bool $selfClose True if this is a self closing tag
183 * @param int $sourceStart The input position
184 * @param int $sourceLength The input position
185 */
186 abstract public function startTag( $name, Attributes $attrs, $selfClose,
187 $sourceStart, $sourceLength );
188
189 /**
190 * @param string $name The tag name being ended
191 * @param int $sourceStart The input position
192 * @param int $sourceLength The input position
193 */
194 abstract public function endTag( $name, $sourceStart, $sourceLength );
195
196 /**
197 * Called when parsing stops.
198 *
199 * @param int $pos The input string length, i.e. the past-the-end position.
200 */
201 abstract public function endDocument( $pos );
202 }