Chris@0: Chris@0: Chris@0: Permission is hereby granted, free of charge, to any person obtaining a Chris@0: copy of this software and associated documentation files (the Chris@0: "Software"), to deal in the Software without restriction, including Chris@0: without limitation the rights to use, copy, modify, merge, publish, Chris@0: distribute, sublicense, and/or sell copies of the Software, and to Chris@0: permit persons to whom the Software is furnished to do so, subject to Chris@0: the following conditions: Chris@0: Chris@0: The above copyright notice and this permission notice shall be included Chris@0: in all copies or substantial portions of the Software. Chris@0: Chris@0: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS Chris@0: OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@0: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. Chris@0: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY Chris@0: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, Chris@0: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE Chris@0: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@0: Chris@0: */ Chris@0: Chris@0: // Some conventions: Chris@0: // - /* */ indicates verbatim text from the HTML 5 specification Chris@0: // MPB: Not sure which version of the spec. Moving from HTML5lib to Chris@0: // HTML5-PHP, I have been using this version: Chris@0: // http://www.w3.org/TR/2012/CR-html5-20121217/Overview.html#contents Chris@0: // Chris@0: // - // indicates regular comments Chris@0: Chris@17: /** Chris@17: * @deprecated since 2.4, to remove in 3.0. Use a string in the scanner instead. Chris@17: */ Chris@0: class StringInputStream implements InputStream Chris@0: { Chris@0: /** Chris@0: * The string data we're parsing. Chris@0: */ Chris@0: private $data; Chris@0: Chris@0: /** Chris@17: * The current integer byte position we are in $data. Chris@0: */ Chris@0: private $char; Chris@0: Chris@0: /** Chris@0: * Length of $data; when $char === $data, we are at the end-of-file. Chris@0: */ Chris@0: private $EOF; Chris@0: Chris@0: /** Chris@0: * Parse errors. Chris@0: */ Chris@0: public $errors = array(); Chris@0: Chris@0: /** Chris@0: * Create a new InputStream wrapper. Chris@0: * Chris@17: * @param string $data Data to parse. Chris@17: * @param string $encoding The encoding to use for the data. Chris@17: * @param string $debug A fprintf format to use to echo the data on stdout. Chris@0: */ Chris@0: public function __construct($data, $encoding = 'UTF-8', $debug = '') Chris@0: { Chris@0: $data = UTF8Utils::convertToUTF8($data, $encoding); Chris@17: if ($debug) { Chris@0: fprintf(STDOUT, $debug, $data, strlen($data)); Chris@17: } Chris@0: Chris@17: // There is good reason to question whether it makes sense to Chris@17: // do this here, since most of these checks are done during Chris@17: // parsing, and since this check doesn't actually *do* anything. Chris@0: $this->errors = UTF8Utils::checkForIllegalCodepoints($data); Chris@0: Chris@0: $data = $this->replaceLinefeeds($data); Chris@0: Chris@0: $this->data = $data; Chris@0: $this->char = 0; Chris@0: $this->EOF = strlen($data); Chris@0: } Chris@0: Chris@17: public function __toString() Chris@17: { Chris@17: return $this->data; Chris@17: } Chris@17: Chris@0: /** Chris@0: * Replace linefeed characters according to the spec. Chris@0: */ Chris@0: protected function replaceLinefeeds($data) Chris@0: { Chris@0: /* Chris@17: * U+000D CARRIAGE RETURN (CR) characters and U+000A LINE FEED (LF) characters are treated specially. Chris@17: * Any CR characters that are followed by LF characters must be removed, and any CR characters not Chris@17: * followed by LF characters must be converted to LF characters. Thus, newlines in HTML DOMs are Chris@17: * represented by LF characters, and there are never any CR characters in the input to the tokenization Chris@17: * stage. Chris@0: */ Chris@0: $crlfTable = array( Chris@0: "\0" => "\xEF\xBF\xBD", Chris@0: "\r\n" => "\n", Chris@17: "\r" => "\n", Chris@0: ); Chris@0: Chris@0: return strtr($data, $crlfTable); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the current line that the tokenizer is at. Chris@0: */ Chris@0: public function currentLine() Chris@0: { Chris@17: if (empty($this->EOF) || 0 === $this->char) { Chris@0: return 1; Chris@0: } Chris@0: // Add one to $this->char because we want the number for the next Chris@0: // byte to be processed. Chris@0: return substr_count($this->data, "\n", 0, min($this->char, $this->EOF)) + 1; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @deprecated Chris@0: */ Chris@0: public function getCurrentLine() Chris@0: { Chris@17: return $this->currentLine(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the current column of the current line that the tokenizer is at. Chris@0: * Newlines are column 0. The first char after a newline is column 1. Chris@0: * Chris@0: * @return int The column number. Chris@0: */ Chris@0: public function columnOffset() Chris@0: { Chris@0: // Short circuit for the first char. Chris@17: if (0 === $this->char) { Chris@0: return 0; Chris@0: } Chris@0: // strrpos is weird, and the offset needs to be negative for what we Chris@0: // want (i.e., the last \n before $this->char). This needs to not have Chris@0: // one (to make it point to the next character, the one we want the Chris@0: // position of) added to it because strrpos's behaviour includes the Chris@0: // final offset byte. Chris@0: $backwardFrom = $this->char - 1 - strlen($this->data); Chris@0: $lastLine = strrpos($this->data, "\n", $backwardFrom); Chris@0: Chris@0: // However, for here we want the length up until the next byte to be Chris@0: // processed, so add one to the current byte ($this->char). Chris@17: if (false !== $lastLine) { Chris@0: $findLengthOf = substr($this->data, $lastLine + 1, $this->char - 1 - $lastLine); Chris@0: } else { Chris@0: // After a newline. Chris@0: $findLengthOf = substr($this->data, 0, $this->char); Chris@0: } Chris@0: Chris@0: return UTF8Utils::countChars($findLengthOf); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @deprecated Chris@0: */ Chris@0: public function getColumnOffset() Chris@0: { Chris@0: return $this->columnOffset(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the current character. Chris@0: * Chris@0: * @return string The current character. Chris@0: */ Chris@0: public function current() Chris@0: { Chris@0: return $this->data[$this->char]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Advance the pointer. Chris@0: * This is part of the Iterator interface. Chris@0: */ Chris@0: public function next() Chris@0: { Chris@17: ++$this->char; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Rewind to the start of the string. Chris@0: */ Chris@0: public function rewind() Chris@0: { Chris@0: $this->char = 0; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is the current pointer location valid. Chris@0: * Chris@17: * @return bool Whether the current pointer location is valid. Chris@0: */ Chris@0: public function valid() Chris@0: { Chris@17: return $this->char < $this->EOF; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get all characters until EOF. Chris@0: * Chris@0: * This reads to the end of the file, and sets the read marker at the Chris@0: * end of the file. Chris@0: * Chris@17: * Note this performs bounds checking. Chris@0: * Chris@0: * @return string Returns the remaining text. If called when the InputStream is Chris@17: * already exhausted, it returns an empty string. Chris@0: */ Chris@0: public function remainingChars() Chris@0: { Chris@0: if ($this->char < $this->EOF) { Chris@0: $data = substr($this->data, $this->char); Chris@0: $this->char = $this->EOF; Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: return ''; // false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Read to a particular match (or until $max bytes are consumed). Chris@0: * Chris@0: * This operates on byte sequences, not characters. Chris@0: * Chris@0: * Matches as far as possible until we reach a certain set of bytes Chris@0: * and returns the matched substring. Chris@0: * Chris@17: * @param string $bytes Bytes to match. Chris@17: * @param int $max Maximum number of bytes to scan. Chris@17: * Chris@0: * @return mixed Index or false if no match is found. You should use strong Chris@17: * equality when checking the result, since index could be 0. Chris@0: */ Chris@0: public function charsUntil($bytes, $max = null) Chris@0: { Chris@0: if ($this->char >= $this->EOF) { Chris@0: return false; Chris@0: } Chris@0: Chris@17: if (0 === $max || $max) { Chris@0: $len = strcspn($this->data, $bytes, $this->char, $max); Chris@0: } else { Chris@0: $len = strcspn($this->data, $bytes, $this->char); Chris@0: } Chris@0: Chris@0: $string = (string) substr($this->data, $this->char, $len); Chris@0: $this->char += $len; Chris@0: Chris@0: return $string; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the string so long as $bytes matches. Chris@0: * Chris@0: * Matches as far as possible with a certain set of bytes Chris@0: * and returns the matched substring. Chris@0: * Chris@17: * @param string $bytes A mask of bytes to match. If ANY byte in this mask matches the Chris@17: * current char, the pointer advances and the char is part of the Chris@17: * substring. Chris@17: * @param int $max The max number of chars to read. Chris@17: * Chris@17: * @return string Chris@0: */ Chris@0: public function charsWhile($bytes, $max = null) Chris@0: { Chris@0: if ($this->char >= $this->EOF) { Chris@0: return false; Chris@0: } Chris@0: Chris@17: if (0 === $max || $max) { Chris@0: $len = strspn($this->data, $bytes, $this->char, $max); Chris@0: } else { Chris@0: $len = strspn($this->data, $bytes, $this->char); Chris@0: } Chris@0: $string = (string) substr($this->data, $this->char, $len); Chris@0: $this->char += $len; Chris@0: Chris@0: return $string; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Unconsume characters. Chris@0: * Chris@17: * @param int $howMany The number of characters to unconsume. Chris@0: */ Chris@0: public function unconsume($howMany = 1) Chris@0: { Chris@0: if (($this->char - $howMany) >= 0) { Chris@17: $this->char -= $howMany; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Look ahead without moving cursor. Chris@0: */ Chris@0: public function peek() Chris@0: { Chris@0: if (($this->char + 1) <= $this->EOF) { Chris@0: return $this->data[$this->char + 1]; Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: public function key() Chris@0: { Chris@0: return $this->char; Chris@0: } Chris@0: }