PageRenderTime 90ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/html-purified/lib/HTMLPurifier/Lexer/PH5P.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 3904 lines | 1977 code | 614 blank | 1313 comment | 305 complexity | fe014dc9d00bc84352578e9183917d24 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. /**
  3. * Experimental HTML5-based parser using Jeroen van der Meer's PH5P library.
  4. * Occupies space in the HTML5 pseudo-namespace, which may cause conflicts.
  5. *
  6. * @note
  7. * Recent changes to PHP's DOM extension have resulted in some fatal
  8. * error conditions with the original version of PH5P. Pending changes,
  9. * this lexer will punt to DirectLex if DOM throughs an exception.
  10. */
  11. class HTMLPurifier_Lexer_PH5P extends HTMLPurifier_Lexer_DOMLex {
  12. public function tokenizeHTML($html, $config, $context) {
  13. $new_html = $this->normalize($html, $config, $context);
  14. $new_html = $this->wrapHTML($new_html, $config, $context);
  15. try {
  16. $parser = new HTML5($new_html);
  17. $doc = $parser->save();
  18. } catch (DOMException $e) {
  19. // Uh oh, it failed. Punt to DirectLex.
  20. $lexer = new HTMLPurifier_Lexer_DirectLex();
  21. $context->register('PH5PError', $e); // save the error, so we can detect it
  22. return $lexer->tokenizeHTML($html, $config, $context); // use original HTML
  23. }
  24. $tokens = array();
  25. $this->tokenizeDOM(
  26. $doc->getElementsByTagName('html')->item(0)-> // <html>
  27. getElementsByTagName('body')->item(0)-> // <body>
  28. getElementsByTagName('div')->item(0) // <div>
  29. , $tokens);
  30. return $tokens;
  31. }
  32. }
  33. /*
  34. Copyright 2007 Jeroen van der Meer <http://jero.net/>
  35. Permission is hereby granted, free of charge, to any person obtaining a
  36. copy of this software and associated documentation files (the
  37. "Software"), to deal in the Software without restriction, including
  38. without limitation the rights to use, copy, modify, merge, publish,
  39. distribute, sublicense, and/or sell copies of the Software, and to
  40. permit persons to whom the Software is furnished to do so, subject to
  41. the following conditions:
  42. The above copyright notice and this permission notice shall be included
  43. in all copies or substantial portions of the Software.
  44. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  45. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  46. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  47. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  48. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  49. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  50. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  51. */
  52. class HTML5 {
  53. private $data;
  54. private $char;
  55. private $EOF;
  56. private $state;
  57. private $tree;
  58. private $token;
  59. private $content_model;
  60. private $escape = false;
  61. private $entities = array('AElig;','AElig','AMP;','AMP','Aacute;','Aacute',
  62. 'Acirc;','Acirc','Agrave;','Agrave','Alpha;','Aring;','Aring','Atilde;',
  63. 'Atilde','Auml;','Auml','Beta;','COPY;','COPY','Ccedil;','Ccedil','Chi;',
  64. 'Dagger;','Delta;','ETH;','ETH','Eacute;','Eacute','Ecirc;','Ecirc','Egrave;',
  65. 'Egrave','Epsilon;','Eta;','Euml;','Euml','GT;','GT','Gamma;','Iacute;',
  66. 'Iacute','Icirc;','Icirc','Igrave;','Igrave','Iota;','Iuml;','Iuml','Kappa;',
  67. 'LT;','LT','Lambda;','Mu;','Ntilde;','Ntilde','Nu;','OElig;','Oacute;',
  68. 'Oacute','Ocirc;','Ocirc','Ograve;','Ograve','Omega;','Omicron;','Oslash;',
  69. 'Oslash','Otilde;','Otilde','Ouml;','Ouml','Phi;','Pi;','Prime;','Psi;',
  70. 'QUOT;','QUOT','REG;','REG','Rho;','Scaron;','Sigma;','THORN;','THORN',
  71. 'TRADE;','Tau;','Theta;','Uacute;','Uacute','Ucirc;','Ucirc','Ugrave;',
  72. 'Ugrave','Upsilon;','Uuml;','Uuml','Xi;','Yacute;','Yacute','Yuml;','Zeta;',
  73. 'aacute;','aacute','acirc;','acirc','acute;','acute','aelig;','aelig',
  74. 'agrave;','agrave','alefsym;','alpha;','amp;','amp','and;','ang;','apos;',
  75. 'aring;','aring','asymp;','atilde;','atilde','auml;','auml','bdquo;','beta;',
  76. 'brvbar;','brvbar','bull;','cap;','ccedil;','ccedil','cedil;','cedil',
  77. 'cent;','cent','chi;','circ;','clubs;','cong;','copy;','copy','crarr;',
  78. 'cup;','curren;','curren','dArr;','dagger;','darr;','deg;','deg','delta;',
  79. 'diams;','divide;','divide','eacute;','eacute','ecirc;','ecirc','egrave;',
  80. 'egrave','empty;','emsp;','ensp;','epsilon;','equiv;','eta;','eth;','eth',
  81. 'euml;','euml','euro;','exist;','fnof;','forall;','frac12;','frac12',
  82. 'frac14;','frac14','frac34;','frac34','frasl;','gamma;','ge;','gt;','gt',
  83. 'hArr;','harr;','hearts;','hellip;','iacute;','iacute','icirc;','icirc',
  84. 'iexcl;','iexcl','igrave;','igrave','image;','infin;','int;','iota;',
  85. 'iquest;','iquest','isin;','iuml;','iuml','kappa;','lArr;','lambda;','lang;',
  86. 'laquo;','laquo','larr;','lceil;','ldquo;','le;','lfloor;','lowast;','loz;',
  87. 'lrm;','lsaquo;','lsquo;','lt;','lt','macr;','macr','mdash;','micro;','micro',
  88. 'middot;','middot','minus;','mu;','nabla;','nbsp;','nbsp','ndash;','ne;',
  89. 'ni;','not;','not','notin;','nsub;','ntilde;','ntilde','nu;','oacute;',
  90. 'oacute','ocirc;','ocirc','oelig;','ograve;','ograve','oline;','omega;',
  91. 'omicron;','oplus;','or;','ordf;','ordf','ordm;','ordm','oslash;','oslash',
  92. 'otilde;','otilde','otimes;','ouml;','ouml','para;','para','part;','permil;',
  93. 'perp;','phi;','pi;','piv;','plusmn;','plusmn','pound;','pound','prime;',
  94. 'prod;','prop;','psi;','quot;','quot','rArr;','radic;','rang;','raquo;',
  95. 'raquo','rarr;','rceil;','rdquo;','real;','reg;','reg','rfloor;','rho;',
  96. 'rlm;','rsaquo;','rsquo;','sbquo;','scaron;','sdot;','sect;','sect','shy;',
  97. 'shy','sigma;','sigmaf;','sim;','spades;','sub;','sube;','sum;','sup1;',
  98. 'sup1','sup2;','sup2','sup3;','sup3','sup;','supe;','szlig;','szlig','tau;',
  99. 'there4;','theta;','thetasym;','thinsp;','thorn;','thorn','tilde;','times;',
  100. 'times','trade;','uArr;','uacute;','uacute','uarr;','ucirc;','ucirc',
  101. 'ugrave;','ugrave','uml;','uml','upsih;','upsilon;','uuml;','uuml','weierp;',
  102. 'xi;','yacute;','yacute','yen;','yen','yuml;','yuml','zeta;','zwj;','zwnj;');
  103. const PCDATA = 0;
  104. const RCDATA = 1;
  105. const CDATA = 2;
  106. const PLAINTEXT = 3;
  107. const DOCTYPE = 0;
  108. const STARTTAG = 1;
  109. const ENDTAG = 2;
  110. const COMMENT = 3;
  111. const CHARACTR = 4;
  112. const EOF = 5;
  113. public function __construct($data) {
  114. $this->data = $data;
  115. $this->char = -1;
  116. $this->EOF = strlen($data);
  117. $this->tree = new HTML5TreeConstructer;
  118. $this->content_model = self::PCDATA;
  119. $this->state = 'data';
  120. while($this->state !== null) {
  121. $this->{$this->state.'State'}();
  122. }
  123. }
  124. public function save() {
  125. return $this->tree->save();
  126. }
  127. private function char() {
  128. return ($this->char < $this->EOF)
  129. ? $this->data[$this->char]
  130. : false;
  131. }
  132. private function character($s, $l = 0) {
  133. if($s + $l < $this->EOF) {
  134. if($l === 0) {
  135. return $this->data[$s];
  136. } else {
  137. return substr($this->data, $s, $l);
  138. }
  139. }
  140. }
  141. private function characters($char_class, $start) {
  142. return preg_replace('#^(['.$char_class.']+).*#s', '\\1', substr($this->data, $start));
  143. }
  144. private function dataState() {
  145. // Consume the next input character
  146. $this->char++;
  147. $char = $this->char();
  148. if($char === '&' && ($this->content_model === self::PCDATA || $this->content_model === self::RCDATA)) {
  149. /* U+0026 AMPERSAND (&)
  150. When the content model flag is set to one of the PCDATA or RCDATA
  151. states: switch to the entity data state. Otherwise: treat it as per
  152. the "anything else" entry below. */
  153. $this->state = 'entityData';
  154. } elseif($char === '-') {
  155. /* If the content model flag is set to either the RCDATA state or
  156. the CDATA state, and the escape flag is false, and there are at
  157. least three characters before this one in the input stream, and the
  158. last four characters in the input stream, including this one, are
  159. U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+002D HYPHEN-MINUS,
  160. and U+002D HYPHEN-MINUS ("<!--"), then set the escape flag to true. */
  161. if(($this->content_model === self::RCDATA || $this->content_model ===
  162. self::CDATA) && $this->escape === false &&
  163. $this->char >= 3 && $this->character($this->char - 4, 4) === '<!--') {
  164. $this->escape = true;
  165. }
  166. /* In any case, emit the input character as a character token. Stay
  167. in the data state. */
  168. $this->emitToken(array(
  169. 'type' => self::CHARACTR,
  170. 'data' => $char
  171. ));
  172. /* U+003C LESS-THAN SIGN (<) */
  173. } elseif($char === '<' && ($this->content_model === self::PCDATA ||
  174. (($this->content_model === self::RCDATA ||
  175. $this->content_model === self::CDATA) && $this->escape === false))) {
  176. /* When the content model flag is set to the PCDATA state: switch
  177. to the tag open state.
  178. When the content model flag is set to either the RCDATA state or
  179. the CDATA state and the escape flag is false: switch to the tag
  180. open state.
  181. Otherwise: treat it as per the "anything else" entry below. */
  182. $this->state = 'tagOpen';
  183. /* U+003E GREATER-THAN SIGN (>) */
  184. } elseif($char === '>') {
  185. /* If the content model flag is set to either the RCDATA state or
  186. the CDATA state, and the escape flag is true, and the last three
  187. characters in the input stream including this one are U+002D
  188. HYPHEN-MINUS, U+002D HYPHEN-MINUS, U+003E GREATER-THAN SIGN ("-->"),
  189. set the escape flag to false. */
  190. if(($this->content_model === self::RCDATA ||
  191. $this->content_model === self::CDATA) && $this->escape === true &&
  192. $this->character($this->char, 3) === '-->') {
  193. $this->escape = false;
  194. }
  195. /* In any case, emit the input character as a character token.
  196. Stay in the data state. */
  197. $this->emitToken(array(
  198. 'type' => self::CHARACTR,
  199. 'data' => $char
  200. ));
  201. } elseif($this->char === $this->EOF) {
  202. /* EOF
  203. Emit an end-of-file token. */
  204. $this->EOF();
  205. } elseif($this->content_model === self::PLAINTEXT) {
  206. /* When the content model flag is set to the PLAINTEXT state
  207. THIS DIFFERS GREATLY FROM THE SPEC: Get the remaining characters of
  208. the text and emit it as a character token. */
  209. $this->emitToken(array(
  210. 'type' => self::CHARACTR,
  211. 'data' => substr($this->data, $this->char)
  212. ));
  213. $this->EOF();
  214. } else {
  215. /* Anything else
  216. THIS DIFFERS GREATLY FROM THE SPEC: Get as many character that
  217. otherwise would also be treated as a character token and emit it
  218. as a single character token. Stay in the data state. */
  219. $len = strcspn($this->data, '<&', $this->char);
  220. $char = substr($this->data, $this->char, $len);
  221. $this->char += $len - 1;
  222. $this->emitToken(array(
  223. 'type' => self::CHARACTR,
  224. 'data' => $char
  225. ));
  226. $this->state = 'data';
  227. }
  228. }
  229. private function entityDataState() {
  230. // Attempt to consume an entity.
  231. $entity = $this->entity();
  232. // If nothing is returned, emit a U+0026 AMPERSAND character token.
  233. // Otherwise, emit the character token that was returned.
  234. $char = (!$entity) ? '&' : $entity;
  235. $this->emitToken(array(
  236. 'type' => self::CHARACTR,
  237. 'data' => $char
  238. ));
  239. // Finally, switch to the data state.
  240. $this->state = 'data';
  241. }
  242. private function tagOpenState() {
  243. switch($this->content_model) {
  244. case self::RCDATA:
  245. case self::CDATA:
  246. /* If the next input character is a U+002F SOLIDUS (/) character,
  247. consume it and switch to the close tag open state. If the next
  248. input character is not a U+002F SOLIDUS (/) character, emit a
  249. U+003C LESS-THAN SIGN character token and switch to the data
  250. state to process the next input character. */
  251. if($this->character($this->char + 1) === '/') {
  252. $this->char++;
  253. $this->state = 'closeTagOpen';
  254. } else {
  255. $this->emitToken(array(
  256. 'type' => self::CHARACTR,
  257. 'data' => '<'
  258. ));
  259. $this->state = 'data';
  260. }
  261. break;
  262. case self::PCDATA:
  263. // If the content model flag is set to the PCDATA state
  264. // Consume the next input character:
  265. $this->char++;
  266. $char = $this->char();
  267. if($char === '!') {
  268. /* U+0021 EXCLAMATION MARK (!)
  269. Switch to the markup declaration open state. */
  270. $this->state = 'markupDeclarationOpen';
  271. } elseif($char === '/') {
  272. /* U+002F SOLIDUS (/)
  273. Switch to the close tag open state. */
  274. $this->state = 'closeTagOpen';
  275. } elseif(preg_match('/^[A-Za-z]$/', $char)) {
  276. /* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
  277. Create a new start tag token, set its tag name to the lowercase
  278. version of the input character (add 0x0020 to the character's code
  279. point), then switch to the tag name state. (Don't emit the token
  280. yet; further details will be filled in before it is emitted.) */
  281. $this->token = array(
  282. 'name' => strtolower($char),
  283. 'type' => self::STARTTAG,
  284. 'attr' => array()
  285. );
  286. $this->state = 'tagName';
  287. } elseif($char === '>') {
  288. /* U+003E GREATER-THAN SIGN (>)
  289. Parse error. Emit a U+003C LESS-THAN SIGN character token and a
  290. U+003E GREATER-THAN SIGN character token. Switch to the data state. */
  291. $this->emitToken(array(
  292. 'type' => self::CHARACTR,
  293. 'data' => '<>'
  294. ));
  295. $this->state = 'data';
  296. } elseif($char === '?') {
  297. /* U+003F QUESTION MARK (?)
  298. Parse error. Switch to the bogus comment state. */
  299. $this->state = 'bogusComment';
  300. } else {
  301. /* Anything else
  302. Parse error. Emit a U+003C LESS-THAN SIGN character token and
  303. reconsume the current input character in the data state. */
  304. $this->emitToken(array(
  305. 'type' => self::CHARACTR,
  306. 'data' => '<'
  307. ));
  308. $this->char--;
  309. $this->state = 'data';
  310. }
  311. break;
  312. }
  313. }
  314. private function closeTagOpenState() {
  315. $next_node = strtolower($this->characters('A-Za-z', $this->char + 1));
  316. $the_same = count($this->tree->stack) > 0 && $next_node === end($this->tree->stack)->nodeName;
  317. if(($this->content_model === self::RCDATA || $this->content_model === self::CDATA) &&
  318. (!$the_same || ($the_same && (!preg_match('/[\t\n\x0b\x0c >\/]/',
  319. $this->character($this->char + 1 + strlen($next_node))) || $this->EOF === $this->char)))) {
  320. /* If the content model flag is set to the RCDATA or CDATA states then
  321. examine the next few characters. If they do not match the tag name of
  322. the last start tag token emitted (case insensitively), or if they do but
  323. they are not immediately followed by one of the following characters:
  324. * U+0009 CHARACTER TABULATION
  325. * U+000A LINE FEED (LF)
  326. * U+000B LINE TABULATION
  327. * U+000C FORM FEED (FF)
  328. * U+0020 SPACE
  329. * U+003E GREATER-THAN SIGN (>)
  330. * U+002F SOLIDUS (/)
  331. * EOF
  332. ...then there is a parse error. Emit a U+003C LESS-THAN SIGN character
  333. token, a U+002F SOLIDUS character token, and switch to the data state
  334. to process the next input character. */
  335. $this->emitToken(array(
  336. 'type' => self::CHARACTR,
  337. 'data' => '</'
  338. ));
  339. $this->state = 'data';
  340. } else {
  341. /* Otherwise, if the content model flag is set to the PCDATA state,
  342. or if the next few characters do match that tag name, consume the
  343. next input character: */
  344. $this->char++;
  345. $char = $this->char();
  346. if(preg_match('/^[A-Za-z]$/', $char)) {
  347. /* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
  348. Create a new end tag token, set its tag name to the lowercase version
  349. of the input character (add 0x0020 to the character's code point), then
  350. switch to the tag name state. (Don't emit the token yet; further details
  351. will be filled in before it is emitted.) */
  352. $this->token = array(
  353. 'name' => strtolower($char),
  354. 'type' => self::ENDTAG
  355. );
  356. $this->state = 'tagName';
  357. } elseif($char === '>') {
  358. /* U+003E GREATER-THAN SIGN (>)
  359. Parse error. Switch to the data state. */
  360. $this->state = 'data';
  361. } elseif($this->char === $this->EOF) {
  362. /* EOF
  363. Parse error. Emit a U+003C LESS-THAN SIGN character token and a U+002F
  364. SOLIDUS character token. Reconsume the EOF character in the data state. */
  365. $this->emitToken(array(
  366. 'type' => self::CHARACTR,
  367. 'data' => '</'
  368. ));
  369. $this->char--;
  370. $this->state = 'data';
  371. } else {
  372. /* Parse error. Switch to the bogus comment state. */
  373. $this->state = 'bogusComment';
  374. }
  375. }
  376. }
  377. private function tagNameState() {
  378. // Consume the next input character:
  379. $this->char++;
  380. $char = $this->character($this->char);
  381. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  382. /* U+0009 CHARACTER TABULATION
  383. U+000A LINE FEED (LF)
  384. U+000B LINE TABULATION
  385. U+000C FORM FEED (FF)
  386. U+0020 SPACE
  387. Switch to the before attribute name state. */
  388. $this->state = 'beforeAttributeName';
  389. } elseif($char === '>') {
  390. /* U+003E GREATER-THAN SIGN (>)
  391. Emit the current tag token. Switch to the data state. */
  392. $this->emitToken($this->token);
  393. $this->state = 'data';
  394. } elseif($this->char === $this->EOF) {
  395. /* EOF
  396. Parse error. Emit the current tag token. Reconsume the EOF
  397. character in the data state. */
  398. $this->emitToken($this->token);
  399. $this->char--;
  400. $this->state = 'data';
  401. } elseif($char === '/') {
  402. /* U+002F SOLIDUS (/)
  403. Parse error unless this is a permitted slash. Switch to the before
  404. attribute name state. */
  405. $this->state = 'beforeAttributeName';
  406. } else {
  407. /* Anything else
  408. Append the current input character to the current tag token's tag name.
  409. Stay in the tag name state. */
  410. $this->token['name'] .= strtolower($char);
  411. $this->state = 'tagName';
  412. }
  413. }
  414. private function beforeAttributeNameState() {
  415. // Consume the next input character:
  416. $this->char++;
  417. $char = $this->character($this->char);
  418. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  419. /* U+0009 CHARACTER TABULATION
  420. U+000A LINE FEED (LF)
  421. U+000B LINE TABULATION
  422. U+000C FORM FEED (FF)
  423. U+0020 SPACE
  424. Stay in the before attribute name state. */
  425. $this->state = 'beforeAttributeName';
  426. } elseif($char === '>') {
  427. /* U+003E GREATER-THAN SIGN (>)
  428. Emit the current tag token. Switch to the data state. */
  429. $this->emitToken($this->token);
  430. $this->state = 'data';
  431. } elseif($char === '/') {
  432. /* U+002F SOLIDUS (/)
  433. Parse error unless this is a permitted slash. Stay in the before
  434. attribute name state. */
  435. $this->state = 'beforeAttributeName';
  436. } elseif($this->char === $this->EOF) {
  437. /* EOF
  438. Parse error. Emit the current tag token. Reconsume the EOF
  439. character in the data state. */
  440. $this->emitToken($this->token);
  441. $this->char--;
  442. $this->state = 'data';
  443. } else {
  444. /* Anything else
  445. Start a new attribute in the current tag token. Set that attribute's
  446. name to the current input character, and its value to the empty string.
  447. Switch to the attribute name state. */
  448. $this->token['attr'][] = array(
  449. 'name' => strtolower($char),
  450. 'value' => null
  451. );
  452. $this->state = 'attributeName';
  453. }
  454. }
  455. private function attributeNameState() {
  456. // Consume the next input character:
  457. $this->char++;
  458. $char = $this->character($this->char);
  459. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  460. /* U+0009 CHARACTER TABULATION
  461. U+000A LINE FEED (LF)
  462. U+000B LINE TABULATION
  463. U+000C FORM FEED (FF)
  464. U+0020 SPACE
  465. Stay in the before attribute name state. */
  466. $this->state = 'afterAttributeName';
  467. } elseif($char === '=') {
  468. /* U+003D EQUALS SIGN (=)
  469. Switch to the before attribute value state. */
  470. $this->state = 'beforeAttributeValue';
  471. } elseif($char === '>') {
  472. /* U+003E GREATER-THAN SIGN (>)
  473. Emit the current tag token. Switch to the data state. */
  474. $this->emitToken($this->token);
  475. $this->state = 'data';
  476. } elseif($char === '/' && $this->character($this->char + 1) !== '>') {
  477. /* U+002F SOLIDUS (/)
  478. Parse error unless this is a permitted slash. Switch to the before
  479. attribute name state. */
  480. $this->state = 'beforeAttributeName';
  481. } elseif($this->char === $this->EOF) {
  482. /* EOF
  483. Parse error. Emit the current tag token. Reconsume the EOF
  484. character in the data state. */
  485. $this->emitToken($this->token);
  486. $this->char--;
  487. $this->state = 'data';
  488. } else {
  489. /* Anything else
  490. Append the current input character to the current attribute's name.
  491. Stay in the attribute name state. */
  492. $last = count($this->token['attr']) - 1;
  493. $this->token['attr'][$last]['name'] .= strtolower($char);
  494. $this->state = 'attributeName';
  495. }
  496. }
  497. private function afterAttributeNameState() {
  498. // Consume the next input character:
  499. $this->char++;
  500. $char = $this->character($this->char);
  501. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  502. /* U+0009 CHARACTER TABULATION
  503. U+000A LINE FEED (LF)
  504. U+000B LINE TABULATION
  505. U+000C FORM FEED (FF)
  506. U+0020 SPACE
  507. Stay in the after attribute name state. */
  508. $this->state = 'afterAttributeName';
  509. } elseif($char === '=') {
  510. /* U+003D EQUALS SIGN (=)
  511. Switch to the before attribute value state. */
  512. $this->state = 'beforeAttributeValue';
  513. } elseif($char === '>') {
  514. /* U+003E GREATER-THAN SIGN (>)
  515. Emit the current tag token. Switch to the data state. */
  516. $this->emitToken($this->token);
  517. $this->state = 'data';
  518. } elseif($char === '/' && $this->character($this->char + 1) !== '>') {
  519. /* U+002F SOLIDUS (/)
  520. Parse error unless this is a permitted slash. Switch to the
  521. before attribute name state. */
  522. $this->state = 'beforeAttributeName';
  523. } elseif($this->char === $this->EOF) {
  524. /* EOF
  525. Parse error. Emit the current tag token. Reconsume the EOF
  526. character in the data state. */
  527. $this->emitToken($this->token);
  528. $this->char--;
  529. $this->state = 'data';
  530. } else {
  531. /* Anything else
  532. Start a new attribute in the current tag token. Set that attribute's
  533. name to the current input character, and its value to the empty string.
  534. Switch to the attribute name state. */
  535. $this->token['attr'][] = array(
  536. 'name' => strtolower($char),
  537. 'value' => null
  538. );
  539. $this->state = 'attributeName';
  540. }
  541. }
  542. private function beforeAttributeValueState() {
  543. // Consume the next input character:
  544. $this->char++;
  545. $char = $this->character($this->char);
  546. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  547. /* U+0009 CHARACTER TABULATION
  548. U+000A LINE FEED (LF)
  549. U+000B LINE TABULATION
  550. U+000C FORM FEED (FF)
  551. U+0020 SPACE
  552. Stay in the before attribute value state. */
  553. $this->state = 'beforeAttributeValue';
  554. } elseif($char === '"') {
  555. /* U+0022 QUOTATION MARK (")
  556. Switch to the attribute value (double-quoted) state. */
  557. $this->state = 'attributeValueDoubleQuoted';
  558. } elseif($char === '&') {
  559. /* U+0026 AMPERSAND (&)
  560. Switch to the attribute value (unquoted) state and reconsume
  561. this input character. */
  562. $this->char--;
  563. $this->state = 'attributeValueUnquoted';
  564. } elseif($char === '\'') {
  565. /* U+0027 APOSTROPHE (')
  566. Switch to the attribute value (single-quoted) state. */
  567. $this->state = 'attributeValueSingleQuoted';
  568. } elseif($char === '>') {
  569. /* U+003E GREATER-THAN SIGN (>)
  570. Emit the current tag token. Switch to the data state. */
  571. $this->emitToken($this->token);
  572. $this->state = 'data';
  573. } else {
  574. /* Anything else
  575. Append the current input character to the current attribute's value.
  576. Switch to the attribute value (unquoted) state. */
  577. $last = count($this->token['attr']) - 1;
  578. $this->token['attr'][$last]['value'] .= $char;
  579. $this->state = 'attributeValueUnquoted';
  580. }
  581. }
  582. private function attributeValueDoubleQuotedState() {
  583. // Consume the next input character:
  584. $this->char++;
  585. $char = $this->character($this->char);
  586. if($char === '"') {
  587. /* U+0022 QUOTATION MARK (")
  588. Switch to the before attribute name state. */
  589. $this->state = 'beforeAttributeName';
  590. } elseif($char === '&') {
  591. /* U+0026 AMPERSAND (&)
  592. Switch to the entity in attribute value state. */
  593. $this->entityInAttributeValueState('double');
  594. } elseif($this->char === $this->EOF) {
  595. /* EOF
  596. Parse error. Emit the current tag token. Reconsume the character
  597. in the data state. */
  598. $this->emitToken($this->token);
  599. $this->char--;
  600. $this->state = 'data';
  601. } else {
  602. /* Anything else
  603. Append the current input character to the current attribute's value.
  604. Stay in the attribute value (double-quoted) state. */
  605. $last = count($this->token['attr']) - 1;
  606. $this->token['attr'][$last]['value'] .= $char;
  607. $this->state = 'attributeValueDoubleQuoted';
  608. }
  609. }
  610. private function attributeValueSingleQuotedState() {
  611. // Consume the next input character:
  612. $this->char++;
  613. $char = $this->character($this->char);
  614. if($char === '\'') {
  615. /* U+0022 QUOTATION MARK (')
  616. Switch to the before attribute name state. */
  617. $this->state = 'beforeAttributeName';
  618. } elseif($char === '&') {
  619. /* U+0026 AMPERSAND (&)
  620. Switch to the entity in attribute value state. */
  621. $this->entityInAttributeValueState('single');
  622. } elseif($this->char === $this->EOF) {
  623. /* EOF
  624. Parse error. Emit the current tag token. Reconsume the character
  625. in the data state. */
  626. $this->emitToken($this->token);
  627. $this->char--;
  628. $this->state = 'data';
  629. } else {
  630. /* Anything else
  631. Append the current input character to the current attribute's value.
  632. Stay in the attribute value (single-quoted) state. */
  633. $last = count($this->token['attr']) - 1;
  634. $this->token['attr'][$last]['value'] .= $char;
  635. $this->state = 'attributeValueSingleQuoted';
  636. }
  637. }
  638. private function attributeValueUnquotedState() {
  639. // Consume the next input character:
  640. $this->char++;
  641. $char = $this->character($this->char);
  642. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  643. /* U+0009 CHARACTER TABULATION
  644. U+000A LINE FEED (LF)
  645. U+000B LINE TABULATION
  646. U+000C FORM FEED (FF)
  647. U+0020 SPACE
  648. Switch to the before attribute name state. */
  649. $this->state = 'beforeAttributeName';
  650. } elseif($char === '&') {
  651. /* U+0026 AMPERSAND (&)
  652. Switch to the entity in attribute value state. */
  653. $this->entityInAttributeValueState();
  654. } elseif($char === '>') {
  655. /* U+003E GREATER-THAN SIGN (>)
  656. Emit the current tag token. Switch to the data state. */
  657. $this->emitToken($this->token);
  658. $this->state = 'data';
  659. } else {
  660. /* Anything else
  661. Append the current input character to the current attribute's value.
  662. Stay in the attribute value (unquoted) state. */
  663. $last = count($this->token['attr']) - 1;
  664. $this->token['attr'][$last]['value'] .= $char;
  665. $this->state = 'attributeValueUnquoted';
  666. }
  667. }
  668. private function entityInAttributeValueState() {
  669. // Attempt to consume an entity.
  670. $entity = $this->entity();
  671. // If nothing is returned, append a U+0026 AMPERSAND character to the
  672. // current attribute's value. Otherwise, emit the character token that
  673. // was returned.
  674. $char = (!$entity)
  675. ? '&'
  676. : $entity;
  677. $last = count($this->token['attr']) - 1;
  678. $this->token['attr'][$last]['value'] .= $char;
  679. }
  680. private function bogusCommentState() {
  681. /* Consume every character up to the first U+003E GREATER-THAN SIGN
  682. character (>) or the end of the file (EOF), whichever comes first. Emit
  683. a comment token whose data is the concatenation of all the characters
  684. starting from and including the character that caused the state machine
  685. to switch into the bogus comment state, up to and including the last
  686. consumed character before the U+003E character, if any, or up to the
  687. end of the file otherwise. (If the comment was started by the end of
  688. the file (EOF), the token is empty.) */
  689. $data = $this->characters('^>', $this->char);
  690. $this->emitToken(array(
  691. 'data' => $data,
  692. 'type' => self::COMMENT
  693. ));
  694. $this->char += strlen($data);
  695. /* Switch to the data state. */
  696. $this->state = 'data';
  697. /* If the end of the file was reached, reconsume the EOF character. */
  698. if($this->char === $this->EOF) {
  699. $this->char = $this->EOF - 1;
  700. }
  701. }
  702. private function markupDeclarationOpenState() {
  703. /* If the next two characters are both U+002D HYPHEN-MINUS (-)
  704. characters, consume those two characters, create a comment token whose
  705. data is the empty string, and switch to the comment state. */
  706. if($this->character($this->char + 1, 2) === '--') {
  707. $this->char += 2;
  708. $this->state = 'comment';
  709. $this->token = array(
  710. 'data' => null,
  711. 'type' => self::COMMENT
  712. );
  713. /* Otherwise if the next seven chacacters are a case-insensitive match
  714. for the word "DOCTYPE", then consume those characters and switch to the
  715. DOCTYPE state. */
  716. } elseif(strtolower($this->character($this->char + 1, 7)) === 'doctype') {
  717. $this->char += 7;
  718. $this->state = 'doctype';
  719. /* Otherwise, is is a parse error. Switch to the bogus comment state.
  720. The next character that is consumed, if any, is the first character
  721. that will be in the comment. */
  722. } else {
  723. $this->char++;
  724. $this->state = 'bogusComment';
  725. }
  726. }
  727. private function commentState() {
  728. /* Consume the next input character: */
  729. $this->char++;
  730. $char = $this->char();
  731. /* U+002D HYPHEN-MINUS (-) */
  732. if($char === '-') {
  733. /* Switch to the comment dash state */
  734. $this->state = 'commentDash';
  735. /* EOF */
  736. } elseif($this->char === $this->EOF) {
  737. /* Parse error. Emit the comment token. Reconsume the EOF character
  738. in the data state. */
  739. $this->emitToken($this->token);
  740. $this->char--;
  741. $this->state = 'data';
  742. /* Anything else */
  743. } else {
  744. /* Append the input character to the comment token's data. Stay in
  745. the comment state. */
  746. $this->token['data'] .= $char;
  747. }
  748. }
  749. private function commentDashState() {
  750. /* Consume the next input character: */
  751. $this->char++;
  752. $char = $this->char();
  753. /* U+002D HYPHEN-MINUS (-) */
  754. if($char === '-') {
  755. /* Switch to the comment end state */
  756. $this->state = 'commentEnd';
  757. /* EOF */
  758. } elseif($this->char === $this->EOF) {
  759. /* Parse error. Emit the comment token. Reconsume the EOF character
  760. in the data state. */
  761. $this->emitToken($this->token);
  762. $this->char--;
  763. $this->state = 'data';
  764. /* Anything else */
  765. } else {
  766. /* Append a U+002D HYPHEN-MINUS (-) character and the input
  767. character to the comment token's data. Switch to the comment state. */
  768. $this->token['data'] .= '-'.$char;
  769. $this->state = 'comment';
  770. }
  771. }
  772. private function commentEndState() {
  773. /* Consume the next input character: */
  774. $this->char++;
  775. $char = $this->char();
  776. if($char === '>') {
  777. $this->emitToken($this->token);
  778. $this->state = 'data';
  779. } elseif($char === '-') {
  780. $this->token['data'] .= '-';
  781. } elseif($this->char === $this->EOF) {
  782. $this->emitToken($this->token);
  783. $this->char--;
  784. $this->state = 'data';
  785. } else {
  786. $this->token['data'] .= '--'.$char;
  787. $this->state = 'comment';
  788. }
  789. }
  790. private function doctypeState() {
  791. /* Consume the next input character: */
  792. $this->char++;
  793. $char = $this->char();
  794. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  795. $this->state = 'beforeDoctypeName';
  796. } else {
  797. $this->char--;
  798. $this->state = 'beforeDoctypeName';
  799. }
  800. }
  801. private function beforeDoctypeNameState() {
  802. /* Consume the next input character: */
  803. $this->char++;
  804. $char = $this->char();
  805. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  806. // Stay in the before DOCTYPE name state.
  807. } elseif(preg_match('/^[a-z]$/', $char)) {
  808. $this->token = array(
  809. 'name' => strtoupper($char),
  810. 'type' => self::DOCTYPE,
  811. 'error' => true
  812. );
  813. $this->state = 'doctypeName';
  814. } elseif($char === '>') {
  815. $this->emitToken(array(
  816. 'name' => null,
  817. 'type' => self::DOCTYPE,
  818. 'error' => true
  819. ));
  820. $this->state = 'data';
  821. } elseif($this->char === $this->EOF) {
  822. $this->emitToken(array(
  823. 'name' => null,
  824. 'type' => self::DOCTYPE,
  825. 'error' => true
  826. ));
  827. $this->char--;
  828. $this->state = 'data';
  829. } else {
  830. $this->token = array(
  831. 'name' => $char,
  832. 'type' => self::DOCTYPE,
  833. 'error' => true
  834. );
  835. $this->state = 'doctypeName';
  836. }
  837. }
  838. private function doctypeNameState() {
  839. /* Consume the next input character: */
  840. $this->char++;
  841. $char = $this->char();
  842. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  843. $this->state = 'AfterDoctypeName';
  844. } elseif($char === '>') {
  845. $this->emitToken($this->token);
  846. $this->state = 'data';
  847. } elseif(preg_match('/^[a-z]$/', $char)) {
  848. $this->token['name'] .= strtoupper($char);
  849. } elseif($this->char === $this->EOF) {
  850. $this->emitToken($this->token);
  851. $this->char--;
  852. $this->state = 'data';
  853. } else {
  854. $this->token['name'] .= $char;
  855. }
  856. $this->token['error'] = ($this->token['name'] === 'HTML')
  857. ? false
  858. : true;
  859. }
  860. private function afterDoctypeNameState() {
  861. /* Consume the next input character: */
  862. $this->char++;
  863. $char = $this->char();
  864. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  865. // Stay in the DOCTYPE name state.
  866. } elseif($char === '>') {
  867. $this->emitToken($this->token);
  868. $this->state = 'data';
  869. } elseif($this->char === $this->EOF) {
  870. $this->emitToken($this->token);
  871. $this->char--;
  872. $this->state = 'data';
  873. } else {
  874. $this->token['error'] = true;
  875. $this->state = 'bogusDoctype';
  876. }
  877. }
  878. private function bogusDoctypeState() {
  879. /* Consume the next input character: */
  880. $this->char++;
  881. $char = $this->char();
  882. if($char === '>') {
  883. $this->emitToken($this->token);
  884. $this->state = 'data';
  885. } elseif($this->char === $this->EOF) {
  886. $this->emitToken($this->token);
  887. $this->char--;
  888. $this->state = 'data';
  889. } else {
  890. // Stay in the bogus DOCTYPE state.
  891. }
  892. }
  893. private function entity() {
  894. $start = $this->char;
  895. // This section defines how to consume an entity. This definition is
  896. // used when parsing entities in text and in attributes.
  897. // The behaviour depends on the identity of the next character (the
  898. // one immediately after the U+0026 AMPERSAND character):
  899. switch($this->character($this->char + 1)) {
  900. // U+0023 NUMBER SIGN (#)
  901. case '#':
  902. // The behaviour further depends on the character after the
  903. // U+0023 NUMBER SIGN:
  904. switch($this->character($this->char + 1)) {
  905. // U+0078 LATIN SMALL LETTER X
  906. // U+0058 LATIN CAPITAL LETTER X
  907. case 'x':
  908. case 'X':
  909. // Follow the steps below, but using the range of
  910. // characters U+0030 DIGIT ZERO through to U+0039 DIGIT
  911. // NINE, U+0061 LATIN SMALL LETTER A through to U+0066
  912. // LATIN SMALL LETTER F, and U+0041 LATIN CAPITAL LETTER
  913. // A, through to U+0046 LATIN CAPITAL LETTER F (in other
  914. // words, 0-9, A-F, a-f).
  915. $char = 1;
  916. $char_class = '0-9A-Fa-f';
  917. break;
  918. // Anything else
  919. default:
  920. // Follow the steps below, but using the range of
  921. // characters U+0030 DIGIT ZERO through to U+0039 DIGIT
  922. // NINE (i.e. just 0-9).
  923. $char = 0;
  924. $char_class = '0-9';
  925. break;
  926. }
  927. // Consume as many characters as match the range of characters
  928. // given above.
  929. $this->char++;
  930. $e_name = $this->characters($char_class, $this->char + $char + 1);
  931. $entity = $this->character($start, $this->char);
  932. $cond = strlen($e_name) > 0;
  933. // The rest of the parsing happens bellow.
  934. break;
  935. // Anything else
  936. default:
  937. // Consume the maximum number of characters possible, with the
  938. // consumed characters case-sensitively matching one of the
  939. // identifiers in the first column of the entities table.
  940. $e_name = $this->characters('0-9A-Za-z;', $this->char + 1);
  941. $len = strlen($e_name);
  942. for($c = 1; $c <= $len; $c++) {
  943. $id = substr($e_name, 0, $c);
  944. $this->char++;
  945. if(in_array($id, $this->entities)) {
  946. if ($e_name[$c-1] !== ';') {
  947. if ($c < $len && $e_name[$c] == ';') {
  948. $this->char++; // consume extra semicolon
  949. }
  950. }
  951. $entity = $id;
  952. break;
  953. }
  954. }
  955. $cond = isset($entity);
  956. // The rest of the parsing happens bellow.
  957. break;
  958. }
  959. if(!$cond) {
  960. // If no match can be made, then this is a parse error. No
  961. // characters are consumed, and nothing is returned.
  962. $this->char = $start;
  963. return false;
  964. }
  965. // Return a character token for the character corresponding to the
  966. // entity name (as given by the second column of the entities table).
  967. return html_entity_decode('&'.$entity.';', ENT_QUOTES, 'UTF-8');
  968. }
  969. private function emitToken($token) {
  970. $emit = $this->tree->emitToken($token);
  971. if(is_int($emit)) {
  972. $this->content_model = $emit;
  973. } elseif($token['type'] === self::ENDTAG) {
  974. $this->content_model = self::PCDATA;
  975. }
  976. }
  977. private function EOF() {
  978. $this->state = null;
  979. $this->tree->emitToken(array(
  980. 'type' => self::EOF
  981. ));
  982. }
  983. }
  984. class HTML5TreeConstructer {
  985. public $stack = array();
  986. private $phase;
  987. private $mode;
  988. private $dom;
  989. private $foster_parent = null;
  990. private $a_formatting = array();
  991. private $head_pointer = null;
  992. private $form_pointer = null;
  993. private $scoping = array('button','caption','html','marquee','object','table','td','th');
  994. private $formatting = array('a','b','big','em','font','i','nobr','s','small','strike','strong','tt','u');
  995. private $special = array('address','area','base','basefont','bgsound',
  996. 'blockquote','body','br','center','col','colgroup','dd','dir','div','dl',
  997. 'dt','embed','fieldset','form','frame','frameset','h1','h2','h3','h4','h5',
  998. 'h6','head','hr','iframe','image','img','input','isindex','li','link',
  999. 'listing','menu','meta','noembed','noframes','noscript','ol','optgroup',
  1000. 'option','p','param','plaintext','pre','script','select','spacer','style',
  1001. 'tbody','textarea','tfoot','thead','title','tr','ul','wbr');
  1002. // The different phases.
  1003. const INIT_PHASE = 0;
  1004. const ROOT_PHASE = 1;
  1005. const MAIN_PHASE = 2;
  1006. const END_PHASE = 3;
  1007. // The different insertion modes for the main phase.
  1008. const BEFOR_HEAD = 0;
  1009. const IN_HEAD = 1;
  1010. const AFTER_HEAD = 2;
  1011. const IN_BODY = 3;
  1012. const IN_TABLE = 4;
  1013. const IN_CAPTION = 5;
  1014. const IN_CGROUP = 6;
  1015. const IN_TBODY = 7;
  1016. const IN_ROW = 8;
  1017. const IN_CELL = 9;
  1018. const IN_SELECT = 10;
  1019. const AFTER_BODY = 11;
  1020. const IN_FRAME = 12;
  1021. const AFTR_FRAME = 13;
  1022. // The different types of elements.
  1023. const SPECIAL = 0;
  1024. const SCOPING = 1;
  1025. const FORMATTING = 2;
  1026. const PHRASING = 3;
  1027. const MARKER = 0;
  1028. public function __construct() {
  1029. $this->phase = self::INIT_PHASE;
  1030. $this->mode = self::BEFOR_HEAD;
  1031. $this->dom = new DOMDocument;
  1032. $this->dom->encoding = 'UTF-8';
  1033. $this->dom->preserveWhiteSpace = true;
  1034. $this->dom->substituteEntities = true;
  1035. $this->dom->strictErrorChecking = false;
  1036. }
  1037. // Process tag tokens
  1038. public function emitToken($token) {
  1039. switch($this->phase) {
  1040. case self::INIT_PHASE: return $this->initPhase($token); break;
  1041. case self::ROOT_PHASE: return $this->rootElementPhase($token); break;
  1042. case self::MAIN_PHASE: return $this->mainPhase($token); break;
  1043. case self::END_PHASE : return $this->trailingEndPhase($token); break;
  1044. }
  1045. }
  1046. private function initPhase($token) {
  1047. /* Initially, the tree construction stage must handle each token
  1048. emitted from the tokenisation stage as follows: */
  1049. /* A DOCTYPE token that is marked as being in error
  1050. A comment token
  1051. A start tag token
  1052. An end tag token
  1053. A character token that is not one of one of U+0009 CHARACTER TABULATION,
  1054. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1055. or U+0020 SPACE
  1056. An end-of-file token */
  1057. if((isset($token['error']) && $token['error']) ||
  1058. $token['type'] === HTML5::COMMENT ||
  1059. $token['type'] === HTML5::STARTTAG ||
  1060. $token['type'] === HTML5::ENDTAG ||
  1061. $token['type'] === HTML5::EOF ||
  1062. ($token['type'] === HTML5::CHARACTR && isset($token['data']) &&
  1063. !preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data']))) {
  1064. /* This specification does not define how to handle this case. In
  1065. particular, user agents may ignore the entirety of this specification
  1066. altogether for such documents, and instead invoke special parse modes
  1067. with a greater emphasis on backwards compatibility. */
  1068. $this->phase = self::ROOT_PHASE;
  1069. return $this->rootElementPhase($token);
  1070. /* A DOCTYPE token marked as being correct */
  1071. } elseif(isset($token['error']) && !$token['error']) {
  1072. /* Append a DocumentType node to the Document node, with the name
  1073. attribute set to the name given in the DOCTYPE token (which will be
  1074. "HTML"), and the other attributes specific to DocumentType objects
  1075. set to null, empty lists, or the empty string as appropriate. */
  1076. $doctype = new DOMDocumentType(null, null, 'HTML');
  1077. /* Then, switch to the root element phase of the tree construction
  1078. stage. */
  1079. $this->phase = self::ROOT_PHASE;
  1080. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1081. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1082. or U+0020 SPACE */
  1083. } elseif(isset($token['data']) && preg_match('/^[\t\n\x0b\x0c ]+$/',
  1084. $token['data'])) {
  1085. /* Append that character to the Document node. */
  1086. $text = $this->dom->createTextNode($token['data']);
  1087. $this->dom->appendChild($text);
  1088. }
  1089. }
  1090. private function rootElementPhase($token) {
  1091. /* After the initial phase, as each token is emitted from the tokenisation
  1092. stage, it must be processed as described in this section. */
  1093. /* A DOCTYPE token */
  1094. if($token['type'] === HTML5::DOCTYPE) {
  1095. // Parse error. Ignore the token.
  1096. /* A comment token */
  1097. } elseif($token['type'] === HTML5::COMMENT) {
  1098. /* Append a Comment node to the Document object with the data
  1099. attribute set to the data given in the comment token. */
  1100. $comment = $this->dom->createComment($token['data']);
  1101. $this->dom->appendChild($comment);
  1102. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1103. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1104. or U+0020 SPACE */
  1105. } elseif($token['type'] === HTML5::CHARACTR &&
  1106. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) {
  1107. /* Append that character to the Document node. */
  1108. $text = $this->dom->createTextNode($token['data']);
  1109. $this->dom->appendChild($text);
  1110. /* A character token that is not one of U+0009 CHARACTER TABULATION,
  1111. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED
  1112. (FF), or U+0020 SPACE
  1113. A start tag token
  1114. An end tag token
  1115. An end-of-file token */
  1116. } elseif(($token['type'] === HTML5::CHARACTR &&
  1117. !preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) ||
  1118. $token['type'] === HTML5::STARTTAG ||
  1119. $token['type'] === HTML5::ENDTAG ||
  1120. $token['type'] === HTML5::EOF) {
  1121. /* Create an HTMLElement node with the tag name html, in the HTML
  1122. namespace. Append it to the Document object. Switch to the main
  1123. phase and reprocess the current token. */
  1124. $html = $this->dom->createElement('html');
  1125. $this->dom->appendChild($html);
  1126. $this->stack[] = $html;
  1127. $this->phase = self::MAIN_PHASE;
  1128. return $this->mainPhase($token);
  1129. }
  1130. }
  1131. private function mainPhase($token) {
  1132. /* Tokens in the main phase must be handled as follows: */
  1133. /* A DOCTYPE token */
  1134. if($token['type'] === HTML5::DOCTYPE) {
  1135. // Parse error. Ignore the token.
  1136. /* A start tag token with the tag name "html" */
  1137. } elseif($token['type'] === HTML5::STARTTAG && $token['name'] === 'html') {
  1138. /* If this start tag token was not the first start tag token, then
  1139. it is a parse error. */
  1140. /* For each attribute on the token, check to see if the attribute
  1141. is already present on the top element of the stack of open elements.
  1142. If it is not, add the attribute and its corresponding value to that
  1143. element. */
  1144. foreach($token['attr'] as $attr) {
  1145. if(!$this->stack[0]->hasAttribute($attr['name'])) {
  1146. $this->stack[0]->setAttribute($attr['name'], $attr['value']);
  1147. }
  1148. }
  1149. /* An end-of-file token */
  1150. } elseif($token['type'] === HTML5::EOF) {
  1151. /* Generate implied end tags. */
  1152. $this->generateImpliedEndTags();
  1153. /* Anything else. */
  1154. } else {
  1155. /* Depends on the insertion mode: */
  1156. switch($this->mode) {
  1157. case self::BEFOR_HEAD: return $this->beforeHead($token); break;
  1158. case self::IN_HEAD: return $this->inHead($token); break;
  1159. case self::AFTER_HEAD: return $this->afterHead($token); break;
  1160. case self::IN_BODY: return $this->inBody($token); break;
  1161. case self::IN_TABLE: return $this->inTable($token); break;
  1162. case self::IN_CAPTION: return $this->inCaption($token); break;
  1163. case self::IN_CGROUP: return $this->inColumnGroup($token); break;
  1164. case self::IN_TBODY: return $this->inTableBody($token); break;
  1165. case self::IN_ROW: return $this->inRow($token); break;
  1166. case self::IN_CELL: return $this->inCell($token); break;
  1167. case self::IN_SELECT: return $this->inSelect($token); break;
  1168. case self::AFTER_BODY: return $this->afterBody($token); break;
  1169. case self::IN_FRAME: return $this->inFrameset($token); break;
  1170. case self::AFTR_FRAME: return $this->afterFrameset($token); break;
  1171. case self::END_PHASE: return $this->trailingEndPhase($token); break;
  1172. }
  1173. }
  1174. }
  1175. private function beforeHead($token) {
  1176. /* Handle the token as follows: */
  1177. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1178. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1179. or U+0020 SPACE */
  1180. if($token['type'] === HTML5::CHARACTR &&
  1181. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) {
  1182. /* Append the character to the current node. */
  1183. $this->insertText($token['data']);
  1184. /* A comment token */
  1185. } elseif($token['type'] === HTML5::COMMENT) {
  1186. /* Append a Comment node to the current node with the data attribute
  1187. set to the data given in the comment token. */
  1188. $this->insertComment($token['data']);
  1189. /* A start tag token with the tag name "head" */
  1190. } elseif($token['type'] === HTML5::STARTTAG && $token['name'] === 'head') {
  1191. /* Create an element for the token, append the new element to the
  1192. current node and push it onto the stack of open elements. */
  1193. $element = $this->insertElement($token);
  1194. /* Set the head element pointer to this new element node. */
  1195. $this->head_pointer = $element;
  1196. /* Change the insertion mode to "in head". */
  1197. $this->mode = self::IN_HEAD;
  1198. /* A start tag token whose tag name is one of: "base", "link", "meta",
  1199. "script", "style", "title". Or an end tag with the tag name "html".
  1200. Or a character token that is not one of U+0009 CHARACTER TABULATION,
  1201. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1202. or U+0020 SPACE. Or any other start tag token */
  1203. } elseif($token['type'] === HTML5::STARTTAG ||
  1204. ($token['type'] === HTML5::ENDTAG && $token['name'] === 'html') ||
  1205. ($token['type'] === HTML5::CHARACTR && !preg_match('/^[\t\n\x0b\x0c ]$/',
  1206. $token['data']))) {
  1207. /* Act as if a start tag token with the tag name "head" and no
  1208. attributes had been seen, then reprocess the current token. */
  1209. $this->beforeHead(array(
  1210. 'name' => 'head',
  1211. 'type' => HTML5::STARTTAG,
  1212. 'attr' => array()
  1213. ));
  1214. return $this->inHead($token);
  1215. /* Any other end tag */
  1216. } elseif($token['type'] === HTML5::ENDTAG) {
  1217. /* Parse error. Ignore the token. */
  1218. }
  1219. }
  1220. private function inHead($token) {
  1221. /* Handle the token as follows: */
  1222. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1223. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1224. or U+0020 SPACE.
  1225. THIS DIFFERS FROM THE SPEC: If the current node is either a title, style
  1226. or script element, append the character to the current node regardless
  1227. of its content. */
  1228. if(($token['type'] === HTML5::CHARACTR &&
  1229. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) || (
  1230. $token['type'] === HTML5::CHARACTR && in_array(end($this->stack)->nodeName,
  1231. array('title', 'style', 'script')))) {
  1232. /* Append the character to the current node. */
  1233. $this->insertText($token['data']);
  1234. /* A comment token */
  1235. } elseif($token['type'] === HTML5::COMMENT) {
  1236. /* Append a Comment node to the current node with the data attribute
  1237. set to the data given in the comment token. */
  1238. $this->insertComment($token['data']);
  1239. } elseif($token['type'] === HTML5::ENDTAG &&
  1240. in_array($token['name'], array('title', 'style', 'script'))) {
  1241. array_pop($this->stack);
  1242. return HTML5::PCDATA;
  1243. /* A start tag with the tag name "title" */
  1244. } elseif($token['type'] === HTML5::STARTTAG && $token['name'] === 'title') {
  1245. /* Create an element for the token and append the new element to the
  1246. node pointed to by the head element pointer, or, if that is null
  1247. (innerHTML case), to the current node. */
  1248. if($this->head_pointer !== null) {
  1249. $element = $this->insertElement($token, false);
  1250. $this->head_pointer->appendChild($element);
  1251. } else {
  1252. $element = $this->insertElement($token);
  1253. }
  1254. /* Switch the tokeniser's content model flag to the RCDATA state. */
  1255. return HTML5::RCDATA;
  1256. /* A start tag with the tag name "style" */
  1257. } elseif($token['type'] === HTML5::STARTTAG && $token['name'] === 'style') {
  1258. /* Create an element for the token and append the new element to the
  1259. node pointed to by the head element pointer, or, if that is null
  1260. (innerHTML case), to the current node. */
  1261. if($this->head_pointer !== null) {
  1262. $element = $this->insertElement($token, false);
  1263. $this->head_pointer->appendChild($element);
  1264. } else {
  1265. $this->insertElement($token);
  1266. }
  1267. /* Switch the tokeniser's content model flag to the CDATA state. */
  1268. return HTML5::CDATA;
  1269. /* A start tag with the tag name "script" */
  1270. } elseif($token['type'] === HTML5::STARTTAG && $token['name'] === 'script') {
  1271. /* Create an element for the token. */
  1272. $element = $this->insertElement($token, false);
  1273. $this->head_pointer->appendChild($element);
  1274. /* Switch the tokeniser's content model flag to the CDATA state. */
  1275. return HTML5::CDATA;
  1276. /* A start tag with the tag name "base", "link", or "meta" */
  1277. } elseif($token['type'] === HTML5::STARTTAG && in_array($token['name'],
  1278. array('base', 'link', 'meta'))) {
  1279. /* Create an element for the token and append the new element to the
  1280. node pointed to by the head element pointer, or, if that is null
  1281. (innerHTML case), to the current node. */
  1282. if($this->head_pointer !== null) {
  1283. $element = $this->insertElement($token, false);
  1284. $this->head_pointer->appendChild($element);
  1285. array_pop($this->stack);
  1286. } else {
  1287. $this->insertElement($token);
  1288. }
  1289. /* An end tag with the tag name "head" */
  1290. } elseif($token['type'] === HTML5::ENDTAG && $token['name'] === 'head') {
  1291. /* If the current node is a head element, pop the current node off
  1292. the stack of open elements. */
  1293. if($this->head_pointer->isSameNode(end($this->stack))) {
  1294. array_pop($this->stack);
  1295. /* Otherwise, this is a parse error. */
  1296. } else {
  1297. // k
  1298. }
  1299. /* Change the insertion mode to "after head". */
  1300. $this->mode = self::AFTER_HEAD;
  1301. /* A start tag with the tag name "head" or an end tag except "html". */
  1302. } elseif(($token['type'] === HTML5::STARTTAG && $token['name'] === 'head') ||
  1303. ($token['type'] === HTML5::ENDTAG && $token['name'] !== 'html')) {
  1304. // Parse error. Ignore the token.
  1305. /* Anything else */
  1306. } else {
  1307. /* If the current node is a head element, act as if an end tag
  1308. token with the tag name "head" had been seen. */
  1309. if($this->head_pointer->isSameNode(end($this->stack))) {
  1310. $this->inHead(array(
  1311. 'name' => 'head',
  1312. 'type' => HTML5::ENDTAG
  1313. ));
  1314. /* Otherwise, change the insertion mode to "after head". */
  1315. } else {
  1316. $this->mode = self::AFTER_HEAD;
  1317. }
  1318. /* Then, reprocess the current token. */
  1319. return $this->afterHead($token);
  1320. }
  1321. }
  1322. private function afterHead($token) {
  1323. /* Handle the token as follows: */
  1324. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1325. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1326. or U+0020 SPACE */
  1327. if($token['type'] === HTML5::CHARACTR &&
  1328. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) {
  1329. /* Append the character to the current node. */
  1330. $this->insertText($token['data']);
  1331. /* A comment token */
  1332. } elseif($token['type'] === HTML5::COMMENT) {
  1333. /* Append a Comment node to the current node with the data attribute
  1334. set to the data given in the comment token. */
  1335. $this->insertComment($token['data']);
  1336. /* A start tag token with the tag name "body" */
  1337. } elseif($token['type'] === HTML5::STARTTAG && $token['name'] === 'body') {
  1338. /* Insert a body element for the token. */
  1339. $this->insertElement($token);
  1340. /* Change the insertion mode to "in body". */
  1341. $this->mode = self::IN_BODY;
  1342. /* A start tag token with the tag name "frameset" */
  1343. } elseif($token['type'] === HTML5::STARTTAG && $token['name'] === 'frameset') {
  1344. /* Insert a frameset element for the token. */
  1345. $this->insertElement($token);
  1346. /* Change the insertion mode to "in frameset". */
  1347. $this->mode = self::IN_FRAME;
  1348. /* A start tag token whose tag name is one of: "base", "link", "meta",
  1349. "script", "style", "title" */
  1350. } elseif($token['type'] === HTML5::STARTTAG && in_array($token['name'],
  1351. array('base', 'link', 'meta', 'script', 'style', 'title'))) {
  1352. /* Parse error. Switch the insertion mode back to "in head" and
  1353. reprocess the token. */
  1354. $this->mode = self::IN_HEAD;
  1355. return $this->inHead($token);
  1356. /* Anything else */
  1357. } else {
  1358. /* Act as if a start tag token with the tag name "body" and no
  1359. attributes had been seen, and then reprocess the current token. */
  1360. $this->afterHead(array(
  1361. 'name' => 'body',
  1362. 'type' => HTML5::STARTTAG,
  1363. 'attr' => array()
  1364. ));
  1365. return $this->inBody($token);
  1366. }
  1367. }
  1368. private function inBody($token) {
  1369. /* Handle the token as follows: */
  1370. switch($token['type']) {
  1371. /* A character token */
  1372. case HTML5::CHARACTR:
  1373. /* Reconstruct the active formatting elements, if any. */
  1374. $this->reconstructActiveFormattingElements();
  1375. /* Append the token's character to the current node. */
  1376. $this->insertText($token['data']);
  1377. break;
  1378. /* A comment token */
  1379. case HTML5::COMMENT:
  1380. /* Append a Comment node to the current node with the data
  1381. attribute set to the data given in the comment token. */
  1382. $this->insertComment($token['data']);
  1383. break;
  1384. case HTML5::STARTTAG:
  1385. switch($token['name']) {
  1386. /* A start tag token whose tag name is one of: "script",
  1387. "style" */
  1388. case 'script': case 'style':
  1389. /* Process the token as if the insertion mode had been "in
  1390. head". */
  1391. return $this->inHead($token);
  1392. break;
  1393. /* A start tag token whose tag name is one of: "base", "link",
  1394. "meta", "title" */
  1395. case 'base': case 'link': case 'meta': case 'title':
  1396. /* Parse error. Process the token as if the insertion mode
  1397. had been "in head". */
  1398. return $this->inHead($token);
  1399. break;
  1400. /* A start tag token with the tag name "body" */
  1401. case 'body':
  1402. /* Parse error. If the second element on the stack of open
  1403. elements is not a body element, or, if the stack of open
  1404. elements has only one node on it, then ignore the token.
  1405. (innerHTML case) */
  1406. if(count($this->stack) === 1 || $this->stack[1]->nodeName !== 'body') {
  1407. // Ignore
  1408. /* Otherwise, for each attribute on the token, check to see
  1409. if the attribute is already present on the body element (the
  1410. second element) on the stack of open elements. If it is not,
  1411. add the attribute and its corresponding value to that
  1412. element. */
  1413. } else {
  1414. foreach($token['attr'] as $attr) {
  1415. if(!$this->stack[1]->hasAttribute($attr['name'])) {
  1416. $this->stack[1]->setAttribute($attr['name'], $attr['value']);
  1417. }
  1418. }
  1419. }
  1420. break;
  1421. /* A start tag whose tag name is one of: "address",
  1422. "blockquote", "center", "dir", "div", "dl", "fieldset",
  1423. "listing", "menu", "ol", "p", "ul" */
  1424. case 'address': case 'blockquote': case 'center': case 'dir':
  1425. case 'div': case 'dl': case 'fieldset': case 'listing':
  1426. case 'menu': case 'ol': case 'p': case 'ul':
  1427. /* If the stack of open elements has a p element in scope,
  1428. then act as if an end tag with the tag name p had been
  1429. seen. */
  1430. if($this->elementInScope('p')) {
  1431. $this->emitToken(array(
  1432. 'name' => 'p',
  1433. 'type' => HTML5::ENDTAG
  1434. ));
  1435. }
  1436. /* Insert an HTML element for the token. */
  1437. $this->insertElement($token);
  1438. break;
  1439. /* A start tag whose tag name is "form" */
  1440. case 'form':
  1441. /* If the form element pointer is not null, ignore the
  1442. token with a parse error. */
  1443. if($this->form_pointer !== null) {
  1444. // Ignore.
  1445. /* Otherwise: */
  1446. } else {
  1447. /* If the stack of open elements has a p element in
  1448. scope, then act as if an end tag with the tag name p
  1449. had been seen. */
  1450. if($this->elementInScope('p')) {
  1451. $this->emitToken(array(
  1452. 'name' => 'p',
  1453. 'type' => HTML5::ENDTAG
  1454. ));
  1455. }
  1456. /* Insert an HTML element for the token, and set the
  1457. form element pointer to point to the element created. */
  1458. $element = $this->insertElement($token);
  1459. $this->form_pointer = $element;
  1460. }
  1461. break;
  1462. /* A start tag whose tag name is "li", "dd" or "dt" */
  1463. case 'li': case 'dd': case 'dt':
  1464. /* If the stack of open elements has a p element in scope,
  1465. then act as if an end tag with the tag name p had been
  1466. seen. */
  1467. if($this->elementInScope('p')) {
  1468. $this->emitToken(array(
  1469. 'name' => 'p',
  1470. 'type' => HTML5::ENDTAG
  1471. ));
  1472. }
  1473. $stack_length = count($this->stack) - 1;
  1474. for($n = $stack_length; 0 <= $n; $n--) {
  1475. /* 1. Initialise node to be the current node (the
  1476. bottommost node of the stack). */
  1477. $stop = false;
  1478. $node = $this->stack[$n];
  1479. $cat = $this->getElementCategory($node->tagName);
  1480. /* 2. If node is an li, dd or dt element, then pop all
  1481. the nodes from the current node up to node, including
  1482. node, then stop this algorithm. */
  1483. if($token['name'] === $node->tagName || ($token['name'] !== 'li'
  1484. && ($node->tagName === 'dd' || $node->tagName === 'dt'))) {
  1485. for($x = $stack_length; $x >= $n ; $x--) {
  1486. array_pop($this->stack);
  1487. }
  1488. break;
  1489. }
  1490. /* 3. If node is not in the formatting category, and is
  1491. not in the phrasing category, and is not an address or
  1492. div element, then stop this algorithm. */
  1493. if($cat !== self::FORMATTING && $cat !== self::PHRASING &&
  1494. $node->tagName !== 'address' && $node->tagName !== 'div') {
  1495. break;
  1496. }
  1497. }
  1498. /* Finally, insert an HTML element with the same tag
  1499. name as the token's. */
  1500. $this->insertElement($token);
  1501. break;
  1502. /* A start tag token whose tag name is "plaintext" */
  1503. case 'plaintext':
  1504. /* If the stack of open elements has a p element in scope,
  1505. then act as if an end tag with the tag name p had been
  1506. seen. */
  1507. if($this->elementInScope('p')) {
  1508. $this->emitToken(array(
  1509. 'name' => 'p',
  1510. 'type' => HTML5::ENDTAG
  1511. ));
  1512. }
  1513. /* Insert an HTML element for the token. */
  1514. $this->insertElement($token);
  1515. return HTML5::PLAINTEXT;
  1516. break;
  1517. /* A start tag whose tag name is one of: "h1", "h2", "h3", "h4",
  1518. "h5", "h6" */
  1519. case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6':
  1520. /* If the stack of open elements has a p element in scope,
  1521. then act as if an end tag with the tag name p had been seen. */
  1522. if($this->elementInScope('p')) {
  1523. $this->emitToken(array(
  1524. 'name' => 'p',
  1525. 'type' => HTML5::ENDTAG
  1526. ));
  1527. }
  1528. /* If the stack of open elements has in scope an element whose
  1529. tag name is one of "h1", "h2", "h3", "h4", "h5", or "h6", then
  1530. this is a parse error; pop elements from the stack until an
  1531. element with one of those tag names has been popped from the
  1532. stack. */
  1533. while($this->elementInScope(array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'))) {
  1534. array_pop($this->stack);
  1535. }
  1536. /* Insert an HTML element for the token. */
  1537. $this->insertElement($token);
  1538. break;
  1539. /* A start tag whose tag name is "a" */
  1540. case 'a':
  1541. /* If the list of active formatting elements contains
  1542. an element whose tag name is "a" between the end of the
  1543. list and the last marker on the list (or the start of
  1544. the list if there is no marker on the list), then this
  1545. is a parse error; act as if an end tag with the tag name
  1546. "a" had been seen, then remove that element from the list
  1547. of active formatting elements and the stack of open
  1548. elements if the end tag didn't already remove it (it
  1549. might not have if the element is not in table scope). */
  1550. $leng = count($this->a_formatting);
  1551. for($n = $leng - 1; $n >= 0; $n--) {
  1552. if($this->a_formatting[$n] === self::MARKER) {
  1553. break;
  1554. } elseif($this->a_formatting[$n]->nodeName === 'a') {
  1555. $this->emitToken(array(
  1556. 'name' => 'a',
  1557. 'type' => HTML5::ENDTAG
  1558. ));
  1559. break;
  1560. }
  1561. }
  1562. /* Reconstruct the active formatting elements, if any. */
  1563. $this->reconstructActiveFormattingElements();
  1564. /* Insert an HTML element for the token. */
  1565. $el = $this->insertElement($token);
  1566. /* Add that element to the list of active formatting
  1567. elements. */
  1568. $this->a_formatting[] = $el;
  1569. break;
  1570. /* A start tag whose tag name is one of: "b", "big", "em", "font",
  1571. "i", "nobr", "s", "small", "strike", "strong", "tt", "u" */
  1572. case 'b': case 'big': case 'em': case 'font': case 'i':
  1573. case 'nobr': case 's': case 'small': case 'strike':
  1574. case 'strong': case 'tt': case 'u':
  1575. /* Reconstruct the active formatting elements, if any. */
  1576. $this->reconstructActiveFormattingElements();
  1577. /* Insert an HTML element for the token. */
  1578. $el = $this->insertElement($token);
  1579. /* Add that element to the list of active formatting
  1580. elements. */
  1581. $this->a_formatting[] = $el;
  1582. break;
  1583. /* A start tag token whose tag name is "button" */
  1584. case 'button':
  1585. /* If the stack of open elements has a button element in scope,
  1586. then this is a parse error; act as if an end tag with the tag
  1587. name "button" had been seen, then reprocess the token. (We don't
  1588. do that. Unnecessary.) */
  1589. if($this->elementInScope('button')) {
  1590. $this->inBody(array(
  1591. 'name' => 'button',
  1592. 'type' => HTML5::ENDTAG
  1593. ));
  1594. }
  1595. /* Reconstruct the active formatting elements, if any. */
  1596. $this->reconstructActiveFormattingElements();
  1597. /* Insert an HTML element for the token. */
  1598. $this->insertElement($token);
  1599. /* Insert a marker at the end of the list of active
  1600. formatting elements. */
  1601. $this->a_formatting[] = self::MARKER;
  1602. break;
  1603. /* A start tag token whose tag name is one of: "marquee", "object" */
  1604. case 'marquee': case 'object':
  1605. /* Reconstruct the active formatting elements, if any. */
  1606. $this->reconstructActiveFormattingElements();
  1607. /* Insert an HTML element for the token. */
  1608. $this->insertElement($token);
  1609. /* Insert a marker at the end of the list of active
  1610. formatting elements. */
  1611. $this->a_formatting[] = self::MARKER;
  1612. break;
  1613. /* A start tag token whose tag name is "xmp" */
  1614. case 'xmp':
  1615. /* Reconstruct the active formatting elements, if any. */
  1616. $this->reconstructActiveFormattingElements();
  1617. /* Insert an HTML element for the token. */
  1618. $this->insertElement($token);
  1619. /* Switch the content model flag to the CDATA state. */
  1620. return HTML5::CDATA;
  1621. break;
  1622. /* A start tag whose tag name is "table" */
  1623. case 'table':
  1624. /* If the stack of open elements has a p element in scope,
  1625. then act as if an end tag with the tag name p had been seen. */
  1626. if($this->elementInScope('p')) {
  1627. $this->emitToken(array(
  1628. 'name' => 'p',
  1629. 'type' => HTML5::ENDTAG
  1630. ));
  1631. }
  1632. /* Insert an HTML element for the token. */
  1633. $this->insertElement($token);
  1634. /* Change the insertion mode to "in table". */
  1635. $this->mode = self::IN_TABLE;
  1636. break;
  1637. /* A start tag whose tag name is one of: "area", "basefont",
  1638. "bgsound", "br", "embed", "img", "param", "spacer", "wbr" */
  1639. case 'area': case 'basefont': case 'bgsound': case 'br':
  1640. case 'embed': case 'img': case 'param': case 'spacer':
  1641. case 'wbr':
  1642. /* Reconstruct the active formatting elements, if any. */
  1643. $this->reconstructActiveFormattingElements();
  1644. /* Insert an HTML element for the token. */
  1645. $this->insertElement($token);
  1646. /* Immediately pop the current node off the stack of open elements. */
  1647. array_pop($this->stack);
  1648. break;
  1649. /* A start tag whose tag name is "hr" */
  1650. case 'hr':
  1651. /* If the stack of open elements has a p element in scope,
  1652. then act as if an end tag with the tag name p had been seen. */
  1653. if($this->elementInScope('p')) {
  1654. $this->emitToken(array(
  1655. 'name' => 'p',
  1656. 'type' => HTML5::ENDTAG
  1657. ));
  1658. }
  1659. /* Insert an HTML element for the token. */
  1660. $this->insertElement($token);
  1661. /* Immediately pop the current node off the stack of open elements. */
  1662. array_pop($this->stack);
  1663. break;
  1664. /* A start tag whose tag name is "image" */
  1665. case 'image':
  1666. /* Parse error. Change the token's tag name to "img" and
  1667. reprocess it. (Don't ask.) */
  1668. $token['name'] = 'img';
  1669. return $this->inBody($token);
  1670. break;
  1671. /* A start tag whose tag name is "input" */
  1672. case 'input':
  1673. /* Reconstruct the active formatting elements, if any. */
  1674. $this->reconstructActiveFormattingElements();
  1675. /* Insert an input element for the token. */
  1676. $element = $this->insertElement($token, false);
  1677. /* If the form element pointer is not null, then associate the
  1678. input element with the form element pointed to by the form
  1679. element pointer. */
  1680. $this->form_pointer !== null
  1681. ? $this->form_pointer->appendChild($element)
  1682. : end($this->stack)->appendChild($element);
  1683. /* Pop that input element off the stack of open elements. */
  1684. array_pop($this->stack);
  1685. break;
  1686. /* A start tag whose tag name is "isindex" */
  1687. case 'isindex':
  1688. /* Parse error. */
  1689. // w/e
  1690. /* If the form element pointer is not null,
  1691. then ignore the token. */
  1692. if($this->form_pointer === null) {
  1693. /* Act as if a start tag token with the tag name "form" had
  1694. been seen. */
  1695. $this->inBody(array(
  1696. 'name' => 'body',
  1697. 'type' => HTML5::STARTTAG,
  1698. 'attr' => array()
  1699. ));
  1700. /* Act as if a start tag token with the tag name "hr" had
  1701. been seen. */
  1702. $this->inBody(array(
  1703. 'name' => 'hr',
  1704. 'type' => HTML5::STARTTAG,
  1705. 'attr' => array()
  1706. ));
  1707. /* Act as if a start tag token with the tag name "p" had
  1708. been seen. */
  1709. $this->inBody(array(
  1710. 'name' => 'p',
  1711. 'type' => HTML5::STARTTAG,
  1712. 'attr' => array()
  1713. ));
  1714. /* Act as if a start tag token with the tag name "label"
  1715. had been seen. */
  1716. $this->inBody(array(
  1717. 'name' => 'label',
  1718. 'type' => HTML5::STARTTAG,
  1719. 'attr' => array()
  1720. ));
  1721. /* Act as if a stream of character tokens had been seen. */
  1722. $this->insertText('This is a searchable index. '.
  1723. 'Insert your search keywords here: ');
  1724. /* Act as if a start tag token with the tag name "input"
  1725. had been seen, with all the attributes from the "isindex"
  1726. token, except with the "name" attribute set to the value
  1727. "isindex" (ignoring any explicit "name" attribute). */
  1728. $attr = $token['attr'];
  1729. $attr[] = array('name' => 'name', 'value' => 'isindex');
  1730. $this->inBody(array(
  1731. 'name' => 'input',
  1732. 'type' => HTML5::STARTTAG,
  1733. 'attr' => $attr
  1734. ));
  1735. /* Act as if a stream of character tokens had been seen
  1736. (see below for what they should say). */
  1737. $this->insertText('This is a searchable index. '.
  1738. 'Insert your search keywords here: ');
  1739. /* Act as if an end tag token with the tag name "label"
  1740. had been seen. */
  1741. $this->inBody(array(
  1742. 'name' => 'label',
  1743. 'type' => HTML5::ENDTAG
  1744. ));
  1745. /* Act as if an end tag token with the tag name "p" had
  1746. been seen. */
  1747. $this->inBody(array(
  1748. 'name' => 'p',
  1749. 'type' => HTML5::ENDTAG
  1750. ));
  1751. /* Act as if a start tag token with the tag name "hr" had
  1752. been seen. */
  1753. $this->inBody(array(
  1754. 'name' => 'hr',
  1755. 'type' => HTML5::ENDTAG
  1756. ));
  1757. /* Act as if an end tag token with the tag name "form" had
  1758. been seen. */
  1759. $this->inBody(array(
  1760. 'name' => 'form',
  1761. 'type' => HTML5::ENDTAG
  1762. ));
  1763. }
  1764. break;
  1765. /* A start tag whose tag name is "textarea" */
  1766. case 'textarea':
  1767. $this->insertElement($token);
  1768. /* Switch the tokeniser's content model flag to the
  1769. RCDATA state. */
  1770. return HTML5::RCDATA;
  1771. break;
  1772. /* A start tag whose tag name is one of: "iframe", "noembed",
  1773. "noframes" */
  1774. case 'iframe': case 'noembed': case 'noframes':
  1775. $this->insertElement($token);
  1776. /* Switch the tokeniser's content model flag to the CDATA state. */
  1777. return HTML5::CDATA;
  1778. break;
  1779. /* A start tag whose tag name is "select" */
  1780. case 'select':
  1781. /* Reconstruct the active formatting elements, if any. */
  1782. $this->reconstructActiveFormattingElements();
  1783. /* Insert an HTML element for the token. */
  1784. $this->insertElement($token);
  1785. /* Change the insertion mode to "in select". */
  1786. $this->mode = self::IN_SELECT;
  1787. break;
  1788. /* A start or end tag whose tag name is one of: "caption", "col",
  1789. "colgroup", "frame", "frameset", "head", "option", "optgroup",
  1790. "tbody", "td", "tfoot", "th", "thead", "tr". */
  1791. case 'caption': case 'col': case 'colgroup': case 'frame':
  1792. case 'frameset': case 'head': case 'option': case 'optgroup':
  1793. case 'tbody': case 'td': case 'tfoot': case 'th': case 'thead':
  1794. case 'tr':
  1795. // Parse error. Ignore the token.
  1796. break;
  1797. /* A start or end tag whose tag name is one of: "event-source",
  1798. "section", "nav", "article", "aside", "header", "footer",
  1799. "datagrid", "command" */
  1800. case 'event-source': case 'section': case 'nav': case 'article':
  1801. case 'aside': case 'header': case 'footer': case 'datagrid':
  1802. case 'command':
  1803. // Work in progress!
  1804. break;
  1805. /* A start tag token not covered by the previous entries */
  1806. default:
  1807. /* Reconstruct the active formatting elements, if any. */
  1808. $this->reconstructActiveFormattingElements();
  1809. $this->insertElement($token, true, true);
  1810. break;
  1811. }
  1812. break;
  1813. case HTML5::ENDTAG:
  1814. switch($token['name']) {
  1815. /* An end tag with the tag name "body" */
  1816. case 'body':
  1817. /* If the second element in the stack of open elements is
  1818. not a body element, this is a parse error. Ignore the token.
  1819. (innerHTML case) */
  1820. if(count($this->stack) < 2 || $this->stack[1]->nodeName !== 'body') {
  1821. // Ignore.
  1822. /* If the current node is not the body element, then this
  1823. is a parse error. */
  1824. } elseif(end($this->stack)->nodeName !== 'body') {
  1825. // Parse error.
  1826. }
  1827. /* Change the insertion mode to "after body". */
  1828. $this->mode = self::AFTER_BODY;
  1829. break;
  1830. /* An end tag with the tag name "html" */
  1831. case 'html':
  1832. /* Act as if an end tag with tag name "body" had been seen,
  1833. then, if that token wasn't ignored, reprocess the current
  1834. token. */
  1835. $this->inBody(array(
  1836. 'name' => 'body',
  1837. 'type' => HTML5::ENDTAG
  1838. ));
  1839. return $this->afterBody($token);
  1840. break;
  1841. /* An end tag whose tag name is one of: "address", "blockquote",
  1842. "center", "dir", "div", "dl", "fieldset", "listing", "menu",
  1843. "ol", "pre", "ul" */
  1844. case 'address': case 'blockquote': case 'center': case 'dir':
  1845. case 'div': case 'dl': case 'fieldset': case 'listing':
  1846. case 'menu': case 'ol': case 'pre': case 'ul':
  1847. /* If the stack of open elements has an element in scope
  1848. with the same tag name as that of the token, then generate
  1849. implied end tags. */
  1850. if($this->elementInScope($token['name'])) {
  1851. $this->generateImpliedEndTags();
  1852. /* Now, if the current node is not an element with
  1853. the same tag name as that of the token, then this
  1854. is a parse error. */
  1855. // w/e
  1856. /* If the stack of open elements has an element in
  1857. scope with the same tag name as that of the token,
  1858. then pop elements from this stack until an element
  1859. with that tag name has been popped from the stack. */
  1860. for($n = count($this->stack) - 1; $n >= 0; $n--) {
  1861. if($this->stack[$n]->nodeName === $token['name']) {
  1862. $n = -1;
  1863. }
  1864. array_pop($this->stack);
  1865. }
  1866. }
  1867. break;
  1868. /* An end tag whose tag name is "form" */
  1869. case 'form':
  1870. /* If the stack of open elements has an element in scope
  1871. with the same tag name as that of the token, then generate
  1872. implied end tags. */
  1873. if($this->elementInScope($token['name'])) {
  1874. $this->generateImpliedEndTags();
  1875. }
  1876. if(end($this->stack)->nodeName !== $token['name']) {
  1877. /* Now, if the current node is not an element with the
  1878. same tag name as that of the token, then this is a parse
  1879. error. */
  1880. // w/e
  1881. } else {
  1882. /* Otherwise, if the current node is an element with
  1883. the same tag name as that of the token pop that element
  1884. from the stack. */
  1885. array_pop($this->stack);
  1886. }
  1887. /* In any case, set the form element pointer to null. */
  1888. $this->form_pointer = null;
  1889. break;
  1890. /* An end tag whose tag name is "p" */
  1891. case 'p':
  1892. /* If the stack of open elements has a p element in scope,
  1893. then generate implied end tags, except for p elements. */
  1894. if($this->elementInScope('p')) {
  1895. $this->generateImpliedEndTags(array('p'));
  1896. /* If the current node is not a p element, then this is
  1897. a parse error. */
  1898. // k
  1899. /* If the stack of open elements has a p element in
  1900. scope, then pop elements from this stack until the stack
  1901. no longer has a p element in scope. */
  1902. for($n = count($this->stack) - 1; $n >= 0; $n--) {
  1903. if($this->elementInScope('p')) {
  1904. array_pop($this->stack);
  1905. } else {
  1906. break;
  1907. }
  1908. }
  1909. }
  1910. break;
  1911. /* An end tag whose tag name is "dd", "dt", or "li" */
  1912. case 'dd': case 'dt': case 'li':
  1913. /* If the stack of open elements has an element in scope
  1914. whose tag name matches the tag name of the token, then
  1915. generate implied end tags, except for elements with the
  1916. same tag name as the token. */
  1917. if($this->elementInScope($token['name'])) {
  1918. $this->generateImpliedEndTags(array($token['name']));
  1919. /* If the current node is not an element with the same
  1920. tag name as the token, then this is a parse error. */
  1921. // w/e
  1922. /* If the stack of open elements has an element in scope
  1923. whose tag name matches the tag name of the token, then
  1924. pop elements from this stack until an element with that
  1925. tag name has been popped from the stack. */
  1926. for($n = count($this->stack) - 1; $n >= 0; $n--) {
  1927. if($this->stack[$n]->nodeName === $token['name']) {
  1928. $n = -1;
  1929. }
  1930. array_pop($this->stack);
  1931. }
  1932. }
  1933. break;
  1934. /* An end tag whose tag name is one of: "h1", "h2", "h3", "h4",
  1935. "h5", "h6" */
  1936. case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6':
  1937. $elements = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6');
  1938. /* If the stack of open elements has in scope an element whose
  1939. tag name is one of "h1", "h2", "h3", "h4", "h5", or "h6", then
  1940. generate implied end tags. */
  1941. if($this->elementInScope($elements)) {
  1942. $this->generateImpliedEndTags();
  1943. /* Now, if the current node is not an element with the same
  1944. tag name as that of the token, then this is a parse error. */
  1945. // w/e
  1946. /* If the stack of open elements has in scope an element
  1947. whose tag name is one of "h1", "h2", "h3", "h4", "h5", or
  1948. "h6", then pop elements from the stack until an element
  1949. with one of those tag names has been popped from the stack. */
  1950. while($this->elementInScope($elements)) {
  1951. array_pop($this->stack);
  1952. }
  1953. }
  1954. break;
  1955. /* An end tag whose tag name is one of: "a", "b", "big", "em",
  1956. "font", "i", "nobr", "s", "small", "strike", "strong", "tt", "u" */
  1957. case 'a': case 'b': case 'big': case 'em': case 'font':
  1958. case 'i': case 'nobr': case 's': case 'small': case 'strike':
  1959. case 'strong': case 'tt': case 'u':
  1960. /* 1. Let the formatting element be the last element in
  1961. the list of active formatting elements that:
  1962. * is between the end of the list and the last scope
  1963. marker in the list, if any, or the start of the list
  1964. otherwise, and
  1965. * has the same tag name as the token.
  1966. */
  1967. while(true) {
  1968. for($a = count($this->a_formatting) - 1; $a >= 0; $a--) {
  1969. if($this->a_formatting[$a] === self::MARKER) {
  1970. break;
  1971. } elseif($this->a_formatting[$a]->tagName === $token['name']) {
  1972. $formatting_element = $this->a_formatting[$a];
  1973. $in_stack = in_array($formatting_element, $this->stack, true);
  1974. $fe_af_pos = $a;
  1975. break;
  1976. }
  1977. }
  1978. /* If there is no such node, or, if that node is
  1979. also in the stack of open elements but the element
  1980. is not in scope, then this is a parse error. Abort
  1981. these steps. The token is ignored. */
  1982. if(!isset($formatting_element) || ($in_stack &&
  1983. !$this->elementInScope($token['name']))) {
  1984. break;
  1985. /* Otherwise, if there is such a node, but that node
  1986. is not in the stack of open elements, then this is a
  1987. parse error; remove the element from the list, and
  1988. abort these steps. */
  1989. } elseif(isset($formatting_element) && !$in_stack) {
  1990. unset($this->a_formatting[$fe_af_pos]);
  1991. $this->a_formatting = array_merge($this->a_formatting);
  1992. break;
  1993. }
  1994. /* 2. Let the furthest block be the topmost node in the
  1995. stack of open elements that is lower in the stack
  1996. than the formatting element, and is not an element in
  1997. the phrasing or formatting categories. There might
  1998. not be one. */
  1999. $fe_s_pos = array_search($formatting_element, $this->stack, true);
  2000. $length = count($this->stack);
  2001. for($s = $fe_s_pos + 1; $s < $length; $s++) {
  2002. $category = $this->getElementCategory($this->stack[$s]->nodeName);
  2003. if($category !== self::PHRASING && $category !== self::FORMATTING) {
  2004. $furthest_block = $this->stack[$s];
  2005. }
  2006. }
  2007. /* 3. If there is no furthest block, then the UA must
  2008. skip the subsequent steps and instead just pop all
  2009. the nodes from the bottom of the stack of open
  2010. elements, from the current node up to the formatting
  2011. element, and remove the formatting element from the
  2012. list of active formatting elements. */
  2013. if(!isset($furthest_block)) {
  2014. for($n = $length - 1; $n >= $fe_s_pos; $n--) {
  2015. array_pop($this->stack);
  2016. }
  2017. unset($this->a_formatting[$fe_af_pos]);
  2018. $this->a_formatting = array_merge($this->a_formatting);
  2019. break;
  2020. }
  2021. /* 4. Let the common ancestor be the element
  2022. immediately above the formatting element in the stack
  2023. of open elements. */
  2024. $common_ancestor = $this->stack[$fe_s_pos - 1];
  2025. /* 5. If the furthest block has a parent node, then
  2026. remove the furthest block from its parent node. */
  2027. if($furthest_block->parentNode !== null) {
  2028. $furthest_block->parentNode->removeChild($furthest_block);
  2029. }
  2030. /* 6. Let a bookmark note the position of the
  2031. formatting element in the list of active formatting
  2032. elements relative to the elements on either side
  2033. of it in the list. */
  2034. $bookmark = $fe_af_pos;
  2035. /* 7. Let node and last node be the furthest block.
  2036. Follow these steps: */
  2037. $node = $furthest_block;
  2038. $last_node = $furthest_block;
  2039. while(true) {
  2040. for($n = array_search($node, $this->stack, true) - 1; $n >= 0; $n--) {
  2041. /* 7.1 Let node be the element immediately
  2042. prior to node in the stack of open elements. */
  2043. $node = $this->stack[$n];
  2044. /* 7.2 If node is not in the list of active
  2045. formatting elements, then remove node from
  2046. the stack of open elements and then go back
  2047. to step 1. */
  2048. if(!in_array($node, $this->a_formatting, true)) {
  2049. unset($this->stack[$n]);
  2050. $this->stack = array_merge($this->stack);
  2051. } else {
  2052. break;
  2053. }
  2054. }
  2055. /* 7.3 Otherwise, if node is the formatting
  2056. element, then go to the next step in the overall
  2057. algorithm. */
  2058. if($node === $formatting_element) {
  2059. break;
  2060. /* 7.4 Otherwise, if last node is the furthest
  2061. block, then move the aforementioned bookmark to
  2062. be immediately after the node in the list of
  2063. active formatting elements. */
  2064. } elseif($last_node === $furthest_block) {
  2065. $bookmark = array_search($node, $this->a_formatting, true) + 1;
  2066. }
  2067. /* 7.5 If node has any children, perform a
  2068. shallow clone of node, replace the entry for
  2069. node in the list of active formatting elements
  2070. with an entry for the clone, replace the entry
  2071. for node in the stack of open elements with an
  2072. entry for the clone, and let node be the clone. */
  2073. if($node->hasChildNodes()) {
  2074. $clone = $node->cloneNode();
  2075. $s_pos = array_search($node, $this->stack, true);
  2076. $a_pos = array_search($node, $this->a_formatting, true);
  2077. $this->stack[$s_pos] = $clone;
  2078. $this->a_formatting[$a_pos] = $clone;
  2079. $node = $clone;
  2080. }
  2081. /* 7.6 Insert last node into node, first removing
  2082. it from its previous parent node if any. */
  2083. if($last_node->parentNode !== null) {
  2084. $last_node->parentNode->removeChild($last_node);
  2085. }
  2086. $node->appendChild($last_node);
  2087. /* 7.7 Let last node be node. */
  2088. $last_node = $node;
  2089. }
  2090. /* 8. Insert whatever last node ended up being in
  2091. the previous step into the common ancestor node,
  2092. first removing it from its previous parent node if
  2093. any. */
  2094. if($last_node->parentNode !== null) {
  2095. $last_node->parentNode->removeChild($last_node);
  2096. }
  2097. $common_ancestor->appendChild($last_node);
  2098. /* 9. Perform a shallow clone of the formatting
  2099. element. */
  2100. $clone = $formatting_element->cloneNode();
  2101. /* 10. Take all of the child nodes of the furthest
  2102. block and append them to the clone created in the
  2103. last step. */
  2104. while($furthest_block->hasChildNodes()) {
  2105. $child = $furthest_block->firstChild;
  2106. $furthest_block->removeChild($child);
  2107. $clone->appendChild($child);
  2108. }
  2109. /* 11. Append that clone to the furthest block. */
  2110. $furthest_block->appendChild($clone);
  2111. /* 12. Remove the formatting element from the list
  2112. of active formatting elements, and insert the clone
  2113. into the list of active formatting elements at the
  2114. position of the aforementioned bookmark. */
  2115. $fe_af_pos = array_search($formatting_element, $this->a_formatting, true);
  2116. unset($this->a_formatting[$fe_af_pos]);
  2117. $this->a_formatting = array_merge($this->a_formatting);
  2118. $af_part1 = array_slice($this->a_formatting, 0, $bookmark - 1);
  2119. $af_part2 = array_slice($this->a_formatting, $bookmark, count($this->a_formatting));
  2120. $this->a_formatting = array_merge($af_part1, array($clone), $af_part2);
  2121. /* 13. Remove the formatting element from the stack
  2122. of open elements, and insert the clone into the stack
  2123. of open elements immediately after (i.e. in a more
  2124. deeply nested position than) the position of the
  2125. furthest block in that stack. */
  2126. $fe_s_pos = array_search($formatting_element, $this->stack, true);
  2127. $fb_s_pos = array_search($furthest_block, $this->stack, true);
  2128. unset($this->stack[$fe_s_pos]);
  2129. $s_part1 = array_slice($this->stack, 0, $fb_s_pos);
  2130. $s_part2 = array_slice($this->stack, $fb_s_pos + 1, count($this->stack));
  2131. $this->stack = array_merge($s_part1, array($clone), $s_part2);
  2132. /* 14. Jump back to step 1 in this series of steps. */
  2133. unset($formatting_element, $fe_af_pos, $fe_s_pos, $furthest_block);
  2134. }
  2135. break;
  2136. /* An end tag token whose tag name is one of: "button",
  2137. "marquee", "object" */
  2138. case 'button': case 'marquee': case 'object':
  2139. /* If the stack of open elements has an element in scope whose
  2140. tag name matches the tag name of the token, then generate implied
  2141. tags. */
  2142. if($this->elementInScope($token['name'])) {
  2143. $this->generateImpliedEndTags();
  2144. /* Now, if the current node is not an element with the same
  2145. tag name as the token, then this is a parse error. */
  2146. // k
  2147. /* Now, if the stack of open elements has an element in scope
  2148. whose tag name matches the tag name of the token, then pop
  2149. elements from the stack until that element has been popped from
  2150. the stack, and clear the list of active formatting elements up
  2151. to the last marker. */
  2152. for($n = count($this->stack) - 1; $n >= 0; $n--) {
  2153. if($this->stack[$n]->nodeName === $token['name']) {
  2154. $n = -1;
  2155. }
  2156. array_pop($this->stack);
  2157. }
  2158. $marker = end(array_keys($this->a_formatting, self::MARKER, true));
  2159. for($n = count($this->a_formatting) - 1; $n > $marker; $n--) {
  2160. array_pop($this->a_formatting);
  2161. }
  2162. }
  2163. break;
  2164. /* Or an end tag whose tag name is one of: "area", "basefont",
  2165. "bgsound", "br", "embed", "hr", "iframe", "image", "img",
  2166. "input", "isindex", "noembed", "noframes", "param", "select",
  2167. "spacer", "table", "textarea", "wbr" */
  2168. case 'area': case 'basefont': case 'bgsound': case 'br':
  2169. case 'embed': case 'hr': case 'iframe': case 'image':
  2170. case 'img': case 'input': case 'isindex': case 'noembed':
  2171. case 'noframes': case 'param': case 'select': case 'spacer':
  2172. case 'table': case 'textarea': case 'wbr':
  2173. // Parse error. Ignore the token.
  2174. break;
  2175. /* An end tag token not covered by the previous entries */
  2176. default:
  2177. for($n = count($this->stack) - 1; $n >= 0; $n--) {
  2178. /* Initialise node to be the current node (the bottommost
  2179. node of the stack). */
  2180. $node = end($this->stack);
  2181. /* If node has the same tag name as the end tag token,
  2182. then: */
  2183. if($token['name'] === $node->nodeName) {
  2184. /* Generate implied end tags. */
  2185. $this->generateImpliedEndTags();
  2186. /* If the tag name of the end tag token does not
  2187. match the tag name of the current node, this is a
  2188. parse error. */
  2189. // k
  2190. /* Pop all the nodes from the current node up to
  2191. node, including node, then stop this algorithm. */
  2192. for($x = count($this->stack) - $n; $x >= $n; $x--) {
  2193. array_pop($this->stack);
  2194. }
  2195. } else {
  2196. $category = $this->getElementCategory($node);
  2197. if($category !== self::SPECIAL && $category !== self::SCOPING) {
  2198. /* Otherwise, if node is in neither the formatting
  2199. category nor the phrasing category, then this is a
  2200. parse error. Stop this algorithm. The end tag token
  2201. is ignored. */
  2202. return false;
  2203. }
  2204. }
  2205. }
  2206. break;
  2207. }
  2208. break;
  2209. }
  2210. }
  2211. private function inTable($token) {
  2212. $clear = array('html', 'table');
  2213. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  2214. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  2215. or U+0020 SPACE */
  2216. if($token['type'] === HTML5::CHARACTR &&
  2217. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) {
  2218. /* Append the character to the current node. */
  2219. $text = $this->dom->createTextNode($token['data']);
  2220. end($this->stack)->appendChild($text);
  2221. /* A comment token */
  2222. } elseif($token['type'] === HTML5::COMMENT) {
  2223. /* Append a Comment node to the current node with the data
  2224. attribute set to the data given in the comment token. */
  2225. $comment = $this->dom->createComment($token['data']);
  2226. end($this->stack)->appendChild($comment);
  2227. /* A start tag whose tag name is "caption" */
  2228. } elseif($token['type'] === HTML5::STARTTAG &&
  2229. $token['name'] === 'caption') {
  2230. /* Clear the stack back to a table context. */
  2231. $this->clearStackToTableContext($clear);
  2232. /* Insert a marker at the end of the list of active
  2233. formatting elements. */
  2234. $this->a_formatting[] = self::MARKER;
  2235. /* Insert an HTML element for the token, then switch the
  2236. insertion mode to "in caption". */
  2237. $this->insertElement($token);
  2238. $this->mode = self::IN_CAPTION;
  2239. /* A start tag whose tag name is "colgroup" */
  2240. } elseif($token['type'] === HTML5::STARTTAG &&
  2241. $token['name'] === 'colgroup') {
  2242. /* Clear the stack back to a table context. */
  2243. $this->clearStackToTableContext($clear);
  2244. /* Insert an HTML element for the token, then switch the
  2245. insertion mode to "in column group". */
  2246. $this->insertElement($token);
  2247. $this->mode = self::IN_CGROUP;
  2248. /* A start tag whose tag name is "col" */
  2249. } elseif($token['type'] === HTML5::STARTTAG &&
  2250. $token['name'] === 'col') {
  2251. $this->inTable(array(
  2252. 'name' => 'colgroup',
  2253. 'type' => HTML5::STARTTAG,
  2254. 'attr' => array()
  2255. ));
  2256. $this->inColumnGroup($token);
  2257. /* A start tag whose tag name is one of: "tbody", "tfoot", "thead" */
  2258. } elseif($token['type'] === HTML5::STARTTAG && in_array($token['name'],
  2259. array('tbody', 'tfoot', 'thead'))) {
  2260. /* Clear the stack back to a table context. */
  2261. $this->clearStackToTableContext($clear);
  2262. /* Insert an HTML element for the token, then switch the insertion
  2263. mode to "in table body". */
  2264. $this->insertElement($token);
  2265. $this->mode = self::IN_TBODY;
  2266. /* A start tag whose tag name is one of: "td", "th", "tr" */
  2267. } elseif($token['type'] === HTML5::STARTTAG &&
  2268. in_array($token['name'], array('td', 'th', 'tr'))) {
  2269. /* Act as if a start tag token with the tag name "tbody" had been
  2270. seen, then reprocess the current token. */
  2271. $this->inTable(array(
  2272. 'name' => 'tbody',
  2273. 'type' => HTML5::STARTTAG,
  2274. 'attr' => array()
  2275. ));
  2276. return $this->inTableBody($token);
  2277. /* A start tag whose tag name is "table" */
  2278. } elseif($token['type'] === HTML5::STARTTAG &&
  2279. $token['name'] === 'table') {
  2280. /* Parse error. Act as if an end tag token with the tag name "table"
  2281. had been seen, then, if that token wasn't ignored, reprocess the
  2282. current token. */
  2283. $this->inTable(array(
  2284. 'name' => 'table',
  2285. 'type' => HTML5::ENDTAG
  2286. ));
  2287. return $this->mainPhase($token);
  2288. /* An end tag whose tag name is "table" */
  2289. } elseif($token['type'] === HTML5::ENDTAG &&
  2290. $token['name'] === 'table') {
  2291. /* If the stack of open elements does not have an element in table
  2292. scope with the same tag name as the token, this is a parse error.
  2293. Ignore the token. (innerHTML case) */
  2294. if(!$this->elementInScope($token['name'], true)) {
  2295. return false;
  2296. /* Otherwise: */
  2297. } else {
  2298. /* Generate implied end tags. */
  2299. $this->generateImpliedEndTags();
  2300. /* Now, if the current node is not a table element, then this
  2301. is a parse error. */
  2302. // w/e
  2303. /* Pop elements from this stack until a table element has been
  2304. popped from the stack. */
  2305. while(true) {
  2306. $current = end($this->stack)->nodeName;
  2307. array_pop($this->stack);
  2308. if($current === 'table') {
  2309. break;
  2310. }
  2311. }
  2312. /* Reset the insertion mode appropriately. */
  2313. $this->resetInsertionMode();
  2314. }
  2315. /* An end tag whose tag name is one of: "body", "caption", "col",
  2316. "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr" */
  2317. } elseif($token['type'] === HTML5::ENDTAG && in_array($token['name'],
  2318. array('body', 'caption', 'col', 'colgroup', 'html', 'tbody', 'td',
  2319. 'tfoot', 'th', 'thead', 'tr'))) {
  2320. // Parse error. Ignore the token.
  2321. /* Anything else */
  2322. } else {
  2323. /* Parse error. Process the token as if the insertion mode was "in
  2324. body", with the following exception: */
  2325. /* If the current node is a table, tbody, tfoot, thead, or tr
  2326. element, then, whenever a node would be inserted into the current
  2327. node, it must instead be inserted into the foster parent element. */
  2328. if(in_array(end($this->stack)->nodeName,
  2329. array('table', 'tbody', 'tfoot', 'thead', 'tr'))) {
  2330. /* The foster parent element is the parent element of the last
  2331. table element in the stack of open elements, if there is a
  2332. table element and it has such a parent element. If there is no
  2333. table element in the stack of open elements (innerHTML case),
  2334. then the foster parent element is the first element in the
  2335. stack of open elements (the html element). Otherwise, if there
  2336. is a table element in the stack of open elements, but the last
  2337. table element in the stack of open elements has no parent, or
  2338. its parent node is not an element, then the foster parent
  2339. element is the element before the last table element in the
  2340. stack of open elements. */
  2341. for($n = count($this->stack) - 1; $n >= 0; $n--) {
  2342. if($this->stack[$n]->nodeName === 'table') {
  2343. $table = $this->stack[$n];
  2344. break;
  2345. }
  2346. }
  2347. if(isset($table) && $table->parentNode !== null) {
  2348. $this->foster_parent = $table->parentNode;
  2349. } elseif(!isset($table)) {
  2350. $this->foster_parent = $this->stack[0];
  2351. } elseif(isset($table) && ($table->parentNode === null ||
  2352. $table->parentNode->nodeType !== XML_ELEMENT_NODE)) {
  2353. $this->foster_parent = $this->stack[$n - 1];
  2354. }
  2355. }
  2356. $this->inBody($token);
  2357. }
  2358. }
  2359. private function inCaption($token) {
  2360. /* An end tag whose tag name is "caption" */
  2361. if($token['type'] === HTML5::ENDTAG && $token['name'] === 'caption') {
  2362. /* If the stack of open elements does not have an element in table
  2363. scope with the same tag name as the token, this is a parse error.
  2364. Ignore the token. (innerHTML case) */
  2365. if(!$this->elementInScope($token['name'], true)) {
  2366. // Ignore
  2367. /* Otherwise: */
  2368. } else {
  2369. /* Generate implied end tags. */
  2370. $this->generateImpliedEndTags();
  2371. /* Now, if the current node is not a caption element, then this
  2372. is a parse error. */
  2373. // w/e
  2374. /* Pop elements from this stack until a caption element has
  2375. been popped from the stack. */
  2376. while(true) {
  2377. $node = end($this->stack)->nodeName;
  2378. array_pop($this->stack);
  2379. if($node === 'caption') {
  2380. break;
  2381. }
  2382. }
  2383. /* Clear the list of active formatting elements up to the last
  2384. marker. */
  2385. $this->clearTheActiveFormattingElementsUpToTheLastMarker();
  2386. /* Switch the insertion mode to "in table". */
  2387. $this->mode = self::IN_TABLE;
  2388. }
  2389. /* A start tag whose tag name is one of: "caption", "col", "colgroup",
  2390. "tbody", "td", "tfoot", "th", "thead", "tr", or an end tag whose tag
  2391. name is "table" */
  2392. } elseif(($token['type'] === HTML5::STARTTAG && in_array($token['name'],
  2393. array('caption', 'col', 'colgroup', 'tbody', 'td', 'tfoot', 'th',
  2394. 'thead', 'tr'))) || ($token['type'] === HTML5::ENDTAG &&
  2395. $token['name'] === 'table')) {
  2396. /* Parse error. Act as if an end tag with the tag name "caption"
  2397. had been seen, then, if that token wasn't ignored, reprocess the
  2398. current token. */
  2399. $this->inCaption(array(
  2400. 'name' => 'caption',
  2401. 'type' => HTML5::ENDTAG
  2402. ));
  2403. return $this->inTable($token);
  2404. /* An end tag whose tag name is one of: "body", "col", "colgroup",
  2405. "html", "tbody", "td", "tfoot", "th", "thead", "tr" */
  2406. } elseif($token['type'] === HTML5::ENDTAG && in_array($token['name'],
  2407. array('body', 'col', 'colgroup', 'html', 'tbody', 'tfoot', 'th',
  2408. 'thead', 'tr'))) {
  2409. // Parse error. Ignore the token.
  2410. /* Anything else */
  2411. } else {
  2412. /* Process the token as if the insertion mode was "in body". */
  2413. $this->inBody($token);
  2414. }
  2415. }
  2416. private function inColumnGroup($token) {
  2417. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  2418. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  2419. or U+0020 SPACE */
  2420. if($token['type'] === HTML5::CHARACTR &&
  2421. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) {
  2422. /* Append the character to the current node. */
  2423. $text = $this->dom->createTextNode($token['data']);
  2424. end($this->stack)->appendChild($text);
  2425. /* A comment token */
  2426. } elseif($token['type'] === HTML5::COMMENT) {
  2427. /* Append a Comment node to the current node with the data
  2428. attribute set to the data given in the comment token. */
  2429. $comment = $this->dom->createComment($token['data']);
  2430. end($this->stack)->appendChild($comment);
  2431. /* A start tag whose tag name is "col" */
  2432. } elseif($token['type'] === HTML5::STARTTAG && $token['name'] === 'col') {
  2433. /* Insert a col element for the token. Immediately pop the current
  2434. node off the stack of open elements. */
  2435. $this->insertElement($token);
  2436. array_pop($this->stack);
  2437. /* An end tag whose tag name is "colgroup" */
  2438. } elseif($token['type'] === HTML5::ENDTAG &&
  2439. $token['name'] === 'colgroup') {
  2440. /* If the current node is the root html element, then this is a
  2441. parse error, ignore the token. (innerHTML case) */
  2442. if(end($this->stack)->nodeName === 'html') {
  2443. // Ignore
  2444. /* Otherwise, pop the current node (which will be a colgroup
  2445. element) from the stack of open elements. Switch the insertion
  2446. mode to "in table". */
  2447. } else {
  2448. array_pop($this->stack);
  2449. $this->mode = self::IN_TABLE;
  2450. }
  2451. /* An end tag whose tag name is "col" */
  2452. } elseif($token['type'] === HTML5::ENDTAG && $token['name'] === 'col') {
  2453. /* Parse error. Ignore the token. */
  2454. /* Anything else */
  2455. } else {
  2456. /* Act as if an end tag with the tag name "colgroup" had been seen,
  2457. and then, if that token wasn't ignored, reprocess the current token. */
  2458. $this->inColumnGroup(array(
  2459. 'name' => 'colgroup',
  2460. 'type' => HTML5::ENDTAG
  2461. ));
  2462. return $this->inTable($token);
  2463. }
  2464. }
  2465. private function inTableBody($token) {
  2466. $clear = array('tbody', 'tfoot', 'thead', 'html');
  2467. /* A start tag whose tag name is "tr" */
  2468. if($token['type'] === HTML5::STARTTAG && $token['name'] === 'tr') {
  2469. /* Clear the stack back to a table body context. */
  2470. $this->clearStackToTableContext($clear);
  2471. /* Insert a tr element for the token, then switch the insertion
  2472. mode to "in row". */
  2473. $this->insertElement($token);
  2474. $this->mode = self::IN_ROW;
  2475. /* A start tag whose tag name is one of: "th", "td" */
  2476. } elseif($token['type'] === HTML5::STARTTAG &&
  2477. ($token['name'] === 'th' || $token['name'] === 'td')) {
  2478. /* Parse error. Act as if a start tag with the tag name "tr" had
  2479. been seen, then reprocess the current token. */
  2480. $this->inTableBody(array(
  2481. 'name' => 'tr',
  2482. 'type' => HTML5::STARTTAG,
  2483. 'attr' => array()
  2484. ));
  2485. return $this->inRow($token);
  2486. /* An end tag whose tag name is one of: "tbody", "tfoot", "thead" */
  2487. } elseif($token['type'] === HTML5::ENDTAG &&
  2488. in_array($token['name'], array('tbody', 'tfoot', 'thead'))) {
  2489. /* If the stack of open elements does not have an element in table
  2490. scope with the same tag name as the token, this is a parse error.
  2491. Ignore the token. */
  2492. if(!$this->elementInScope($token['name'], true)) {
  2493. // Ignore
  2494. /* Otherwise: */
  2495. } else {
  2496. /* Clear the stack back to a table body context. */
  2497. $this->clearStackToTableContext($clear);
  2498. /* Pop the current node from the stack of open elements. Switch
  2499. the insertion mode to "in table". */
  2500. array_pop($this->stack);
  2501. $this->mode = self::IN_TABLE;
  2502. }
  2503. /* A start tag whose tag name is one of: "caption", "col", "colgroup",
  2504. "tbody", "tfoot", "thead", or an end tag whose tag name is "table" */
  2505. } elseif(($token['type'] === HTML5::STARTTAG && in_array($token['name'],
  2506. array('caption', 'col', 'colgroup', 'tbody', 'tfoor', 'thead'))) ||
  2507. ($token['type'] === HTML5::STARTTAG && $token['name'] === 'table')) {
  2508. /* If the stack of open elements does not have a tbody, thead, or
  2509. tfoot element in table scope, this is a parse error. Ignore the
  2510. token. (innerHTML case) */
  2511. if(!$this->elementInScope(array('tbody', 'thead', 'tfoot'), true)) {
  2512. // Ignore.
  2513. /* Otherwise: */
  2514. } else {
  2515. /* Clear the stack back to a table body context. */
  2516. $this->clearStackToTableContext($clear);
  2517. /* Act as if an end tag with the same tag name as the current
  2518. node ("tbody", "tfoot", or "thead") had been seen, then
  2519. reprocess the current token. */
  2520. $this->inTableBody(array(
  2521. 'name' => end($this->stack)->nodeName,
  2522. 'type' => HTML5::ENDTAG
  2523. ));
  2524. return $this->mainPhase($token);
  2525. }
  2526. /* An end tag whose tag name is one of: "body", "caption", "col",
  2527. "colgroup", "html", "td", "th", "tr" */
  2528. } elseif($token['type'] === HTML5::ENDTAG && in_array($token['name'],
  2529. array('body', 'caption', 'col', 'colgroup', 'html', 'td', 'th', 'tr'))) {
  2530. /* Parse error. Ignore the token. */
  2531. /* Anything else */
  2532. } else {
  2533. /* Process the token as if the insertion mode was "in table". */
  2534. $this->inTable($token);
  2535. }
  2536. }
  2537. private function inRow($token) {
  2538. $clear = array('tr', 'html');
  2539. /* A start tag whose tag name is one of: "th", "td" */
  2540. if($token['type'] === HTML5::STARTTAG &&
  2541. ($token['name'] === 'th' || $token['name'] === 'td')) {
  2542. /* Clear the stack back to a table row context. */
  2543. $this->clearStackToTableContext($clear);
  2544. /* Insert an HTML element for the token, then switch the insertion
  2545. mode to "in cell". */
  2546. $this->insertElement($token);
  2547. $this->mode = self::IN_CELL;
  2548. /* Insert a marker at the end of the list of active formatting
  2549. elements. */
  2550. $this->a_formatting[] = self::MARKER;
  2551. /* An end tag whose tag name is "tr" */
  2552. } elseif($token['type'] === HTML5::ENDTAG && $token['name'] === 'tr') {
  2553. /* If the stack of open elements does not have an element in table
  2554. scope with the same tag name as the token, this is a parse error.
  2555. Ignore the token. (innerHTML case) */
  2556. if(!$this->elementInScope($token['name'], true)) {
  2557. // Ignore.
  2558. /* Otherwise: */
  2559. } else {
  2560. /* Clear the stack back to a table row context. */
  2561. $this->clearStackToTableContext($clear);
  2562. /* Pop the current node (which will be a tr element) from the
  2563. stack of open elements. Switch the insertion mode to "in table
  2564. body". */
  2565. array_pop($this->stack);
  2566. $this->mode = self::IN_TBODY;
  2567. }
  2568. /* A start tag whose tag name is one of: "caption", "col", "colgroup",
  2569. "tbody", "tfoot", "thead", "tr" or an end tag whose tag name is "table" */
  2570. } elseif($token['type'] === HTML5::STARTTAG && in_array($token['name'],
  2571. array('caption', 'col', 'colgroup', 'tbody', 'tfoot', 'thead', 'tr'))) {
  2572. /* Act as if an end tag with the tag name "tr" had been seen, then,
  2573. if that token wasn't ignored, reprocess the current token. */
  2574. $this->inRow(array(
  2575. 'name' => 'tr',
  2576. 'type' => HTML5::ENDTAG
  2577. ));
  2578. return $this->inCell($token);
  2579. /* An end tag whose tag name is one of: "tbody", "tfoot", "thead" */
  2580. } elseif($token['type'] === HTML5::ENDTAG &&
  2581. in_array($token['name'], array('tbody', 'tfoot', 'thead'))) {
  2582. /* If the stack of open elements does not have an element in table
  2583. scope with the same tag name as the token, this is a parse error.
  2584. Ignore the token. */
  2585. if(!$this->elementInScope($token['name'], true)) {
  2586. // Ignore.
  2587. /* Otherwise: */
  2588. } else {
  2589. /* Otherwise, act as if an end tag with the tag name "tr" had
  2590. been seen, then reprocess the current token. */
  2591. $this->inRow(array(
  2592. 'name' => 'tr',
  2593. 'type' => HTML5::ENDTAG
  2594. ));
  2595. return $this->inCell($token);
  2596. }
  2597. /* An end tag whose tag name is one of: "body", "caption", "col",
  2598. "colgroup", "html", "td", "th" */
  2599. } elseif($token['type'] === HTML5::ENDTAG && in_array($token['name'],
  2600. array('body', 'caption', 'col', 'colgroup', 'html', 'td', 'th', 'tr'))) {
  2601. /* Parse error. Ignore the token. */
  2602. /* Anything else */
  2603. } else {
  2604. /* Process the token as if the insertion mode was "in table". */
  2605. $this->inTable($token);
  2606. }
  2607. }
  2608. private function inCell($token) {
  2609. /* An end tag whose tag name is one of: "td", "th" */
  2610. if($token['type'] === HTML5::ENDTAG &&
  2611. ($token['name'] === 'td' || $token['name'] === 'th')) {
  2612. /* If the stack of open elements does not have an element in table
  2613. scope with the same tag name as that of the token, then this is a
  2614. parse error and the token must be ignored. */
  2615. if(!$this->elementInScope($token['name'], true)) {
  2616. // Ignore.
  2617. /* Otherwise: */
  2618. } else {
  2619. /* Generate implied end tags, except for elements with the same
  2620. tag name as the token. */
  2621. $this->generateImpliedEndTags(array($token['name']));
  2622. /* Now, if the current node is not an element with the same tag
  2623. name as the token, then this is a parse error. */
  2624. // k
  2625. /* Pop elements from this stack until an element with the same
  2626. tag name as the token has been popped from the stack. */
  2627. while(true) {
  2628. $node = end($this->stack)->nodeName;
  2629. array_pop($this->stack);
  2630. if($node === $token['name']) {
  2631. break;
  2632. }
  2633. }
  2634. /* Clear the list of active formatting elements up to the last
  2635. marker. */
  2636. $this->clearTheActiveFormattingElementsUpToTheLastMarker();
  2637. /* Switch the insertion mode to "in row". (The current node
  2638. will be a tr element at this point.) */
  2639. $this->mode = self::IN_ROW;
  2640. }
  2641. /* A start tag whose tag name is one of: "caption", "col", "colgroup",
  2642. "tbody", "td", "tfoot", "th", "thead", "tr" */
  2643. } elseif($token['type'] === HTML5::STARTTAG && in_array($token['name'],
  2644. array('caption', 'col', 'colgroup', 'tbody', 'td', 'tfoot', 'th',
  2645. 'thead', 'tr'))) {
  2646. /* If the stack of open elements does not have a td or th element
  2647. in table scope, then this is a parse error; ignore the token.
  2648. (innerHTML case) */
  2649. if(!$this->elementInScope(array('td', 'th'), true)) {
  2650. // Ignore.
  2651. /* Otherwise, close the cell (see below) and reprocess the current
  2652. token. */
  2653. } else {
  2654. $this->closeCell();
  2655. return $this->inRow($token);
  2656. }
  2657. /* A start tag whose tag name is one of: "caption", "col", "colgroup",
  2658. "tbody", "td", "tfoot", "th", "thead", "tr" */
  2659. } elseif($token['type'] === HTML5::STARTTAG && in_array($token['name'],
  2660. array('caption', 'col', 'colgroup', 'tbody', 'td', 'tfoot', 'th',
  2661. 'thead', 'tr'))) {
  2662. /* If the stack of open elements does not have a td or th element
  2663. in table scope, then this is a parse error; ignore the token.
  2664. (innerHTML case) */
  2665. if(!$this->elementInScope(array('td', 'th'), true)) {
  2666. // Ignore.
  2667. /* Otherwise, close the cell (see below) and reprocess the current
  2668. token. */
  2669. } else {
  2670. $this->closeCell();
  2671. return $this->inRow($token);
  2672. }
  2673. /* An end tag whose tag name is one of: "body", "caption", "col",
  2674. "colgroup", "html" */
  2675. } elseif($token['type'] === HTML5::ENDTAG && in_array($token['name'],
  2676. array('body', 'caption', 'col', 'colgroup', 'html'))) {
  2677. /* Parse error. Ignore the token. */
  2678. /* An end tag whose tag name is one of: "table", "tbody", "tfoot",
  2679. "thead", "tr" */
  2680. } elseif($token['type'] === HTML5::ENDTAG && in_array($token['name'],
  2681. array('table', 'tbody', 'tfoot', 'thead', 'tr'))) {
  2682. /* If the stack of open elements does not have an element in table
  2683. scope with the same tag name as that of the token (which can only
  2684. happen for "tbody", "tfoot" and "thead", or, in the innerHTML case),
  2685. then this is a parse error and the token must be ignored. */
  2686. if(!$this->elementInScope($token['name'], true)) {
  2687. // Ignore.
  2688. /* Otherwise, close the cell (see below) and reprocess the current
  2689. token. */
  2690. } else {
  2691. $this->closeCell();
  2692. return $this->inRow($token);
  2693. }
  2694. /* Anything else */
  2695. } else {
  2696. /* Process the token as if the insertion mode was "in body". */
  2697. $this->inBody($token);
  2698. }
  2699. }
  2700. private function inSelect($token) {
  2701. /* Handle the token as follows: */
  2702. /* A character token */
  2703. if($token['type'] === HTML5::CHARACTR) {
  2704. /* Append the token's character to the current node. */
  2705. $this->insertText($token['data']);
  2706. /* A comment token */
  2707. } elseif($token['type'] === HTML5::COMMENT) {
  2708. /* Append a Comment node to the current node with the data
  2709. attribute set to the data given in the comment token. */
  2710. $this->insertComment($token['data']);
  2711. /* A start tag token whose tag name is "option" */
  2712. } elseif($token['type'] === HTML5::STARTTAG &&
  2713. $token['name'] === 'option') {
  2714. /* If the current node is an option element, act as if an end tag
  2715. with the tag name "option" had been seen. */
  2716. if(end($this->stack)->nodeName === 'option') {
  2717. $this->inSelect(array(
  2718. 'name' => 'option',
  2719. 'type' => HTML5::ENDTAG
  2720. ));
  2721. }
  2722. /* Insert an HTML element for the token. */
  2723. $this->insertElement($token);
  2724. /* A start tag token whose tag name is "optgroup" */
  2725. } elseif($token['type'] === HTML5::STARTTAG &&
  2726. $token['name'] === 'optgroup') {
  2727. /* If the current node is an option element, act as if an end tag
  2728. with the tag name "option" had been seen. */
  2729. if(end($this->stack)->nodeName === 'option') {
  2730. $this->inSelect(array(
  2731. 'name' => 'option',
  2732. 'type' => HTML5::ENDTAG
  2733. ));
  2734. }
  2735. /* If the current node is an optgroup element, act as if an end tag
  2736. with the tag name "optgroup" had been seen. */
  2737. if(end($this->stack)->nodeName === 'optgroup') {
  2738. $this->inSelect(array(
  2739. 'name' => 'optgroup',
  2740. 'type' => HTML5::ENDTAG
  2741. ));
  2742. }
  2743. /* Insert an HTML element for the token. */
  2744. $this->insertElement($token);
  2745. /* An end tag token whose tag name is "optgroup" */
  2746. } elseif($token['type'] === HTML5::ENDTAG &&
  2747. $token['name'] === 'optgroup') {
  2748. /* First, if the current node is an option element, and the node
  2749. immediately before it in the stack of open elements is an optgroup
  2750. element, then act as if an end tag with the tag name "option" had
  2751. been seen. */
  2752. $elements_in_stack = count($this->stack);
  2753. if($this->stack[$elements_in_stack - 1]->nodeName === 'option' &&
  2754. $this->stack[$elements_in_stack - 2]->nodeName === 'optgroup') {
  2755. $this->inSelect(array(
  2756. 'name' => 'option',
  2757. 'type' => HTML5::ENDTAG
  2758. ));
  2759. }
  2760. /* If the current node is an optgroup element, then pop that node
  2761. from the stack of open elements. Otherwise, this is a parse error,
  2762. ignore the token. */
  2763. if($this->stack[$elements_in_stack - 1] === 'optgroup') {
  2764. array_pop($this->stack);
  2765. }
  2766. /* An end tag token whose tag name is "option" */
  2767. } elseif($token['type'] === HTML5::ENDTAG &&
  2768. $token['name'] === 'option') {
  2769. /* If the current node is an option element, then pop that node
  2770. from the stack of open elements. Otherwise, this is a parse error,
  2771. ignore the token. */
  2772. if(end($this->stack)->nodeName === 'option') {
  2773. array_pop($this->stack);
  2774. }
  2775. /* An end tag whose tag name is "select" */
  2776. } elseif($token['type'] === HTML5::ENDTAG &&
  2777. $token['name'] === 'select') {
  2778. /* If the stack of open elements does not have an element in table
  2779. scope with the same tag name as the token, this is a parse error.
  2780. Ignore the token. (innerHTML case) */
  2781. if(!$this->elementInScope($token['name'], true)) {
  2782. // w/e
  2783. /* Otherwise: */
  2784. } else {
  2785. /* Pop elements from the stack of open elements until a select
  2786. element has been popped from the stack. */
  2787. while(true) {
  2788. $current = end($this->stack)->nodeName;
  2789. array_pop($this->stack);
  2790. if($current === 'select') {
  2791. break;
  2792. }
  2793. }
  2794. /* Reset the insertion mode appropriately. */
  2795. $this->resetInsertionMode();
  2796. }
  2797. /* A start tag whose tag name is "select" */
  2798. } elseif($token['name'] === 'select' &&
  2799. $token['type'] === HTML5::STARTTAG) {
  2800. /* Parse error. Act as if the token had been an end tag with the
  2801. tag name "select" instead. */
  2802. $this->inSelect(array(
  2803. 'name' => 'select',
  2804. 'type' => HTML5::ENDTAG
  2805. ));
  2806. /* An end tag whose tag name is one of: "caption", "table", "tbody",
  2807. "tfoot", "thead", "tr", "td", "th" */
  2808. } elseif(in_array($token['name'], array('caption', 'table', 'tbody',
  2809. 'tfoot', 'thead', 'tr', 'td', 'th')) && $token['type'] === HTML5::ENDTAG) {
  2810. /* Parse error. */
  2811. // w/e
  2812. /* If the stack of open elements has an element in table scope with
  2813. the same tag name as that of the token, then act as if an end tag
  2814. with the tag name "select" had been seen, and reprocess the token.
  2815. Otherwise, ignore the token. */
  2816. if($this->elementInScope($token['name'], true)) {
  2817. $this->inSelect(array(
  2818. 'name' => 'select',
  2819. 'type' => HTML5::ENDTAG
  2820. ));
  2821. $this->mainPhase($token);
  2822. }
  2823. /* Anything else */
  2824. } else {
  2825. /* Parse error. Ignore the token. */
  2826. }
  2827. }
  2828. private function afterBody($token) {
  2829. /* Handle the token as follows: */
  2830. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  2831. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  2832. or U+0020 SPACE */
  2833. if($token['type'] === HTML5::CHARACTR &&
  2834. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) {
  2835. /* Process the token as it would be processed if the insertion mode
  2836. was "in body". */
  2837. $this->inBody($token);
  2838. /* A comment token */
  2839. } elseif($token['type'] === HTML5::COMMENT) {
  2840. /* Append a Comment node to the first element in the stack of open
  2841. elements (the html element), with the data attribute set to the
  2842. data given in the comment token. */
  2843. $comment = $this->dom->createComment($token['data']);
  2844. $this->stack[0]->appendChild($comment);
  2845. /* An end tag with the tag name "html" */
  2846. } elseif($token['type'] === HTML5::ENDTAG && $token['name'] === 'html') {
  2847. /* If the parser was originally created in order to handle the
  2848. setting of an element's innerHTML attribute, this is a parse error;
  2849. ignore the token. (The element will be an html element in this
  2850. case.) (innerHTML case) */
  2851. /* Otherwise, switch to the trailing end phase. */
  2852. $this->phase = self::END_PHASE;
  2853. /* Anything else */
  2854. } else {
  2855. /* Parse error. Set the insertion mode to "in body" and reprocess
  2856. the token. */
  2857. $this->mode = self::IN_BODY;
  2858. return $this->inBody($token);
  2859. }
  2860. }
  2861. private function inFrameset($token) {
  2862. /* Handle the token as follows: */
  2863. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  2864. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  2865. U+000D CARRIAGE RETURN (CR), or U+0020 SPACE */
  2866. if($token['type'] === HTML5::CHARACTR &&
  2867. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) {
  2868. /* Append the character to the current node. */
  2869. $this->insertText($token['data']);
  2870. /* A comment token */
  2871. } elseif($token['type'] === HTML5::COMMENT) {
  2872. /* Append a Comment node to the current node with the data
  2873. attribute set to the data given in the comment token. */
  2874. $this->insertComment($token['data']);
  2875. /* A start tag with the tag name "frameset" */
  2876. } elseif($token['name'] === 'frameset' &&
  2877. $token['type'] === HTML5::STARTTAG) {
  2878. $this->insertElement($token);
  2879. /* An end tag with the tag name "frameset" */
  2880. } elseif($token['name'] === 'frameset' &&
  2881. $token['type'] === HTML5::ENDTAG) {
  2882. /* If the current node is the root html element, then this is a
  2883. parse error; ignore the token. (innerHTML case) */
  2884. if(end($this->stack)->nodeName === 'html') {
  2885. // Ignore
  2886. } else {
  2887. /* Otherwise, pop the current node from the stack of open
  2888. elements. */
  2889. array_pop($this->stack);
  2890. /* If the parser was not originally created in order to handle
  2891. the setting of an element's innerHTML attribute (innerHTML case),
  2892. and the current node is no longer a frameset element, then change
  2893. the insertion mode to "after frameset". */
  2894. $this->mode = self::AFTR_FRAME;
  2895. }
  2896. /* A start tag with the tag name "frame" */
  2897. } elseif($token['name'] === 'frame' &&
  2898. $token['type'] === HTML5::STARTTAG) {
  2899. /* Insert an HTML element for the token. */
  2900. $this->insertElement($token);
  2901. /* Immediately pop the current node off the stack of open elements. */
  2902. array_pop($this->stack);
  2903. /* A start tag with the tag name "noframes" */
  2904. } elseif($token['name'] === 'noframes' &&
  2905. $token['type'] === HTML5::STARTTAG) {
  2906. /* Process the token as if the insertion mode had been "in body". */
  2907. $this->inBody($token);
  2908. /* Anything else */
  2909. } else {
  2910. /* Parse error. Ignore the token. */
  2911. }
  2912. }
  2913. private function afterFrameset($token) {
  2914. /* Handle the token as follows: */
  2915. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  2916. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  2917. U+000D CARRIAGE RETURN (CR), or U+0020 SPACE */
  2918. if($token['type'] === HTML5::CHARACTR &&
  2919. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) {
  2920. /* Append the character to the current node. */
  2921. $this->insertText($token['data']);
  2922. /* A comment token */
  2923. } elseif($token['type'] === HTML5::COMMENT) {
  2924. /* Append a Comment node to the current node with the data
  2925. attribute set to the data given in the comment token. */
  2926. $this->insertComment($token['data']);
  2927. /* An end tag with the tag name "html" */
  2928. } elseif($token['name'] === 'html' &&
  2929. $token['type'] === HTML5::ENDTAG) {
  2930. /* Switch to the trailing end phase. */
  2931. $this->phase = self::END_PHASE;
  2932. /* A start tag with the tag name "noframes" */
  2933. } elseif($token['name'] === 'noframes' &&
  2934. $token['type'] === HTML5::STARTTAG) {
  2935. /* Process the token as if the insertion mode had been "in body". */
  2936. $this->inBody($token);
  2937. /* Anything else */
  2938. } else {
  2939. /* Parse error. Ignore the token. */
  2940. }
  2941. }
  2942. private function trailingEndPhase($token) {
  2943. /* After the main phase, as each token is emitted from the tokenisation
  2944. stage, it must be processed as described in this section. */
  2945. /* A DOCTYPE token */
  2946. if($token['type'] === HTML5::DOCTYPE) {
  2947. // Parse error. Ignore the token.
  2948. /* A comment token */
  2949. } elseif($token['type'] === HTML5::COMMENT) {
  2950. /* Append a Comment node to the Document object with the data
  2951. attribute set to the data given in the comment token. */
  2952. $comment = $this->dom->createComment($token['data']);
  2953. $this->dom->appendChild($comment);
  2954. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  2955. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  2956. or U+0020 SPACE */
  2957. } elseif($token['type'] === HTML5::CHARACTR &&
  2958. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) {
  2959. /* Process the token as it would be processed in the main phase. */
  2960. $this->mainPhase($token);
  2961. /* A character token that is not one of U+0009 CHARACTER TABULATION,
  2962. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  2963. or U+0020 SPACE. Or a start tag token. Or an end tag token. */
  2964. } elseif(($token['type'] === HTML5::CHARACTR &&
  2965. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) ||
  2966. $token['type'] === HTML5::STARTTAG || $token['type'] === HTML5::ENDTAG) {
  2967. /* Parse error. Switch back to the main phase and reprocess the
  2968. token. */
  2969. $this->phase = self::MAIN_PHASE;
  2970. return $this->mainPhase($token);
  2971. /* An end-of-file token */
  2972. } elseif($token['type'] === HTML5::EOF) {
  2973. /* OMG DONE!! */
  2974. }
  2975. }
  2976. private function insertElement($token, $append = true, $check = false) {
  2977. // Proprietary workaround for libxml2's limitations with tag names
  2978. if ($check) {
  2979. // Slightly modified HTML5 tag-name modification,
  2980. // removing anything that's not an ASCII letter, digit, or hyphen
  2981. $token['name'] = preg_replace('/[^a-z0-9-]/i', '', $token['name']);
  2982. // Remove leading hyphens and numbers
  2983. $token['name'] = ltrim($token['name'], '-0..9');
  2984. // In theory, this should ever be needed, but just in case
  2985. if ($token['name'] === '') $token['name'] = 'span'; // arbitrary generic choice
  2986. }
  2987. $el = $this->dom->createElement($token['name']);
  2988. foreach($token['attr'] as $attr) {
  2989. if(!$el->hasAttribute($attr['name'])) {
  2990. $el->setAttribute($attr['name'], $attr['value']);
  2991. }
  2992. }
  2993. $this->appendToRealParent($el);
  2994. $this->stack[] = $el;
  2995. return $el;
  2996. }
  2997. private function insertText($data) {
  2998. $text = $this->dom->createTextNode($data);
  2999. $this->appendToRealParent($text);
  3000. }
  3001. private function insertComment($data) {
  3002. $comment = $this->dom->createComment($data);
  3003. $this->appendToRealParent($comment);
  3004. }
  3005. private function appendToRealParent($node) {
  3006. if($this->foster_parent === null) {
  3007. end($this->stack)->appendChild($node);
  3008. } elseif($this->foster_parent !== null) {
  3009. /* If the foster parent element is the parent element of the
  3010. last table element in the stack of open elements, then the new
  3011. node must be inserted immediately before the last table element
  3012. in the stack of open elements in the foster parent element;
  3013. otherwise, the new node must be appended to the foster parent
  3014. element. */
  3015. for($n = count($this->stack) - 1; $n >= 0; $n--) {
  3016. if($this->stack[$n]->nodeName === 'table' &&
  3017. $this->stack[$n]->parentNode !== null) {
  3018. $table = $this->stack[$n];
  3019. break;
  3020. }
  3021. }
  3022. if(isset($table) && $this->foster_parent->isSameNode($table->parentNode))
  3023. $this->foster_parent->insertBefore($node, $table);
  3024. else
  3025. $this->foster_parent->appendChild($node);
  3026. $this->foster_parent = null;
  3027. }
  3028. }
  3029. private function elementInScope($el, $table = false) {
  3030. if(is_array($el)) {
  3031. foreach($el as $element) {
  3032. if($this->elementInScope($element, $table)) {
  3033. return true;
  3034. }
  3035. }
  3036. return false;
  3037. }
  3038. $leng = count($this->stack);
  3039. for($n = 0; $n < $leng; $n++) {
  3040. /* 1. Initialise node to be the current node (the bottommost node of
  3041. the stack). */
  3042. $node = $this->stack[$leng - 1 - $n];
  3043. if($node->tagName === $el) {
  3044. /* 2. If node is the target node, terminate in a match state. */
  3045. return true;
  3046. } elseif($node->tagName === 'table') {
  3047. /* 3. Otherwise, if node is a table element, terminate in a failure
  3048. state. */
  3049. return false;
  3050. } elseif($table === true && in_array($node->tagName, array('caption', 'td',
  3051. 'th', 'button', 'marquee', 'object'))) {
  3052. /* 4. Otherwise, if the algorithm is the "has an element in scope"
  3053. variant (rather than the "has an element in table scope" variant),
  3054. and node is one of the following, terminate in a failure state. */
  3055. return false;
  3056. } elseif($node === $node->ownerDocument->documentElement) {
  3057. /* 5. Otherwise, if node is an html element (root element), terminate
  3058. in a failure state. (This can only happen if the node is the topmost
  3059. node of the stack of open elements, and prevents the next step from
  3060. being invoked if there are no more elements in the stack.) */
  3061. return false;
  3062. }
  3063. /* Otherwise, set node to the previous entry in the stack of open
  3064. elements and return to step 2. (This will never fail, since the loop
  3065. will always terminate in the previous step if the top of the stack
  3066. is reached.) */
  3067. }
  3068. }
  3069. private function reconstructActiveFormattingElements() {
  3070. /* 1. If there are no entries in the list of active formatting elements,
  3071. then there is nothing to reconstruct; stop this algorithm. */
  3072. $formatting_elements = count($this->a_formatting);
  3073. if($formatting_elements === 0) {
  3074. return false;
  3075. }
  3076. /* 3. Let entry be the last (most recently added) element in the list
  3077. of active formatting elements. */
  3078. $entry = end($this->a_formatting);
  3079. /* 2. If the last (most recently added) entry in the list of active
  3080. formatting elements is a marker, or if it is an element that is in the
  3081. stack of open elements, then there is nothing to reconstruct; stop this
  3082. algorithm. */
  3083. if($entry === self::MARKER || in_array($entry, $this->stack, true)) {
  3084. return false;
  3085. }
  3086. for($a = $formatting_elements - 1; $a >= 0; true) {
  3087. /* 4. If there are no entries before entry in the list of active
  3088. formatting elements, then jump to step 8. */
  3089. if($a === 0) {
  3090. $step_seven = false;
  3091. break;
  3092. }
  3093. /* 5. Let entry be the entry one earlier than entry in the list of
  3094. active formatting elements. */
  3095. $a--;
  3096. $entry = $this->a_formatting[$a];
  3097. /* 6. If entry is neither a marker nor an element that is also in
  3098. thetack of open elements, go to step 4. */
  3099. if($entry === self::MARKER || in_array($entry, $this->stack, true)) {
  3100. break;
  3101. }
  3102. }
  3103. while(true) {
  3104. /* 7. Let entry be the element one later than entry in the list of
  3105. active formatting elements. */
  3106. if(isset($step_seven) && $step_seven === true) {
  3107. $a++;
  3108. $entry = $this->a_formatting[$a];
  3109. }
  3110. /* 8. Perform a shallow clone of the element entry to obtain clone. */
  3111. $clone = $entry->cloneNode();
  3112. /* 9. Append clone to the current node and push it onto the stack
  3113. of open elements so that it is the new current node. */
  3114. end($this->stack)->appendChild($clone);
  3115. $this->stack[] = $clone;
  3116. /* 10. Replace the entry for entry in the list with an entry for
  3117. clone. */
  3118. $this->a_formatting[$a] = $clone;
  3119. /* 11. If the entry for clone in the list of active formatting
  3120. elements is not the last entry in the list, return to step 7. */
  3121. if(end($this->a_formatting) !== $clone) {
  3122. $step_seven = true;
  3123. } else {
  3124. break;
  3125. }
  3126. }
  3127. }
  3128. private function clearTheActiveFormattingElementsUpToTheLastMarker() {
  3129. /* When the steps below require the UA to clear the list of active
  3130. formatting elements up to the last marker, the UA must perform the
  3131. following steps: */
  3132. while(true) {
  3133. /* 1. Let entry be the last (most recently added) entry in the list
  3134. of active formatting elements. */
  3135. $entry = end($this->a_formatting);
  3136. /* 2. Remove entry from the list of active formatting elements. */
  3137. array_pop($this->a_formatting);
  3138. /* 3. If entry was a marker, then stop the algorithm at this point.
  3139. The list has been cleared up to the last marker. */
  3140. if($entry === self::MARKER) {
  3141. break;
  3142. }
  3143. }
  3144. }
  3145. private function generateImpliedEndTags($exclude = array()) {
  3146. /* When the steps below require the UA to generate implied end tags,
  3147. then, if the current node is a dd element, a dt element, an li element,
  3148. a p element, a td element, a th element, or a tr element, the UA must
  3149. act as if an end tag with the respective tag name had been seen and
  3150. then generate implied end tags again. */
  3151. $node = end($this->stack);
  3152. $elements = array_diff(array('dd', 'dt', 'li', 'p', 'td', 'th', 'tr'), $exclude);
  3153. while(in_array(end($this->stack)->nodeName, $elements)) {
  3154. array_pop($this->stack);
  3155. }
  3156. }
  3157. private function getElementCategory($node) {
  3158. $name = $node->tagName;
  3159. if(in_array($name, $this->special))
  3160. return self::SPECIAL;
  3161. elseif(in_array($name, $this->scoping))
  3162. return self::SCOPING;
  3163. elseif(in_array($name, $this->formatting))
  3164. return self::FORMATTING;
  3165. else
  3166. return self::PHRASING;
  3167. }
  3168. private function clearStackToTableContext($elements) {
  3169. /* When the steps above require the UA to clear the stack back to a
  3170. table context, it means that the UA must, while the current node is not
  3171. a table element or an html element, pop elements from the stack of open
  3172. elements. If this causes any elements to be popped from the stack, then
  3173. this is a parse error. */
  3174. while(true) {
  3175. $node = end($this->stack)->nodeName;
  3176. if(in_array($node, $elements)) {
  3177. break;
  3178. } else {
  3179. array_pop($this->stack);
  3180. }
  3181. }
  3182. }
  3183. private function resetInsertionMode() {
  3184. /* 1. Let last be false. */
  3185. $last = false;
  3186. $leng = count($this->stack);
  3187. for($n = $leng - 1; $n >= 0; $n--) {
  3188. /* 2. Let node be the last node in the stack of open elements. */
  3189. $node = $this->stack[$n];
  3190. /* 3. If node is the first node in the stack of open elements, then
  3191. set last to true. If the element whose innerHTML attribute is being
  3192. set is neither a td element nor a th element, then set node to the
  3193. element whose innerHTML attribute is being set. (innerHTML case) */
  3194. if($this->stack[0]->isSameNode($node)) {
  3195. $last = true;
  3196. }
  3197. /* 4. If node is a select element, then switch the insertion mode to
  3198. "in select" and abort these steps. (innerHTML case) */
  3199. if($node->nodeName === 'select') {
  3200. $this->mode = self::IN_SELECT;
  3201. break;
  3202. /* 5. If node is a td or th element, then switch the insertion mode
  3203. to "in cell" and abort these steps. */
  3204. } elseif($node->nodeName === 'td' || $node->nodeName === 'th') {
  3205. $this->mode = self::IN_CELL;
  3206. break;
  3207. /* 6. If node is a tr element, then switch the insertion mode to
  3208. "in row" and abort these steps. */
  3209. } elseif($node->nodeName === 'tr') {
  3210. $this->mode = self::IN_ROW;
  3211. break;
  3212. /* 7. If node is a tbody, thead, or tfoot element, then switch the
  3213. insertion mode to "in table body" and abort these steps. */
  3214. } elseif(in_array($node->nodeName, array('tbody', 'thead', 'tfoot'))) {
  3215. $this->mode = self::IN_TBODY;
  3216. break;
  3217. /* 8. If node is a caption element, then switch the insertion mode
  3218. to "in caption" and abort these steps. */
  3219. } elseif($node->nodeName === 'caption') {
  3220. $this->mode = self::IN_CAPTION;
  3221. break;
  3222. /* 9. If node is a colgroup element, then switch the insertion mode
  3223. to "in column group" and abort these steps. (innerHTML case) */
  3224. } elseif($node->nodeName === 'colgroup') {
  3225. $this->mode = self::IN_CGROUP;
  3226. break;
  3227. /* 10. If node is a table element, then switch the insertion mode
  3228. to "in table" and abort these steps. */
  3229. } elseif($node->nodeName === 'table') {
  3230. $this->mode = self::IN_TABLE;
  3231. break;
  3232. /* 11. If node is a head element, then switch the insertion mode
  3233. to "in body" ("in body"! not "in head"!) and abort these steps.
  3234. (innerHTML case) */
  3235. } elseif($node->nodeName === 'head') {
  3236. $this->mode = self::IN_BODY;
  3237. break;
  3238. /* 12. If node is a body element, then switch the insertion mode to
  3239. "in body" and abort these steps. */
  3240. } elseif($node->nodeName === 'body') {
  3241. $this->mode = self::IN_BODY;
  3242. break;
  3243. /* 13. If node is a frameset element, then switch the insertion
  3244. mode to "in frameset" and abort these steps. (innerHTML case) */
  3245. } elseif($node->nodeName === 'frameset') {
  3246. $this->mode = self::IN_FRAME;
  3247. break;
  3248. /* 14. If node is an html element, then: if the head element
  3249. pointer is null, switch the insertion mode to "before head",
  3250. otherwise, switch the insertion mode to "after head". In either
  3251. case, abort these steps. (innerHTML case) */
  3252. } elseif($node->nodeName === 'html') {
  3253. $this->mode = ($this->head_pointer === null)
  3254. ? self::BEFOR_HEAD
  3255. : self::AFTER_HEAD;
  3256. break;
  3257. /* 15. If last is true, then set the insertion mode to "in body"
  3258. and abort these steps. (innerHTML case) */
  3259. } elseif($last) {
  3260. $this->mode = self::IN_BODY;
  3261. break;
  3262. }
  3263. }
  3264. }
  3265. private function closeCell() {
  3266. /* If the stack of open elements has a td or th element in table scope,
  3267. then act as if an end tag token with that tag name had been seen. */
  3268. foreach(array('td', 'th') as $cell) {
  3269. if($this->elementInScope($cell, true)) {
  3270. $this->inCell(array(
  3271. 'name' => $cell,
  3272. 'type' => HTML5::ENDTAG
  3273. ));
  3274. break;
  3275. }
  3276. }
  3277. }
  3278. public function save() {
  3279. return $this->dom;
  3280. }
  3281. }
  3282. ?>