Mercurial > hg > isophonics-drupal-site
diff vendor/masterminds/html5/src/HTML5/Parser/Scanner.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 4c8ae668cc8c |
children |
line wrap: on
line diff
--- a/vendor/masterminds/html5/src/HTML5/Parser/Scanner.php Tue Jul 10 15:07:59 2018 +0100 +++ b/vendor/masterminds/html5/src/HTML5/Parser/Scanner.php Thu Feb 28 13:21:36 2019 +0000 @@ -1,34 +1,90 @@ <?php + namespace Masterminds\HTML5\Parser; +use Masterminds\HTML5\Exception; + /** - * The scanner. - * - * This scans over an input stream. + * The scanner scans over a given data input to react appropriately to characters. */ class Scanner { - const CHARS_HEX = 'abcdefABCDEF01234567890'; - const CHARS_ALNUM = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890'; - const CHARS_ALPHA = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - protected $is; + /** + * The string data we're parsing. + */ + private $data; - // Flipping this to true will give minisculely more debugging info. - public $debug = false; + /** + * The current integer byte position we are in $data. + */ + private $char; + + /** + * Length of $data; when $char === $data, we are at the end-of-file. + */ + private $EOF; + + /** + * Parse errors. + */ + public $errors = array(); /** * Create a new Scanner. * - * @param \Masterminds\HTML5\Parser\InputStream $input - * An InputStream to be scanned. + * @param string $data Data to parse. + * @param string $encoding The encoding to use for the data. + * + * @throws Exception If the given data cannot be encoded to UTF-8. */ - public function __construct($input) + public function __construct($data, $encoding = 'UTF-8') { - $this->is = $input; + if ($data instanceof InputStream) { + @trigger_error('InputStream objects are deprecated since version 2.4 and will be removed in 3.0. Use strings instead.', E_USER_DEPRECATED); + $data = (string) $data; + } + + $data = UTF8Utils::convertToUTF8($data, $encoding); + + // There is good reason to question whether it makes sense to + // do this here, since most of these checks are done during + // parsing, and since this check doesn't actually *do* anything. + $this->errors = UTF8Utils::checkForIllegalCodepoints($data); + + $data = $this->replaceLinefeeds($data); + + $this->data = $data; + $this->char = 0; + $this->EOF = strlen($data); + } + + /** + * Check if upcomming chars match the given sequence. + * + * This will read the stream for the $sequence. If it's + * found, this will return true. If not, return false. + * Since this unconsumes any chars it reads, the caller + * will still need to read the next sequence, even if + * this returns true. + * + * Example: $this->scanner->sequenceMatches('</script>') will + * see if the input stream is at the start of a + * '</script>' string. + * + * @param string $sequence + * @param bool $caseSensitive + * + * @return bool + */ + public function sequenceMatches($sequence, $caseSensitive = true) + { + $portion = substr($this->data, $this->char, strlen($sequence)); + + return $caseSensitive ? $portion === $sequence : 0 === strcasecmp($portion, $sequence); } /** @@ -38,7 +94,7 @@ */ public function position() { - return $this->is->key(); + return $this->char; } /** @@ -48,23 +104,25 @@ */ public function peek() { - return $this->is->peek(); + if (($this->char + 1) <= $this->EOF) { + return $this->data[$this->char + 1]; + } + + return false; } /** * Get the next character. - * * Note: This advances the pointer. * * @return string The next character. */ public function next() { - $this->is->next(); - if ($this->is->valid()) { - if ($this->debug) - fprintf(STDOUT, "> %s\n", $this->is->current()); - return $this->is->current(); + ++$this->char; + + if ($this->char < $this->EOF) { + return $this->data[$this->char]; } return false; @@ -72,15 +130,14 @@ /** * Get the current character. - * * Note, this does not advance the pointer. * * @return string The current character. */ public function current() { - if ($this->is->valid()) { - return $this->is->current(); + if ($this->char < $this->EOF) { + return $this->data[$this->char]; } return false; @@ -88,29 +145,29 @@ /** * Silently consume N chars. + * + * @param int $count */ public function consume($count = 1) { - for ($i = 0; $i < $count; ++ $i) { - $this->next(); - } + $this->char += $count; } /** * Unconsume some of the data. * This moves the data pointer backwards. * - * @param int $howMany - * The number of characters to move the pointer back. + * @param int $howMany The number of characters to move the pointer back. */ public function unconsume($howMany = 1) { - $this->is->unconsume($howMany); + if (($this->char - $howMany) >= 0) { + $this->char -= $howMany; + } } /** * Get the next group of that contains hex characters. - * * Note, along with getting the characters the pointer in the data will be * moved as well. * @@ -118,12 +175,11 @@ */ public function getHex() { - return $this->is->charsWhile(static::CHARS_HEX); + return $this->doCharsWhile(static::CHARS_HEX); } /** * Get the next group of characters that are ASCII Alpha characters. - * * Note, along with getting the characters the pointer in the data will be * moved as well. * @@ -131,12 +187,11 @@ */ public function getAsciiAlpha() { - return $this->is->charsWhile(static::CHARS_ALPHA); + return $this->doCharsWhile(static::CHARS_ALPHA); } /** * Get the next group of characters that are ASCII Alpha characters and numbers. - * * Note, along with getting the characters the pointer in the data will be * moved as well. * @@ -144,12 +199,11 @@ */ public function getAsciiAlphaNum() { - return $this->is->charsWhile(static::CHARS_ALNUM); + return $this->doCharsWhile(static::CHARS_ALNUM); } /** * Get the next group of numbers. - * * Note, along with getting the characters the pointer in the data will be * moved as well. * @@ -157,17 +211,26 @@ */ public function getNumeric() { - return $this->is->charsWhile('0123456789'); + return $this->doCharsWhile('0123456789'); } /** * Consume whitespace. + * Whitespace in HTML5 is: formfeed, tab, newline, space. * - * Whitespace in HTML5 is: formfeed, tab, newline, space. + * @return int The length of the matched whitespaces. */ public function whitespace() { - return $this->is->charsWhile("\n\t\f "); + if ($this->char >= $this->EOF) { + return false; + } + + $len = strspn($this->data, "\n\t\f ", $this->char); + + $this->char += $len; + + return $len; } /** @@ -177,23 +240,37 @@ */ public function currentLine() { - return $this->is->currentLine(); + if (empty($this->EOF) || 0 === $this->char) { + return 1; + } + + // Add one to $this->char because we want the number for the next + // byte to be processed. + return substr_count($this->data, "\n", 0, min($this->char, $this->EOF)) + 1; } /** * Read chars until something in the mask is encountered. + * + * @param string $mask + * + * @return mixed */ public function charsUntil($mask) { - return $this->is->charsUntil($mask); + return $this->doCharsUntil($mask); } /** * Read chars as long as the mask matches. + * + * @param string $mask + * + * @return int */ public function charsWhile($mask) { - return $this->is->charsWhile($mask); + return $this->doCharsWhile($mask); } /** @@ -205,7 +282,29 @@ */ public function columnOffset() { - return $this->is->columnOffset(); + // Short circuit for the first char. + if (0 === $this->char) { + return 0; + } + + // strrpos is weird, and the offset needs to be negative for what we + // want (i.e., the last \n before $this->char). This needs to not have + // one (to make it point to the next character, the one we want the + // position of) added to it because strrpos's behaviour includes the + // final offset byte. + $backwardFrom = $this->char - 1 - strlen($this->data); + $lastLine = strrpos($this->data, "\n", $backwardFrom); + + // However, for here we want the length up until the next byte to be + // processed, so add one to the current byte ($this->char). + if (false !== $lastLine) { + $findLengthOf = substr($this->data, $lastLine + 1, $this->char - 1 - $lastLine); + } else { + // After a newline. + $findLengthOf = substr($this->data, 0, $this->char); + } + + return UTF8Utils::countChars($findLengthOf); } /** @@ -217,6 +316,101 @@ */ public function remainingChars() { - return $this->is->remainingChars(); + if ($this->char < $this->EOF) { + $data = substr($this->data, $this->char); + $this->char = $this->EOF; + + return $data; + } + + return ''; // false; + } + + /** + * Replace linefeed characters according to the spec. + * + * @param $data + * + * @return string + */ + private function replaceLinefeeds($data) + { + /* + * 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. + */ + $crlfTable = array( + "\0" => "\xEF\xBF\xBD", + "\r\n" => "\n", + "\r" => "\n", + ); + + return strtr($data, $crlfTable); + } + + /** + * Read to a particular match (or until $max bytes are consumed). + * + * This operates on byte sequences, not characters. + * + * Matches as far as possible until we reach a certain set of bytes + * and returns the matched substring. + * + * @param string $bytes Bytes to match. + * @param int $max Maximum number of bytes to scan. + * + * @return mixed Index or false if no match is found. You should use strong + * equality when checking the result, since index could be 0. + */ + private function doCharsUntil($bytes, $max = null) + { + if ($this->char >= $this->EOF) { + return false; + } + + if (0 === $max || $max) { + $len = strcspn($this->data, $bytes, $this->char, $max); + } else { + $len = strcspn($this->data, $bytes, $this->char); + } + + $string = (string) substr($this->data, $this->char, $len); + $this->char += $len; + + return $string; + } + + /** + * Returns the string so long as $bytes matches. + * + * Matches as far as possible with a certain set of bytes + * and returns the matched substring. + * + * @param string $bytes A mask of bytes to match. If ANY byte in this mask matches the + * current char, the pointer advances and the char is part of the + * substring. + * @param int $max The max number of chars to read. + * + * @return string + */ + private function doCharsWhile($bytes, $max = null) + { + if ($this->char >= $this->EOF) { + return false; + } + + if (0 === $max || $max) { + $len = strspn($this->data, $bytes, $this->char, $max); + } else { + $len = strspn($this->data, $bytes, $this->char); + } + + $string = (string) substr($this->data, $this->char, $len); + $this->char += $len; + + return $string; } }