PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/school/605.441.Databases/project/htmlpurifier/maintenance/PH5P.php

https://github.com/hank/life
PHP | 3824 lines | 1943 code | 607 blank | 1274 comment | 299 complexity | f902c78700b9d61daafecaa569cf0b81 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. class HTML5 {
  3. private $data;
  4. private $char;
  5. private $EOF;
  6. private $state;
  7. private $tree;
  8. private $token;
  9. private $content_model;
  10. private $escape = false;
  11. private $entities = array('AElig;','AElig','AMP;','AMP','Aacute;','Aacute',
  12. 'Acirc;','Acirc','Agrave;','Agrave','Alpha;','Aring;','Aring','Atilde;',
  13. 'Atilde','Auml;','Auml','Beta;','COPY;','COPY','Ccedil;','Ccedil','Chi;',
  14. 'Dagger;','Delta;','ETH;','ETH','Eacute;','Eacute','Ecirc;','Ecirc','Egrave;',
  15. 'Egrave','Epsilon;','Eta;','Euml;','Euml','GT;','GT','Gamma;','Iacute;',
  16. 'Iacute','Icirc;','Icirc','Igrave;','Igrave','Iota;','Iuml;','Iuml','Kappa;',
  17. 'LT;','LT','Lambda;','Mu;','Ntilde;','Ntilde','Nu;','OElig;','Oacute;',
  18. 'Oacute','Ocirc;','Ocirc','Ograve;','Ograve','Omega;','Omicron;','Oslash;',
  19. 'Oslash','Otilde;','Otilde','Ouml;','Ouml','Phi;','Pi;','Prime;','Psi;',
  20. 'QUOT;','QUOT','REG;','REG','Rho;','Scaron;','Sigma;','THORN;','THORN',
  21. 'TRADE;','Tau;','Theta;','Uacute;','Uacute','Ucirc;','Ucirc','Ugrave;',
  22. 'Ugrave','Upsilon;','Uuml;','Uuml','Xi;','Yacute;','Yacute','Yuml;','Zeta;',
  23. 'aacute;','aacute','acirc;','acirc','acute;','acute','aelig;','aelig',
  24. 'agrave;','agrave','alefsym;','alpha;','amp;','amp','and;','ang;','apos;',
  25. 'aring;','aring','asymp;','atilde;','atilde','auml;','auml','bdquo;','beta;',
  26. 'brvbar;','brvbar','bull;','cap;','ccedil;','ccedil','cedil;','cedil',
  27. 'cent;','cent','chi;','circ;','clubs;','cong;','copy;','copy','crarr;',
  28. 'cup;','curren;','curren','dArr;','dagger;','darr;','deg;','deg','delta;',
  29. 'diams;','divide;','divide','eacute;','eacute','ecirc;','ecirc','egrave;',
  30. 'egrave','empty;','emsp;','ensp;','epsilon;','equiv;','eta;','eth;','eth',
  31. 'euml;','euml','euro;','exist;','fnof;','forall;','frac12;','frac12',
  32. 'frac14;','frac14','frac34;','frac34','frasl;','gamma;','ge;','gt;','gt',
  33. 'hArr;','harr;','hearts;','hellip;','iacute;','iacute','icirc;','icirc',
  34. 'iexcl;','iexcl','igrave;','igrave','image;','infin;','int;','iota;',
  35. 'iquest;','iquest','isin;','iuml;','iuml','kappa;','lArr;','lambda;','lang;',
  36. 'laquo;','laquo','larr;','lceil;','ldquo;','le;','lfloor;','lowast;','loz;',
  37. 'lrm;','lsaquo;','lsquo;','lt;','lt','macr;','macr','mdash;','micro;','micro',
  38. 'middot;','middot','minus;','mu;','nabla;','nbsp;','nbsp','ndash;','ne;',
  39. 'ni;','not;','not','notin;','nsub;','ntilde;','ntilde','nu;','oacute;',
  40. 'oacute','ocirc;','ocirc','oelig;','ograve;','ograve','oline;','omega;',
  41. 'omicron;','oplus;','or;','ordf;','ordf','ordm;','ordm','oslash;','oslash',
  42. 'otilde;','otilde','otimes;','ouml;','ouml','para;','para','part;','permil;',
  43. 'perp;','phi;','pi;','piv;','plusmn;','plusmn','pound;','pound','prime;',
  44. 'prod;','prop;','psi;','quot;','quot','rArr;','radic;','rang;','raquo;',
  45. 'raquo','rarr;','rceil;','rdquo;','real;','reg;','reg','rfloor;','rho;',
  46. 'rlm;','rsaquo;','rsquo;','sbquo;','scaron;','sdot;','sect;','sect','shy;',
  47. 'shy','sigma;','sigmaf;','sim;','spades;','sub;','sube;','sum;','sup1;',
  48. 'sup1','sup2;','sup2','sup3;','sup3','sup;','supe;','szlig;','szlig','tau;',
  49. 'there4;','theta;','thetasym;','thinsp;','thorn;','thorn','tilde;','times;',
  50. 'times','trade;','uArr;','uacute;','uacute','uarr;','ucirc;','ucirc',
  51. 'ugrave;','ugrave','uml;','uml','upsih;','upsilon;','uuml;','uuml','weierp;',
  52. 'xi;','yacute;','yacute','yen;','yen','yuml;','yuml','zeta;','zwj;','zwnj;');
  53. const PCDATA = 0;
  54. const RCDATA = 1;
  55. const CDATA = 2;
  56. const PLAINTEXT = 3;
  57. const DOCTYPE = 0;
  58. const STARTTAG = 1;
  59. const ENDTAG = 2;
  60. const COMMENT = 3;
  61. const CHARACTR = 4;
  62. const EOF = 5;
  63. public function __construct($data) {
  64. $data = str_replace("\r\n", "\n", $data);
  65. $date = str_replace("\r", null, $data);
  66. $this->data = $data;
  67. $this->char = -1;
  68. $this->EOF = strlen($data);
  69. $this->tree = new HTML5TreeConstructer;
  70. $this->content_model = self::PCDATA;
  71. $this->state = 'data';
  72. while($this->state !== null) {
  73. $this->{$this->state.'State'}();
  74. }
  75. }
  76. public function save() {
  77. return $this->tree->save();
  78. }
  79. private function char() {
  80. return ($this->char < $this->EOF)
  81. ? $this->data[$this->char]
  82. : false;
  83. }
  84. private function character($s, $l = 0) {
  85. if($s + $l < $this->EOF) {
  86. if($l === 0) {
  87. return $this->data[$s];
  88. } else {
  89. return substr($this->data, $s, $l);
  90. }
  91. }
  92. }
  93. private function characters($char_class, $start) {
  94. return preg_replace('#^(['.$char_class.']+).*#s', '\\1', substr($this->data, $start));
  95. }
  96. private function dataState() {
  97. // Consume the next input character
  98. $this->char++;
  99. $char = $this->char();
  100. if($char === '&' && ($this->content_model === self::PCDATA || $this->content_model === self::RCDATA)) {
  101. /* U+0026 AMPERSAND (&)
  102. When the content model flag is set to one of the PCDATA or RCDATA
  103. states: switch to the entity data state. Otherwise: treat it as per
  104. the "anything else" entry below. */
  105. $this->state = 'entityData';
  106. } elseif($char === '-') {
  107. /* If the content model flag is set to either the RCDATA state or
  108. the CDATA state, and the escape flag is false, and there are at
  109. least three characters before this one in the input stream, and the
  110. last four characters in the input stream, including this one, are
  111. U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+002D HYPHEN-MINUS,
  112. and U+002D HYPHEN-MINUS ("<!--"), then set the escape flag to true. */
  113. if(($this->content_model === self::RCDATA || $this->content_model ===
  114. self::CDATA) && $this->escape === false &&
  115. $this->char >= 3 && $this->character($this->char - 4, 4) === '<!--') {
  116. $this->escape = true;
  117. }
  118. /* In any case, emit the input character as a character token. Stay
  119. in the data state. */
  120. $this->emitToken(array(
  121. 'type' => self::CHARACTR,
  122. 'data' => $char
  123. ));
  124. /* U+003C LESS-THAN SIGN (<) */
  125. } elseif($char === '<' && ($this->content_model === self::PCDATA ||
  126. (($this->content_model === self::RCDATA ||
  127. $this->content_model === self::CDATA) && $this->escape === false))) {
  128. /* When the content model flag is set to the PCDATA state: switch
  129. to the tag open state.
  130. When the content model flag is set to either the RCDATA state or
  131. the CDATA state and the escape flag is false: switch to the tag
  132. open state.
  133. Otherwise: treat it as per the "anything else" entry below. */
  134. $this->state = 'tagOpen';
  135. /* U+003E GREATER-THAN SIGN (>) */
  136. } elseif($char === '>') {
  137. /* If the content model flag is set to either the RCDATA state or
  138. the CDATA state, and the escape flag is true, and the last three
  139. characters in the input stream including this one are U+002D
  140. HYPHEN-MINUS, U+002D HYPHEN-MINUS, U+003E GREATER-THAN SIGN ("-->"),
  141. set the escape flag to false. */
  142. if(($this->content_model === self::RCDATA ||
  143. $this->content_model === self::CDATA) && $this->escape === true &&
  144. $this->character($this->char, 3) === '-->') {
  145. $this->escape = false;
  146. }
  147. /* In any case, emit the input character as a character token.
  148. Stay in the data state. */
  149. $this->emitToken(array(
  150. 'type' => self::CHARACTR,
  151. 'data' => $char
  152. ));
  153. } elseif($this->char === $this->EOF) {
  154. /* EOF
  155. Emit an end-of-file token. */
  156. $this->EOF();
  157. } elseif($this->content_model === self::PLAINTEXT) {
  158. /* When the content model flag is set to the PLAINTEXT state
  159. THIS DIFFERS GREATLY FROM THE SPEC: Get the remaining characters of
  160. the text and emit it as a character token. */
  161. $this->emitToken(array(
  162. 'type' => self::CHARACTR,
  163. 'data' => substr($this->data, $this->char)
  164. ));
  165. $this->EOF();
  166. } else {
  167. /* Anything else
  168. THIS DIFFERS GREATLY FROM THE SPEC: Get as many character that
  169. otherwise would also be treated as a character token and emit it
  170. as a single character token. Stay in the data state. */
  171. $len = strcspn($this->data, '<&', $this->char);
  172. $char = substr($this->data, $this->char, $len);
  173. $this->char += $len - 1;
  174. $this->emitToken(array(
  175. 'type' => self::CHARACTR,
  176. 'data' => $char
  177. ));
  178. $this->state = 'data';
  179. }
  180. }
  181. private function entityDataState() {
  182. // Attempt to consume an entity.
  183. $entity = $this->entity();
  184. // If nothing is returned, emit a U+0026 AMPERSAND character token.
  185. // Otherwise, emit the character token that was returned.
  186. $char = (!$entity) ? '&' : $entity;
  187. $this->emitToken($char);
  188. // Finally, switch to the data state.
  189. $this->state = 'data';
  190. }
  191. private function tagOpenState() {
  192. switch($this->content_model) {
  193. case self::RCDATA:
  194. case self::CDATA:
  195. /* If the next input character is a U+002F SOLIDUS (/) character,
  196. consume it and switch to the close tag open state. If the next
  197. input character is not a U+002F SOLIDUS (/) character, emit a
  198. U+003C LESS-THAN SIGN character token and switch to the data
  199. state to process the next input character. */
  200. if($this->character($this->char + 1) === '/') {
  201. $this->char++;
  202. $this->state = 'closeTagOpen';
  203. } else {
  204. $this->emitToken(array(
  205. 'type' => self::CHARACTR,
  206. 'data' => '<'
  207. ));
  208. $this->state = 'data';
  209. }
  210. break;
  211. case self::PCDATA:
  212. // If the content model flag is set to the PCDATA state
  213. // Consume the next input character:
  214. $this->char++;
  215. $char = $this->char();
  216. if($char === '!') {
  217. /* U+0021 EXCLAMATION MARK (!)
  218. Switch to the markup declaration open state. */
  219. $this->state = 'markupDeclarationOpen';
  220. } elseif($char === '/') {
  221. /* U+002F SOLIDUS (/)
  222. Switch to the close tag open state. */
  223. $this->state = 'closeTagOpen';
  224. } elseif(preg_match('/^[A-Za-z]$/', $char)) {
  225. /* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
  226. Create a new start tag token, set its tag name to the lowercase
  227. version of the input character (add 0x0020 to the character's code
  228. point), then switch to the tag name state. (Don't emit the token
  229. yet; further details will be filled in before it is emitted.) */
  230. $this->token = array(
  231. 'name' => strtolower($char),
  232. 'type' => self::STARTTAG,
  233. 'attr' => array()
  234. );
  235. $this->state = 'tagName';
  236. } elseif($char === '>') {
  237. /* U+003E GREATER-THAN SIGN (>)
  238. Parse error. Emit a U+003C LESS-THAN SIGN character token and a
  239. U+003E GREATER-THAN SIGN character token. Switch to the data state. */
  240. $this->emitToken(array(
  241. 'type' => self::CHARACTR,
  242. 'data' => '<>'
  243. ));
  244. $this->state = 'data';
  245. } elseif($char === '?') {
  246. /* U+003F QUESTION MARK (?)
  247. Parse error. Switch to the bogus comment state. */
  248. $this->state = 'bogusComment';
  249. } else {
  250. /* Anything else
  251. Parse error. Emit a U+003C LESS-THAN SIGN character token and
  252. reconsume the current input character in the data state. */
  253. $this->emitToken(array(
  254. 'type' => self::CHARACTR,
  255. 'data' => '<'
  256. ));
  257. $this->char--;
  258. $this->state = 'data';
  259. }
  260. break;
  261. }
  262. }
  263. private function closeTagOpenState() {
  264. $next_node = strtolower($this->characters('A-Za-z', $this->char + 1));
  265. $the_same = count($this->tree->stack) > 0 && $next_node === end($this->tree->stack)->nodeName;
  266. if(($this->content_model === self::RCDATA || $this->content_model === self::CDATA) &&
  267. (!$the_same || ($the_same && (!preg_match('/[\t\n\x0b\x0c >\/]/',
  268. $this->character($this->char + 1 + strlen($next_node))) || $this->EOF === $this->char)))) {
  269. /* If the content model flag is set to the RCDATA or CDATA states then
  270. examine the next few characters. If they do not match the tag name of
  271. the last start tag token emitted (case insensitively), or if they do but
  272. they are not immediately followed by one of the following characters:
  273. * U+0009 CHARACTER TABULATION
  274. * U+000A LINE FEED (LF)
  275. * U+000B LINE TABULATION
  276. * U+000C FORM FEED (FF)
  277. * U+0020 SPACE
  278. * U+003E GREATER-THAN SIGN (>)
  279. * U+002F SOLIDUS (/)
  280. * EOF
  281. ...then there is a parse error. Emit a U+003C LESS-THAN SIGN character
  282. token, a U+002F SOLIDUS character token, and switch to the data state
  283. to process the next input character. */
  284. $this->emitToken(array(
  285. 'type' => self::CHARACTR,
  286. 'data' => '</'
  287. ));
  288. $this->state = 'data';
  289. } else {
  290. /* Otherwise, if the content model flag is set to the PCDATA state,
  291. or if the next few characters do match that tag name, consume the
  292. next input character: */
  293. $this->char++;
  294. $char = $this->char();
  295. if(preg_match('/^[A-Za-z]$/', $char)) {
  296. /* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
  297. Create a new end tag token, set its tag name to the lowercase version
  298. of the input character (add 0x0020 to the character's code point), then
  299. switch to the tag name state. (Don't emit the token yet; further details
  300. will be filled in before it is emitted.) */
  301. $this->token = array(
  302. 'name' => strtolower($char),
  303. 'type' => self::ENDTAG
  304. );
  305. $this->state = 'tagName';
  306. } elseif($char === '>') {
  307. /* U+003E GREATER-THAN SIGN (>)
  308. Parse error. Switch to the data state. */
  309. $this->state = 'data';
  310. } elseif($this->char === $this->EOF) {
  311. /* EOF
  312. Parse error. Emit a U+003C LESS-THAN SIGN character token and a U+002F
  313. SOLIDUS character token. Reconsume the EOF character in the data state. */
  314. $this->emitToken(array(
  315. 'type' => self::CHARACTR,
  316. 'data' => '</'
  317. ));
  318. $this->char--;
  319. $this->state = 'data';
  320. } else {
  321. /* Parse error. Switch to the bogus comment state. */
  322. $this->state = 'bogusComment';
  323. }
  324. }
  325. }
  326. private function tagNameState() {
  327. // Consume the next input character:
  328. $this->char++;
  329. $char = $this->character($this->char);
  330. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  331. /* U+0009 CHARACTER TABULATION
  332. U+000A LINE FEED (LF)
  333. U+000B LINE TABULATION
  334. U+000C FORM FEED (FF)
  335. U+0020 SPACE
  336. Switch to the before attribute name state. */
  337. $this->state = 'beforeAttributeName';
  338. } elseif($char === '>') {
  339. /* U+003E GREATER-THAN SIGN (>)
  340. Emit the current tag token. Switch to the data state. */
  341. $this->emitToken($this->token);
  342. $this->state = 'data';
  343. } elseif($this->char === $this->EOF) {
  344. /* EOF
  345. Parse error. Emit the current tag token. Reconsume the EOF
  346. character in the data state. */
  347. $this->emitToken($this->token);
  348. $this->char--;
  349. $this->state = 'data';
  350. } elseif($char === '/') {
  351. /* U+002F SOLIDUS (/)
  352. Parse error unless this is a permitted slash. Switch to the before
  353. attribute name state. */
  354. $this->state = 'beforeAttributeName';
  355. } else {
  356. /* Anything else
  357. Append the current input character to the current tag token's tag name.
  358. Stay in the tag name state. */
  359. $this->token['name'] .= strtolower($char);
  360. $this->state = 'tagName';
  361. }
  362. }
  363. private function beforeAttributeNameState() {
  364. // Consume the next input character:
  365. $this->char++;
  366. $char = $this->character($this->char);
  367. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  368. /* U+0009 CHARACTER TABULATION
  369. U+000A LINE FEED (LF)
  370. U+000B LINE TABULATION
  371. U+000C FORM FEED (FF)
  372. U+0020 SPACE
  373. Stay in the before attribute name state. */
  374. $this->state = 'beforeAttributeName';
  375. } elseif($char === '>') {
  376. /* U+003E GREATER-THAN SIGN (>)
  377. Emit the current tag token. Switch to the data state. */
  378. $this->emitToken($this->token);
  379. $this->state = 'data';
  380. } elseif($char === '/') {
  381. /* U+002F SOLIDUS (/)
  382. Parse error unless this is a permitted slash. Stay in the before
  383. attribute name state. */
  384. $this->state = 'beforeAttributeName';
  385. } elseif($this->char === $this->EOF) {
  386. /* EOF
  387. Parse error. Emit the current tag token. Reconsume the EOF
  388. character in the data state. */
  389. $this->emitToken($this->token);
  390. $this->char--;
  391. $this->state = 'data';
  392. } else {
  393. /* Anything else
  394. Start a new attribute in the current tag token. Set that attribute's
  395. name to the current input character, and its value to the empty string.
  396. Switch to the attribute name state. */
  397. $this->token['attr'][] = array(
  398. 'name' => strtolower($char),
  399. 'value' => null
  400. );
  401. $this->state = 'attributeName';
  402. }
  403. }
  404. private function attributeNameState() {
  405. // Consume the next input character:
  406. $this->char++;
  407. $char = $this->character($this->char);
  408. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  409. /* U+0009 CHARACTER TABULATION
  410. U+000A LINE FEED (LF)
  411. U+000B LINE TABULATION
  412. U+000C FORM FEED (FF)
  413. U+0020 SPACE
  414. Stay in the before attribute name state. */
  415. $this->state = 'afterAttributeName';
  416. } elseif($char === '=') {
  417. /* U+003D EQUALS SIGN (=)
  418. Switch to the before attribute value state. */
  419. $this->state = 'beforeAttributeValue';
  420. } elseif($char === '>') {
  421. /* U+003E GREATER-THAN SIGN (>)
  422. Emit the current tag token. Switch to the data state. */
  423. $this->emitToken($this->token);
  424. $this->state = 'data';
  425. } elseif($char === '/' && $this->character($this->char + 1) !== '>') {
  426. /* U+002F SOLIDUS (/)
  427. Parse error unless this is a permitted slash. Switch to the before
  428. attribute name state. */
  429. $this->state = 'beforeAttributeName';
  430. } elseif($this->char === $this->EOF) {
  431. /* EOF
  432. Parse error. Emit the current tag token. Reconsume the EOF
  433. character in the data state. */
  434. $this->emitToken($this->token);
  435. $this->char--;
  436. $this->state = 'data';
  437. } else {
  438. /* Anything else
  439. Append the current input character to the current attribute's name.
  440. Stay in the attribute name state. */
  441. $last = count($this->token['attr']) - 1;
  442. $this->token['attr'][$last]['name'] .= strtolower($char);
  443. $this->state = 'attributeName';
  444. }
  445. }
  446. private function afterAttributeNameState() {
  447. // Consume the next input character:
  448. $this->char++;
  449. $char = $this->character($this->char);
  450. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  451. /* U+0009 CHARACTER TABULATION
  452. U+000A LINE FEED (LF)
  453. U+000B LINE TABULATION
  454. U+000C FORM FEED (FF)
  455. U+0020 SPACE
  456. Stay in the after attribute name state. */
  457. $this->state = 'afterAttributeName';
  458. } elseif($char === '=') {
  459. /* U+003D EQUALS SIGN (=)
  460. Switch to the before attribute value state. */
  461. $this->state = 'beforeAttributeValue';
  462. } elseif($char === '>') {
  463. /* U+003E GREATER-THAN SIGN (>)
  464. Emit the current tag token. Switch to the data state. */
  465. $this->emitToken($this->token);
  466. $this->state = 'data';
  467. } elseif($char === '/' && $this->character($this->char + 1) !== '>') {
  468. /* U+002F SOLIDUS (/)
  469. Parse error unless this is a permitted slash. Switch to the
  470. before attribute name state. */
  471. $this->state = 'beforeAttributeName';
  472. } elseif($this->char === $this->EOF) {
  473. /* EOF
  474. Parse error. Emit the current tag token. Reconsume the EOF
  475. character in the data state. */
  476. $this->emitToken($this->token);
  477. $this->char--;
  478. $this->state = 'data';
  479. } else {
  480. /* Anything else
  481. Start a new attribute in the current tag token. Set that attribute's
  482. name to the current input character, and its value to the empty string.
  483. Switch to the attribute name state. */
  484. $this->token['attr'][] = array(
  485. 'name' => strtolower($char),
  486. 'value' => null
  487. );
  488. $this->state = 'attributeName';
  489. }
  490. }
  491. private function beforeAttributeValueState() {
  492. // Consume the next input character:
  493. $this->char++;
  494. $char = $this->character($this->char);
  495. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  496. /* U+0009 CHARACTER TABULATION
  497. U+000A LINE FEED (LF)
  498. U+000B LINE TABULATION
  499. U+000C FORM FEED (FF)
  500. U+0020 SPACE
  501. Stay in the before attribute value state. */
  502. $this->state = 'beforeAttributeValue';
  503. } elseif($char === '"') {
  504. /* U+0022 QUOTATION MARK (")
  505. Switch to the attribute value (double-quoted) state. */
  506. $this->state = 'attributeValueDoubleQuoted';
  507. } elseif($char === '&') {
  508. /* U+0026 AMPERSAND (&)
  509. Switch to the attribute value (unquoted) state and reconsume
  510. this input character. */
  511. $this->char--;
  512. $this->state = 'attributeValueUnquoted';
  513. } elseif($char === '\'') {
  514. /* U+0027 APOSTROPHE (')
  515. Switch to the attribute value (single-quoted) state. */
  516. $this->state = 'attributeValueSingleQuoted';
  517. } elseif($char === '>') {
  518. /* U+003E GREATER-THAN SIGN (>)
  519. Emit the current tag token. Switch to the data state. */
  520. $this->emitToken($this->token);
  521. $this->state = 'data';
  522. } else {
  523. /* Anything else
  524. Append the current input character to the current attribute's value.
  525. Switch to the attribute value (unquoted) state. */
  526. $last = count($this->token['attr']) - 1;
  527. $this->token['attr'][$last]['value'] .= $char;
  528. $this->state = 'attributeValueUnquoted';
  529. }
  530. }
  531. private function attributeValueDoubleQuotedState() {
  532. // Consume the next input character:
  533. $this->char++;
  534. $char = $this->character($this->char);
  535. if($char === '"') {
  536. /* U+0022 QUOTATION MARK (")
  537. Switch to the before attribute name state. */
  538. $this->state = 'beforeAttributeName';
  539. } elseif($char === '&') {
  540. /* U+0026 AMPERSAND (&)
  541. Switch to the entity in attribute value state. */
  542. $this->entityInAttributeValueState('double');
  543. } elseif($this->char === $this->EOF) {
  544. /* EOF
  545. Parse error. Emit the current tag token. Reconsume the character
  546. in the data state. */
  547. $this->emitToken($this->token);
  548. $this->char--;
  549. $this->state = 'data';
  550. } else {
  551. /* Anything else
  552. Append the current input character to the current attribute's value.
  553. Stay in the attribute value (double-quoted) state. */
  554. $last = count($this->token['attr']) - 1;
  555. $this->token['attr'][$last]['value'] .= $char;
  556. $this->state = 'attributeValueDoubleQuoted';
  557. }
  558. }
  559. private function attributeValueSingleQuotedState() {
  560. // Consume the next input character:
  561. $this->char++;
  562. $char = $this->character($this->char);
  563. if($char === '\'') {
  564. /* U+0022 QUOTATION MARK (')
  565. Switch to the before attribute name state. */
  566. $this->state = 'beforeAttributeName';
  567. } elseif($char === '&') {
  568. /* U+0026 AMPERSAND (&)
  569. Switch to the entity in attribute value state. */
  570. $this->entityInAttributeValueState('single');
  571. } elseif($this->char === $this->EOF) {
  572. /* EOF
  573. Parse error. Emit the current tag token. Reconsume the character
  574. in the data state. */
  575. $this->emitToken($this->token);
  576. $this->char--;
  577. $this->state = 'data';
  578. } else {
  579. /* Anything else
  580. Append the current input character to the current attribute's value.
  581. Stay in the attribute value (single-quoted) state. */
  582. $last = count($this->token['attr']) - 1;
  583. $this->token['attr'][$last]['value'] .= $char;
  584. $this->state = 'attributeValueSingleQuoted';
  585. }
  586. }
  587. private function attributeValueUnquotedState() {
  588. // Consume the next input character:
  589. $this->char++;
  590. $char = $this->character($this->char);
  591. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  592. /* U+0009 CHARACTER TABULATION
  593. U+000A LINE FEED (LF)
  594. U+000B LINE TABULATION
  595. U+000C FORM FEED (FF)
  596. U+0020 SPACE
  597. Switch to the before attribute name state. */
  598. $this->state = 'beforeAttributeName';
  599. } elseif($char === '&') {
  600. /* U+0026 AMPERSAND (&)
  601. Switch to the entity in attribute value state. */
  602. $this->entityInAttributeValueState('non');
  603. } elseif($char === '>') {
  604. /* U+003E GREATER-THAN SIGN (>)
  605. Emit the current tag token. Switch to the data state. */
  606. $this->emitToken($this->token);
  607. $this->state = 'data';
  608. } else {
  609. /* Anything else
  610. Append the current input character to the current attribute's value.
  611. Stay in the attribute value (unquoted) state. */
  612. $last = count($this->token['attr']) - 1;
  613. $this->token['attr'][$last]['value'] .= $char;
  614. $this->state = 'attributeValueUnquoted';
  615. }
  616. }
  617. private function entityInAttributeValueState() {
  618. // Attempt to consume an entity.
  619. $entity = $this->entity();
  620. // If nothing is returned, append a U+0026 AMPERSAND character to the
  621. // current attribute's value. Otherwise, emit the character token that
  622. // was returned.
  623. $char = (!$entity)
  624. ? '&'
  625. : $entity;
  626. $this->emitToken($char);
  627. }
  628. private function bogusCommentState() {
  629. /* Consume every character up to the first U+003E GREATER-THAN SIGN
  630. character (>) or the end of the file (EOF), whichever comes first. Emit
  631. a comment token whose data is the concatenation of all the characters
  632. starting from and including the character that caused the state machine
  633. to switch into the bogus comment state, up to and including the last
  634. consumed character before the U+003E character, if any, or up to the
  635. end of the file otherwise. (If the comment was started by the end of
  636. the file (EOF), the token is empty.) */
  637. $data = $this->characters('^>', $this->char);
  638. $this->emitToken(array(
  639. 'data' => $data,
  640. 'type' => self::COMMENT
  641. ));
  642. $this->char += strlen($data);
  643. /* Switch to the data state. */
  644. $this->state = 'data';
  645. /* If the end of the file was reached, reconsume the EOF character. */
  646. if($this->char === $this->EOF) {
  647. $this->char = $this->EOF - 1;
  648. }
  649. }
  650. private function markupDeclarationOpenState() {
  651. /* If the next two characters are both U+002D HYPHEN-MINUS (-)
  652. characters, consume those two characters, create a comment token whose
  653. data is the empty string, and switch to the comment state. */
  654. if($this->character($this->char + 1, 2) === '--') {
  655. $this->char += 2;
  656. $this->state = 'comment';
  657. $this->token = array(
  658. 'data' => null,
  659. 'type' => self::COMMENT
  660. );
  661. /* Otherwise if the next seven chacacters are a case-insensitive match
  662. for the word "DOCTYPE", then consume those characters and switch to the
  663. DOCTYPE state. */
  664. } elseif(strtolower($this->character($this->char + 1, 7)) === 'doctype') {
  665. $this->char += 7;
  666. $this->state = 'doctype';
  667. /* Otherwise, is is a parse error. Switch to the bogus comment state.
  668. The next character that is consumed, if any, is the first character
  669. that will be in the comment. */
  670. } else {
  671. $this->char++;
  672. $this->state = 'bogusComment';
  673. }
  674. }
  675. private function commentState() {
  676. /* Consume the next input character: */
  677. $this->char++;
  678. $char = $this->char();
  679. /* U+002D HYPHEN-MINUS (-) */
  680. if($char === '-') {
  681. /* Switch to the comment dash state */
  682. $this->state = 'commentDash';
  683. /* EOF */
  684. } elseif($this->char === $this->EOF) {
  685. /* Parse error. Emit the comment token. Reconsume the EOF character
  686. in the data state. */
  687. $this->emitToken($this->token);
  688. $this->char--;
  689. $this->state = 'data';
  690. /* Anything else */
  691. } else {
  692. /* Append the input character to the comment token's data. Stay in
  693. the comment state. */
  694. $this->token['data'] .= $char;
  695. }
  696. }
  697. private function commentDashState() {
  698. /* Consume the next input character: */
  699. $this->char++;
  700. $char = $this->char();
  701. /* U+002D HYPHEN-MINUS (-) */
  702. if($char === '-') {
  703. /* Switch to the comment end state */
  704. $this->state = 'commentEnd';
  705. /* EOF */
  706. } elseif($this->char === $this->EOF) {
  707. /* Parse error. Emit the comment token. Reconsume the EOF character
  708. in the data state. */
  709. $this->emitToken($this->token);
  710. $this->char--;
  711. $this->state = 'data';
  712. /* Anything else */
  713. } else {
  714. /* Append a U+002D HYPHEN-MINUS (-) character and the input
  715. character to the comment token's data. Switch to the comment state. */
  716. $this->token['data'] .= '-'.$char;
  717. $this->state = 'comment';
  718. }
  719. }
  720. private function commentEndState() {
  721. /* Consume the next input character: */
  722. $this->char++;
  723. $char = $this->char();
  724. if($char === '>') {
  725. $this->emitToken($this->token);
  726. $this->state = 'data';
  727. } elseif($char === '-') {
  728. $this->token['data'] .= '-';
  729. } elseif($this->char === $this->EOF) {
  730. $this->emitToken($this->token);
  731. $this->char--;
  732. $this->state = 'data';
  733. } else {
  734. $this->token['data'] .= '--'.$char;
  735. $this->state = 'comment';
  736. }
  737. }
  738. private function doctypeState() {
  739. /* Consume the next input character: */
  740. $this->char++;
  741. $char = $this->char();
  742. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  743. $this->state = 'beforeDoctypeName';
  744. } else {
  745. $this->char--;
  746. $this->state = 'beforeDoctypeName';
  747. }
  748. }
  749. private function beforeDoctypeNameState() {
  750. /* Consume the next input character: */
  751. $this->char++;
  752. $char = $this->char();
  753. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  754. // Stay in the before DOCTYPE name state.
  755. } elseif(preg_match('/^[a-z]$/', $char)) {
  756. $this->token = array(
  757. 'name' => strtoupper($char),
  758. 'type' => self::DOCTYPE,
  759. 'error' => true
  760. );
  761. $this->state = 'doctypeName';
  762. } elseif($char === '>') {
  763. $this->emitToken(array(
  764. 'name' => null,
  765. 'type' => self::DOCTYPE,
  766. 'error' => true
  767. ));
  768. $this->state = 'data';
  769. } elseif($this->char === $this->EOF) {
  770. $this->emitToken(array(
  771. 'name' => null,
  772. 'type' => self::DOCTYPE,
  773. 'error' => true
  774. ));
  775. $this->char--;
  776. $this->state = 'data';
  777. } else {
  778. $this->token = array(
  779. 'name' => $char,
  780. 'type' => self::DOCTYPE,
  781. 'error' => true
  782. );
  783. $this->state = 'doctypeName';
  784. }
  785. }
  786. private function doctypeNameState() {
  787. /* Consume the next input character: */
  788. $this->char++;
  789. $char = $this->char();
  790. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  791. $this->state = 'AfterDoctypeName';
  792. } elseif($char === '>') {
  793. $this->emitToken($this->token);
  794. $this->state = 'data';
  795. } elseif(preg_match('/^[a-z]$/', $char)) {
  796. $this->token['name'] .= strtoupper($char);
  797. } elseif($this->char === $this->EOF) {
  798. $this->emitToken($this->token);
  799. $this->char--;
  800. $this->state = 'data';
  801. } else {
  802. $this->token['name'] .= $char;
  803. }
  804. $this->token['error'] = ($this->token['name'] === 'HTML')
  805. ? false
  806. : true;
  807. }
  808. private function afterDoctypeNameState() {
  809. /* Consume the next input character: */
  810. $this->char++;
  811. $char = $this->char();
  812. if(preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  813. // Stay in the DOCTYPE name state.
  814. } elseif($char === '>') {
  815. $this->emitToken($this->token);
  816. $this->state = 'data';
  817. } elseif($this->char === $this->EOF) {
  818. $this->emitToken($this->token);
  819. $this->char--;
  820. $this->state = 'data';
  821. } else {
  822. $this->token['error'] = true;
  823. $this->state = 'bogusDoctype';
  824. }
  825. }
  826. private function bogusDoctypeState() {
  827. /* Consume the next input character: */
  828. $this->char++;
  829. $char = $this->char();
  830. if($char === '>') {
  831. $this->emitToken($this->token);
  832. $this->state = 'data';
  833. } elseif($this->char === $this->EOF) {
  834. $this->emitToken($this->token);
  835. $this->char--;
  836. $this->state = 'data';
  837. } else {
  838. // Stay in the bogus DOCTYPE state.
  839. }
  840. }
  841. private function entity() {
  842. $start = $this->char;
  843. // This section defines how to consume an entity. This definition is
  844. // used when parsing entities in text and in attributes.
  845. // The behaviour depends on the identity of the next character (the
  846. // one immediately after the U+0026 AMPERSAND character):
  847. switch($this->character($this->char + 1)) {
  848. // U+0023 NUMBER SIGN (#)
  849. case '#':
  850. // The behaviour further depends on the character after the
  851. // U+0023 NUMBER SIGN:
  852. switch($this->character($this->char + 1)) {
  853. // U+0078 LATIN SMALL LETTER X
  854. // U+0058 LATIN CAPITAL LETTER X
  855. case 'x':
  856. case 'X':
  857. // Follow the steps below, but using the range of
  858. // characters U+0030 DIGIT ZERO through to U+0039 DIGIT
  859. // NINE, U+0061 LATIN SMALL LETTER A through to U+0066
  860. // LATIN SMALL LETTER F, and U+0041 LATIN CAPITAL LETTER
  861. // A, through to U+0046 LATIN CAPITAL LETTER F (in other
  862. // words, 0-9, A-F, a-f).
  863. $char = 1;
  864. $char_class = '0-9A-Fa-f';
  865. break;
  866. // Anything else
  867. default:
  868. // Follow the steps below, but using the range of
  869. // characters U+0030 DIGIT ZERO through to U+0039 DIGIT
  870. // NINE (i.e. just 0-9).
  871. $char = 0;
  872. $char_class = '0-9';
  873. break;
  874. }
  875. // Consume as many characters as match the range of characters
  876. // given above.
  877. $this->char++;
  878. $e_name = $this->characters($char_class, $this->char + $char + 1);
  879. $entity = $this->character($start, $this->char);
  880. $cond = strlen($e_name) > 0;
  881. // The rest of the parsing happens bellow.
  882. break;
  883. // Anything else
  884. default:
  885. // Consume the maximum number of characters possible, with the
  886. // consumed characters case-sensitively matching one of the
  887. // identifiers in the first column of the entities table.
  888. $e_name = $this->characters('0-9A-Za-z;', $this->char + 1);
  889. $len = strlen($e_name);
  890. for($c = 1; $c <= $len; $c++) {
  891. $id = substr($e_name, 0, $c);
  892. $this->char++;
  893. if(in_array($id, $this->entities)) {
  894. $entity = $id;
  895. break;
  896. }
  897. }
  898. $cond = isset($entity);
  899. // The rest of the parsing happens bellow.
  900. break;
  901. }
  902. if(!$cond) {
  903. // If no match can be made, then this is a parse error. No
  904. // characters are consumed, and nothing is returned.
  905. $this->char = $start;
  906. return false;
  907. }
  908. // Return a character token for the character corresponding to the
  909. // entity name (as given by the second column of the entities table).
  910. return html_entity_decode('&'.$entity.';', ENT_QUOTES, 'UTF-8');
  911. }
  912. private function emitToken($token) {
  913. $emit = $this->tree->emitToken($token);
  914. if(is_int($emit)) {
  915. $this->content_model = $emit;
  916. } elseif($token['type'] === self::ENDTAG) {
  917. $this->content_model = self::PCDATA;
  918. }
  919. }
  920. private function EOF() {
  921. $this->state = null;
  922. $this->tree->emitToken(array(
  923. 'type' => self::EOF
  924. ));
  925. }
  926. }
  927. class HTML5TreeConstructer {
  928. public $stack = array();
  929. private $phase;
  930. private $mode;
  931. private $dom;
  932. private $foster_parent = null;
  933. private $a_formatting = array();
  934. private $head_pointer = null;
  935. private $form_pointer = null;
  936. private $scoping = array('button','caption','html','marquee','object','table','td','th');
  937. private $formatting = array('a','b','big','em','font','i','nobr','s','small','strike','strong','tt','u');
  938. private $special = array('address','area','base','basefont','bgsound',
  939. 'blockquote','body','br','center','col','colgroup','dd','dir','div','dl',
  940. 'dt','embed','fieldset','form','frame','frameset','h1','h2','h3','h4','h5',
  941. 'h6','head','hr','iframe','image','img','input','isindex','li','link',
  942. 'listing','menu','meta','noembed','noframes','noscript','ol','optgroup',
  943. 'option','p','param','plaintext','pre','script','select','spacer','style',
  944. 'tbody','textarea','tfoot','thead','title','tr','ul','wbr');
  945. // The different phases.
  946. const INIT_PHASE = 0;
  947. const ROOT_PHASE = 1;
  948. const MAIN_PHASE = 2;
  949. const END_PHASE = 3;
  950. // The different insertion modes for the main phase.
  951. const BEFOR_HEAD = 0;
  952. const IN_HEAD = 1;
  953. const AFTER_HEAD = 2;
  954. const IN_BODY = 3;
  955. const IN_TABLE = 4;
  956. const IN_CAPTION = 5;
  957. const IN_CGROUP = 6;
  958. const IN_TBODY = 7;
  959. const IN_ROW = 8;
  960. const IN_CELL = 9;
  961. const IN_SELECT = 10;
  962. const AFTER_BODY = 11;
  963. const IN_FRAME = 12;
  964. const AFTR_FRAME = 13;
  965. // The different types of elements.
  966. const SPECIAL = 0;
  967. const SCOPING = 1;
  968. const FORMATTING = 2;
  969. const PHRASING = 3;
  970. const MARKER = 0;
  971. public function __construct() {
  972. $this->phase = self::INIT_PHASE;
  973. $this->mode = self::BEFOR_HEAD;
  974. $this->dom = new DOMDocument;
  975. $this->dom->encoding = 'UTF-8';
  976. $this->dom->preserveWhiteSpace = true;
  977. $this->dom->substituteEntities = true;
  978. $this->dom->strictErrorChecking = false;
  979. }
  980. // Process tag tokens
  981. public function emitToken($token) {
  982. switch($this->phase) {
  983. case self::INIT_PHASE: return $this->initPhase($token); break;
  984. case self::ROOT_PHASE: return $this->rootElementPhase($token); break;
  985. case self::MAIN_PHASE: return $this->mainPhase($token); break;
  986. case self::END_PHASE : return $this->trailingEndPhase($token); break;
  987. }
  988. }
  989. private function initPhase($token) {
  990. /* Initially, the tree construction stage must handle each token
  991. emitted from the tokenisation stage as follows: */
  992. /* A DOCTYPE token that is marked as being in error
  993. A comment token
  994. A start tag token
  995. An end tag token
  996. A character token that is not one of one of U+0009 CHARACTER TABULATION,
  997. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  998. or U+0020 SPACE
  999. An end-of-file token */
  1000. if((isset($token['error']) && $token['error']) ||
  1001. $token['type'] === HTML5::COMMENT ||
  1002. $token['type'] === HTML5::STARTTAG ||
  1003. $token['type'] === HTML5::ENDTAG ||
  1004. $token['type'] === HTML5::EOF ||
  1005. ($token['type'] === HTML5::CHARACTR && isset($token['data']) &&
  1006. !preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data']))) {
  1007. /* This specification does not define how to handle this case. In
  1008. particular, user agents may ignore the entirety of this specification
  1009. altogether for such documents, and instead invoke special parse modes
  1010. with a greater emphasis on backwards compatibility. */
  1011. $this->phase = self::ROOT_PHASE;
  1012. return $this->rootElementPhase($token);
  1013. /* A DOCTYPE token marked as being correct */
  1014. } elseif(isset($token['error']) && !$token['error']) {
  1015. /* Append a DocumentType node to the Document node, with the name
  1016. attribute set to the name given in the DOCTYPE token (which will be
  1017. "HTML"), and the other attributes specific to DocumentType objects
  1018. set to null, empty lists, or the empty string as appropriate. */
  1019. $doctype = new DOMDocumentType(null, null, 'HTML');
  1020. /* Then, switch to the root element phase of the tree construction
  1021. stage. */
  1022. $this->phase = self::ROOT_PHASE;
  1023. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1024. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1025. or U+0020 SPACE */
  1026. } elseif(isset($token['data']) && preg_match('/^[\t\n\x0b\x0c ]+$/',
  1027. $token['data'])) {
  1028. /* Append that character to the Document node. */
  1029. $text = $this->dom->createTextNode($token['data']);
  1030. $this->dom->appendChild($text);
  1031. }
  1032. }
  1033. private function rootElementPhase($token) {
  1034. /* After the initial phase, as each token is emitted from the tokenisation
  1035. stage, it must be processed as described in this section. */
  1036. /* A DOCTYPE token */
  1037. if($token['type'] === HTML5::DOCTYPE) {
  1038. // Parse error. Ignore the token.
  1039. /* A comment token */
  1040. } elseif($token['type'] === HTML5::COMMENT) {
  1041. /* Append a Comment node to the Document object with the data
  1042. attribute set to the data given in the comment token. */
  1043. $comment = $this->dom->createComment($token['data']);
  1044. $this->dom->appendChild($comment);
  1045. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1046. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1047. or U+0020 SPACE */
  1048. } elseif($token['type'] === HTML5::CHARACTR &&
  1049. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) {
  1050. /* Append that character to the Document node. */
  1051. $text = $this->dom->createTextNode($token['data']);
  1052. $this->dom->appendChild($text);
  1053. /* A character token that is not one of U+0009 CHARACTER TABULATION,
  1054. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED
  1055. (FF), or U+0020 SPACE
  1056. A start tag token
  1057. An end tag token
  1058. An end-of-file token */
  1059. } elseif(($token['type'] === HTML5::CHARACTR &&
  1060. !preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) ||
  1061. $token['type'] === HTML5::STARTTAG ||
  1062. $token['type'] === HTML5::ENDTAG ||
  1063. $token['type'] === HTML5::EOF) {
  1064. /* Create an HTMLElement node with the tag name html, in the HTML
  1065. namespace. Append it to the Document object. Switch to the main
  1066. phase and reprocess the current token. */
  1067. $html = $this->dom->createElement('html');
  1068. $this->dom->appendChild($html);
  1069. $this->stack[] = $html;
  1070. $this->phase = self::MAIN_PHASE;
  1071. return $this->mainPhase($token);
  1072. }
  1073. }
  1074. private function mainPhase($token) {
  1075. /* Tokens in the main phase must be handled as follows: */
  1076. /* A DOCTYPE token */
  1077. if($token['type'] === HTML5::DOCTYPE) {
  1078. // Parse error. Ignore the token.
  1079. /* A start tag token with the tag name "html" */
  1080. } elseif($token['type'] === HTML5::STARTTAG && $token['name'] === 'html') {
  1081. /* If this start tag token was not the first start tag token, then
  1082. it is a parse error. */
  1083. /* For each attribute on the token, check to see if the attribute
  1084. is already present on the top element of the stack of open elements.
  1085. If it is not, add the attribute and its corresponding value to that
  1086. element. */
  1087. foreach($token['attr'] as $attr) {
  1088. if(!$this->stack[0]->hasAttribute($attr['name'])) {
  1089. $this->stack[0]->setAttribute($attr['name'], $attr['value']);
  1090. }
  1091. }
  1092. /* An end-of-file token */
  1093. } elseif($token['type'] === HTML5::EOF) {
  1094. /* Generate implied end tags. */
  1095. $this->generateImpliedEndTags();
  1096. /* Anything else. */
  1097. } else {
  1098. /* Depends on the insertion mode: */
  1099. switch($this->mode) {
  1100. case self::BEFOR_HEAD: return $this->beforeHead($token); break;
  1101. case self::IN_HEAD: return $this->inHead($token); break;
  1102. case self::AFTER_HEAD: return $this->afterHead($token); break;
  1103. case self::IN_BODY: return $this->inBody($token); break;
  1104. case self::IN_TABLE: return $this->inTable($token); break;
  1105. case self::IN_CAPTION: return $this->inCaption($token); break;
  1106. case self::I

Large files files are truncated, but you can click here to view the full file