PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.6
WindPress – Tailwind CSS integration for WordPress v3.0.6
3.2.89 3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 All 143 releases
windpress / vendor / masterminds / html5 / src / HTML5 / Parser / StringInputStream.php

StringInputStream.php in WindPress – Tailwind CSS integration for WordPress 3.0.6, at vendor/masterminds/html5/src/HTML5/Parser/StringInputStream.php

293 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Loads a string to be parsed.
5 */
6 namespace WindPressPackages\Masterminds\HTML5\Parser;
7
8 /*
9 *
10 * Based on code from html5lib:
11 Copyright 2009 Geoffrey Sneddon <http://gsnedders.com/>
12 Permission is hereby granted, free of charge, to any person obtaining a
13 copy of this software and associated documentation files (the
14 "Software"), to deal in the Software without restriction, including
15 without limitation the rights to use, copy, modify, merge, publish,
16 distribute, sublicense, and/or sell copies of the Software, and to
17 permit persons to whom the Software is furnished to do so, subject to
18 the following conditions:
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29 // Some conventions:
30 // - /* */ indicates verbatim text from the HTML 5 specification
31 // MPB: Not sure which version of the spec. Moving from HTML5lib to
32 // HTML5-PHP, I have been using this version:
33 // http://www.w3.org/TR/2012/CR-html5-20121217/Overview.html#contents
34 //
35 // - // indicates regular comments
36 /**
37 * @deprecated since 2.4, to remove in 3.0. Use a string in the scanner instead.
38 */
39 class StringInputStream implements InputStream
40 {
41 /**
42 * The string data we're parsing.
43 */
44 private $data;
45 /**
46 * The current integer byte position we are in $data.
47 */
48 private $char;
49 /**
50 * Length of $data; when $char === $data, we are at the end-of-file.
51 */
52 private $EOF;
53 /**
54 * Parse errors.
55 */
56 public $errors = array();
57 /**
58 * Create a new InputStream wrapper.
59 *
60 * @param string $data Data to parse.
61 * @param string $encoding The encoding to use for the data.
62 * @param string $debug A fprintf format to use to echo the data on stdout.
63 */
64 public function __construct($data, $encoding = 'UTF-8', $debug = '')
65 {
66 $data = UTF8Utils::convertToUTF8($data, $encoding);
67 if ($debug) {
68 \fprintf(\STDOUT, $debug, $data, \strlen($data));
69 }
70 // There is good reason to question whether it makes sense to
71 // do this here, since most of these checks are done during
72 // parsing, and since this check doesn't actually *do* anything.
73 $this->errors = UTF8Utils::checkForIllegalCodepoints($data);
74 $data = $this->replaceLinefeeds($data);
75 $this->data = $data;
76 $this->char = 0;
77 $this->EOF = \strlen($data);
78 }
79 public function __toString()
80 {
81 return $this->data;
82 }
83 /**
84 * Replace linefeed characters according to the spec.
85 */
86 protected function replaceLinefeeds($data)
87 {
88 /*
89 * U+000D CARRIAGE RETURN (CR) characters and U+000A LINE FEED (LF) characters are treated specially.
90 * Any CR characters that are followed by LF characters must be removed, and any CR characters not
91 * followed by LF characters must be converted to LF characters. Thus, newlines in HTML DOMs are
92 * represented by LF characters, and there are never any CR characters in the input to the tokenization
93 * stage.
94 */
95 $crlfTable = array("\x00" => "", "\r\n" => "\n", "\r" => "\n");
96 return \strtr($data, $crlfTable);
97 }
98 /**
99 * Returns the current line that the tokenizer is at.
100 */
101 public function currentLine()
102 {
103 if (empty($this->EOF) || 0 === $this->char) {
104 return 1;
105 }
106 // Add one to $this->char because we want the number for the next
107 // byte to be processed.
108 return \substr_count($this->data, "\n", 0, \min($this->char, $this->EOF)) + 1;
109 }
110 /**
111 * @deprecated
112 */
113 public function getCurrentLine()
114 {
115 return $this->currentLine();
116 }
117 /**
118 * Returns the current column of the current line that the tokenizer is at.
119 * Newlines are column 0. The first char after a newline is column 1.
120 *
121 * @return int The column number.
122 */
123 public function columnOffset()
124 {
125 // Short circuit for the first char.
126 if (0 === $this->char) {
127 return 0;
128 }
129 // strrpos is weird, and the offset needs to be negative for what we
130 // want (i.e., the last \n before $this->char). This needs to not have
131 // one (to make it point to the next character, the one we want the
132 // position of) added to it because strrpos's behaviour includes the
133 // final offset byte.
134 $backwardFrom = $this->char - 1 - \strlen($this->data);
135 $lastLine = \strrpos($this->data, "\n", $backwardFrom);
136 // However, for here we want the length up until the next byte to be
137 // processed, so add one to the current byte ($this->char).
138 if (\false !== $lastLine) {
139 $findLengthOf = \substr($this->data, $lastLine + 1, $this->char - 1 - $lastLine);
140 } else {
141 // After a newline.
142 $findLengthOf = \substr($this->data, 0, $this->char);
143 }
144 return UTF8Utils::countChars($findLengthOf);
145 }
146 /**
147 * @deprecated
148 */
149 public function getColumnOffset()
150 {
151 return $this->columnOffset();
152 }
153 /**
154 * Get the current character.
155 *
156 * @return string The current character.
157 */
158 #[\ReturnTypeWillChange]
159 public function current()
160 {
161 return $this->data[$this->char];
162 }
163 /**
164 * Advance the pointer.
165 * This is part of the Iterator interface.
166 */
167 #[\ReturnTypeWillChange]
168 public function next()
169 {
170 ++$this->char;
171 }
172 /**
173 * Rewind to the start of the string.
174 */
175 #[\ReturnTypeWillChange]
176 public function rewind()
177 {
178 $this->char = 0;
179 }
180 /**
181 * Is the current pointer location valid.
182 *
183 * @return bool Whether the current pointer location is valid.
184 */
185 #[\ReturnTypeWillChange]
186 public function valid()
187 {
188 return $this->char < $this->EOF;
189 }
190 /**
191 * Get all characters until EOF.
192 *
193 * This reads to the end of the file, and sets the read marker at the
194 * end of the file.
195 *
196 * Note this performs bounds checking.
197 *
198 * @return string Returns the remaining text. If called when the InputStream is
199 * already exhausted, it returns an empty string.
200 */
201 public function remainingChars()
202 {
203 if ($this->char < $this->EOF) {
204 $data = \substr($this->data, $this->char);
205 $this->char = $this->EOF;
206 return $data;
207 }
208 return '';
209 // false;
210 }
211 /**
212 * Read to a particular match (or until $max bytes are consumed).
213 *
214 * This operates on byte sequences, not characters.
215 *
216 * Matches as far as possible until we reach a certain set of bytes
217 * and returns the matched substring.
218 *
219 * @param string $bytes Bytes to match.
220 * @param int $max Maximum number of bytes to scan.
221 *
222 * @return mixed Index or false if no match is found. You should use strong
223 * equality when checking the result, since index could be 0.
224 */
225 public function charsUntil($bytes, $max = null)
226 {
227 if ($this->char >= $this->EOF) {
228 return \false;
229 }
230 if (0 === $max || $max) {
231 $len = \strcspn($this->data, $bytes, $this->char, $max);
232 } else {
233 $len = \strcspn($this->data, $bytes, $this->char);
234 }
235 $string = (string) \substr($this->data, $this->char, $len);
236 $this->char += $len;
237 return $string;
238 }
239 /**
240 * Returns the string so long as $bytes matches.
241 *
242 * Matches as far as possible with a certain set of bytes
243 * and returns the matched substring.
244 *
245 * @param string $bytes A mask of bytes to match. If ANY byte in this mask matches the
246 * current char, the pointer advances and the char is part of the
247 * substring.
248 * @param int $max The max number of chars to read.
249 *
250 * @return string
251 */
252 public function charsWhile($bytes, $max = null)
253 {
254 if ($this->char >= $this->EOF) {
255 return \false;
256 }
257 if (0 === $max || $max) {
258 $len = \strspn($this->data, $bytes, $this->char, $max);
259 } else {
260 $len = \strspn($this->data, $bytes, $this->char);
261 }
262 $string = (string) \substr($this->data, $this->char, $len);
263 $this->char += $len;
264 return $string;
265 }
266 /**
267 * Unconsume characters.
268 *
269 * @param int $howMany The number of characters to unconsume.
270 */
271 public function unconsume($howMany = 1)
272 {
273 if ($this->char - $howMany >= 0) {
274 $this->char -= $howMany;
275 }
276 }
277 /**
278 * Look ahead without moving cursor.
279 */
280 public function peek()
281 {
282 if ($this->char + 1 <= $this->EOF) {
283 return $this->data[$this->char + 1];
284 }
285 return \false;
286 }
287 #[\ReturnTypeWillChange]
288 public function key()
289 {
290 return $this->char;
291 }
292 }
293