3 * Loads a string to be parsed.
5 namespace Masterminds\HTML5\Parser;
9 * Based on code from html5lib:
11 Copyright 2009 Geoffrey Sneddon <http://gsnedders.com/>
13 Permission is hereby granted, free of charge, to any person obtaining a
14 copy of this software and associated documentation files (the
15 "Software"), to deal in the Software without restriction, including
16 without limitation the rights to use, copy, modify, merge, publish,
17 distribute, sublicense, and/or sell copies of the Software, and to
18 permit persons to whom the Software is furnished to do so, subject to
19 the following conditions:
21 The above copyright notice and this permission notice shall be included
22 in all copies or substantial portions of the Software.
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 // - /* */ indicates verbatim text from the HTML 5 specification
36 // MPB: Not sure which version of the spec. Moving from HTML5lib to
37 // HTML5-PHP, I have been using this version:
38 // http://www.w3.org/TR/2012/CR-html5-20121217/Overview.html#contents
40 // - // indicates regular comments
42 class StringInputStream implements InputStream
46 * The string data we're parsing.
51 * The current integer byte position we are in $data
56 * Length of $data; when $char === $data, we are at the end-of-file.
63 public $errors = array();
66 * Create a new InputStream wrapper.
71 public function __construct($data, $encoding = 'UTF-8', $debug = '')
73 $data = UTF8Utils::convertToUTF8($data, $encoding);
75 fprintf(STDOUT, $debug, $data, strlen($data));
77 // There is good reason to question whether it makes sense to
78 // do this here, since most of these checks are done during
79 // parsing, and since this check doesn't actually *do* anything.
80 $this->errors = UTF8Utils::checkForIllegalCodepoints($data);
82 // throw new ParseError("UTF-8 encoding issues: " . implode(', ', $e));
85 $data = $this->replaceLinefeeds($data);
89 $this->EOF = strlen($data);
93 * Replace linefeed characters according to the spec.
95 protected function replaceLinefeeds($data)
98 * U+000D CARRIAGE RETURN (CR) characters and U+000A LINE FEED (LF) characters are treated specially. Any CR characters that are followed by LF characters must be removed, and any CR characters not followed by LF characters must be converted to LF characters. Thus, newlines in HTML DOMs are represented by LF characters, and there are never any CR characters in the input to the tokenization stage.
101 "\0" => "\xEF\xBF\xBD",
106 return strtr($data, $crlfTable);
110 * Returns the current line that the tokenizer is at.
112 public function currentLine()
114 if (empty($this->EOF) || $this->char == 0) {
117 // Add one to $this->char because we want the number for the next
118 // byte to be processed.
119 return substr_count($this->data, "\n", 0, min($this->char, $this->EOF)) + 1;
127 public function getCurrentLine()
129 return currentLine();
133 * Returns the current column of the current line that the tokenizer is at.
135 * Newlines are column 0. The first char after a newline is column 1.
137 * @return int The column number.
139 public function columnOffset()
141 // Short circuit for the first char.
142 if ($this->char == 0) {
145 // strrpos is weird, and the offset needs to be negative for what we
146 // want (i.e., the last \n before $this->char). This needs to not have
147 // one (to make it point to the next character, the one we want the
148 // position of) added to it because strrpos's behaviour includes the
149 // final offset byte.
150 $backwardFrom = $this->char - 1 - strlen($this->data);
151 $lastLine = strrpos($this->data, "\n", $backwardFrom);
153 // However, for here we want the length up until the next byte to be
154 // processed, so add one to the current byte ($this->char).
155 if ($lastLine !== false) {
156 $findLengthOf = substr($this->data, $lastLine + 1, $this->char - 1 - $lastLine);
159 $findLengthOf = substr($this->data, 0, $this->char);
162 return UTF8Utils::countChars($findLengthOf);
170 public function getColumnOffset()
172 return $this->columnOffset();
176 * Get the current character.
178 * @return string The current character.
180 public function current()
182 return $this->data[$this->char];
186 * Advance the pointer.
187 * This is part of the Iterator interface.
189 public function next()
195 * Rewind to the start of the string.
197 public function rewind()
203 * Is the current pointer location valid.
205 * @return bool Is the current pointer location valid.
207 public function valid()
209 if ($this->char < $this->EOF) {
217 * Get all characters until EOF.
219 * This reads to the end of the file, and sets the read marker at the
222 * @note This performs bounds checking
224 * @return string Returns the remaining text. If called when the InputStream is
225 * already exhausted, it returns an empty string.
227 public function remainingChars()
229 if ($this->char < $this->EOF) {
230 $data = substr($this->data, $this->char);
231 $this->char = $this->EOF;
240 * Read to a particular match (or until $max bytes are consumed).
242 * This operates on byte sequences, not characters.
244 * Matches as far as possible until we reach a certain set of bytes
245 * and returns the matched substring.
247 * @param string $bytes
250 * Maximum number of bytes to scan.
251 * @return mixed Index or false if no match is found. You should use strong
252 * equality when checking the result, since index could be 0.
254 public function charsUntil($bytes, $max = null)
256 if ($this->char >= $this->EOF) {
260 if ($max === 0 || $max) {
261 $len = strcspn($this->data, $bytes, $this->char, $max);
263 $len = strcspn($this->data, $bytes, $this->char);
266 $string = (string) substr($this->data, $this->char, $len);
273 * Returns the string so long as $bytes matches.
275 * Matches as far as possible with a certain set of bytes
276 * and returns the matched substring.
278 * @param string $bytes
279 * A mask of bytes to match. If ANY byte in this mask matches the
280 * current char, the pointer advances and the char is part of the
283 * The max number of chars to read.
285 public function charsWhile($bytes, $max = null)
287 if ($this->char >= $this->EOF) {
291 if ($max === 0 || $max) {
292 $len = strspn($this->data, $bytes, $this->char, $max);
294 $len = strspn($this->data, $bytes, $this->char);
296 $string = (string) substr($this->data, $this->char, $len);
303 * Unconsume characters.
305 * @param int $howMany
306 * The number of characters to unconsume.
308 public function unconsume($howMany = 1)
310 if (($this->char - $howMany) >= 0) {
311 $this->char = $this->char - $howMany;
316 * Look ahead without moving cursor.
318 public function peek()
320 if (($this->char + 1) <= $this->EOF) {
321 return $this->data[$this->char + 1];
327 public function key()