Example #1
0
 public function offsetGet($offset)
 {
     if ($offset === 'data') {
         return $this->data->toValue();
     }
     if ($offset === 'source') {
         return $this->source->toValue();
     }
     if (property_exists($this, $offset)) {
         return $this->{$offset};
     }
     return $this->_properties[$offset];
 }
 /**
  * Performs the actual parsing of the document.
  * @return mixed|array Tokens array, ast tree (anything getResult() will return)
  */
 public function parse()
 {
     // Current state
     $state = 'data';
     // This is used to avoid having to have look-behind in the data state.
     $lastFourChars = new StringBuffer(4);
     /**
      * Escape flag as specified by the HTML5 specification: "used to
      * control the behavior of the tokenizer. It is either true or
      * false, and initially must be set to the false state."
      */
     $escape = false;
     //echo "\n\n";
     while ($state !== null) {
         /*echo $state . ' ';
           switch ($this->content_model) {
               case self::PCDATA: echo 'PCDATA'; break;
               case self::RCDATA: echo 'RCDATA'; break;
               case self::CDATA: echo 'CDATA'; break;
               case self::PLAINTEXT: echo 'PLAINTEXT'; break;
           }
           if ($escape) echo " escape";
           echo "\n";*/
         switch ($state) {
             case 'data':
                 /* Consume the next input character */
                 $symbol = $this->stream->symbol();
                 $buffer = new Buffer($symbol);
                 $lastFourChars->push($symbol);
                 // see below for meaning
                 $hyp_cond = !$escape && ($this->content_model === self::RCDATA || $this->content_model === self::CDATA);
                 $lt_cond = $this->content_model === self::PCDATA || !$escape && ($this->content_model === self::RCDATA || $this->content_model === self::CDATA);
                 $gt_cond = $escape && ($this->content_model === self::RCDATA || $this->content_model === self::CDATA);
                 if ($symbol === '-' && $hyp_cond && $lastFourChars->endsWith('<!--')) {
                     /*
                                             U+002D HYPHEN-MINUS (-)
                                             If the content model flag is set to either the RCDATA state or
                                             the CDATA state, and the escape flag is false, and there are at
                                             least three characters before this one in the input stream, and the
                                             last four characters in the input stream, including this one, are
                                             U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+002D HYPHEN-MINUS,
                                             and U+002D HYPHEN-MINUS ("<!--"), then set the escape flag to true. */
                     $escape = true;
                     /* In any case, emit the input character as a character token. Stay
                        in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_CHARACTER, 'data' => '-'));
                     // We do the "any case" part as part of "anything else".
                     /* U+003C LESS-THAN SIGN (<) */
                 } elseif ($symbol === '<' && $lt_cond) {
                     /* When the content model flag is set to the PCDATA state: switch
                                             to the tag open state.
                     
                                             When the content model flag is set to either the RCDATA state or
                                             the CDATA state and the escape flag is false: switch to the tag
                                             open state.
                     
                                             Otherwise: treat it as per the "anything else" entry below. */
                     $state = 'tag open';
                     /* U+003E GREATER-THAN SIGN (>) */
                 } elseif ($symbol === '>' && $gt_cond && $lastFourChars->endsWith('-->')) {
                     /* If the content model flag is set to either the RCDATA state or
                        the CDATA state, and the escape flag is true, and the last three
                        characters in the input stream including this one are U+002D
                        HYPHEN-MINUS, U+002D HYPHEN-MINUS, U+003E GREATER-THAN SIGN ("-->"),
                        set the escape flag to false. */
                     $escape = false;
                     /* In any case, emit the input character as a character token.
                        Stay in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_CHARACTER, 'data' => '>'));
                     // We do the "any case" part as part of "anything else".
                 } elseif ($symbol === false) {
                     /* EOF
                        Emit an end-of-file token. */
                     $state = null;
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_EOF));
                 } elseif ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     // Directly after emitting a token you switch back to the "data
                     // state". At that point spaceCharacters are important so they are
                     // emitted separately.
                     $chars = $buffer->push($this->stream->yieldWhile(self::WHITESPACE));
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_SPACECHARACTER, 'data' => $buffer->toValue()));
                     $lastFourChars->push($chars);
                 } else {
                     /* Anything else
                        THIS IS AN OPTIMIZATION: Get as many character that
                        otherwise would also be treated as a character token and emit it
                        as a single character token. Stay in the data state. */
                     $mask = '';
                     if ($hyp_cond) {
                         $mask .= '-';
                     }
                     if ($lt_cond) {
                         $mask .= '<';
                     }
                     if ($gt_cond) {
                         $mask .= '>';
                     }
                     if ($mask === '') {
                         $symbols = $buffer->push($this->stream->yieldRemaining());
                     } else {
                         $symbols = $buffer->push($this->stream->yieldUntil($mask));
                     }
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_CHARACTER, 'data' => $buffer->toValue()));
                     $lastFourChars->push($symbols);
                     $state = 'data';
                 }
                 break;
             case 'tag open':
                 $symbol = $this->stream->symbol();
                 switch ($this->content_model) {
                     case self::RCDATA:
                     case self::CDATA:
                         /* Consume the next input character. If it is a
                            U+002F SOLIDUS (/) character, switch to the close
                            tag open state. Otherwise, emit a U+003C LESS-THAN
                            SIGN character token and reconsume the current input
                            character in the data state. */
                         // We consumed above.
                         if ($symbol === '/') {
                             $state = 'close tag open';
                         } else {
                             $this->_emitToken(array('type' => Tokenizer::TOKEN_CHARACTER, 'data' => '<'));
                             $this->stream->undo();
                             $state = 'data';
                         }
                         break;
                     case self::PCDATA:
                         /* If the content model flag is set to the PCDATA state
                            Consume the next input character: */
                         // We consumed above.
                         if ($symbol === '!') {
                             /* U+0021 EXCLAMATION MARK (!)
                                Switch to the markup declaration open state. */
                             $state = 'markup declaration open';
                         } elseif ($symbol === '/') {
                             /* U+002F SOLIDUS (/)
                                Switch to the close tag open state. */
                             $state = 'close tag open';
                         } elseif (is_string($symbol) && 'A' <= $symbol && $symbol <= 'Z' || is_string($symbol) && 'a' <= $symbol && $symbol <= 'z' || $symbol instanceof Node) {
                             /* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
                                   U+0061 LATIN SMALL LETTER A through to U+007A LATIN SMALL LETTER Z
                                   PHP NODE
                                Create a new start tag token, set its tag name to the input character,
                                then switch to the tag name state. (Don't emit the token
                                yet; further details will be filled in before it is emitted.) */
                             $this->token = new Token(array('name' => $symbol, 'type' => Tokenizer::TOKEN_STARTTAG, 'attr' => array(), 'source' => ['<', $symbol]));
                             $state = 'tag name';
                         } elseif ($symbol === '>') {
                             /* U+003E GREATER-THAN SIGN (>)
                                Parse error. Emit a U+003C LESS-THAN SIGN character token and a
                                U+003E GREATER-THAN SIGN character token. Switch to the data state. */
                             $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-tag-name-but-got-right-bracket'));
                             $this->_emitToken(array('type' => Tokenizer::TOKEN_CHARACTER, 'data' => '<>'));
                             $state = 'data';
                         } elseif ($symbol === '?') {
                             /* U+003F QUESTION MARK (?)
                                Parse error. Switch to the bogus comment state. */
                             $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-tag-name-but-got-question-mark'));
                             $this->token = new Token(array('data' => '?', 'type' => Tokenizer::TOKEN_COMMENT, 'source' => '<?'));
                             $state = 'bogus comment';
                         } else {
                             /* Anything else
                                Parse error. Emit a U+003C LESS-THAN SIGN character token and
                                reconsume the current input character in the data state. */
                             $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-tag-name'));
                             $this->_emitToken(array('type' => Tokenizer::TOKEN_CHARACTER, 'data' => '<'));
                             $state = 'data';
                             $this->stream->undo();
                         }
                         break;
                 }
                 break;
             case 'close tag open':
                 if ($this->content_model === self::RCDATA || $this->content_model === self::CDATA) {
                     /* If the content model flag is set to the RCDATA or CDATA
                        states... */
                     $buffer = new Buffer();
                     $name = $buffer->push($this->stream->yieldWhile(self::ALPHA));
                     $following = $this->stream->symbol();
                     $this->stream->undo();
                     if (!$this->token || !$this->token->name || (string) $this->token->name !== (string) $name || (string) $this->token->name === (string) $name && !in_array($following, array("\t", "\n", "\f", " ", ">", "/", false))) {
                         /* if no start tag token has ever been emitted by this instance
                                                     of the tokenizer (fragment case), or, if the next few
                                                     characters do not match the tag name of the last start tag
                                                     token emitted (compared in an ASCII case-insensitive manner),
                                                     or if they do but they are not immediately followed by one of
                                                     the following characters:
                         
                                                         * U+0009 CHARACTER TABULATION
                                                         * U+000A LINE FEED (LF)
                                                         * U+000C FORM FEED (FF)
                                                         * U+0020 SPACE
                                                         * U+003E GREATER-THAN SIGN (>)
                                                         * U+002F SOLIDUS (/)
                                                         * EOF
                         
                                                     ...then emit a U+003C LESS-THAN SIGN character token, a
                                                     U+002F SOLIDUS character token, and switch to the data
                                                     state to process the next input character. */
                         // XXX: Probably ought to replace in_array with $following === x ||...
                         // We also need to emit $name now we've consumed that, as we
                         // know it'll just be emitted as a character token.
                         $buffer->prepend('</');
                         $this->_emitToken(array('type' => Tokenizer::TOKEN_CHARACTER, 'data' => $buffer->toValue()));
                         $state = 'data';
                     } else {
                         // This matches what would happen if we actually did the
                         // otherwise below (but we can't because we've consumed too
                         // much).
                         // Start the end tag token with the name we already have.
                         $this->token = new Token(array('name' => $name, 'type' => Tokenizer::TOKEN_ENDTAG, 'source' => ['</', $name]));
                         // Change to tag name state.
                         $state = 'tag name';
                     }
                 } elseif ($this->content_model === self::PCDATA) {
                     /* Otherwise, if the content model flag is set to the PCDATA
                        state [...]: */
                     $symbol = $this->stream->symbol();
                     if ($symbol instanceof Node || is_string($symbol) && 'A' <= $symbol && $symbol <= 'Z' || is_string($symbol) && 'a' <= $symbol && $symbol <= 'z') {
                         /* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
                               U+0061 LATIN SMALL LETTER A through to U+007A LATIN SMALL LETTER Z
                               PHP NODE
                            Create a new end tag token, set its tag name to the input symbol, then
                            switch to the tag name state. (Don't emit the token yet; further details
                            will be filled in before it is emitted.) */
                         $this->token = new Token(array('name' => $symbol, 'type' => Tokenizer::TOKEN_ENDTAG, 'source' => ['</', $symbol]));
                         $state = 'tag name';
                     } elseif ($symbol === '>') {
                         /* U+003E GREATER-THAN SIGN (>)
                            Parse error. Switch to the data state. */
                         $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-closing-tag-but-got-right-bracket'));
                         $this->_emitToken(['type' => Tokenizer::TOKEN_CHARACTER, 'data' => '</>']);
                         $state = 'data';
                     } elseif ($symbol === false) {
                         /* EOF
                            Parse error. Emit a U+003C LESS-THAN SIGN character token and a U+002F
                            SOLIDUS character token. Reconsume the EOF character in the data state. */
                         $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-closing-tag-but-got-eof'));
                         $this->_emitToken(array('type' => Tokenizer::TOKEN_CHARACTER, 'data' => '</'));
                         $this->stream->undo();
                         $state = 'data';
                     } else {
                         /* Parse error. Switch to the bogus comment state. */
                         $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-closing-tag-but-got-char'));
                         $this->token = new Token(['data' => $symbol, 'type' => Tokenizer::TOKEN_COMMENT, 'source' => ['</', $symbol]]);
                         $state = 'bogus comment';
                     }
                 }
                 break;
             case 'tag name':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                        U+000A LINE FEED (LF)
                        U+000C FORM FEED (FF)
                        U+0020 SPACE
                        Switch to the before attribute name state. */
                     $state = 'before attribute name';
                     $this->token->source->push($symbol);
                 } elseif ($symbol === '/') {
                     /* U+002F SOLIDUS (/)
                        Switch to the self-closing start tag state. */
                     $state = 'self-closing start tag';
                     $this->token->source->push($symbol);
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the current tag token. Switch to the data state. */
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Reconsume the EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-tag-name'));
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Append the current input character to the current tag token's tag name.
                        Stay in the tag name state. */
                     $buffer = new Buffer([$symbol, $this->stream->yieldUntil("\t\n\f />" . self::UPPER_ALPHA)]);
                     $this->token->name->push($buffer);
                     $this->token->source->push($buffer);
                     $state = 'tag name';
                 }
                 break;
             case 'before attribute name':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                        U+000A LINE FEED (LF)
                        U+000C FORM FEED (FF)
                        U+0020 SPACE
                        Stay in the before attribute name state. */
                     $this->token->source->push($symbol);
                     $state = 'before attribute name';
                 } elseif ($symbol === '/') {
                     /* U+002F SOLIDUS (/)
                        Switch to the self-closing start tag state. */
                     $this->token->source->push($symbol);
                     $state = 'self-closing start tag';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the current tag token. Switch to the data state. */
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Reconsume the EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-attribute-name-but-got-eof'));
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* U+0022 QUOTATION MARK (")
                           U+0027 APOSTROPHE (')
                           U+003C LESS-THAN SIGN (<)
                           U+003D EQUALS SIGN (=)
                        Parse error. Treat it as per the "anything else" entry
                        below. */
                     if ($symbol === '"' || $symbol === "'" || $symbol === '<' || $symbol === '=') {
                         $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'invalid-character-in-attribute-name'));
                     }
                     /* Anything else
                        Start a new attribute in the current tag token. Set that attribute's
                        name to the current input character, and its value to the empty string.
                        Switch to the attribute name state. */
                     $this->token->attr[] = new TokenAttribute($symbol);
                     $this->token->source->push($symbol);
                     $state = 'attribute name';
                 }
                 break;
             case 'attribute name':
                 // Consume the next input character:
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                        U+000A LINE FEED (LF)
                        U+000C FORM FEED (FF)
                        U+0020 SPACE
                        Switch to the after attribute name state. */
                     $this->token->source->push($symbol);
                     $state = 'after attribute name';
                 } elseif ($symbol === '/') {
                     /* U+002F SOLIDUS (/)
                        Switch to the self-closing start tag state. */
                     $this->token->source->push($symbol);
                     $state = 'self-closing start tag';
                 } elseif ($symbol === '=') {
                     /* U+003D EQUALS SIGN (=)
                        Switch to the before attribute value state. */
                     $this->token->source->push($symbol);
                     $state = 'before attribute value';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the current tag token. Switch to the data state. */
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Reconsume the EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-attribute-name'));
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* U+0022 QUOTATION MARK (")
                           U+0027 APOSTROPHE (')
                           U+003C LESS-THAN SIGN (<)
                        Parse error. Treat it as per the "anything else"
                        entry below. */
                     if ($symbol === '"' || $symbol === "'" || $symbol === '<') {
                         $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'invalid-character-in-attribute-name'));
                     }
                     /* Anything else
                        Append the current input symbols to the current attribute's name.
                        Stay in the attribute name state. */
                     $last = count($this->token->attr) - 1;
                     $buffer = new Buffer([$symbol, $this->stream->yieldUntil("\t\n\f /=>\"'" . self::UPPER_ALPHA)]);
                     $this->token->attr[$last]->name->push($buffer);
                     $this->token->source->push($buffer);
                     $state = 'attribute name';
                 }
                 /* When the user agent leaves the attribute name state
                    (and before emitting the tag token, if appropriate), the
                    complete attribute's name must be compared to the other
                    attributes on the same token; if there is already an
                    attribute on the token with the exact same name, then this
                    is a parse error and the new attribute must be dropped, along
                    with the value that gets associated with it (if any). */
                 // this might be implemented in the emitToken method
                 break;
             case 'after attribute name':
                 // Consume the next input character:
                 $symbol = $this->stream->symbol();
                 // this is an optimized conditional, check the bottom
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                        U+000A LINE FEED (LF)
                        U+000C FORM FEED (FF)
                        U+0020 SPACE
                        Stay in the after attribute name state. */
                     $this->token->source->push($symbol);
                     $state = 'after attribute name';
                 } elseif ($symbol === '/') {
                     /* U+002F SOLIDUS (/)
                        Switch to the self-closing start tag state. */
                     $this->token->source->push($symbol);
                     $state = 'self-closing start tag';
                 } elseif ($symbol === '=') {
                     /* U+003D EQUALS SIGN (=)
                        Switch to the before attribute value state. */
                     $this->token->source->push($symbol);
                     $state = 'before attribute value';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the current tag token. Switch to the data state. */
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Reconsume the EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-end-of-tag-but-got-eof'));
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* U+0022 QUOTATION MARK (")
                           U+0027 APOSTROPHE (')
                           U+003C LESS-THAN SIGN(<)
                        Parse error. Treat it as per the "anything else"
                        entry below. */
                     if ($symbol === '"' || $symbol === "'" || $symbol === "<") {
                         $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'invalid-character-after-attribute-name'));
                     }
                     /* Anything else
                        Start a new attribute in the current tag token. Set that attribute's
                        name to the current input character, and its value to the empty string.
                        Switch to the attribute name state. */
                     $this->token->attr[] = new TokenAttribute($symbol);
                     $this->token->source->push($symbol);
                     $state = 'attribute name';
                 }
                 break;
             case 'before attribute value':
                 // Consume the next input character:
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                        U+000A LINE FEED (LF)
                        U+000C FORM FEED (FF)
                        U+0020 SPACE
                        Stay in the before attribute value state. */
                     $this->token->source->push($symbol);
                     $state = 'before attribute value';
                 } elseif ($symbol === '"') {
                     /* U+0022 QUOTATION MARK (")
                        Switch to the attribute value (double-quoted) state. */
                     $this->token->source->push($symbol);
                     $state = 'attribute value (double-quoted)';
                 } elseif ($symbol === '&') {
                     /* U+0026 AMPERSAND (&)
                        Switch to the attribute value (unquoted) state and reconsume
                        this input character. */
                     $this->stream->undo();
                     $state = 'attribute value (unquoted)';
                 } elseif ($symbol === '\'') {
                     /* U+0027 APOSTROPHE (')
                        Switch to the attribute value (single-quoted) state. */
                     $this->token->source->push($symbol);
                     $state = 'attribute value (single-quoted)';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Parse error. Emit the current tag token. Switch to the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-attribute-value-but-got-right-bracket'));
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Reconsume the EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-attribute-value-but-got-eof'));
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* U+003D EQUALS SIGN (=)
                         * U+003C LESS-THAN SIGN (<)
                        Parse error. Treat it as per the "anything else" entry below. */
                     if ($symbol === '=' || $symbol === '<') {
                         $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'equals-in-unquoted-attribute-value'));
                     }
                     /* Anything else
                        Append the current input character to the current attribute's value.
                        Switch to the attribute value (unquoted) state. */
                     $last = count($this->token->attr) - 1;
                     $this->token->attr[$last]->value->push($symbol);
                     $this->token->source->push($symbol);
                     $state = 'attribute value (unquoted)';
                 }
                 break;
             case 'attribute value (double-quoted)':
                 // Consume the next input character:
                 $symbol = $this->stream->symbol();
                 if ($symbol === '"') {
                     /* U+0022 QUOTATION MARK (")
                        Switch to the after attribute value (quoted) state. */
                     $this->token->source->push($symbol);
                     $state = 'after attribute value (quoted)';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Reconsume the EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-attribute-value-double-quote'));
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Append the current input character to the current attribute's value.
                        Stay in the attribute value (double-quoted) state. */
                     $last = count($this->token->attr) - 1;
                     $buffer = new Buffer([$symbol, $this->stream->yieldUntil('"&')]);
                     $this->token->attr[$last]->value->push($buffer);
                     $this->token->source->push($buffer);
                     $state = 'attribute value (double-quoted)';
                 }
                 break;
             case 'attribute value (single-quoted)':
                 // Consume the next input character:
                 $symbol = $this->stream->symbol();
                 if ($symbol === "'") {
                     /* U+0022 QUOTATION MARK (')
                        Switch to the after attribute value state. */
                     $this->token->source->push($symbol);
                     $state = 'after attribute value (quoted)';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Reconsume the EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-attribute-value-single-quote'));
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Append the current input character to the current attribute's value.
                        Stay in the attribute value (single-quoted) state. */
                     $last = count($this->token->attr) - 1;
                     $buffer = new Buffer([$symbol, $this->stream->yieldUntil("'&")]);
                     $this->token->attr[$last]->value->push($buffer);
                     $this->token->source->push($buffer);
                     $state = 'attribute value (single-quoted)';
                 }
                 break;
             case 'attribute value (unquoted)':
                 // Consume the next input character:
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                        U+000A LINE FEED (LF)
                        U+000C FORM FEED (FF)
                        U+0020 SPACE
                        Switch to the before attribute name state. */
                     $this->token->source->push($symbol);
                     $state = 'before attribute name';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the current tag token. Switch to the data state. */
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Reconsume the EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-attribute-value-no-quotes'));
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* U+0022 QUOTATION MARK (")
                           U+0027 APOSTROPHE (')
                           U+003C LESS-THAN SIGN (<)
                           U+003D EQUALS SIGN (=)
                        Parse error. Treat it as per the "anything else"
                        entry below. */
                     if ($symbol === '"' || $symbol === "'" || $symbol === '=' || $symbol == '<') {
                         $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-character-in-unquoted-attribute-value'));
                     }
                     /* Anything else
                        Append the current input character to the current attribute's value.
                        Stay in the attribute value (unquoted) state. */
                     $last = count($this->token->attr) - 1;
                     $buffer = new Buffer([$symbol, $this->stream->yieldUntil("\t\n\f &>\"'=")]);
                     $this->token->attr[$last]->value->push($buffer);
                     $this->token->source->push($buffer);
                     $state = 'attribute value (unquoted)';
                 }
                 break;
             case 'after attribute value (quoted)':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                           U+000A LINE FEED (LF)
                           U+000C FORM FEED (FF)
                           U+0020 SPACE
                        Switch to the before attribute name state. */
                     $this->token->source->push($symbol);
                     $state = 'before attribute name';
                 } elseif ($symbol === '/') {
                     /* U+002F SOLIDUS (/)
                        Switch to the self-closing start tag state. */
                     $this->token->source->push($symbol);
                     $state = 'self-closing start tag';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the current tag token. Switch to the data state. */
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Reconsume the EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-EOF-after-attribute-value'));
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Parse error. Reconsume the character in the before attribute
                        name state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-character-after-attribute-value'));
                     $this->stream->undo();
                     $state = 'before attribute name';
                 }
                 break;
             case 'self-closing start tag':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Set the self-closing flag of the current tag token.
                        Emit the current tag token. Switch to the data state. */
                     // not sure if this is the name we want
                     $this->token['self-closing'] = true;
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Reconsume the EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-eof-after-self-closing'));
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Parse error. Reconsume the character in the before attribute name state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-character-after-self-closing'));
                     $this->stream->undo();
                     $state = 'before attribute name';
                 }
                 break;
             case 'bogus comment':
                 /* (This can only happen if the content model flag is set to the PCDATA state.) */
                 /* Consume every character up to the first U+003E GREATER-THAN SIGN
                    character (>) or the end of the file (EOF), whichever comes first. Emit
                    a comment token whose data is the concatenation of all the characters
                    starting from and including the character that caused the state machine
                    to switch into the bogus comment state, up to and including the last
                    consumed character before the U+003E character, if any, or up to the
                    end of the file otherwise. (If the comment was started by the end of
                    the file (EOF), the token is empty.) */
                 $buffer = new Buffer($this->stream->yieldUntil('>'));
                 $this->token->data->push($buffer);
                 $this->token->source->push([$buffer, '>']);
                 $this->stream->symbol();
                 $this->_emitToken($this->token->toArray());
                 /* Switch to the data state. */
                 $state = 'data';
                 break;
             case 'markup declaration open':
                 // Consume for below
                 $hyphens = new Buffer($this->stream->yieldWhile('-', true, 2));
                 if ((string) $hyphens === '-') {
                     $this->stream->undo();
                 }
                 if ((string) $hyphens !== '--') {
                     $alpha = new Buffer($this->stream->yieldWhile(self::ALPHA, true, 7));
                 }
                 /* If the next two characters are both U+002D HYPHEN-MINUS (-)
                    characters, consume those two characters, create a comment token whose
                    data is the empty string, and switch to the comment state. */
                 if ($hyphens == '--') {
                     $state = 'comment start';
                     $this->token = new Token(['data' => '', 'type' => Tokenizer::TOKEN_COMMENT, 'source' => '<!--']);
                     /* Otherwise if the next seven characters are a case-insensitive match
                        for the word "DOCTYPE", then consume those characters and switch to the
                        DOCTYPE state. */
                 } elseif (strtoupper($alpha) === 'DOCTYPE') {
                     $this->token = new Token(['type' => Tokenizer::TOKEN_DOCTYPE, 'name' => '', 'error' => true, 'force-quirks' => false, 'source' => '<!' . $alpha]);
                     $state = 'DOCTYPE';
                     // XXX not implemented
                     /* Otherwise, if the insertion mode is "in foreign content"
                        and the current node is not an element in the HTML namespace
                        and the next seven characters are an ASCII case-sensitive
                        match for the string "[CDATA[" (the five uppercase letters
                        "CDATA" with a U+005B LEFT SQUARE BRACKET character before
                        and after), then consume those characters and switch to the
                        CDATA section state (which is unrelated to the content model
                        flag's CDATA state). */
                     /* Otherwise, is is a parse error. Switch to the bogus comment state.
                        The next character that is consumed, if any, is the first character
                        that will be in the comment. */
                 } else {
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-dashes-or-doctype'));
                     $this->token = new Token(array('data' => (string) $alpha, 'type' => Tokenizer::TOKEN_COMMENT, 'source' => ['<!', $alpha]));
                     $state = 'bogus comment';
                 }
                 break;
             case 'comment start':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === '-') {
                     /* U+002D HYPHEN-MINUS (-)
                        Switch to the comment start dash state. */
                     $state = 'comment start dash';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Parse error. Emit the comment token. Switch to the
                        data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'incorrect-comment'));
                     $this->token->source->push('>');
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Emit the comment token. Reconsume the
                        EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-comment'));
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Append the input character to the comment token's
                        data. Switch to the comment state. */
                     $this->token->data->push($symbol);
                     $this->token->source->push($symbol);
                     $state = 'comment';
                 }
                 break;
             case 'comment start dash':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === '-') {
                     /* U+002D HYPHEN-MINUS (-)
                        Switch to the comment end state */
                     $state = 'comment end';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Parse error. Emit the comment token. Switch to the
                        data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'incorrect-comment'));
                     $this->token->source->push('->');
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* Parse error. Emit the comment token. Reconsume the
                        EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-comment'));
                     $this->token->source->push('-');
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     $this->token->data->push(['-', $symbol]);
                     $this->token->source->push(['-', $symbol]);
                     $state = 'comment';
                 }
                 break;
             case 'comment':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === '-') {
                     /* U+002D HYPHEN-MINUS (-)
                        Switch to the comment end dash state */
                     $state = 'comment end dash';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Emit the comment token. Reconsume the EOF character
                        in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-comment'));
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Append the input character to the comment token's data. Stay in
                        the comment state. */
                     $buffer = new Buffer([$symbol, $this->stream->yieldUntil('-')]);
                     $this->token->data->push($buffer);
                     $this->token->source->push($buffer);
                 }
                 break;
             case 'comment end dash':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === '-') {
                     /* U+002D HYPHEN-MINUS (-)
                        Switch to the comment end state  */
                     $state = 'comment end';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Emit the comment token. Reconsume the EOF character
                        in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-comment-end-dash'));
                     $this->token->source->push('-');
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Append a U+002D HYPHEN-MINUS (-) character and the input
                        character to the comment token's data. Switch to the comment state. */
                     $this->token->data->push(['-', $symbol]);
                     $this->token->source->push(['-', $symbol]);
                     $state = 'comment';
                 }
                 break;
             case 'comment end':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the comment token. Switch to the data state. */
                     $this->token->source->push('-->');
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === '-') {
                     /* U+002D HYPHEN-MINUS (-)
                        Parse error. Append a U+002D HYPHEN-MINUS (-) character
                        to the comment token's data. Stay in the comment end
                        state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-dash-after-double-dash-in-comment'));
                     $this->token->data->push('-');
                     $this->token->source->push('-');
                 } elseif ($symbol === "\t" || $symbol === "\n" || $symbol === "\n" || $symbol === ' ') {
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-space-after-double-dash-in-comment'));
                     $this->token->data->push(['--', $symbol]);
                     $this->token->source->push(['--', $symbol]);
                     $state = 'comment end space';
                 } elseif ($symbol === '!') {
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-bang-after-double-dash-in-comment'));
                     $state = 'comment end bang';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Emit the comment token. Reconsume the
                        EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-comment-double-dash'));
                     $this->token->source->push('--');
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Parse error. Append two U+002D HYPHEN-MINUS (-)
                        characters and the input character to the comment token's
                        data. Switch to the comment state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-char-in-comment'));
                     $this->token->data->push(['--', $symbol]);
                     $this->token->source->push(['--', $symbol]);
                     $state = 'comment';
                 }
                 break;
             case 'comment end bang':
                 $symbol = $this->stream->symbol();
                 if ($symbol === '>') {
                     $this->token->source->push('--!>');
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === "-") {
                     $this->token->source->push('--!');
                     $this->token->data->push('--!');
                     $state = 'comment end dash';
                 } elseif ($symbol === false) {
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-comment-end-bang'));
                     $this->token->source->push('--!');
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     $this->token->data->push(['--!', $symbol]);
                     $this->token->source->push(['--!', $symbol]);
                     $state = 'comment';
                 }
                 break;
             case 'comment end space':
                 $symbol = $this->stream->symbol();
                 if ($symbol === '>') {
                     $this->token->source->push('>');
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === '-') {
                     $state = 'comment end dash';
                 } elseif ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     $this->token->source->push($symbol);
                     $this->token->data->push($symbol);
                 } elseif ($symbol === false) {
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-eof-in-comment-end-space'));
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     $this->token->data->push($symbol);
                     $this->token->source->push($symbol);
                     $state = 'comment';
                 }
                 break;
             case 'DOCTYPE':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                           U+000A LINE FEED (LF)
                           U+000C FORM FEED (FF)
                           U+0020 SPACE
                        Switch to the before DOCTYPE name state. */
                     $this->token->source->push($symbol);
                     $state = 'before DOCTYPE name';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Create a new DOCTYPE token. Set its
                        force-quirks flag to on. Emit the token. Reconsume the
                        EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'need-space-after-doctype-but-got-eof'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Parse error. Reconsume the current character in the
                        before DOCTYPE name state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'need-space-after-doctype'));
                     $this->stream->undo();
                     $state = 'before DOCTYPE name';
                 }
                 break;
             case 'before DOCTYPE name':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                           U+000A LINE FEED (LF)
                           U+000C FORM FEED (FF)
                           U+0020 SPACE
                        Stay in the before DOCTYPE name state. */
                     $this->token->source->push($symbol);
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Parse error. Create a new DOCTYPE token. Set its
                        force-quirks flag to on. Emit the token. Switch to the
                        data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-doctype-name-but-got-right-bracket'));
                     $this->token['force-quirks'] = true;
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif (is_string($symbol) && 'A' <= $symbol && $symbol <= 'Z') {
                     /* U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
                        Create a new DOCTYPE token. Set the token's name to the
                        lowercase version of the input character (add 0x0020 to
                        the character's code point). Switch to the DOCTYPE name
                        state. */
                     $this->token->name->push(strtolower($symbol));
                     $this->token->source->push($symbol);
                     $state = 'DOCTYPE name';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Create a new DOCTYPE token. Set its
                        force-quirks flag to on. Emit the token. Reconsume the
                        EOF character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-doctype-name-but-got-eof'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Create a new DOCTYPE token. Set the token's name to the
                        current input character. Switch to the DOCTYPE name state. */
                     $this->token->name->push($symbol);
                     $this->token->source->push($symbol);
                     $state = 'DOCTYPE name';
                 }
                 break;
             case 'DOCTYPE name':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                           U+000A LINE FEED (LF)
                           U+000C FORM FEED (FF)
                           U+0020 SPACE
                        Switch to the after DOCTYPE name state. */
                     $this->token->source->push($symbol);
                     $state = 'after DOCTYPE name';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the current DOCTYPE token. Switch to the data state. */
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif (is_string($symbol) && 'A' <= $symbol && $symbol <= 'Z') {
                     /* U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z
                        Append the lowercase version of the input character
                        (add 0x0020 to the character's code point) to the current
                        DOCTYPE token's name. Stay in the DOCTYPE name state. */
                     $this->token->name->push(strtolower($symbol));
                     $this->token->source->push($symbol);
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Reconsume the EOF
                        character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-doctype-name'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Append the current input character to the current
                        DOCTYPE token's name. Stay in the DOCTYPE name state. */
                     $this->token->name->push($symbol);
                     $this->token->source->push($symbol);
                 }
                 // XXX this is probably some sort of quirks mode designation,
                 // check tree-builder to be sure. In general 'error' needs
                 // to be specified, this probably means removing it at the end
                 $this->token['error'] = $this->token->name->getLength() == 4 && strtoupper($this->token->name) === 'HTML' ? false : true;
                 break;
             case 'after DOCTYPE name':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                           U+000A LINE FEED (LF)
                           U+000C FORM FEED (FF)
                           U+0020 SPACE
                        Stay in the after DOCTYPE name state. */
                     $this->token->source->push($symbol);
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the current DOCTYPE token. Switch to the data state. */
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Reconsume the EOF
                        character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else */
                     $nextSix = new Buffer([$symbol, $this->stream->yieldWhile(self::ALPHA, true, 5)]);
                     $this->token->source->push($nextSix);
                     if ($nextSix->equals('PUBLIC')) {
                         /* If the next six characters are an ASCII
                            case-insensitive match for the word "PUBLIC", then
                            consume those characters and switch to the before
                            DOCTYPE public identifier state. */
                         $state = 'before DOCTYPE public identifier';
                     } elseif ($nextSix->equals('SYSTEM')) {
                         /* Otherwise, if the next six characters are an ASCII
                            case-insensitive match for the word "SYSTEM", then
                            consume those characters and switch to the before
                            DOCTYPE system identifier state. */
                         $state = 'before DOCTYPE system identifier';
                     } else {
                         /* Otherwise, this is the parse error. Set the DOCTYPE
                            token's force-quirks flag to on. Switch to the bogus
                            DOCTYPE state. */
                         $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'expected-space-or-right-bracket-in-doctype'));
                         $this->token['force-quirks'] = true;
                         $this->token['error'] = true;
                         $state = 'bogus DOCTYPE';
                     }
                 }
                 break;
             case 'before DOCTYPE public identifier':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                           U+000A LINE FEED (LF)
                           U+000C FORM FEED (FF)
                           U+0020 SPACE
                        Stay in the before DOCTYPE public identifier state. */
                     $this->token->source->push($symbol);
                 } elseif ($symbol === '"') {
                     /* U+0022 QUOTATION MARK (")
                        Set the DOCTYPE token's public identifier to the empty
                        string (not missing), then switch to the DOCTYPE public
                        identifier (double-quoted) state. */
                     $this->token['public'] = '';
                     $this->token->source->push($symbol);
                     $state = 'DOCTYPE public identifier (double-quoted)';
                 } elseif ($symbol === "'") {
                     /* U+0027 APOSTROPHE (')
                        Set the DOCTYPE token's public identifier to the empty
                        string (not missing), then switch to the DOCTYPE public
                        identifier (single-quoted) state. */
                     $this->token['public'] = '';
                     $state = 'DOCTYPE public identifier (single-quoted)';
                     $this->token->source->push($symbol);
                 } elseif ($symbol === '>') {
                     /* Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Switch to the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-end-of-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* Parse error. Set the DOCTYPE token's force-quirks
                        flag to on. Emit that DOCTYPE token. Reconsume the EOF
                        character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Switch to the bogus DOCTYPE state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-char-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->token->source->push($symbol);
                     $state = 'bogus DOCTYPE';
                 }
                 break;
             case 'DOCTYPE public identifier (double-quoted)':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === '"') {
                     /* U+0022 QUOTATION MARK (")
                        Switch to the after DOCTYPE public identifier state. */
                     $this->token->source->push($symbol);
                     $state = 'after DOCTYPE public identifier';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Switch to the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-end-of-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Reconsume the EOF
                        character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Append the current input character to the current
                        DOCTYPE token's public identifier. Stay in the DOCTYPE
                        public identifier (double-quoted) state. */
                     $public = new Buffer([$this->token['public'], $symbol]);
                     $this->token['public'] = $public->toValue();
                     $this->token->source->push($symbol);
                 }
                 break;
             case 'DOCTYPE public identifier (single-quoted)':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "'") {
                     /* U+0027 APOSTROPHE (')
                        Switch to the after DOCTYPE public identifier state. */
                     $this->token->source->push($symbol);
                     $state = 'after DOCTYPE public identifier';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Switch to the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-end-of-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Reconsume the EOF
                        character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Append the current input character to the current
                        DOCTYPE token's public identifier. Stay in the DOCTYPE
                        public identifier (double-quoted) state. */
                     $public = new Buffer([$this->token['public'], $symbol]);
                     $this->token['public'] = $public->toValue();
                     $this->token->source->push($symbol);
                 }
                 break;
             case 'after DOCTYPE public identifier':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                           U+000A LINE FEED (LF)
                           U+000C FORM FEED (FF)
                           U+0020 SPACE
                        Stay in $symbol after DOCTYPE public identifier state. */
                     $this->token->source->push($symbol);
                 } elseif ($symbol === '"') {
                     /* U+0022 QUOTATION MARK (")
                        Set the DOCTYPE token's system identifier to the
                        empty string (not missing), then switch to the DOCTYPE
                        system identifier (double-quoted) state. */
                     $this->token['system'] = '';
                     $this->token->source->push($symbol);
                     $state = 'DOCTYPE system identifier (double-quoted)';
                 } elseif ($symbol === "'") {
                     /* U+0027 APOSTROPHE (')
                        Set the DOCTYPE token's system identifier to the
                        empty string (not missing), then switch to the DOCTYPE
                        system identifier (single-quoted) state. */
                     $this->token['system'] = '';
                     $this->token->source->push($symbol);
                     $state = 'DOCTYPE system identifier (single-quoted)';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the current DOCTYPE token. Switch to the data state. */
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* Parse error. Set the DOCTYPE token's force-quirks
                        flag to on. Emit that DOCTYPE token. Reconsume the EOF
                        character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Switch to the bogus DOCTYPE state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-char-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->token->source->push($symbol);
                     $state = 'bogus DOCTYPE';
                 }
                 break;
             case 'before DOCTYPE system identifier':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                           U+000A LINE FEED (LF)
                           U+000C FORM FEED (FF)
                           U+0020 SPACE
                        Stay in the before DOCTYPE system identifier state. */
                     $this->token->source->push($symbol);
                 } elseif ($symbol === '"') {
                     /* U+0022 QUOTATION MARK (")
                        Set the DOCTYPE token's system identifier to the empty
                        string (not missing), then switch to the DOCTYPE system
                        identifier (double-quoted) state. */
                     $this->token['system'] = '';
                     $this->token->source->push($symbol);
                     $state = 'DOCTYPE system identifier (double-quoted)';
                 } elseif ($symbol === "'") {
                     /* U+0027 APOSTROPHE (')
                        Set the DOCTYPE token's system identifier to the empty
                        string (not missing), then switch to the DOCTYPE system
                        identifier (single-quoted) state. */
                     $this->token['system'] = '';
                     $this->token->source->push($symbol);
                     $state = 'DOCTYPE system identifier (single-quoted)';
                 } elseif ($symbol === '>') {
                     /* Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Switch to the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-char-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* Parse error. Set the DOCTYPE token's force-quirks
                        flag to on. Emit that DOCTYPE token. Reconsume the EOF
                        character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Switch to the bogus DOCTYPE state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-char-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->token->source->push($symbol);
                     $state = 'bogus DOCTYPE';
                 }
                 break;
             case 'DOCTYPE system identifier (double-quoted)':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === '"') {
                     /* U+0022 QUOTATION MARK (")
                        Switch to the after DOCTYPE system identifier state. */
                     $this->token->source->push($symbol);
                     $state = 'after DOCTYPE system identifier';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Switch to the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-end-of-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Reconsume the EOF
                        character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Append the current input character to the current
                        DOCTYPE token's system identifier. Stay in the DOCTYPE
                        system identifier (double-quoted) state. */
                     $system = new Buffer([$this->token['system'], $symbol]);
                     $this->token['system'] .= $system->toValue();
                     $this->token->source->push($symbol);
                 }
                 break;
             case 'DOCTYPE system identifier (single-quoted)':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "'") {
                     /* U+0027 APOSTROPHE (')
                        Switch to the after DOCTYPE system identifier state. */
                     $this->token->source->push($symbol);
                     $state = 'after DOCTYPE system identifier';
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Switch to the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-end-of-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Parse error. Set the DOCTYPE token's force-quirks flag
                        to on. Emit that DOCTYPE token. Reconsume the EOF
                        character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     /* Anything else
                        Append the current input character to the current
                        DOCTYPE token's system identifier. Stay in the DOCTYPE
                        system identifier (double-quoted) state. */
                     $system = new Buffer([$this->token['system'], $symbol]);
                     $this->token['system'] = $system->toValue();
                     $this->token->source->push($symbol);
                 }
                 break;
             case 'after DOCTYPE system identifier':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === "\t" || $symbol === "\n" || $symbol === "\f" || $symbol === ' ') {
                     /* U+0009 CHARACTER TABULATION
                           U+000A LINE FEED (LF)
                           U+000C FORM FEED (FF)
                           U+0020 SPACE
                        Stay in the after DOCTYPE system identifier state. */
                     $this->token->source->push($symbol);
                 } elseif ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the current DOCTYPE token. Switch to the data state. */
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* Parse error. Set the DOCTYPE token's force-quirks
                        flag to on. Emit that DOCTYPE token. Reconsume the EOF
                        character in the data state. */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'eof-in-doctype'));
                     $this->token['force-quirks'] = true;
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } elseif ($symbol instanceof Node) {
                     $this->token->source->push($symbol);
                 } else {
                     /* Anything else
                        Parse error. Switch to the bogus DOCTYPE state.
                        (This does not set the DOCTYPE token's force-quirks
                        flag to on.) */
                     $this->_emitToken(array('type' => Tokenizer::TOKEN_PARSEERROR, 'data' => 'unexpected-char-in-doctype'));
                     $state = 'bogus DOCTYPE';
                 }
                 break;
             case 'bogus DOCTYPE':
                 /* Consume the next input character: */
                 $symbol = $this->stream->symbol();
                 if ($symbol === '>') {
                     /* U+003E GREATER-THAN SIGN (>)
                        Emit the DOCTYPE token. Switch to the data state. */
                     $this->token->source->push($symbol);
                     $this->_emitToken($this->token->toArray());
                     $state = 'data';
                 } elseif ($symbol === false) {
                     /* EOF
                        Emit the DOCTYPE token. Reconsume the EOF character in
                        the data state. */
                     $this->_emitToken($this->token->toArray());
                     $this->stream->undo();
                     $state = 'data';
                 } else {
                     $this->token->source->push($symbol);
                     /* Anything else
                        Stay in the bogus DOCTYPE state. */
                 }
                 break;
                 // case 'cdataSection':
         }
     }
     return $this->getResult();
 }
Example #3
0
 /**
  * @param string|Buffer $compareTo
  * @return bool
  */
 public function equals($compareTo)
 {
     if ($compareTo instanceof Buffer) {
         // cheap checks
         if ($this->getTextLength() != $compareTo->getTextLength()) {
             return false;
         }
         if ($this->getLength() != $compareTo->getLength()) {
             return false;
         }
         // full check
         $current_value = $this->toValue();
         $value = $compareTo->toValue();
         if (!is_array($current_value) && !is_array($value)) {
             return $current_value === $value;
         } elseif (is_array($current_value) && is_array($value)) {
             for ($i = 0; $i < count($current_value); $i++) {
                 if ($current_value[$i] !== $value[$i]) {
                     return false;
                 }
             }
             return true;
         }
         return false;
     } else {
         return $this->strLength == $this->length && $this->strLength == strlen($compareTo) && $this->__toString() === $compareTo;
     }
 }
 /**
  * @return array
  */
 public function toArray()
 {
     return ['name' => $this->name->toValue(), 'value' => $this->value->toValue()];
 }