4 * Experimental HTML5-based parser using Jeroen van der Meer's PH5P library.
5 * Occupies space in the HTML5 pseudo-namespace, which may cause conflicts.
8 * Recent changes to PHP's DOM extension have resulted in some fatal
9 * error conditions with the original version of PH5P. Pending changes,
10 * this lexer will punt to DirectLex if DOM throws an exception.
13 class HTMLPurifier_Lexer_PH5P
extends HTMLPurifier_Lexer_DOMLex
17 * @param HTMLPurifier_Config $config
18 * @param HTMLPurifier_Context $context
19 * @return HTMLPurifier_Token[]
21 public function tokenizeHTML($html, $config, $context)
23 $new_html = $this->normalize($html, $config, $context);
24 $new_html = $this->wrapHTML($new_html, $config, $context, false /* no div */);
26 $parser = new HTML5($new_html);
27 $doc = $parser->save();
28 } catch (DOMException
$e) {
29 // Uh oh, it failed. Punt to DirectLex.
30 $lexer = new HTMLPurifier_Lexer_DirectLex();
31 $context->register('PH5PError', $e); // save the error, so we can detect it
32 return $lexer->tokenizeHTML($html, $config, $context); // use original HTML
36 $doc->getElementsByTagName('html')->item(0)-> // <html>
37 getElementsByTagName('body')->item(0) // <body>
47 Copyright 2007 Jeroen van der Meer <http://jero.net/>
49 Permission is hereby granted, free of charge, to any person obtaining a
50 copy of this software and associated documentation files (the
51 "Software"), to deal in the Software without restriction, including
52 without limitation the rights to use, copy, modify, merge, publish,
53 distribute, sublicense, and/or sell copies of the Software, and to
54 permit persons to whom the Software is furnished to do so, subject to
55 the following conditions:
57 The above copyright notice and this permission notice shall be included
58 in all copies or substantial portions of the Software.
60 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
61 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
63 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
64 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
65 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
66 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
78 private $content_model;
79 private $escape = false;
80 private $entities = array(
461 public function __construct($data)
465 $this->EOF
= strlen($data);
466 $this->tree
= new HTML5TreeConstructer
;
467 $this->content_model
= self
::PCDATA
;
469 $this->state
= 'data';
471 while ($this->state
!== null) {
472 $this->{$this->state
. 'State'}();
476 public function save()
478 return $this->tree
->save();
481 private function char()
483 return ($this->char
< $this->EOF
)
484 ?
$this->data
[$this->char
]
488 private function character($s, $l = 0)
490 if ($s +
$l < $this->EOF
) {
492 return $this->data
[$s];
494 return substr($this->data
, $s, $l);
499 private function characters($char_class, $start)
501 return preg_replace('#^([' . $char_class . ']+).*#s', '\\1', substr($this->data
, $start));
504 private function dataState()
506 // Consume the next input character
508 $char = $this->char();
510 if ($char === '&' && ($this->content_model
=== self
::PCDATA ||
$this->content_model
=== self
::RCDATA
)) {
511 /* U+0026 AMPERSAND (&)
512 When the content model flag is set to one of the PCDATA or RCDATA
513 states: switch to the entity data state. Otherwise: treat it as per
514 the "anything else" entry below. */
515 $this->state
= 'entityData';
517 } elseif ($char === '-') {
518 /* If the content model flag is set to either the RCDATA state or
519 the CDATA state, and the escape flag is false, and there are at
520 least three characters before this one in the input stream, and the
521 last four characters in the input stream, including this one, are
522 U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+002D HYPHEN-MINUS,
523 and U+002D HYPHEN-MINUS ("<!--"), then set the escape flag to true. */
524 if (($this->content_model
=== self
::RCDATA ||
$this->content_model
===
525 self
::CDATA
) && $this->escape
=== false &&
526 $this->char
>= 3 && $this->character($this->char
- 4, 4) === '<!--'
528 $this->escape
= true;
531 /* In any case, emit the input character as a character token. Stay
532 in the data state. */
535 'type' => self
::CHARACTR
,
540 /* U+003C LESS-THAN SIGN (<) */
541 } elseif ($char === '<' && ($this->content_model
=== self
::PCDATA ||
542 (($this->content_model
=== self
::RCDATA ||
543 $this->content_model
=== self
::CDATA
) && $this->escape
=== false))
545 /* When the content model flag is set to the PCDATA state: switch
546 to the tag open state.
548 When the content model flag is set to either the RCDATA state or
549 the CDATA state and the escape flag is false: switch to the tag
552 Otherwise: treat it as per the "anything else" entry below. */
553 $this->state
= 'tagOpen';
555 /* U+003E GREATER-THAN SIGN (>) */
556 } elseif ($char === '>') {
557 /* If the content model flag is set to either the RCDATA state or
558 the CDATA state, and the escape flag is true, and the last three
559 characters in the input stream including this one are U+002D
560 HYPHEN-MINUS, U+002D HYPHEN-MINUS, U+003E GREATER-THAN SIGN ("-->"),
561 set the escape flag to false. */
562 if (($this->content_model
=== self
::RCDATA ||
563 $this->content_model
=== self
::CDATA
) && $this->escape
=== true &&
564 $this->character($this->char
, 3) === '-->'
566 $this->escape
= false;
569 /* In any case, emit the input character as a character token.
570 Stay in the data state. */
573 'type' => self
::CHARACTR
,
578 } elseif ($this->char
=== $this->EOF
) {
580 Emit an end-of-file token. */
583 } elseif ($this->content_model
=== self
::PLAINTEXT
) {
584 /* When the content model flag is set to the PLAINTEXT state
585 THIS DIFFERS GREATLY FROM THE SPEC: Get the remaining characters of
586 the text and emit it as a character token. */
589 'type' => self
::CHARACTR
,
590 'data' => substr($this->data
, $this->char
)
598 THIS DIFFERS GREATLY FROM THE SPEC: Get as many character that
599 otherwise would also be treated as a character token and emit it
600 as a single character token. Stay in the data state. */
601 $len = strcspn($this->data
, '<&', $this->char
);
602 $char = substr($this->data
, $this->char
, $len);
603 $this->char +
= $len - 1;
607 'type' => self
::CHARACTR
,
612 $this->state
= 'data';
616 private function entityDataState()
618 // Attempt to consume an entity.
619 $entity = $this->entity();
621 // If nothing is returned, emit a U+0026 AMPERSAND character token.
622 // Otherwise, emit the character token that was returned.
623 $char = (!$entity) ?
'&' : $entity;
626 'type' => self
::CHARACTR
,
631 // Finally, switch to the data state.
632 $this->state
= 'data';
635 private function tagOpenState()
637 switch ($this->content_model
) {
640 /* If the next input character is a U+002F SOLIDUS (/) character,
641 consume it and switch to the close tag open state. If the next
642 input character is not a U+002F SOLIDUS (/) character, emit a
643 U+003C LESS-THAN SIGN character token and switch to the data
644 state to process the next input character. */
645 if ($this->character($this->char +
1) === '/') {
647 $this->state
= 'closeTagOpen';
652 'type' => self
::CHARACTR
,
657 $this->state
= 'data';
662 // If the content model flag is set to the PCDATA state
663 // Consume the next input character:
665 $char = $this->char();
668 /* U+0021 EXCLAMATION MARK (!)
669 Switch to the markup declaration open state. */
670 $this->state
= 'markupDeclarationOpen';
672 } elseif ($char === '/') {
673 /* U+002F SOLIDUS (/)
674 Switch to the close tag open state. */
675 $this->state
= 'closeTagOpen';
677 } elseif (preg_match('/^[A-Za-z]$/', $char)) {
678 /* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
679 Create a new start tag token, set its tag name to the lowercase
680 version of the input character (add 0x0020 to the character's code
681 point), then switch to the tag name state. (Don't emit the token
682 yet; further details will be filled in before it is emitted.) */
683 $this->token
= array(
684 'name' => strtolower($char),
685 'type' => self
::STARTTAG
,
689 $this->state
= 'tagName';
691 } elseif ($char === '>') {
692 /* U+003E GREATER-THAN SIGN (>)
693 Parse error. Emit a U+003C LESS-THAN SIGN character token and a
694 U+003E GREATER-THAN SIGN character token. Switch to the data state. */
697 'type' => self
::CHARACTR
,
702 $this->state
= 'data';
704 } elseif ($char === '?') {
705 /* U+003F QUESTION MARK (?)
706 Parse error. Switch to the bogus comment state. */
707 $this->state
= 'bogusComment';
711 Parse error. Emit a U+003C LESS-THAN SIGN character token and
712 reconsume the current input character in the data state. */
715 'type' => self
::CHARACTR
,
721 $this->state
= 'data';
727 private function closeTagOpenState()
729 $next_node = strtolower($this->characters('A-Za-z', $this->char +
1));
730 $the_same = count($this->tree
->stack
) > 0 && $next_node === end($this->tree
->stack
)->nodeName
;
732 if (($this->content_model
=== self
::RCDATA ||
$this->content_model
=== self
::CDATA
) &&
733 (!$the_same ||
($the_same && (!preg_match(
734 '/[\t\n\x0b\x0c >\/]/',
735 $this->character($this->char +
1 +
strlen($next_node))
736 ) ||
$this->EOF
=== $this->char
)))
738 /* If the content model flag is set to the RCDATA or CDATA states then
739 examine the next few characters. If they do not match the tag name of
740 the last start tag token emitted (case insensitively), or if they do but
741 they are not immediately followed by one of the following characters:
742 * U+0009 CHARACTER TABULATION
743 * U+000A LINE FEED (LF)
744 * U+000B LINE TABULATION
745 * U+000C FORM FEED (FF)
747 * U+003E GREATER-THAN SIGN (>)
750 ...then there is a parse error. Emit a U+003C LESS-THAN SIGN character
751 token, a U+002F SOLIDUS character token, and switch to the data state
752 to process the next input character. */
755 'type' => self
::CHARACTR
,
760 $this->state
= 'data';
763 /* Otherwise, if the content model flag is set to the PCDATA state,
764 or if the next few characters do match that tag name, consume the
765 next input character: */
767 $char = $this->char();
769 if (preg_match('/^[A-Za-z]$/', $char)) {
770 /* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
771 Create a new end tag token, set its tag name to the lowercase version
772 of the input character (add 0x0020 to the character's code point), then
773 switch to the tag name state. (Don't emit the token yet; further details
774 will be filled in before it is emitted.) */
775 $this->token
= array(
776 'name' => strtolower($char),
777 'type' => self
::ENDTAG
780 $this->state
= 'tagName';
782 } elseif ($char === '>') {
783 /* U+003E GREATER-THAN SIGN (>)
784 Parse error. Switch to the data state. */
785 $this->state
= 'data';
787 } elseif ($this->char
=== $this->EOF
) {
789 Parse error. Emit a U+003C LESS-THAN SIGN character token and a U+002F
790 SOLIDUS character token. Reconsume the EOF character in the data state. */
793 'type' => self
::CHARACTR
,
799 $this->state
= 'data';
802 /* Parse error. Switch to the bogus comment state. */
803 $this->state
= 'bogusComment';
808 private function tagNameState()
810 // Consume the next input character:
812 $char = $this->character($this->char
);
814 if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
815 /* U+0009 CHARACTER TABULATION
816 U+000A LINE FEED (LF)
817 U+000B LINE TABULATION
818 U+000C FORM FEED (FF)
820 Switch to the before attribute name state. */
821 $this->state
= 'beforeAttributeName';
823 } elseif ($char === '>') {
824 /* U+003E GREATER-THAN SIGN (>)
825 Emit the current tag token. Switch to the data state. */
826 $this->emitToken($this->token
);
827 $this->state
= 'data';
829 } elseif ($this->char
=== $this->EOF
) {
831 Parse error. Emit the current tag token. Reconsume the EOF
832 character in the data state. */
833 $this->emitToken($this->token
);
836 $this->state
= 'data';
838 } elseif ($char === '/') {
839 /* U+002F SOLIDUS (/)
840 Parse error unless this is a permitted slash. Switch to the before
841 attribute name state. */
842 $this->state
= 'beforeAttributeName';
846 Append the current input character to the current tag token's tag name.
847 Stay in the tag name state. */
848 $this->token
['name'] .= strtolower($char);
849 $this->state
= 'tagName';
853 private function beforeAttributeNameState()
855 // Consume the next input character:
857 $char = $this->character($this->char
);
859 if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
860 /* U+0009 CHARACTER TABULATION
861 U+000A LINE FEED (LF)
862 U+000B LINE TABULATION
863 U+000C FORM FEED (FF)
865 Stay in the before attribute name state. */
866 $this->state
= 'beforeAttributeName';
868 } elseif ($char === '>') {
869 /* U+003E GREATER-THAN SIGN (>)
870 Emit the current tag token. Switch to the data state. */
871 $this->emitToken($this->token
);
872 $this->state
= 'data';
874 } elseif ($char === '/') {
875 /* U+002F SOLIDUS (/)
876 Parse error unless this is a permitted slash. Stay in the before
877 attribute name state. */
878 $this->state
= 'beforeAttributeName';
880 } elseif ($this->char
=== $this->EOF
) {
882 Parse error. Emit the current tag token. Reconsume the EOF
883 character in the data state. */
884 $this->emitToken($this->token
);
887 $this->state
= 'data';
891 Start a new attribute in the current tag token. Set that attribute's
892 name to the current input character, and its value to the empty string.
893 Switch to the attribute name state. */
894 $this->token
['attr'][] = array(
895 'name' => strtolower($char),
899 $this->state
= 'attributeName';
903 private function attributeNameState()
905 // Consume the next input character:
907 $char = $this->character($this->char
);
909 if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
910 /* U+0009 CHARACTER TABULATION
911 U+000A LINE FEED (LF)
912 U+000B LINE TABULATION
913 U+000C FORM FEED (FF)
915 Stay in the before attribute name state. */
916 $this->state
= 'afterAttributeName';
918 } elseif ($char === '=') {
919 /* U+003D EQUALS SIGN (=)
920 Switch to the before attribute value state. */
921 $this->state
= 'beforeAttributeValue';
923 } elseif ($char === '>') {
924 /* U+003E GREATER-THAN SIGN (>)
925 Emit the current tag token. Switch to the data state. */
926 $this->emitToken($this->token
);
927 $this->state
= 'data';
929 } elseif ($char === '/' && $this->character($this->char +
1) !== '>') {
930 /* U+002F SOLIDUS (/)
931 Parse error unless this is a permitted slash. Switch to the before
932 attribute name state. */
933 $this->state
= 'beforeAttributeName';
935 } elseif ($this->char
=== $this->EOF
) {
937 Parse error. Emit the current tag token. Reconsume the EOF
938 character in the data state. */
939 $this->emitToken($this->token
);
942 $this->state
= 'data';
946 Append the current input character to the current attribute's name.
947 Stay in the attribute name state. */
948 $last = count($this->token
['attr']) - 1;
949 $this->token
['attr'][$last]['name'] .= strtolower($char);
951 $this->state
= 'attributeName';
955 private function afterAttributeNameState()
957 // Consume the next input character:
959 $char = $this->character($this->char
);
961 if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
962 /* U+0009 CHARACTER TABULATION
963 U+000A LINE FEED (LF)
964 U+000B LINE TABULATION
965 U+000C FORM FEED (FF)
967 Stay in the after attribute name state. */
968 $this->state
= 'afterAttributeName';
970 } elseif ($char === '=') {
971 /* U+003D EQUALS SIGN (=)
972 Switch to the before attribute value state. */
973 $this->state
= 'beforeAttributeValue';
975 } elseif ($char === '>') {
976 /* U+003E GREATER-THAN SIGN (>)
977 Emit the current tag token. Switch to the data state. */
978 $this->emitToken($this->token
);
979 $this->state
= 'data';
981 } elseif ($char === '/' && $this->character($this->char +
1) !== '>') {
982 /* U+002F SOLIDUS (/)
983 Parse error unless this is a permitted slash. Switch to the
984 before attribute name state. */
985 $this->state
= 'beforeAttributeName';
987 } elseif ($this->char
=== $this->EOF
) {
989 Parse error. Emit the current tag token. Reconsume the EOF
990 character in the data state. */
991 $this->emitToken($this->token
);
994 $this->state
= 'data';
998 Start a new attribute in the current tag token. Set that attribute's
999 name to the current input character, and its value to the empty string.
1000 Switch to the attribute name state. */
1001 $this->token
['attr'][] = array(
1002 'name' => strtolower($char),
1006 $this->state
= 'attributeName';
1010 private function beforeAttributeValueState()
1012 // Consume the next input character:
1014 $char = $this->character($this->char
);
1016 if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
1017 /* U+0009 CHARACTER TABULATION
1018 U+000A LINE FEED (LF)
1019 U+000B LINE TABULATION
1020 U+000C FORM FEED (FF)
1022 Stay in the before attribute value state. */
1023 $this->state
= 'beforeAttributeValue';
1025 } elseif ($char === '"') {
1026 /* U+0022 QUOTATION MARK (")
1027 Switch to the attribute value (double-quoted) state. */
1028 $this->state
= 'attributeValueDoubleQuoted';
1030 } elseif ($char === '&') {
1031 /* U+0026 AMPERSAND (&)
1032 Switch to the attribute value (unquoted) state and reconsume
1033 this input character. */
1035 $this->state
= 'attributeValueUnquoted';
1037 } elseif ($char === '\'') {
1038 /* U+0027 APOSTROPHE (')
1039 Switch to the attribute value (single-quoted) state. */
1040 $this->state
= 'attributeValueSingleQuoted';
1042 } elseif ($char === '>') {
1043 /* U+003E GREATER-THAN SIGN (>)
1044 Emit the current tag token. Switch to the data state. */
1045 $this->emitToken($this->token
);
1046 $this->state
= 'data';
1050 Append the current input character to the current attribute's value.
1051 Switch to the attribute value (unquoted) state. */
1052 $last = count($this->token
['attr']) - 1;
1053 $this->token
['attr'][$last]['value'] .= $char;
1055 $this->state
= 'attributeValueUnquoted';
1059 private function attributeValueDoubleQuotedState()
1061 // Consume the next input character:
1063 $char = $this->character($this->char
);
1065 if ($char === '"') {
1066 /* U+0022 QUOTATION MARK (")
1067 Switch to the before attribute name state. */
1068 $this->state
= 'beforeAttributeName';
1070 } elseif ($char === '&') {
1071 /* U+0026 AMPERSAND (&)
1072 Switch to the entity in attribute value state. */
1073 $this->entityInAttributeValueState('double');
1075 } elseif ($this->char
=== $this->EOF
) {
1077 Parse error. Emit the current tag token. Reconsume the character
1078 in the data state. */
1079 $this->emitToken($this->token
);
1082 $this->state
= 'data';
1086 Append the current input character to the current attribute's value.
1087 Stay in the attribute value (double-quoted) state. */
1088 $last = count($this->token
['attr']) - 1;
1089 $this->token
['attr'][$last]['value'] .= $char;
1091 $this->state
= 'attributeValueDoubleQuoted';
1095 private function attributeValueSingleQuotedState()
1097 // Consume the next input character:
1099 $char = $this->character($this->char
);
1101 if ($char === '\'') {
1102 /* U+0022 QUOTATION MARK (')
1103 Switch to the before attribute name state. */
1104 $this->state
= 'beforeAttributeName';
1106 } elseif ($char === '&') {
1107 /* U+0026 AMPERSAND (&)
1108 Switch to the entity in attribute value state. */
1109 $this->entityInAttributeValueState('single');
1111 } elseif ($this->char
=== $this->EOF
) {
1113 Parse error. Emit the current tag token. Reconsume the character
1114 in the data state. */
1115 $this->emitToken($this->token
);
1118 $this->state
= 'data';
1122 Append the current input character to the current attribute's value.
1123 Stay in the attribute value (single-quoted) state. */
1124 $last = count($this->token
['attr']) - 1;
1125 $this->token
['attr'][$last]['value'] .= $char;
1127 $this->state
= 'attributeValueSingleQuoted';
1131 private function attributeValueUnquotedState()
1133 // Consume the next input character:
1135 $char = $this->character($this->char
);
1137 if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
1138 /* U+0009 CHARACTER TABULATION
1139 U+000A LINE FEED (LF)
1140 U+000B LINE TABULATION
1141 U+000C FORM FEED (FF)
1143 Switch to the before attribute name state. */
1144 $this->state
= 'beforeAttributeName';
1146 } elseif ($char === '&') {
1147 /* U+0026 AMPERSAND (&)
1148 Switch to the entity in attribute value state. */
1149 $this->entityInAttributeValueState();
1151 } elseif ($char === '>') {
1152 /* U+003E GREATER-THAN SIGN (>)
1153 Emit the current tag token. Switch to the data state. */
1154 $this->emitToken($this->token
);
1155 $this->state
= 'data';
1159 Append the current input character to the current attribute's value.
1160 Stay in the attribute value (unquoted) state. */
1161 $last = count($this->token
['attr']) - 1;
1162 $this->token
['attr'][$last]['value'] .= $char;
1164 $this->state
= 'attributeValueUnquoted';
1168 private function entityInAttributeValueState()
1170 // Attempt to consume an entity.
1171 $entity = $this->entity();
1173 // If nothing is returned, append a U+0026 AMPERSAND character to the
1174 // current attribute's value. Otherwise, emit the character token that
1180 $last = count($this->token
['attr']) - 1;
1181 $this->token
['attr'][$last]['value'] .= $char;
1184 private function bogusCommentState()
1186 /* Consume every character up to the first U+003E GREATER-THAN SIGN
1187 character (>) or the end of the file (EOF), whichever comes first. Emit
1188 a comment token whose data is the concatenation of all the characters
1189 starting from and including the character that caused the state machine
1190 to switch into the bogus comment state, up to and including the last
1191 consumed character before the U+003E character, if any, or up to the
1192 end of the file otherwise. (If the comment was started by the end of
1193 the file (EOF), the token is empty.) */
1194 $data = $this->characters('^>', $this->char
);
1198 'type' => self
::COMMENT
1202 $this->char +
= strlen($data);
1204 /* Switch to the data state. */
1205 $this->state
= 'data';
1207 /* If the end of the file was reached, reconsume the EOF character. */
1208 if ($this->char
=== $this->EOF
) {
1209 $this->char
= $this->EOF
- 1;
1213 private function markupDeclarationOpenState()
1215 /* If the next two characters are both U+002D HYPHEN-MINUS (-)
1216 characters, consume those two characters, create a comment token whose
1217 data is the empty string, and switch to the comment state. */
1218 if ($this->character($this->char +
1, 2) === '--') {
1220 $this->state
= 'comment';
1221 $this->token
= array(
1223 'type' => self
::COMMENT
1226 /* Otherwise if the next seven chacacters are a case-insensitive match
1227 for the word "DOCTYPE", then consume those characters and switch to the
1229 } elseif (strtolower($this->character($this->char +
1, 7)) === 'doctype') {
1231 $this->state
= 'doctype';
1233 /* Otherwise, is is a parse error. Switch to the bogus comment state.
1234 The next character that is consumed, if any, is the first character
1235 that will be in the comment. */
1238 $this->state
= 'bogusComment';
1242 private function commentState()
1244 /* Consume the next input character: */
1246 $char = $this->char();
1248 /* U+002D HYPHEN-MINUS (-) */
1249 if ($char === '-') {
1250 /* Switch to the comment dash state */
1251 $this->state
= 'commentDash';
1254 } elseif ($this->char
=== $this->EOF
) {
1255 /* Parse error. Emit the comment token. Reconsume the EOF character
1256 in the data state. */
1257 $this->emitToken($this->token
);
1259 $this->state
= 'data';
1263 /* Append the input character to the comment token's data. Stay in
1264 the comment state. */
1265 $this->token
['data'] .= $char;
1269 private function commentDashState()
1271 /* Consume the next input character: */
1273 $char = $this->char();
1275 /* U+002D HYPHEN-MINUS (-) */
1276 if ($char === '-') {
1277 /* Switch to the comment end state */
1278 $this->state
= 'commentEnd';
1281 } elseif ($this->char
=== $this->EOF
) {
1282 /* Parse error. Emit the comment token. Reconsume the EOF character
1283 in the data state. */
1284 $this->emitToken($this->token
);
1286 $this->state
= 'data';
1290 /* Append a U+002D HYPHEN-MINUS (-) character and the input
1291 character to the comment token's data. Switch to the comment state. */
1292 $this->token
['data'] .= '-' . $char;
1293 $this->state
= 'comment';
1297 private function commentEndState()
1299 /* Consume the next input character: */
1301 $char = $this->char();
1303 if ($char === '>') {
1304 $this->emitToken($this->token
);
1305 $this->state
= 'data';
1307 } elseif ($char === '-') {
1308 $this->token
['data'] .= '-';
1310 } elseif ($this->char
=== $this->EOF
) {
1311 $this->emitToken($this->token
);
1313 $this->state
= 'data';
1316 $this->token
['data'] .= '--' . $char;
1317 $this->state
= 'comment';
1321 private function doctypeState()
1323 /* Consume the next input character: */
1325 $char = $this->char();
1327 if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
1328 $this->state
= 'beforeDoctypeName';
1332 $this->state
= 'beforeDoctypeName';
1336 private function beforeDoctypeNameState()
1338 /* Consume the next input character: */
1340 $char = $this->char();
1342 if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
1343 // Stay in the before DOCTYPE name state.
1345 } elseif (preg_match('/^[a-z]$/', $char)) {
1346 $this->token
= array(
1347 'name' => strtoupper($char),
1348 'type' => self
::DOCTYPE
,
1352 $this->state
= 'doctypeName';
1354 } elseif ($char === '>') {
1358 'type' => self
::DOCTYPE
,
1363 $this->state
= 'data';
1365 } elseif ($this->char
=== $this->EOF
) {
1369 'type' => self
::DOCTYPE
,
1375 $this->state
= 'data';
1378 $this->token
= array(
1380 'type' => self
::DOCTYPE
,
1384 $this->state
= 'doctypeName';
1388 private function doctypeNameState()
1390 /* Consume the next input character: */
1392 $char = $this->char();
1394 if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
1395 $this->state
= 'AfterDoctypeName';
1397 } elseif ($char === '>') {
1398 $this->emitToken($this->token
);
1399 $this->state
= 'data';
1401 } elseif (preg_match('/^[a-z]$/', $char)) {
1402 $this->token
['name'] .= strtoupper($char);
1404 } elseif ($this->char
=== $this->EOF
) {
1405 $this->emitToken($this->token
);
1407 $this->state
= 'data';
1410 $this->token
['name'] .= $char;
1413 $this->token
['error'] = ($this->token
['name'] === 'HTML')
1418 private function afterDoctypeNameState()
1420 /* Consume the next input character: */
1422 $char = $this->char();
1424 if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
1425 // Stay in the DOCTYPE name state.
1427 } elseif ($char === '>') {
1428 $this->emitToken($this->token
);
1429 $this->state
= 'data';
1431 } elseif ($this->char
=== $this->EOF
) {
1432 $this->emitToken($this->token
);
1434 $this->state
= 'data';
1437 $this->token
['error'] = true;
1438 $this->state
= 'bogusDoctype';
1442 private function bogusDoctypeState()
1444 /* Consume the next input character: */
1446 $char = $this->char();
1448 if ($char === '>') {
1449 $this->emitToken($this->token
);
1450 $this->state
= 'data';
1452 } elseif ($this->char
=== $this->EOF
) {
1453 $this->emitToken($this->token
);
1455 $this->state
= 'data';
1458 // Stay in the bogus DOCTYPE state.
1462 private function entity()
1464 $start = $this->char
;
1466 // This section defines how to consume an entity. This definition is
1467 // used when parsing entities in text and in attributes.
1469 // The behaviour depends on the identity of the next character (the
1470 // one immediately after the U+0026 AMPERSAND character):
1472 switch ($this->character($this->char +
1)) {
1473 // U+0023 NUMBER SIGN (#)
1476 // The behaviour further depends on the character after the
1477 // U+0023 NUMBER SIGN:
1478 switch ($this->character($this->char +
1)) {
1479 // U+0078 LATIN SMALL LETTER X
1480 // U+0058 LATIN CAPITAL LETTER X
1483 // Follow the steps below, but using the range of
1484 // characters U+0030 DIGIT ZERO through to U+0039 DIGIT
1485 // NINE, U+0061 LATIN SMALL LETTER A through to U+0066
1486 // LATIN SMALL LETTER F, and U+0041 LATIN CAPITAL LETTER
1487 // A, through to U+0046 LATIN CAPITAL LETTER F (in other
1488 // words, 0-9, A-F, a-f).
1490 $char_class = '0-9A-Fa-f';
1495 // Follow the steps below, but using the range of
1496 // characters U+0030 DIGIT ZERO through to U+0039 DIGIT
1497 // NINE (i.e. just 0-9).
1499 $char_class = '0-9';
1503 // Consume as many characters as match the range of characters
1506 $e_name = $this->characters($char_class, $this->char +
$char +
1);
1507 $entity = $this->character($start, $this->char
);
1508 $cond = strlen($e_name) > 0;
1510 // The rest of the parsing happens bellow.
1515 // Consume the maximum number of characters possible, with the
1516 // consumed characters case-sensitively matching one of the
1517 // identifiers in the first column of the entities table.
1519 $e_name = $this->characters('0-9A-Za-z;', $this->char +
1);
1520 $len = strlen($e_name);
1522 for ($c = 1; $c <= $len; $c++
) {
1523 $id = substr($e_name, 0, $c);
1526 if (in_array($id, $this->entities
)) {
1527 if ($e_name[$c - 1] !== ';') {
1528 if ($c < $len && $e_name[$c] == ';') {
1529 $this->char++
; // consume extra semicolon
1537 $cond = isset($entity);
1538 // The rest of the parsing happens bellow.
1543 // If no match can be made, then this is a parse error. No
1544 // characters are consumed, and nothing is returned.
1545 $this->char
= $start;
1549 // Return a character token for the character corresponding to the
1550 // entity name (as given by the second column of the entities table).
1551 return html_entity_decode('&' . rtrim($entity, ';') . ';', ENT_QUOTES
, 'UTF-8');
1554 private function emitToken($token)
1556 $emit = $this->tree
->emitToken($token);
1558 if (is_int($emit)) {
1559 $this->content_model
= $emit;
1561 } elseif ($token['type'] === self
::ENDTAG
) {
1562 $this->content_model
= self
::PCDATA
;
1566 private function EOF()
1568 $this->state
= null;
1569 $this->tree
->emitToken(
1577 class HTML5TreeConstructer
1579 public $stack = array();
1584 private $foster_parent = null;
1585 private $a_formatting = array();
1587 private $head_pointer = null;
1588 private $form_pointer = null;
1590 private $scoping = array('button', 'caption', 'html', 'marquee', 'object', 'table', 'td', 'th');
1591 private $formatting = array(
1606 private $special = array(
1670 // The different phases.
1671 const INIT_PHASE
= 0;
1672 const ROOT_PHASE
= 1;
1673 const MAIN_PHASE
= 2;
1674 const END_PHASE
= 3;
1676 // The different insertion modes for the main phase.
1677 const BEFOR_HEAD
= 0;
1679 const AFTER_HEAD
= 2;
1682 const IN_CAPTION
= 5;
1683 const IN_CGROUP
= 6;
1687 const IN_SELECT
= 10;
1688 const AFTER_BODY
= 11;
1689 const IN_FRAME
= 12;
1690 const AFTR_FRAME
= 13;
1692 // The different types of elements.
1695 const FORMATTING
= 2;
1700 public function __construct()
1702 $this->phase
= self
::INIT_PHASE
;
1703 $this->mode
= self
::BEFOR_HEAD
;
1704 $this->dom
= new DOMDocument
;
1706 $this->dom
->encoding
= 'UTF-8';
1707 $this->dom
->preserveWhiteSpace
= true;
1708 $this->dom
->substituteEntities
= true;
1709 $this->dom
->strictErrorChecking
= false;
1712 // Process tag tokens
1713 public function emitToken($token)
1715 switch ($this->phase
) {
1716 case self
::INIT_PHASE
:
1717 return $this->initPhase($token);
1719 case self
::ROOT_PHASE
:
1720 return $this->rootElementPhase($token);
1722 case self
::MAIN_PHASE
:
1723 return $this->mainPhase($token);
1725 case self
::END_PHASE
:
1726 return $this->trailingEndPhase($token);
1731 private function initPhase($token)
1733 /* Initially, the tree construction stage must handle each token
1734 emitted from the tokenisation stage as follows: */
1736 /* A DOCTYPE token that is marked as being in error
1740 A character token that is not one of one of U+0009 CHARACTER TABULATION,
1741 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
1743 An end-of-file token */
1744 if ((isset($token['error']) && $token['error']) ||
1745 $token['type'] === HTML5
::COMMENT ||
1746 $token['type'] === HTML5
::STARTTAG ||
1747 $token['type'] === HTML5
::ENDTAG ||
1748 $token['type'] === HTML5
::EOF ||
1749 ($token['type'] === HTML5
::CHARACTR
&& isset($token['data']) &&
1750 !preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data']))
1752 /* This specification does not define how to handle this case. In
1753 particular, user agents may ignore the entirety of this specification
1754 altogether for such documents, and instead invoke special parse modes
1755 with a greater emphasis on backwards compatibility. */
1757 $this->phase
= self
::ROOT_PHASE
;
1758 return $this->rootElementPhase($token);
1760 /* A DOCTYPE token marked as being correct */
1761 } elseif (isset($token['error']) && !$token['error']) {
1762 /* Append a DocumentType node to the Document node, with the name
1763 attribute set to the name given in the DOCTYPE token (which will be
1764 "HTML"), and the other attributes specific to DocumentType objects
1765 set to null, empty lists, or the empty string as appropriate. */
1766 $doctype = new DOMDocumentType(null, null, 'HTML');
1768 /* Then, switch to the root element phase of the tree construction
1770 $this->phase
= self
::ROOT_PHASE
;
1772 /* A character token that is one of one of U+0009 CHARACTER TABULATION,
1773 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
1775 } elseif (isset($token['data']) && preg_match(
1776 '/^[\t\n\x0b\x0c ]+$/',
1780 /* Append that character to the Document node. */
1781 $text = $this->dom
->createTextNode($token['data']);
1782 $this->dom
->appendChild($text);
1786 private function rootElementPhase($token)
1788 /* After the initial phase, as each token is emitted from the tokenisation
1789 stage, it must be processed as described in this section. */
1791 /* A DOCTYPE token */
1792 if ($token['type'] === HTML5
::DOCTYPE
) {
1793 // Parse error. Ignore the token.
1795 /* A comment token */
1796 } elseif ($token['type'] === HTML5
::COMMENT
) {
1797 /* Append a Comment node to the Document object with the data
1798 attribute set to the data given in the comment token. */
1799 $comment = $this->dom
->createComment($token['data']);
1800 $this->dom
->appendChild($comment);
1802 /* A character token that is one of one of U+0009 CHARACTER TABULATION,
1803 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
1805 } elseif ($token['type'] === HTML5
::CHARACTR
&&
1806 preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
1808 /* Append that character to the Document node. */
1809 $text = $this->dom
->createTextNode($token['data']);
1810 $this->dom
->appendChild($text);
1812 /* A character token that is not one of U+0009 CHARACTER TABULATION,
1813 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED
1814 (FF), or U+0020 SPACE
1817 An end-of-file token */
1818 } elseif (($token['type'] === HTML5
::CHARACTR
&&
1819 !preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) ||
1820 $token['type'] === HTML5
::STARTTAG ||
1821 $token['type'] === HTML5
::ENDTAG ||
1822 $token['type'] === HTML5
::EOF
1824 /* Create an HTMLElement node with the tag name html, in the HTML
1825 namespace. Append it to the Document object. Switch to the main
1826 phase and reprocess the current token. */
1827 $html = $this->dom
->createElement('html');
1828 $this->dom
->appendChild($html);
1829 $this->stack
[] = $html;
1831 $this->phase
= self
::MAIN_PHASE
;
1832 return $this->mainPhase($token);
1836 private function mainPhase($token)
1838 /* Tokens in the main phase must be handled as follows: */
1840 /* A DOCTYPE token */
1841 if ($token['type'] === HTML5
::DOCTYPE
) {
1842 // Parse error. Ignore the token.
1844 /* A start tag token with the tag name "html" */
1845 } elseif ($token['type'] === HTML5
::STARTTAG
&& $token['name'] === 'html') {
1846 /* If this start tag token was not the first start tag token, then
1847 it is a parse error. */
1849 /* For each attribute on the token, check to see if the attribute
1850 is already present on the top element of the stack of open elements.
1851 If it is not, add the attribute and its corresponding value to that
1853 foreach ($token['attr'] as $attr) {
1854 if (!$this->stack
[0]->hasAttribute($attr['name'])) {
1855 $this->stack
[0]->setAttribute($attr['name'], $attr['value']);
1859 /* An end-of-file token */
1860 } elseif ($token['type'] === HTML5
::EOF
) {
1861 /* Generate implied end tags. */
1862 $this->generateImpliedEndTags();
1864 /* Anything else. */
1866 /* Depends on the insertion mode: */
1867 switch ($this->mode
) {
1868 case self
::BEFOR_HEAD
:
1869 return $this->beforeHead($token);
1872 return $this->inHead($token);
1874 case self
::AFTER_HEAD
:
1875 return $this->afterHead($token);
1878 return $this->inBody($token);
1880 case self
::IN_TABLE
:
1881 return $this->inTable($token);
1883 case self
::IN_CAPTION
:
1884 return $this->inCaption($token);
1886 case self
::IN_CGROUP
:
1887 return $this->inColumnGroup($token);
1889 case self
::IN_TBODY
:
1890 return $this->inTableBody($token);
1893 return $this->inRow($token);
1896 return $this->inCell($token);
1898 case self
::IN_SELECT
:
1899 return $this->inSelect($token);
1901 case self
::AFTER_BODY
:
1902 return $this->afterBody($token);
1904 case self
::IN_FRAME
:
1905 return $this->inFrameset($token);
1907 case self
::AFTR_FRAME
:
1908 return $this->afterFrameset($token);
1910 case self
::END_PHASE
:
1911 return $this->trailingEndPhase($token);
1917 private function beforeHead($token)
1919 /* Handle the token as follows: */
1921 /* A character token that is one of one of U+0009 CHARACTER TABULATION,
1922 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
1924 if ($token['type'] === HTML5
::CHARACTR
&&
1925 preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
1927 /* Append the character to the current node. */
1928 $this->insertText($token['data']);
1930 /* A comment token */
1931 } elseif ($token['type'] === HTML5
::COMMENT
) {
1932 /* Append a Comment node to the current node with the data attribute
1933 set to the data given in the comment token. */
1934 $this->insertComment($token['data']);
1936 /* A start tag token with the tag name "head" */
1937 } elseif ($token['type'] === HTML5
::STARTTAG
&& $token['name'] === 'head') {
1938 /* Create an element for the token, append the new element to the
1939 current node and push it onto the stack of open elements. */
1940 $element = $this->insertElement($token);
1942 /* Set the head element pointer to this new element node. */
1943 $this->head_pointer
= $element;
1945 /* Change the insertion mode to "in head". */
1946 $this->mode
= self
::IN_HEAD
;
1948 /* A start tag token whose tag name is one of: "base", "link", "meta",
1949 "script", "style", "title". Or an end tag with the tag name "html".
1950 Or a character token that is not one of U+0009 CHARACTER TABULATION,
1951 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
1952 or U+0020 SPACE. Or any other start tag token */
1953 } elseif ($token['type'] === HTML5
::STARTTAG ||
1954 ($token['type'] === HTML5
::ENDTAG
&& $token['name'] === 'html') ||
1955 ($token['type'] === HTML5
::CHARACTR
&& !preg_match(
1956 '/^[\t\n\x0b\x0c ]$/',
1960 /* Act as if a start tag token with the tag name "head" and no
1961 attributes had been seen, then reprocess the current token. */
1965 'type' => HTML5
::STARTTAG
,
1970 return $this->inHead($token);
1972 /* Any other end tag */
1973 } elseif ($token['type'] === HTML5
::ENDTAG
) {
1974 /* Parse error. Ignore the token. */
1978 private function inHead($token)
1980 /* Handle the token as follows: */
1982 /* A character token that is one of one of U+0009 CHARACTER TABULATION,
1983 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
1986 THIS DIFFERS FROM THE SPEC: If the current node is either a title, style
1987 or script element, append the character to the current node regardless
1989 if (($token['type'] === HTML5
::CHARACTR
&&
1990 preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) ||
(
1991 $token['type'] === HTML5
::CHARACTR
&& in_array(
1992 end($this->stack
)->nodeName
,
1993 array('title', 'style', 'script')
1996 /* Append the character to the current node. */
1997 $this->insertText($token['data']);
1999 /* A comment token */
2000 } elseif ($token['type'] === HTML5
::COMMENT
) {
2001 /* Append a Comment node to the current node with the data attribute
2002 set to the data given in the comment token. */
2003 $this->insertComment($token['data']);
2005 } elseif ($token['type'] === HTML5
::ENDTAG
&&
2006 in_array($token['name'], array('title', 'style', 'script'))
2008 array_pop($this->stack
);
2009 return HTML5
::PCDATA
;
2011 /* A start tag with the tag name "title" */
2012 } elseif ($token['type'] === HTML5
::STARTTAG
&& $token['name'] === 'title') {
2013 /* Create an element for the token and append the new element to the
2014 node pointed to by the head element pointer, or, if that is null
2015 (innerHTML case), to the current node. */
2016 if ($this->head_pointer
!== null) {
2017 $element = $this->insertElement($token, false);
2018 $this->head_pointer
->appendChild($element);
2021 $element = $this->insertElement($token);
2024 /* Switch the tokeniser's content model flag to the RCDATA state. */
2025 return HTML5
::RCDATA
;
2027 /* A start tag with the tag name "style" */
2028 } elseif ($token['type'] === HTML5
::STARTTAG
&& $token['name'] === 'style') {
2029 /* Create an element for the token and append the new element to the
2030 node pointed to by the head element pointer, or, if that is null
2031 (innerHTML case), to the current node. */
2032 if ($this->head_pointer
!== null) {
2033 $element = $this->insertElement($token, false);
2034 $this->head_pointer
->appendChild($element);
2037 $this->insertElement($token);
2040 /* Switch the tokeniser's content model flag to the CDATA state. */
2041 return HTML5
::CDATA
;
2043 /* A start tag with the tag name "script" */
2044 } elseif ($token['type'] === HTML5
::STARTTAG
&& $token['name'] === 'script') {
2045 /* Create an element for the token. */
2046 $element = $this->insertElement($token, false);
2047 $this->head_pointer
->appendChild($element);
2049 /* Switch the tokeniser's content model flag to the CDATA state. */
2050 return HTML5
::CDATA
;
2052 /* A start tag with the tag name "base", "link", or "meta" */
2053 } elseif ($token['type'] === HTML5
::STARTTAG
&& in_array(
2055 array('base', 'link', 'meta')
2058 /* Create an element for the token and append the new element to the
2059 node pointed to by the head element pointer, or, if that is null
2060 (innerHTML case), to the current node. */
2061 if ($this->head_pointer
!== null) {
2062 $element = $this->insertElement($token, false);
2063 $this->head_pointer
->appendChild($element);
2064 array_pop($this->stack
);
2067 $this->insertElement($token);
2070 /* An end tag with the tag name "head" */
2071 } elseif ($token['type'] === HTML5
::ENDTAG
&& $token['name'] === 'head') {
2072 /* If the current node is a head element, pop the current node off
2073 the stack of open elements. */
2074 if ($this->head_pointer
->isSameNode(end($this->stack
))) {
2075 array_pop($this->stack
);
2077 /* Otherwise, this is a parse error. */
2082 /* Change the insertion mode to "after head". */
2083 $this->mode
= self
::AFTER_HEAD
;
2085 /* A start tag with the tag name "head" or an end tag except "html". */
2086 } elseif (($token['type'] === HTML5
::STARTTAG
&& $token['name'] === 'head') ||
2087 ($token['type'] === HTML5
::ENDTAG
&& $token['name'] !== 'html')
2089 // Parse error. Ignore the token.
2093 /* If the current node is a head element, act as if an end tag
2094 token with the tag name "head" had been seen. */
2095 if ($this->head_pointer
->isSameNode(end($this->stack
))) {
2099 'type' => HTML5
::ENDTAG
2103 /* Otherwise, change the insertion mode to "after head". */
2105 $this->mode
= self
::AFTER_HEAD
;
2108 /* Then, reprocess the current token. */
2109 return $this->afterHead($token);
2113 private function afterHead($token)
2115 /* Handle the token as follows: */
2117 /* A character token that is one of one of U+0009 CHARACTER TABULATION,
2118 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
2120 if ($token['type'] === HTML5
::CHARACTR
&&
2121 preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
2123 /* Append the character to the current node. */
2124 $this->insertText($token['data']);
2126 /* A comment token */
2127 } elseif ($token['type'] === HTML5
::COMMENT
) {
2128 /* Append a Comment node to the current node with the data attribute
2129 set to the data given in the comment token. */
2130 $this->insertComment($token['data']);
2132 /* A start tag token with the tag name "body" */
2133 } elseif ($token['type'] === HTML5
::STARTTAG
&& $token['name'] === 'body') {
2134 /* Insert a body element for the token. */
2135 $this->insertElement($token);
2137 /* Change the insertion mode to "in body". */
2138 $this->mode
= self
::IN_BODY
;
2140 /* A start tag token with the tag name "frameset" */
2141 } elseif ($token['type'] === HTML5
::STARTTAG
&& $token['name'] === 'frameset') {
2142 /* Insert a frameset element for the token. */
2143 $this->insertElement($token);
2145 /* Change the insertion mode to "in frameset". */
2146 $this->mode
= self
::IN_FRAME
;
2148 /* A start tag token whose tag name is one of: "base", "link", "meta",
2149 "script", "style", "title" */
2150 } elseif ($token['type'] === HTML5
::STARTTAG
&& in_array(
2152 array('base', 'link', 'meta', 'script', 'style', 'title')
2155 /* Parse error. Switch the insertion mode back to "in head" and
2156 reprocess the token. */
2157 $this->mode
= self
::IN_HEAD
;
2158 return $this->inHead($token);
2162 /* Act as if a start tag token with the tag name "body" and no
2163 attributes had been seen, and then reprocess the current token. */
2167 'type' => HTML5
::STARTTAG
,
2172 return $this->inBody($token);
2176 private function inBody($token)
2178 /* Handle the token as follows: */
2180 switch ($token['type']) {
2181 /* A character token */
2182 case HTML5
::CHARACTR
:
2183 /* Reconstruct the active formatting elements, if any. */
2184 $this->reconstructActiveFormattingElements();
2186 /* Append the token's character to the current node. */
2187 $this->insertText($token['data']);
2190 /* A comment token */
2191 case HTML5
::COMMENT
:
2192 /* Append a Comment node to the current node with the data
2193 attribute set to the data given in the comment token. */
2194 $this->insertComment($token['data']);
2197 case HTML5
::STARTTAG
:
2198 switch ($token['name']) {
2199 /* A start tag token whose tag name is one of: "script",
2203 /* Process the token as if the insertion mode had been "in
2205 return $this->inHead($token);
2208 /* A start tag token whose tag name is one of: "base", "link",
2214 /* Parse error. Process the token as if the insertion mode
2215 had been "in head". */
2216 return $this->inHead($token);
2219 /* A start tag token with the tag name "body" */
2221 /* Parse error. If the second element on the stack of open
2222 elements is not a body element, or, if the stack of open
2223 elements has only one node on it, then ignore the token.
2225 if (count($this->stack
) === 1 ||
$this->stack
[1]->nodeName
!== 'body') {
2228 /* Otherwise, for each attribute on the token, check to see
2229 if the attribute is already present on the body element (the
2230 second element) on the stack of open elements. If it is not,
2231 add the attribute and its corresponding value to that
2234 foreach ($token['attr'] as $attr) {
2235 if (!$this->stack
[1]->hasAttribute($attr['name'])) {
2236 $this->stack
[1]->setAttribute($attr['name'], $attr['value']);
2242 /* A start tag whose tag name is one of: "address",
2243 "blockquote", "center", "dir", "div", "dl", "fieldset",
2244 "listing", "menu", "ol", "p", "ul" */
2257 /* If the stack of open elements has a p element in scope,
2258 then act as if an end tag with the tag name p had been
2260 if ($this->elementInScope('p')) {
2264 'type' => HTML5
::ENDTAG
2269 /* Insert an HTML element for the token. */
2270 $this->insertElement($token);
2273 /* A start tag whose tag name is "form" */
2275 /* If the form element pointer is not null, ignore the
2276 token with a parse error. */
2277 if ($this->form_pointer
!== null) {
2282 /* If the stack of open elements has a p element in
2283 scope, then act as if an end tag with the tag name p
2285 if ($this->elementInScope('p')) {
2289 'type' => HTML5
::ENDTAG
2294 /* Insert an HTML element for the token, and set the
2295 form element pointer to point to the element created. */
2296 $element = $this->insertElement($token);
2297 $this->form_pointer
= $element;
2301 /* A start tag whose tag name is "li", "dd" or "dt" */
2305 /* If the stack of open elements has a p element in scope,
2306 then act as if an end tag with the tag name p had been
2308 if ($this->elementInScope('p')) {
2312 'type' => HTML5
::ENDTAG
2317 $stack_length = count($this->stack
) - 1;
2319 for ($n = $stack_length; 0 <= $n; $n--) {
2320 /* 1. Initialise node to be the current node (the
2321 bottommost node of the stack). */
2323 $node = $this->stack
[$n];
2324 $cat = $this->getElementCategory($node->tagName
);
2326 /* 2. If node is an li, dd or dt element, then pop all
2327 the nodes from the current node up to node, including
2328 node, then stop this algorithm. */
2329 if ($token['name'] === $node->tagName ||
($token['name'] !== 'li'
2330 && ($node->tagName
=== 'dd' ||
$node->tagName
=== 'dt'))
2332 for ($x = $stack_length; $x >= $n; $x--) {
2333 array_pop($this->stack
);
2339 /* 3. If node is not in the formatting category, and is
2340 not in the phrasing category, and is not an address or
2341 div element, then stop this algorithm. */
2342 if ($cat !== self
::FORMATTING
&& $cat !== self
::PHRASING
&&
2343 $node->tagName
!== 'address' && $node->tagName
!== 'div'
2349 /* Finally, insert an HTML element with the same tag
2350 name as the token's. */
2351 $this->insertElement($token);
2354 /* A start tag token whose tag name is "plaintext" */
2356 /* If the stack of open elements has a p element in scope,
2357 then act as if an end tag with the tag name p had been
2359 if ($this->elementInScope('p')) {
2363 'type' => HTML5
::ENDTAG
2368 /* Insert an HTML element for the token. */
2369 $this->insertElement($token);
2371 return HTML5
::PLAINTEXT
;
2374 /* A start tag whose tag name is one of: "h1", "h2", "h3", "h4",
2382 /* If the stack of open elements has a p element in scope,
2383 then act as if an end tag with the tag name p had been seen. */
2384 if ($this->elementInScope('p')) {
2388 'type' => HTML5
::ENDTAG
2393 /* If the stack of open elements has in scope an element whose
2394 tag name is one of "h1", "h2", "h3", "h4", "h5", or "h6", then
2395 this is a parse error; pop elements from the stack until an
2396 element with one of those tag names has been popped from the
2398 while ($this->elementInScope(array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'))) {
2399 array_pop($this->stack
);
2402 /* Insert an HTML element for the token. */
2403 $this->insertElement($token);
2406 /* A start tag whose tag name is "a" */
2408 /* If the list of active formatting elements contains
2409 an element whose tag name is "a" between the end of the
2410 list and the last marker on the list (or the start of
2411 the list if there is no marker on the list), then this
2412 is a parse error; act as if an end tag with the tag name
2413 "a" had been seen, then remove that element from the list
2414 of active formatting elements and the stack of open
2415 elements if the end tag didn't already remove it (it
2416 might not have if the element is not in table scope). */
2417 $leng = count($this->a_formatting
);
2419 for ($n = $leng - 1; $n >= 0; $n--) {
2420 if ($this->a_formatting
[$n] === self
::MARKER
) {
2423 } elseif ($this->a_formatting
[$n]->nodeName
=== 'a') {
2427 'type' => HTML5
::ENDTAG
2434 /* Reconstruct the active formatting elements, if any. */
2435 $this->reconstructActiveFormattingElements();
2437 /* Insert an HTML element for the token. */
2438 $el = $this->insertElement($token);
2440 /* Add that element to the list of active formatting
2442 $this->a_formatting
[] = $el;
2445 /* A start tag whose tag name is one of: "b", "big", "em", "font",
2446 "i", "nobr", "s", "small", "strike", "strong", "tt", "u" */
2459 /* Reconstruct the active formatting elements, if any. */
2460 $this->reconstructActiveFormattingElements();
2462 /* Insert an HTML element for the token. */
2463 $el = $this->insertElement($token);
2465 /* Add that element to the list of active formatting
2467 $this->a_formatting
[] = $el;
2470 /* A start tag token whose tag name is "button" */
2472 /* If the stack of open elements has a button element in scope,
2473 then this is a parse error; act as if an end tag with the tag
2474 name "button" had been seen, then reprocess the token. (We don't
2475 do that. Unnecessary.) */
2476 if ($this->elementInScope('button')) {
2480 'type' => HTML5
::ENDTAG
2485 /* Reconstruct the active formatting elements, if any. */
2486 $this->reconstructActiveFormattingElements();
2488 /* Insert an HTML element for the token. */
2489 $this->insertElement($token);
2491 /* Insert a marker at the end of the list of active
2492 formatting elements. */
2493 $this->a_formatting
[] = self
::MARKER
;
2496 /* A start tag token whose tag name is one of: "marquee", "object" */
2499 /* Reconstruct the active formatting elements, if any. */
2500 $this->reconstructActiveFormattingElements();
2502 /* Insert an HTML element for the token. */
2503 $this->insertElement($token);
2505 /* Insert a marker at the end of the list of active
2506 formatting elements. */
2507 $this->a_formatting
[] = self
::MARKER
;
2510 /* A start tag token whose tag name is "xmp" */
2512 /* Reconstruct the active formatting elements, if any. */
2513 $this->reconstructActiveFormattingElements();
2515 /* Insert an HTML element for the token. */
2516 $this->insertElement($token);
2518 /* Switch the content model flag to the CDATA state. */
2519 return HTML5
::CDATA
;
2522 /* A start tag whose tag name is "table" */
2524 /* If the stack of open elements has a p element in scope,
2525 then act as if an end tag with the tag name p had been seen. */
2526 if ($this->elementInScope('p')) {
2530 'type' => HTML5
::ENDTAG
2535 /* Insert an HTML element for the token. */
2536 $this->insertElement($token);
2538 /* Change the insertion mode to "in table". */
2539 $this->mode
= self
::IN_TABLE
;
2542 /* A start tag whose tag name is one of: "area", "basefont",
2543 "bgsound", "br", "embed", "img", "param", "spacer", "wbr" */
2553 /* Reconstruct the active formatting elements, if any. */
2554 $this->reconstructActiveFormattingElements();
2556 /* Insert an HTML element for the token. */
2557 $this->insertElement($token);
2559 /* Immediately pop the current node off the stack of open elements. */
2560 array_pop($this->stack
);
2563 /* A start tag whose tag name is "hr" */
2565 /* If the stack of open elements has a p element in scope,
2566 then act as if an end tag with the tag name p had been seen. */
2567 if ($this->elementInScope('p')) {
2571 'type' => HTML5
::ENDTAG
2576 /* Insert an HTML element for the token. */
2577 $this->insertElement($token);
2579 /* Immediately pop the current node off the stack of open elements. */
2580 array_pop($this->stack
);
2583 /* A start tag whose tag name is "image" */
2585 /* Parse error. Change the token's tag name to "img" and
2586 reprocess it. (Don't ask.) */
2587 $token['name'] = 'img';
2588 return $this->inBody($token);
2591 /* A start tag whose tag name is "input" */
2593 /* Reconstruct the active formatting elements, if any. */
2594 $this->reconstructActiveFormattingElements();
2596 /* Insert an input element for the token. */
2597 $element = $this->insertElement($token, false);
2599 /* If the form element pointer is not null, then associate the
2600 input element with the form element pointed to by the form
2602 $this->form_pointer
!== null
2603 ?
$this->form_pointer
->appendChild($element)
2604 : end($this->stack
)->appendChild($element);
2606 /* Pop that input element off the stack of open elements. */
2607 array_pop($this->stack
);
2610 /* A start tag whose tag name is "isindex" */
2615 /* If the form element pointer is not null,
2616 then ignore the token. */
2617 if ($this->form_pointer
=== null) {
2618 /* Act as if a start tag token with the tag name "form" had
2623 'type' => HTML5
::STARTTAG
,
2628 /* Act as if a start tag token with the tag name "hr" had
2633 'type' => HTML5
::STARTTAG
,
2638 /* Act as if a start tag token with the tag name "p" had
2643 'type' => HTML5
::STARTTAG
,
2648 /* Act as if a start tag token with the tag name "label"
2653 'type' => HTML5
::STARTTAG
,
2658 /* Act as if a stream of character tokens had been seen. */
2660 'This is a searchable index. ' .
2661 'Insert your search keywords here: '
2664 /* Act as if a start tag token with the tag name "input"
2665 had been seen, with all the attributes from the "isindex"
2666 token, except with the "name" attribute set to the value
2667 "isindex" (ignoring any explicit "name" attribute). */
2668 $attr = $token['attr'];
2669 $attr[] = array('name' => 'name', 'value' => 'isindex');
2674 'type' => HTML5
::STARTTAG
,
2679 /* Act as if a stream of character tokens had been seen
2680 (see below for what they should say). */
2682 'This is a searchable index. ' .
2683 'Insert your search keywords here: '
2686 /* Act as if an end tag token with the tag name "label"
2691 'type' => HTML5
::ENDTAG
2695 /* Act as if an end tag token with the tag name "p" had
2700 'type' => HTML5
::ENDTAG
2704 /* Act as if a start tag token with the tag name "hr" had
2709 'type' => HTML5
::ENDTAG
2713 /* Act as if an end tag token with the tag name "form" had
2718 'type' => HTML5
::ENDTAG
2724 /* A start tag whose tag name is "textarea" */
2726 $this->insertElement($token);
2728 /* Switch the tokeniser's content model flag to the
2730 return HTML5
::RCDATA
;
2733 /* A start tag whose tag name is one of: "iframe", "noembed",
2738 $this->insertElement($token);
2740 /* Switch the tokeniser's content model flag to the CDATA state. */
2741 return HTML5
::CDATA
;
2744 /* A start tag whose tag name is "select" */
2746 /* Reconstruct the active formatting elements, if any. */
2747 $this->reconstructActiveFormattingElements();
2749 /* Insert an HTML element for the token. */
2750 $this->insertElement($token);
2752 /* Change the insertion mode to "in select". */
2753 $this->mode
= self
::IN_SELECT
;
2756 /* A start or end tag whose tag name is one of: "caption", "col",
2757 "colgroup", "frame", "frameset", "head", "option", "optgroup",
2758 "tbody", "td", "tfoot", "th", "thead", "tr". */
2773 // Parse error. Ignore the token.
2776 /* A start or end tag whose tag name is one of: "event-source",
2777 "section", "nav", "article", "aside", "header", "footer",
2778 "datagrid", "command" */
2779 case 'event-source':
2788 // Work in progress!
2791 /* A start tag token not covered by the previous entries */
2793 /* Reconstruct the active formatting elements, if any. */
2794 $this->reconstructActiveFormattingElements();
2796 $this->insertElement($token, true, true);
2802 switch ($token['name']) {
2803 /* An end tag with the tag name "body" */
2805 /* If the second element in the stack of open elements is
2806 not a body element, this is a parse error. Ignore the token.
2808 if (count($this->stack
) < 2 ||
$this->stack
[1]->nodeName
!== 'body') {
2811 /* If the current node is not the body element, then this
2812 is a parse error. */
2813 } elseif (end($this->stack
)->nodeName
!== 'body') {
2817 /* Change the insertion mode to "after body". */
2818 $this->mode
= self
::AFTER_BODY
;
2821 /* An end tag with the tag name "html" */
2823 /* Act as if an end tag with tag name "body" had been seen,
2824 then, if that token wasn't ignored, reprocess the current
2829 'type' => HTML5
::ENDTAG
2833 return $this->afterBody($token);
2836 /* An end tag whose tag name is one of: "address", "blockquote",
2837 "center", "dir", "div", "dl", "fieldset", "listing", "menu",
2838 "ol", "pre", "ul" */
2851 /* If the stack of open elements has an element in scope
2852 with the same tag name as that of the token, then generate
2853 implied end tags. */
2854 if ($this->elementInScope($token['name'])) {
2855 $this->generateImpliedEndTags();
2857 /* Now, if the current node is not an element with
2858 the same tag name as that of the token, then this
2859 is a parse error. */
2862 /* If the stack of open elements has an element in
2863 scope with the same tag name as that of the token,
2864 then pop elements from this stack until an element
2865 with that tag name has been popped from the stack. */
2866 for ($n = count($this->stack
) - 1; $n >= 0; $n--) {
2867 if ($this->stack
[$n]->nodeName
=== $token['name']) {
2871 array_pop($this->stack
);
2876 /* An end tag whose tag name is "form" */
2878 /* If the stack of open elements has an element in scope
2879 with the same tag name as that of the token, then generate
2880 implied end tags. */
2881 if ($this->elementInScope($token['name'])) {
2882 $this->generateImpliedEndTags();
2886 if (end($this->stack
)->nodeName
!== $token['name']) {
2887 /* Now, if the current node is not an element with the
2888 same tag name as that of the token, then this is a parse
2893 /* Otherwise, if the current node is an element with
2894 the same tag name as that of the token pop that element
2896 array_pop($this->stack
);
2899 /* In any case, set the form element pointer to null. */
2900 $this->form_pointer
= null;
2903 /* An end tag whose tag name is "p" */
2905 /* If the stack of open elements has a p element in scope,
2906 then generate implied end tags, except for p elements. */
2907 if ($this->elementInScope('p')) {
2908 $this->generateImpliedEndTags(array('p'));
2910 /* If the current node is not a p element, then this is
2914 /* If the stack of open elements has a p element in
2915 scope, then pop elements from this stack until the stack
2916 no longer has a p element in scope. */
2917 for ($n = count($this->stack
) - 1; $n >= 0; $n--) {
2918 if ($this->elementInScope('p')) {
2919 array_pop($this->stack
);
2928 /* An end tag whose tag name is "dd", "dt", or "li" */
2932 /* If the stack of open elements has an element in scope
2933 whose tag name matches the tag name of the token, then
2934 generate implied end tags, except for elements with the
2935 same tag name as the token. */
2936 if ($this->elementInScope($token['name'])) {
2937 $this->generateImpliedEndTags(array($token['name']));
2939 /* If the current node is not an element with the same
2940 tag name as the token, then this is a parse error. */
2943 /* If the stack of open elements has an element in scope
2944 whose tag name matches the tag name of the token, then
2945 pop elements from this stack until an element with that
2946 tag name has been popped from the stack. */
2947 for ($n = count($this->stack
) - 1; $n >= 0; $n--) {
2948 if ($this->stack
[$n]->nodeName
=== $token['name']) {
2952 array_pop($this->stack
);
2957 /* An end tag whose tag name is one of: "h1", "h2", "h3", "h4",
2965 $elements = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6');
2967 /* If the stack of open elements has in scope an element whose
2968 tag name is one of "h1", "h2", "h3", "h4", "h5", or "h6", then
2969 generate implied end tags. */
2970 if ($this->elementInScope($elements)) {
2971 $this->generateImpliedEndTags();
2973 /* Now, if the current node is not an element with the same
2974 tag name as that of the token, then this is a parse error. */
2977 /* If the stack of open elements has in scope an element
2978 whose tag name is one of "h1", "h2", "h3", "h4", "h5", or
2979 "h6", then pop elements from the stack until an element
2980 with one of those tag names has been popped from the stack. */
2981 while ($this->elementInScope($elements)) {
2982 array_pop($this->stack
);
2987 /* An end tag whose tag name is one of: "a", "b", "big", "em",
2988 "font", "i", "nobr", "s", "small", "strike", "strong", "tt", "u" */
3002 /* 1. Let the formatting element be the last element in
3003 the list of active formatting elements that:
3004 * is between the end of the list and the last scope
3005 marker in the list, if any, or the start of the list
3007 * has the same tag name as the token.
3010 for ($a = count($this->a_formatting
) - 1; $a >= 0; $a--) {
3011 if ($this->a_formatting
[$a] === self
::MARKER
) {
3014 } elseif ($this->a_formatting
[$a]->tagName
=== $token['name']) {
3015 $formatting_element = $this->a_formatting
[$a];
3016 $in_stack = in_array($formatting_element, $this->stack
, true);
3022 /* If there is no such node, or, if that node is
3023 also in the stack of open elements but the element
3024 is not in scope, then this is a parse error. Abort
3025 these steps. The token is ignored. */
3026 if (!isset($formatting_element) ||
($in_stack &&
3027 !$this->elementInScope($token['name']))
3031 /* Otherwise, if there is such a node, but that node
3032 is not in the stack of open elements, then this is a
3033 parse error; remove the element from the list, and
3034 abort these steps. */
3035 } elseif (isset($formatting_element) && !$in_stack) {
3036 unset($this->a_formatting
[$fe_af_pos]);
3037 $this->a_formatting
= array_merge($this->a_formatting
);
3041 /* 2. Let the furthest block be the topmost node in the
3042 stack of open elements that is lower in the stack
3043 than the formatting element, and is not an element in
3044 the phrasing or formatting categories. There might
3046 $fe_s_pos = array_search($formatting_element, $this->stack
, true);
3047 $length = count($this->stack
);
3049 for ($s = $fe_s_pos +
1; $s < $length; $s++
) {
3050 $category = $this->getElementCategory($this->stack
[$s]->nodeName
);
3052 if ($category !== self
::PHRASING
&& $category !== self
::FORMATTING
) {
3053 $furthest_block = $this->stack
[$s];
3057 /* 3. If there is no furthest block, then the UA must
3058 skip the subsequent steps and instead just pop all
3059 the nodes from the bottom of the stack of open
3060 elements, from the current node up to the formatting
3061 element, and remove the formatting element from the
3062 list of active formatting elements. */
3063 if (!isset($furthest_block)) {
3064 for ($n = $length - 1; $n >= $fe_s_pos; $n--) {
3065 array_pop($this->stack
);
3068 unset($this->a_formatting
[$fe_af_pos]);
3069 $this->a_formatting
= array_merge($this->a_formatting
);
3073 /* 4. Let the common ancestor be the element
3074 immediately above the formatting element in the stack
3075 of open elements. */
3076 $common_ancestor = $this->stack
[$fe_s_pos - 1];
3078 /* 5. If the furthest block has a parent node, then
3079 remove the furthest block from its parent node. */
3080 if ($furthest_block->parentNode
!== null) {
3081 $furthest_block->parentNode
->removeChild($furthest_block);
3084 /* 6. Let a bookmark note the position of the
3085 formatting element in the list of active formatting
3086 elements relative to the elements on either side
3087 of it in the list. */
3088 $bookmark = $fe_af_pos;
3090 /* 7. Let node and last node be the furthest block.
3091 Follow these steps: */
3092 $node = $furthest_block;
3093 $last_node = $furthest_block;
3096 for ($n = array_search($node, $this->stack
, true) - 1; $n >= 0; $n--) {
3097 /* 7.1 Let node be the element immediately
3098 prior to node in the stack of open elements. */
3099 $node = $this->stack
[$n];
3101 /* 7.2 If node is not in the list of active
3102 formatting elements, then remove node from
3103 the stack of open elements and then go back
3105 if (!in_array($node, $this->a_formatting
, true)) {
3106 unset($this->stack
[$n]);
3107 $this->stack
= array_merge($this->stack
);
3114 /* 7.3 Otherwise, if node is the formatting
3115 element, then go to the next step in the overall
3117 if ($node === $formatting_element) {
3120 /* 7.4 Otherwise, if last node is the furthest
3121 block, then move the aforementioned bookmark to
3122 be immediately after the node in the list of
3123 active formatting elements. */
3124 } elseif ($last_node === $furthest_block) {
3125 $bookmark = array_search($node, $this->a_formatting
, true) +
1;
3128 /* 7.5 If node has any children, perform a
3129 shallow clone of node, replace the entry for
3130 node in the list of active formatting elements
3131 with an entry for the clone, replace the entry
3132 for node in the stack of open elements with an
3133 entry for the clone, and let node be the clone. */
3134 if ($node->hasChildNodes()) {
3135 $clone = $node->cloneNode();
3136 $s_pos = array_search($node, $this->stack
, true);
3137 $a_pos = array_search($node, $this->a_formatting
, true);
3139 $this->stack
[$s_pos] = $clone;
3140 $this->a_formatting
[$a_pos] = $clone;
3144 /* 7.6 Insert last node into node, first removing
3145 it from its previous parent node if any. */
3146 if ($last_node->parentNode
!== null) {
3147 $last_node->parentNode
->removeChild($last_node);
3150 $node->appendChild($last_node);
3152 /* 7.7 Let last node be node. */
3156 /* 8. Insert whatever last node ended up being in
3157 the previous step into the common ancestor node,
3158 first removing it from its previous parent node if
3160 if ($last_node->parentNode
!== null) {
3161 $last_node->parentNode
->removeChild($last_node);
3164 $common_ancestor->appendChild($last_node);
3166 /* 9. Perform a shallow clone of the formatting
3168 $clone = $formatting_element->cloneNode();
3170 /* 10. Take all of the child nodes of the furthest
3171 block and append them to the clone created in the
3173 while ($furthest_block->hasChildNodes()) {
3174 $child = $furthest_block->firstChild
;
3175 $furthest_block->removeChild($child);
3176 $clone->appendChild($child);
3179 /* 11. Append that clone to the furthest block. */
3180 $furthest_block->appendChild($clone);
3182 /* 12. Remove the formatting element from the list
3183 of active formatting elements, and insert the clone
3184 into the list of active formatting elements at the
3185 position of the aforementioned bookmark. */
3186 $fe_af_pos = array_search($formatting_element, $this->a_formatting
, true);
3187 unset($this->a_formatting
[$fe_af_pos]);
3188 $this->a_formatting
= array_merge($this->a_formatting
);
3190 $af_part1 = array_slice($this->a_formatting
, 0, $bookmark - 1);
3191 $af_part2 = array_slice($this->a_formatting
, $bookmark, count($this->a_formatting
));
3192 $this->a_formatting
= array_merge($af_part1, array($clone), $af_part2);
3194 /* 13. Remove the formatting element from the stack
3195 of open elements, and insert the clone into the stack
3196 of open elements immediately after (i.e. in a more
3197 deeply nested position than) the position of the
3198 furthest block in that stack. */
3199 $fe_s_pos = array_search($formatting_element, $this->stack
, true);
3200 $fb_s_pos = array_search($furthest_block, $this->stack
, true);
3201 unset($this->stack
[$fe_s_pos]);
3203 $s_part1 = array_slice($this->stack
, 0, $fb_s_pos);
3204 $s_part2 = array_slice($this->stack
, $fb_s_pos +
1, count($this->stack
));
3205 $this->stack
= array_merge($s_part1, array($clone), $s_part2);
3207 /* 14. Jump back to step 1 in this series of steps. */
3208 unset($formatting_element, $fe_af_pos, $fe_s_pos, $furthest_block);
3212 /* An end tag token whose tag name is one of: "button",
3213 "marquee", "object" */
3217 /* If the stack of open elements has an element in scope whose
3218 tag name matches the tag name of the token, then generate implied
3220 if ($this->elementInScope($token['name'])) {
3221 $this->generateImpliedEndTags();
3223 /* Now, if the current node is not an element with the same
3224 tag name as the token, then this is a parse error. */
3227 /* Now, if the stack of open elements has an element in scope
3228 whose tag name matches the tag name of the token, then pop
3229 elements from the stack until that element has been popped from
3230 the stack, and clear the list of active formatting elements up
3231 to the last marker. */
3232 for ($n = count($this->stack
) - 1; $n >= 0; $n--) {
3233 if ($this->stack
[$n]->nodeName
=== $token['name']) {
3237 array_pop($this->stack
);
3240 $marker = end(array_keys($this->a_formatting
, self
::MARKER
, true));
3242 for ($n = count($this->a_formatting
) - 1; $n > $marker; $n--) {
3243 array_pop($this->a_formatting
);
3248 /* Or an end tag whose tag name is one of: "area", "basefont",
3249 "bgsound", "br", "embed", "hr", "iframe", "image", "img",
3250 "input", "isindex", "noembed", "noframes", "param", "select",
3251 "spacer", "table", "textarea", "wbr" */
3271 // Parse error. Ignore the token.
3274 /* An end tag token not covered by the previous entries */
3276 for ($n = count($this->stack
) - 1; $n >= 0; $n--) {
3277 /* Initialise node to be the current node (the bottommost
3278 node of the stack). */
3279 $node = end($this->stack
);
3281 /* If node has the same tag name as the end tag token,
3283 if ($token['name'] === $node->nodeName
) {
3284 /* Generate implied end tags. */
3285 $this->generateImpliedEndTags();
3287 /* If the tag name of the end tag token does not
3288 match the tag name of the current node, this is a
3292 /* Pop all the nodes from the current node up to
3293 node, including node, then stop this algorithm. */
3294 for ($x = count($this->stack
) - $n; $x >= $n; $x--) {
3295 array_pop($this->stack
);
3299 $category = $this->getElementCategory($node);
3301 if ($category !== self
::SPECIAL
&& $category !== self
::SCOPING
) {
3302 /* Otherwise, if node is in neither the formatting
3303 category nor the phrasing category, then this is a
3304 parse error. Stop this algorithm. The end tag token
3316 private function inTable($token)
3318 $clear = array('html', 'table');
3320 /* A character token that is one of one of U+0009 CHARACTER TABULATION,
3321 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
3323 if ($token['type'] === HTML5
::CHARACTR
&&
3324 preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
3326 /* Append the character to the current node. */
3327 $text = $this->dom
->createTextNode($token['data']);
3328 end($this->stack
)->appendChild($text);
3330 /* A comment token */
3331 } elseif ($token['type'] === HTML5
::COMMENT
) {
3332 /* Append a Comment node to the current node with the data
3333 attribute set to the data given in the comment token. */
3334 $comment = $this->dom
->createComment($token['data']);
3335 end($this->stack
)->appendChild($comment);
3337 /* A start tag whose tag name is "caption" */
3338 } elseif ($token['type'] === HTML5
::STARTTAG
&&
3339 $token['name'] === 'caption'
3341 /* Clear the stack back to a table context. */
3342 $this->clearStackToTableContext($clear);
3344 /* Insert a marker at the end of the list of active
3345 formatting elements. */
3346 $this->a_formatting
[] = self
::MARKER
;
3348 /* Insert an HTML element for the token, then switch the
3349 insertion mode to "in caption". */
3350 $this->insertElement($token);
3351 $this->mode
= self
::IN_CAPTION
;
3353 /* A start tag whose tag name is "colgroup" */
3354 } elseif ($token['type'] === HTML5
::STARTTAG
&&
3355 $token['name'] === 'colgroup'
3357 /* Clear the stack back to a table context. */
3358 $this->clearStackToTableContext($clear);
3360 /* Insert an HTML element for the token, then switch the
3361 insertion mode to "in column group". */
3362 $this->insertElement($token);
3363 $this->mode
= self
::IN_CGROUP
;
3365 /* A start tag whose tag name is "col" */
3366 } elseif ($token['type'] === HTML5
::STARTTAG
&&
3367 $token['name'] === 'col'
3371 'name' => 'colgroup',
3372 'type' => HTML5
::STARTTAG
,
3377 $this->inColumnGroup($token);
3379 /* A start tag whose tag name is one of: "tbody", "tfoot", "thead" */
3380 } elseif ($token['type'] === HTML5
::STARTTAG
&& in_array(
3382 array('tbody', 'tfoot', 'thead')
3385 /* Clear the stack back to a table context. */
3386 $this->clearStackToTableContext($clear);
3388 /* Insert an HTML element for the token, then switch the insertion
3389 mode to "in table body". */
3390 $this->insertElement($token);
3391 $this->mode
= self
::IN_TBODY
;
3393 /* A start tag whose tag name is one of: "td", "th", "tr" */
3394 } elseif ($token['type'] === HTML5
::STARTTAG
&&
3395 in_array($token['name'], array('td', 'th', 'tr'))
3397 /* Act as if a start tag token with the tag name "tbody" had been
3398 seen, then reprocess the current token. */
3402 'type' => HTML5
::STARTTAG
,
3407 return $this->inTableBody($token);
3409 /* A start tag whose tag name is "table" */
3410 } elseif ($token['type'] === HTML5
::STARTTAG
&&
3411 $token['name'] === 'table'
3413 /* Parse error. Act as if an end tag token with the tag name "table"
3414 had been seen, then, if that token wasn't ignored, reprocess the
3419 'type' => HTML5
::ENDTAG
3423 return $this->mainPhase($token);
3425 /* An end tag whose tag name is "table" */
3426 } elseif ($token['type'] === HTML5
::ENDTAG
&&
3427 $token['name'] === 'table'
3429 /* If the stack of open elements does not have an element in table
3430 scope with the same tag name as the token, this is a parse error.
3431 Ignore the token. (innerHTML case) */
3432 if (!$this->elementInScope($token['name'], true)) {
3437 /* Generate implied end tags. */
3438 $this->generateImpliedEndTags();
3440 /* Now, if the current node is not a table element, then this
3441 is a parse error. */
3444 /* Pop elements from this stack until a table element has been
3445 popped from the stack. */
3447 $current = end($this->stack
)->nodeName
;
3448 array_pop($this->stack
);
3450 if ($current === 'table') {
3455 /* Reset the insertion mode appropriately. */
3456 $this->resetInsertionMode();
3459 /* An end tag whose tag name is one of: "body", "caption", "col",
3460 "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr" */
3461 } elseif ($token['type'] === HTML5
::ENDTAG
&& in_array(
3478 // Parse error. Ignore the token.
3482 /* Parse error. Process the token as if the insertion mode was "in
3483 body", with the following exception: */
3485 /* If the current node is a table, tbody, tfoot, thead, or tr
3486 element, then, whenever a node would be inserted into the current
3487 node, it must instead be inserted into the foster parent element. */
3489 end($this->stack
)->nodeName
,
3490 array('table', 'tbody', 'tfoot', 'thead', 'tr')
3493 /* The foster parent element is the parent element of the last
3494 table element in the stack of open elements, if there is a
3495 table element and it has such a parent element. If there is no
3496 table element in the stack of open elements (innerHTML case),
3497 then the foster parent element is the first element in the
3498 stack of open elements (the html element). Otherwise, if there
3499 is a table element in the stack of open elements, but the last
3500 table element in the stack of open elements has no parent, or
3501 its parent node is not an element, then the foster parent
3502 element is the element before the last table element in the
3503 stack of open elements. */
3504 for ($n = count($this->stack
) - 1; $n >= 0; $n--) {
3505 if ($this->stack
[$n]->nodeName
=== 'table') {
3506 $table = $this->stack
[$n];
3511 if (isset($table) && $table->parentNode
!== null) {
3512 $this->foster_parent
= $table->parentNode
;
3514 } elseif (!isset($table)) {
3515 $this->foster_parent
= $this->stack
[0];
3517 } elseif (isset($table) && ($table->parentNode
=== null ||
3518 $table->parentNode
->nodeType
!== XML_ELEMENT_NODE
)
3520 $this->foster_parent
= $this->stack
[$n - 1];
3524 $this->inBody($token);
3528 private function inCaption($token)
3530 /* An end tag whose tag name is "caption" */
3531 if ($token['type'] === HTML5
::ENDTAG
&& $token['name'] === 'caption') {
3532 /* If the stack of open elements does not have an element in table
3533 scope with the same tag name as the token, this is a parse error.
3534 Ignore the token. (innerHTML case) */
3535 if (!$this->elementInScope($token['name'], true)) {
3540 /* Generate implied end tags. */
3541 $this->generateImpliedEndTags();
3543 /* Now, if the current node is not a caption element, then this
3544 is a parse error. */
3547 /* Pop elements from this stack until a caption element has
3548 been popped from the stack. */
3550 $node = end($this->stack
)->nodeName
;
3551 array_pop($this->stack
);
3553 if ($node === 'caption') {
3558 /* Clear the list of active formatting elements up to the last
3560 $this->clearTheActiveFormattingElementsUpToTheLastMarker();
3562 /* Switch the insertion mode to "in table". */
3563 $this->mode
= self
::IN_TABLE
;
3566 /* A start tag whose tag name is one of: "caption", "col", "colgroup",
3567 "tbody", "td", "tfoot", "th", "thead", "tr", or an end tag whose tag
3569 } elseif (($token['type'] === HTML5
::STARTTAG
&& in_array(
3582 )) ||
($token['type'] === HTML5
::ENDTAG
&&
3583 $token['name'] === 'table')
3585 /* Parse error. Act as if an end tag with the tag name "caption"
3586 had been seen, then, if that token wasn't ignored, reprocess the
3590 'name' => 'caption',
3591 'type' => HTML5
::ENDTAG
3595 return $this->inTable($token);
3597 /* An end tag whose tag name is one of: "body", "col", "colgroup",
3598 "html", "tbody", "td", "tfoot", "th", "thead", "tr" */
3599 } elseif ($token['type'] === HTML5
::ENDTAG
&& in_array(
3614 // Parse error. Ignore the token.
3618 /* Process the token as if the insertion mode was "in body". */
3619 $this->inBody($token);
3623 private function inColumnGroup($token)
3625 /* A character token that is one of one of U+0009 CHARACTER TABULATION,
3626 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
3628 if ($token['type'] === HTML5
::CHARACTR
&&
3629 preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
3631 /* Append the character to the current node. */
3632 $text = $this->dom
->createTextNode($token['data']);
3633 end($this->stack
)->appendChild($text);
3635 /* A comment token */
3636 } elseif ($token['type'] === HTML5
::COMMENT
) {
3637 /* Append a Comment node to the current node with the data
3638 attribute set to the data given in the comment token. */
3639 $comment = $this->dom
->createComment($token['data']);
3640 end($this->stack
)->appendChild($comment);
3642 /* A start tag whose tag name is "col" */
3643 } elseif ($token['type'] === HTML5
::STARTTAG
&& $token['name'] === 'col') {
3644 /* Insert a col element for the token. Immediately pop the current
3645 node off the stack of open elements. */
3646 $this->insertElement($token);
3647 array_pop($this->stack
);
3649 /* An end tag whose tag name is "colgroup" */
3650 } elseif ($token['type'] === HTML5
::ENDTAG
&&
3651 $token['name'] === 'colgroup'
3653 /* If the current node is the root html element, then this is a
3654 parse error, ignore the token. (innerHTML case) */
3655 if (end($this->stack
)->nodeName
=== 'html') {
3658 /* Otherwise, pop the current node (which will be a colgroup
3659 element) from the stack of open elements. Switch the insertion
3660 mode to "in table". */
3662 array_pop($this->stack
);
3663 $this->mode
= self
::IN_TABLE
;
3666 /* An end tag whose tag name is "col" */
3667 } elseif ($token['type'] === HTML5
::ENDTAG
&& $token['name'] === 'col') {
3668 /* Parse error. Ignore the token. */
3672 /* Act as if an end tag with the tag name "colgroup" had been seen,
3673 and then, if that token wasn't ignored, reprocess the current token. */
3674 $this->inColumnGroup(
3676 'name' => 'colgroup',
3677 'type' => HTML5
::ENDTAG
3681 return $this->inTable($token);
3685 private function inTableBody($token)
3687 $clear = array('tbody', 'tfoot', 'thead', 'html');
3689 /* A start tag whose tag name is "tr" */
3690 if ($token['type'] === HTML5
::STARTTAG
&& $token['name'] === 'tr') {
3691 /* Clear the stack back to a table body context. */
3692 $this->clearStackToTableContext($clear);
3694 /* Insert a tr element for the token, then switch the insertion
3695 mode to "in row". */
3696 $this->insertElement($token);
3697 $this->mode
= self
::IN_ROW
;
3699 /* A start tag whose tag name is one of: "th", "td" */
3700 } elseif ($token['type'] === HTML5
::STARTTAG
&&
3701 ($token['name'] === 'th' ||
$token['name'] === 'td')
3703 /* Parse error. Act as if a start tag with the tag name "tr" had
3704 been seen, then reprocess the current token. */
3708 'type' => HTML5
::STARTTAG
,
3713 return $this->inRow($token);
3715 /* An end tag whose tag name is one of: "tbody", "tfoot", "thead" */
3716 } elseif ($token['type'] === HTML5
::ENDTAG
&&
3717 in_array($token['name'], array('tbody', 'tfoot', 'thead'))
3719 /* If the stack of open elements does not have an element in table
3720 scope with the same tag name as the token, this is a parse error.
3721 Ignore the token. */
3722 if (!$this->elementInScope($token['name'], true)) {
3727 /* Clear the stack back to a table body context. */
3728 $this->clearStackToTableContext($clear);
3730 /* Pop the current node from the stack of open elements. Switch
3731 the insertion mode to "in table". */
3732 array_pop($this->stack
);
3733 $this->mode
= self
::IN_TABLE
;
3736 /* A start tag whose tag name is one of: "caption", "col", "colgroup",
3737 "tbody", "tfoot", "thead", or an end tag whose tag name is "table" */
3738 } elseif (($token['type'] === HTML5
::STARTTAG
&& in_array(
3740 array('caption', 'col', 'colgroup', 'tbody', 'tfoor', 'thead')
3742 ($token['type'] === HTML5
::STARTTAG
&& $token['name'] === 'table')
3744 /* If the stack of open elements does not have a tbody, thead, or
3745 tfoot element in table scope, this is a parse error. Ignore the
3746 token. (innerHTML case) */
3747 if (!$this->elementInScope(array('tbody', 'thead', 'tfoot'), true)) {
3752 /* Clear the stack back to a table body context. */
3753 $this->clearStackToTableContext($clear);
3755 /* Act as if an end tag with the same tag name as the current
3756 node ("tbody", "tfoot", or "thead") had been seen, then
3757 reprocess the current token. */
3760 'name' => end($this->stack
)->nodeName
,
3761 'type' => HTML5
::ENDTAG
3765 return $this->mainPhase($token);
3768 /* An end tag whose tag name is one of: "body", "caption", "col",
3769 "colgroup", "html", "td", "th", "tr" */
3770 } elseif ($token['type'] === HTML5
::ENDTAG
&& in_array(
3772 array('body', 'caption', 'col', 'colgroup', 'html', 'td', 'th', 'tr')
3775 /* Parse error. Ignore the token. */
3779 /* Process the token as if the insertion mode was "in table". */
3780 $this->inTable($token);
3784 private function inRow($token)
3786 $clear = array('tr', 'html');
3788 /* A start tag whose tag name is one of: "th", "td" */
3789 if ($token['type'] === HTML5
::STARTTAG
&&
3790 ($token['name'] === 'th' ||
$token['name'] === 'td')
3792 /* Clear the stack back to a table row context. */
3793 $this->clearStackToTableContext($clear);
3795 /* Insert an HTML element for the token, then switch the insertion
3796 mode to "in cell". */
3797 $this->insertElement($token);
3798 $this->mode
= self
::IN_CELL
;
3800 /* Insert a marker at the end of the list of active formatting
3802 $this->a_formatting
[] = self
::MARKER
;
3804 /* An end tag whose tag name is "tr" */
3805 } elseif ($token['type'] === HTML5
::ENDTAG
&& $token['name'] === 'tr') {
3806 /* If the stack of open elements does not have an element in table
3807 scope with the same tag name as the token, this is a parse error.
3808 Ignore the token. (innerHTML case) */
3809 if (!$this->elementInScope($token['name'], true)) {
3814 /* Clear the stack back to a table row context. */
3815 $this->clearStackToTableContext($clear);
3817 /* Pop the current node (which will be a tr element) from the
3818 stack of open elements. Switch the insertion mode to "in table
3820 array_pop($this->stack
);
3821 $this->mode
= self
::IN_TBODY
;
3824 /* A start tag whose tag name is one of: "caption", "col", "colgroup",
3825 "tbody", "tfoot", "thead", "tr" or an end tag whose tag name is "table" */
3826 } elseif ($token['type'] === HTML5
::STARTTAG
&& in_array(
3828 array('caption', 'col', 'colgroup', 'tbody', 'tfoot', 'thead', 'tr')
3831 /* Act as if an end tag with the tag name "tr" had been seen, then,
3832 if that token wasn't ignored, reprocess the current token. */
3836 'type' => HTML5
::ENDTAG
3840 return $this->inCell($token);
3842 /* An end tag whose tag name is one of: "tbody", "tfoot", "thead" */
3843 } elseif ($token['type'] === HTML5
::ENDTAG
&&
3844 in_array($token['name'], array('tbody', 'tfoot', 'thead'))
3846 /* If the stack of open elements does not have an element in table
3847 scope with the same tag name as the token, this is a parse error.
3848 Ignore the token. */
3849 if (!$this->elementInScope($token['name'], true)) {
3854 /* Otherwise, act as if an end tag with the tag name "tr" had
3855 been seen, then reprocess the current token. */
3859 'type' => HTML5
::ENDTAG
3863 return $this->inCell($token);
3866 /* An end tag whose tag name is one of: "body", "caption", "col",
3867 "colgroup", "html", "td", "th" */
3868 } elseif ($token['type'] === HTML5
::ENDTAG
&& in_array(
3870 array('body', 'caption', 'col', 'colgroup', 'html', 'td', 'th', 'tr')
3873 /* Parse error. Ignore the token. */
3877 /* Process the token as if the insertion mode was "in table". */
3878 $this->inTable($token);
3882 private function inCell($token)
3884 /* An end tag whose tag name is one of: "td", "th" */
3885 if ($token['type'] === HTML5
::ENDTAG
&&
3886 ($token['name'] === 'td' ||
$token['name'] === 'th')
3888 /* If the stack of open elements does not have an element in table
3889 scope with the same tag name as that of the token, then this is a
3890 parse error and the token must be ignored. */
3891 if (!$this->elementInScope($token['name'], true)) {
3896 /* Generate implied end tags, except for elements with the same
3897 tag name as the token. */
3898 $this->generateImpliedEndTags(array($token['name']));
3900 /* Now, if the current node is not an element with the same tag
3901 name as the token, then this is a parse error. */
3904 /* Pop elements from this stack until an element with the same
3905 tag name as the token has been popped from the stack. */
3907 $node = end($this->stack
)->nodeName
;
3908 array_pop($this->stack
);
3910 if ($node === $token['name']) {
3915 /* Clear the list of active formatting elements up to the last
3917 $this->clearTheActiveFormattingElementsUpToTheLastMarker();
3919 /* Switch the insertion mode to "in row". (The current node
3920 will be a tr element at this point.) */
3921 $this->mode
= self
::IN_ROW
;
3924 /* A start tag whose tag name is one of: "caption", "col", "colgroup",
3925 "tbody", "td", "tfoot", "th", "thead", "tr" */
3926 } elseif ($token['type'] === HTML5
::STARTTAG
&& in_array(
3941 /* If the stack of open elements does not have a td or th element
3942 in table scope, then this is a parse error; ignore the token.
3944 if (!$this->elementInScope(array('td', 'th'), true)) {
3947 /* Otherwise, close the cell (see below) and reprocess the current
3951 return $this->inRow($token);
3954 /* A start tag whose tag name is one of: "caption", "col", "colgroup",
3955 "tbody", "td", "tfoot", "th", "thead", "tr" */
3956 } elseif ($token['type'] === HTML5
::STARTTAG
&& in_array(
3971 /* If the stack of open elements does not have a td or th element
3972 in table scope, then this is a parse error; ignore the token.
3974 if (!$this->elementInScope(array('td', 'th'), true)) {
3977 /* Otherwise, close the cell (see below) and reprocess the current
3981 return $this->inRow($token);
3984 /* An end tag whose tag name is one of: "body", "caption", "col",
3985 "colgroup", "html" */
3986 } elseif ($token['type'] === HTML5
::ENDTAG
&& in_array(
3988 array('body', 'caption', 'col', 'colgroup', 'html')
3991 /* Parse error. Ignore the token. */
3993 /* An end tag whose tag name is one of: "table", "tbody", "tfoot",
3995 } elseif ($token['type'] === HTML5
::ENDTAG
&& in_array(
3997 array('table', 'tbody', 'tfoot', 'thead', 'tr')
4000 /* If the stack of open elements does not have an element in table
4001 scope with the same tag name as that of the token (which can only
4002 happen for "tbody", "tfoot" and "thead", or, in the innerHTML case),
4003 then this is a parse error and the token must be ignored. */
4004 if (!$this->elementInScope($token['name'], true)) {
4007 /* Otherwise, close the cell (see below) and reprocess the current
4011 return $this->inRow($token);
4016 /* Process the token as if the insertion mode was "in body". */
4017 $this->inBody($token);
4021 private function inSelect($token)
4023 /* Handle the token as follows: */
4025 /* A character token */
4026 if ($token['type'] === HTML5
::CHARACTR
) {
4027 /* Append the token's character to the current node. */
4028 $this->insertText($token['data']);
4030 /* A comment token */
4031 } elseif ($token['type'] === HTML5
::COMMENT
) {
4032 /* Append a Comment node to the current node with the data
4033 attribute set to the data given in the comment token. */
4034 $this->insertComment($token['data']);
4036 /* A start tag token whose tag name is "option" */
4037 } elseif ($token['type'] === HTML5
::STARTTAG
&&
4038 $token['name'] === 'option'
4040 /* If the current node is an option element, act as if an end tag
4041 with the tag name "option" had been seen. */
4042 if (end($this->stack
)->nodeName
=== 'option') {
4046 'type' => HTML5
::ENDTAG
4051 /* Insert an HTML element for the token. */
4052 $this->insertElement($token);
4054 /* A start tag token whose tag name is "optgroup" */
4055 } elseif ($token['type'] === HTML5
::STARTTAG
&&
4056 $token['name'] === 'optgroup'
4058 /* If the current node is an option element, act as if an end tag
4059 with the tag name "option" had been seen. */
4060 if (end($this->stack
)->nodeName
=== 'option') {
4064 'type' => HTML5
::ENDTAG
4069 /* If the current node is an optgroup element, act as if an end tag
4070 with the tag name "optgroup" had been seen. */
4071 if (end($this->stack
)->nodeName
=== 'optgroup') {
4074 'name' => 'optgroup',
4075 'type' => HTML5
::ENDTAG
4080 /* Insert an HTML element for the token. */
4081 $this->insertElement($token);
4083 /* An end tag token whose tag name is "optgroup" */
4084 } elseif ($token['type'] === HTML5
::ENDTAG
&&
4085 $token['name'] === 'optgroup'
4087 /* First, if the current node is an option element, and the node
4088 immediately before it in the stack of open elements is an optgroup
4089 element, then act as if an end tag with the tag name "option" had
4091 $elements_in_stack = count($this->stack
);
4093 if ($this->stack
[$elements_in_stack - 1]->nodeName
=== 'option' &&
4094 $this->stack
[$elements_in_stack - 2]->nodeName
=== 'optgroup'
4099 'type' => HTML5
::ENDTAG
4104 /* If the current node is an optgroup element, then pop that node
4105 from the stack of open elements. Otherwise, this is a parse error,
4106 ignore the token. */
4107 if ($this->stack
[$elements_in_stack - 1] === 'optgroup') {
4108 array_pop($this->stack
);
4111 /* An end tag token whose tag name is "option" */
4112 } elseif ($token['type'] === HTML5
::ENDTAG
&&
4113 $token['name'] === 'option'
4115 /* If the current node is an option element, then pop that node
4116 from the stack of open elements. Otherwise, this is a parse error,
4117 ignore the token. */
4118 if (end($this->stack
)->nodeName
=== 'option') {
4119 array_pop($this->stack
);
4122 /* An end tag whose tag name is "select" */
4123 } elseif ($token['type'] === HTML5
::ENDTAG
&&
4124 $token['name'] === 'select'
4126 /* If the stack of open elements does not have an element in table
4127 scope with the same tag name as the token, this is a parse error.
4128 Ignore the token. (innerHTML case) */
4129 if (!$this->elementInScope($token['name'], true)) {
4134 /* Pop elements from the stack of open elements until a select
4135 element has been popped from the stack. */
4137 $current = end($this->stack
)->nodeName
;
4138 array_pop($this->stack
);
4140 if ($current === 'select') {
4145 /* Reset the insertion mode appropriately. */
4146 $this->resetInsertionMode();
4149 /* A start tag whose tag name is "select" */
4150 } elseif ($token['name'] === 'select' &&
4151 $token['type'] === HTML5
::STARTTAG
4153 /* Parse error. Act as if the token had been an end tag with the
4154 tag name "select" instead. */
4158 'type' => HTML5
::ENDTAG
4162 /* An end tag whose tag name is one of: "caption", "table", "tbody",
4163 "tfoot", "thead", "tr", "td", "th" */
4176 ) && $token['type'] === HTML5
::ENDTAG
4181 /* If the stack of open elements has an element in table scope with
4182 the same tag name as that of the token, then act as if an end tag
4183 with the tag name "select" had been seen, and reprocess the token.
4184 Otherwise, ignore the token. */
4185 if ($this->elementInScope($token['name'], true)) {
4189 'type' => HTML5
::ENDTAG
4193 $this->mainPhase($token);
4198 /* Parse error. Ignore the token. */
4202 private function afterBody($token)
4204 /* Handle the token as follows: */
4206 /* A character token that is one of one of U+0009 CHARACTER TABULATION,
4207 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
4209 if ($token['type'] === HTML5
::CHARACTR
&&
4210 preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
4212 /* Process the token as it would be processed if the insertion mode
4214 $this->inBody($token);
4216 /* A comment token */
4217 } elseif ($token['type'] === HTML5
::COMMENT
) {
4218 /* Append a Comment node to the first element in the stack of open
4219 elements (the html element), with the data attribute set to the
4220 data given in the comment token. */
4221 $comment = $this->dom
->createComment($token['data']);
4222 $this->stack
[0]->appendChild($comment);
4224 /* An end tag with the tag name "html" */
4225 } elseif ($token['type'] === HTML5
::ENDTAG
&& $token['name'] === 'html') {
4226 /* If the parser was originally created in order to handle the
4227 setting of an element's innerHTML attribute, this is a parse error;
4228 ignore the token. (The element will be an html element in this
4229 case.) (innerHTML case) */
4231 /* Otherwise, switch to the trailing end phase. */
4232 $this->phase
= self
::END_PHASE
;
4236 /* Parse error. Set the insertion mode to "in body" and reprocess
4238 $this->mode
= self
::IN_BODY
;
4239 return $this->inBody($token);
4243 private function inFrameset($token)
4245 /* Handle the token as follows: */
4247 /* A character token that is one of one of U+0009 CHARACTER TABULATION,
4248 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
4249 U+000D CARRIAGE RETURN (CR), or U+0020 SPACE */
4250 if ($token['type'] === HTML5
::CHARACTR
&&
4251 preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
4253 /* Append the character to the current node. */
4254 $this->insertText($token['data']);
4256 /* A comment token */
4257 } elseif ($token['type'] === HTML5
::COMMENT
) {
4258 /* Append a Comment node to the current node with the data
4259 attribute set to the data given in the comment token. */
4260 $this->insertComment($token['data']);
4262 /* A start tag with the tag name "frameset" */
4263 } elseif ($token['name'] === 'frameset' &&
4264 $token['type'] === HTML5
::STARTTAG
4266 $this->insertElement($token);
4268 /* An end tag with the tag name "frameset" */
4269 } elseif ($token['name'] === 'frameset' &&
4270 $token['type'] === HTML5
::ENDTAG
4272 /* If the current node is the root html element, then this is a
4273 parse error; ignore the token. (innerHTML case) */
4274 if (end($this->stack
)->nodeName
=== 'html') {
4278 /* Otherwise, pop the current node from the stack of open
4280 array_pop($this->stack
);
4282 /* If the parser was not originally created in order to handle
4283 the setting of an element's innerHTML attribute (innerHTML case),
4284 and the current node is no longer a frameset element, then change
4285 the insertion mode to "after frameset". */
4286 $this->mode
= self
::AFTR_FRAME
;
4289 /* A start tag with the tag name "frame" */
4290 } elseif ($token['name'] === 'frame' &&
4291 $token['type'] === HTML5
::STARTTAG
4293 /* Insert an HTML element for the token. */
4294 $this->insertElement($token);
4296 /* Immediately pop the current node off the stack of open elements. */
4297 array_pop($this->stack
);
4299 /* A start tag with the tag name "noframes" */
4300 } elseif ($token['name'] === 'noframes' &&
4301 $token['type'] === HTML5
::STARTTAG
4303 /* Process the token as if the insertion mode had been "in body". */
4304 $this->inBody($token);
4308 /* Parse error. Ignore the token. */
4312 private function afterFrameset($token)
4314 /* Handle the token as follows: */
4316 /* A character token that is one of one of U+0009 CHARACTER TABULATION,
4317 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
4318 U+000D CARRIAGE RETURN (CR), or U+0020 SPACE */
4319 if ($token['type'] === HTML5
::CHARACTR
&&
4320 preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
4322 /* Append the character to the current node. */
4323 $this->insertText($token['data']);
4325 /* A comment token */
4326 } elseif ($token['type'] === HTML5
::COMMENT
) {
4327 /* Append a Comment node to the current node with the data
4328 attribute set to the data given in the comment token. */
4329 $this->insertComment($token['data']);
4331 /* An end tag with the tag name "html" */
4332 } elseif ($token['name'] === 'html' &&
4333 $token['type'] === HTML5
::ENDTAG
4335 /* Switch to the trailing end phase. */
4336 $this->phase
= self
::END_PHASE
;
4338 /* A start tag with the tag name "noframes" */
4339 } elseif ($token['name'] === 'noframes' &&
4340 $token['type'] === HTML5
::STARTTAG
4342 /* Process the token as if the insertion mode had been "in body". */
4343 $this->inBody($token);
4347 /* Parse error. Ignore the token. */
4351 private function trailingEndPhase($token)
4353 /* After the main phase, as each token is emitted from the tokenisation
4354 stage, it must be processed as described in this section. */
4356 /* A DOCTYPE token */
4357 if ($token['type'] === HTML5
::DOCTYPE
) {
4358 // Parse error. Ignore the token.
4360 /* A comment token */
4361 } elseif ($token['type'] === HTML5
::COMMENT
) {
4362 /* Append a Comment node to the Document object with the data
4363 attribute set to the data given in the comment token. */
4364 $comment = $this->dom
->createComment($token['data']);
4365 $this->dom
->appendChild($comment);
4367 /* A character token that is one of one of U+0009 CHARACTER TABULATION,
4368 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
4370 } elseif ($token['type'] === HTML5
::CHARACTR
&&
4371 preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
4373 /* Process the token as it would be processed in the main phase. */
4374 $this->mainPhase($token);
4376 /* A character token that is not one of U+0009 CHARACTER TABULATION,
4377 U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
4378 or U+0020 SPACE. Or a start tag token. Or an end tag token. */
4379 } elseif (($token['type'] === HTML5
::CHARACTR
&&
4380 preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) ||
4381 $token['type'] === HTML5
::STARTTAG ||
$token['type'] === HTML5
::ENDTAG
4383 /* Parse error. Switch back to the main phase and reprocess the
4385 $this->phase
= self
::MAIN_PHASE
;
4386 return $this->mainPhase($token);
4388 /* An end-of-file token */
4389 } elseif ($token['type'] === HTML5
::EOF
) {
4394 private function insertElement($token, $append = true, $check = false)
4396 // Proprietary workaround for libxml2's limitations with tag names
4398 // Slightly modified HTML5 tag-name modification,
4399 // removing anything that's not an ASCII letter, digit, or hyphen
4400 $token['name'] = preg_replace('/[^a-z0-9-]/i', '', $token['name']);
4401 // Remove leading hyphens and numbers
4402 $token['name'] = ltrim($token['name'], '-0..9');
4403 // In theory, this should ever be needed, but just in case
4404 if ($token['name'] === '') {
4405 $token['name'] = 'span';
4406 } // arbitrary generic choice
4409 $el = $this->dom
->createElement($token['name']);
4411 foreach ($token['attr'] as $attr) {
4412 if (!$el->hasAttribute($attr['name'])) {
4413 $el->setAttribute($attr['name'], $attr['value']);
4417 $this->appendToRealParent($el);
4418 $this->stack
[] = $el;
4423 private function insertText($data)
4425 $text = $this->dom
->createTextNode($data);
4426 $this->appendToRealParent($text);
4429 private function insertComment($data)
4431 $comment = $this->dom
->createComment($data);
4432 $this->appendToRealParent($comment);
4435 private function appendToRealParent($node)
4437 if ($this->foster_parent
=== null) {
4438 end($this->stack
)->appendChild($node);
4440 } elseif ($this->foster_parent
!== null) {
4441 /* If the foster parent element is the parent element of the
4442 last table element in the stack of open elements, then the new
4443 node must be inserted immediately before the last table element
4444 in the stack of open elements in the foster parent element;
4445 otherwise, the new node must be appended to the foster parent
4447 for ($n = count($this->stack
) - 1; $n >= 0; $n--) {
4448 if ($this->stack
[$n]->nodeName
=== 'table' &&
4449 $this->stack
[$n]->parentNode
!== null
4451 $table = $this->stack
[$n];
4456 if (isset($table) && $this->foster_parent
->isSameNode($table->parentNode
)) {
4457 $this->foster_parent
->insertBefore($node, $table);
4459 $this->foster_parent
->appendChild($node);
4462 $this->foster_parent
= null;
4466 private function elementInScope($el, $table = false)
4468 if (is_array($el)) {
4469 foreach ($el as $element) {
4470 if ($this->elementInScope($element, $table)) {
4478 $leng = count($this->stack
);
4480 for ($n = 0; $n < $leng; $n++
) {
4481 /* 1. Initialise node to be the current node (the bottommost node of
4483 $node = $this->stack
[$leng - 1 - $n];
4485 if ($node->tagName
=== $el) {
4486 /* 2. If node is the target node, terminate in a match state. */
4489 } elseif ($node->tagName
=== 'table') {
4490 /* 3. Otherwise, if node is a table element, terminate in a failure
4494 } elseif ($table === true && in_array(
4506 /* 4. Otherwise, if the algorithm is the "has an element in scope"
4507 variant (rather than the "has an element in table scope" variant),
4508 and node is one of the following, terminate in a failure state. */
4511 } elseif ($node === $node->ownerDocument
->documentElement
) {
4512 /* 5. Otherwise, if node is an html element (root element), terminate
4513 in a failure state. (This can only happen if the node is the topmost
4514 node of the stack of open elements, and prevents the next step from
4515 being invoked if there are no more elements in the stack.) */
4519 /* Otherwise, set node to the previous entry in the stack of open
4520 elements and return to step 2. (This will never fail, since the loop
4521 will always terminate in the previous step if the top of the stack
4526 private function reconstructActiveFormattingElements()
4528 /* 1. If there are no entries in the list of active formatting elements,
4529 then there is nothing to reconstruct; stop this algorithm. */
4530 $formatting_elements = count($this->a_formatting
);
4532 if ($formatting_elements === 0) {
4536 /* 3. Let entry be the last (most recently added) element in the list
4537 of active formatting elements. */
4538 $entry = end($this->a_formatting
);
4540 /* 2. If the last (most recently added) entry in the list of active
4541 formatting elements is a marker, or if it is an element that is in the
4542 stack of open elements, then there is nothing to reconstruct; stop this
4544 if ($entry === self
::MARKER ||
in_array($entry, $this->stack
, true)) {
4548 for ($a = $formatting_elements - 1; $a >= 0; true) {
4549 /* 4. If there are no entries before entry in the list of active
4550 formatting elements, then jump to step 8. */
4552 $step_seven = false;
4556 /* 5. Let entry be the entry one earlier than entry in the list of
4557 active formatting elements. */
4559 $entry = $this->a_formatting
[$a];
4561 /* 6. If entry is neither a marker nor an element that is also in
4562 thetack of open elements, go to step 4. */
4563 if ($entry === self
::MARKER ||
in_array($entry, $this->stack
, true)) {
4569 /* 7. Let entry be the element one later than entry in the list of
4570 active formatting elements. */
4571 if (isset($step_seven) && $step_seven === true) {
4573 $entry = $this->a_formatting
[$a];
4576 /* 8. Perform a shallow clone of the element entry to obtain clone. */
4577 $clone = $entry->cloneNode();
4579 /* 9. Append clone to the current node and push it onto the stack
4580 of open elements so that it is the new current node. */
4581 end($this->stack
)->appendChild($clone);
4582 $this->stack
[] = $clone;
4584 /* 10. Replace the entry for entry in the list with an entry for
4586 $this->a_formatting
[$a] = $clone;
4588 /* 11. If the entry for clone in the list of active formatting
4589 elements is not the last entry in the list, return to step 7. */
4590 if (end($this->a_formatting
) !== $clone) {
4598 private function clearTheActiveFormattingElementsUpToTheLastMarker()
4600 /* When the steps below require the UA to clear the list of active
4601 formatting elements up to the last marker, the UA must perform the
4605 /* 1. Let entry be the last (most recently added) entry in the list
4606 of active formatting elements. */
4607 $entry = end($this->a_formatting
);
4609 /* 2. Remove entry from the list of active formatting elements. */
4610 array_pop($this->a_formatting
);
4612 /* 3. If entry was a marker, then stop the algorithm at this point.
4613 The list has been cleared up to the last marker. */
4614 if ($entry === self
::MARKER
) {
4620 private function generateImpliedEndTags($exclude = array())
4622 /* When the steps below require the UA to generate implied end tags,
4623 then, if the current node is a dd element, a dt element, an li element,
4624 a p element, a td element, a th element, or a tr element, the UA must
4625 act as if an end tag with the respective tag name had been seen and
4626 then generate implied end tags again. */
4627 $node = end($this->stack
);
4628 $elements = array_diff(array('dd', 'dt', 'li', 'p', 'td', 'th', 'tr'), $exclude);
4630 while (in_array(end($this->stack
)->nodeName
, $elements)) {
4631 array_pop($this->stack
);
4635 private function getElementCategory($node)
4637 $name = $node->tagName
;
4638 if (in_array($name, $this->special
)) {
4639 return self
::SPECIAL
;
4640 } elseif (in_array($name, $this->scoping
)) {
4641 return self
::SCOPING
;
4642 } elseif (in_array($name, $this->formatting
)) {
4643 return self
::FORMATTING
;
4645 return self
::PHRASING
;
4649 private function clearStackToTableContext($elements)
4651 /* When the steps above require the UA to clear the stack back to a
4652 table context, it means that the UA must, while the current node is not
4653 a table element or an html element, pop elements from the stack of open
4654 elements. If this causes any elements to be popped from the stack, then
4655 this is a parse error. */
4657 $node = end($this->stack
)->nodeName
;
4659 if (in_array($node, $elements)) {
4662 array_pop($this->stack
);
4667 private function resetInsertionMode()
4669 /* 1. Let last be false. */
4671 $leng = count($this->stack
);
4673 for ($n = $leng - 1; $n >= 0; $n--) {
4674 /* 2. Let node be the last node in the stack of open elements. */
4675 $node = $this->stack
[$n];
4677 /* 3. If node is the first node in the stack of open elements, then
4678 set last to true. If the element whose innerHTML attribute is being
4679 set is neither a td element nor a th element, then set node to the
4680 element whose innerHTML attribute is being set. (innerHTML case) */
4681 if ($this->stack
[0]->isSameNode($node)) {
4685 /* 4. If node is a select element, then switch the insertion mode to
4686 "in select" and abort these steps. (innerHTML case) */
4687 if ($node->nodeName
=== 'select') {
4688 $this->mode
= self
::IN_SELECT
;
4691 /* 5. If node is a td or th element, then switch the insertion mode
4692 to "in cell" and abort these steps. */
4693 } elseif ($node->nodeName
=== 'td' ||
$node->nodeName
=== 'th') {
4694 $this->mode
= self
::IN_CELL
;
4697 /* 6. If node is a tr element, then switch the insertion mode to
4698 "in row" and abort these steps. */
4699 } elseif ($node->nodeName
=== 'tr') {
4700 $this->mode
= self
::IN_ROW
;
4703 /* 7. If node is a tbody, thead, or tfoot element, then switch the
4704 insertion mode to "in table body" and abort these steps. */
4705 } elseif (in_array($node->nodeName
, array('tbody', 'thead', 'tfoot'))) {
4706 $this->mode
= self
::IN_TBODY
;
4709 /* 8. If node is a caption element, then switch the insertion mode
4710 to "in caption" and abort these steps. */
4711 } elseif ($node->nodeName
=== 'caption') {
4712 $this->mode
= self
::IN_CAPTION
;
4715 /* 9. If node is a colgroup element, then switch the insertion mode
4716 to "in column group" and abort these steps. (innerHTML case) */
4717 } elseif ($node->nodeName
=== 'colgroup') {
4718 $this->mode
= self
::IN_CGROUP
;
4721 /* 10. If node is a table element, then switch the insertion mode
4722 to "in table" and abort these steps. */
4723 } elseif ($node->nodeName
=== 'table') {
4724 $this->mode
= self
::IN_TABLE
;
4727 /* 11. If node is a head element, then switch the insertion mode
4728 to "in body" ("in body"! not "in head"!) and abort these steps.
4730 } elseif ($node->nodeName
=== 'head') {
4731 $this->mode
= self
::IN_BODY
;
4734 /* 12. If node is a body element, then switch the insertion mode to
4735 "in body" and abort these steps. */
4736 } elseif ($node->nodeName
=== 'body') {
4737 $this->mode
= self
::IN_BODY
;
4740 /* 13. If node is a frameset element, then switch the insertion
4741 mode to "in frameset" and abort these steps. (innerHTML case) */
4742 } elseif ($node->nodeName
=== 'frameset') {
4743 $this->mode
= self
::IN_FRAME
;
4746 /* 14. If node is an html element, then: if the head element
4747 pointer is null, switch the insertion mode to "before head",
4748 otherwise, switch the insertion mode to "after head". In either
4749 case, abort these steps. (innerHTML case) */
4750 } elseif ($node->nodeName
=== 'html') {
4751 $this->mode
= ($this->head_pointer
=== null)
4757 /* 15. If last is true, then set the insertion mode to "in body"
4758 and abort these steps. (innerHTML case) */
4760 $this->mode
= self
::IN_BODY
;
4766 private function closeCell()
4768 /* If the stack of open elements has a td or th element in table scope,
4769 then act as if an end tag token with that tag name had been seen. */
4770 foreach (array('td', 'th') as $cell) {
4771 if ($this->elementInScope($cell, true)) {
4775 'type' => HTML5
::ENDTAG
4784 public function save()