PageRenderTime 133ms CodeModel.GetById 17ms RepoModel.GetById 6ms app.codeStats 1ms

/external_lib/HTMLPurifier/HTMLPurifier/Lexer/PH5P.php

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