annotate vendor/masterminds/html5/src/HTML5/Parser/StringInputStream.php @ 0:4c8ae668cc8c

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