CharacterReference.php
3 years ago
DOMTreeBuilder.php
3 years ago
EventHandler.php
3 years ago
FileInputStream.php
3 years ago
InputStream.php
3 years ago
ParseError.php
3 years ago
Scanner.php
3 years ago
StringInputStream.php
3 years ago
Tokenizer.php
3 years ago
TreeBuildingRules.php
3 years ago
UTF8Utils.php
3 years ago
StringInputStream.php
288 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Loads a string to be parsed. |
| 5 | */ |
| 6 | namespace IAWP\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 | public function current() |
| 159 | { |
| 160 | return $this->data[$this->char]; |
| 161 | } |
| 162 | /** |
| 163 | * Advance the pointer. |
| 164 | * This is part of the Iterator interface. |
| 165 | */ |
| 166 | public function next() |
| 167 | { |
| 168 | ++$this->char; |
| 169 | } |
| 170 | /** |
| 171 | * Rewind to the start of the string. |
| 172 | */ |
| 173 | public function rewind() |
| 174 | { |
| 175 | $this->char = 0; |
| 176 | } |
| 177 | /** |
| 178 | * Is the current pointer location valid. |
| 179 | * |
| 180 | * @return bool Whether the current pointer location is valid. |
| 181 | */ |
| 182 | public function valid() |
| 183 | { |
| 184 | return $this->char < $this->EOF; |
| 185 | } |
| 186 | /** |
| 187 | * Get all characters until EOF. |
| 188 | * |
| 189 | * This reads to the end of the file, and sets the read marker at the |
| 190 | * end of the file. |
| 191 | * |
| 192 | * Note this performs bounds checking. |
| 193 | * |
| 194 | * @return string Returns the remaining text. If called when the InputStream is |
| 195 | * already exhausted, it returns an empty string. |
| 196 | */ |
| 197 | public function remainingChars() |
| 198 | { |
| 199 | if ($this->char < $this->EOF) { |
| 200 | $data = \substr($this->data, $this->char); |
| 201 | $this->char = $this->EOF; |
| 202 | return $data; |
| 203 | } |
| 204 | return ''; |
| 205 | // false; |
| 206 | } |
| 207 | /** |
| 208 | * Read to a particular match (or until $max bytes are consumed). |
| 209 | * |
| 210 | * This operates on byte sequences, not characters. |
| 211 | * |
| 212 | * Matches as far as possible until we reach a certain set of bytes |
| 213 | * and returns the matched substring. |
| 214 | * |
| 215 | * @param string $bytes Bytes to match. |
| 216 | * @param int $max Maximum number of bytes to scan. |
| 217 | * |
| 218 | * @return mixed Index or false if no match is found. You should use strong |
| 219 | * equality when checking the result, since index could be 0. |
| 220 | */ |
| 221 | public function charsUntil($bytes, $max = null) |
| 222 | { |
| 223 | if ($this->char >= $this->EOF) { |
| 224 | return \false; |
| 225 | } |
| 226 | if (0 === $max || $max) { |
| 227 | $len = \strcspn($this->data, $bytes, $this->char, $max); |
| 228 | } else { |
| 229 | $len = \strcspn($this->data, $bytes, $this->char); |
| 230 | } |
| 231 | $string = (string) \substr($this->data, $this->char, $len); |
| 232 | $this->char += $len; |
| 233 | return $string; |
| 234 | } |
| 235 | /** |
| 236 | * Returns the string so long as $bytes matches. |
| 237 | * |
| 238 | * Matches as far as possible with a certain set of bytes |
| 239 | * and returns the matched substring. |
| 240 | * |
| 241 | * @param string $bytes A mask of bytes to match. If ANY byte in this mask matches the |
| 242 | * current char, the pointer advances and the char is part of the |
| 243 | * substring. |
| 244 | * @param int $max The max number of chars to read. |
| 245 | * |
| 246 | * @return string |
| 247 | */ |
| 248 | public function charsWhile($bytes, $max = null) |
| 249 | { |
| 250 | if ($this->char >= $this->EOF) { |
| 251 | return \false; |
| 252 | } |
| 253 | if (0 === $max || $max) { |
| 254 | $len = \strspn($this->data, $bytes, $this->char, $max); |
| 255 | } else { |
| 256 | $len = \strspn($this->data, $bytes, $this->char); |
| 257 | } |
| 258 | $string = (string) \substr($this->data, $this->char, $len); |
| 259 | $this->char += $len; |
| 260 | return $string; |
| 261 | } |
| 262 | /** |
| 263 | * Unconsume characters. |
| 264 | * |
| 265 | * @param int $howMany The number of characters to unconsume. |
| 266 | */ |
| 267 | public function unconsume($howMany = 1) |
| 268 | { |
| 269 | if ($this->char - $howMany >= 0) { |
| 270 | $this->char -= $howMany; |
| 271 | } |
| 272 | } |
| 273 | /** |
| 274 | * Look ahead without moving cursor. |
| 275 | */ |
| 276 | public function peek() |
| 277 | { |
| 278 | if ($this->char + 1 <= $this->EOF) { |
| 279 | return $this->data[$this->char + 1]; |
| 280 | } |
| 281 | return \false; |
| 282 | } |
| 283 | public function key() |
| 284 | { |
| 285 | return $this->char; |
| 286 | } |
| 287 | } |
| 288 |