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