HTML5_Tokenizer

The WooCommerce PDF Invoices & Packing Slips HTML5 Tokenizer class.

Defined (1)

The class is defined in the following location(s).

/lib/dompdf/lib/html5lib/Tokenizer.php  
  1. class HTML5_Tokenizer { 
  2. /** 
  3. * Points to an InputStream object. 
  4. */ 
  5. protected $stream; 
  6.  
  7. /** 
  8. * Tree builder that the tokenizer emits token to. 
  9. */ 
  10. private $tree; 
  11.  
  12. /** 
  13. * Current content model we are parsing as. 
  14. */ 
  15. protected $content_model; 
  16.  
  17. /** 
  18. * Current token that is being built, but not yet emitted. Also 
  19. * is the last token emitted, if applicable. 
  20. */ 
  21. protected $token; 
  22.  
  23. // These are constants describing the content model 
  24. const PCDATA = 0; 
  25. const RCDATA = 1; 
  26. const CDATA = 2; 
  27. const PLAINTEXT = 3; 
  28.  
  29. // These are constants describing tokens 
  30. // XXX should probably be moved somewhere else, probably the 
  31. // HTML5 class. 
  32. const DOCTYPE = 0; 
  33. const STARTTAG = 1; 
  34. const ENDTAG = 2; 
  35. const COMMENT = 3; 
  36. const CHARACTER = 4; 
  37. const SPACECHARACTER = 5; 
  38. const EOF = 6; 
  39. const PARSEERROR = 7; 
  40.  
  41. // These are constants representing bunches of characters. 
  42. const ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; 
  43. const UPPER_ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
  44. const LOWER_ALPHA = 'abcdefghijklmnopqrstuvwxyz'; 
  45. const DIGIT = '0123456789'; 
  46. const HEX = '0123456789ABCDEFabcdef'; 
  47. const WHITESPACE = "\t\n\x0c "; 
  48.  
  49. /** 
  50. * @param $data Data to parse 
  51. */ 
  52. public function __construct($data, $builder = null) { 
  53. $this->stream = new HTML5_InputStream($data); 
  54. if (!$builder) $this->tree = new HTML5_TreeBuilder; 
  55. else $this->tree = $builder; 
  56. $this->content_model = self::PCDATA; 
  57.  
  58. public function parseFragment($context = null) { 
  59. $this->tree->setupContext($context); 
  60. if ($this->tree->content_model) { 
  61. $this->content_model = $this->tree->content_model; 
  62. $this->tree->content_model = null; 
  63. $this->parse(); 
  64.  
  65. // XXX maybe convert this into an iterator? regardless, this function 
  66. // and the save function should go into a Parser facade of some sort 
  67. /** 
  68. * Performs the actual parsing of the document. 
  69. */ 
  70. public function parse() { 
  71. // Current state 
  72. $state = 'data'; 
  73. // This is used to avoid having to have look-behind in the data state. 
  74. $lastFourChars = ''; 
  75. /** 
  76. * Escape flag as specified by the HTML5 specification: "used to 
  77. * control the behavior of the tokeniser. It is either true or 
  78. * false, and initially must be set to the false state." 
  79. */ 
  80. $escape = false; 
  81. //echo "\n\n"; 
  82. while($state !== null) { 
  83.  
  84. /**echo $state . ' '; 
  85. switch ($this->content_model) { 
  86. case self::PCDATA: echo 'PCDATA'; break; 
  87. case self::RCDATA: echo 'RCDATA'; break; 
  88. case self::CDATA: echo 'CDATA'; break; 
  89. case self::PLAINTEXT: echo 'PLAINTEXT'; break; 
  90. if ($escape) echo " escape"; 
  91. echo "\n";*/ 
  92.  
  93. switch($state) { 
  94. case 'data': 
  95.  
  96. /** Consume the next input character */ 
  97. $char = $this->stream->char(); 
  98. $lastFourChars .= $char; 
  99. if (strlen($lastFourChars) > 4) $lastFourChars = substr($lastFourChars, -4); 
  100.  
  101. // see below for meaning 
  102. $hyp_cond =  
  103. !$escape && 
  104. $this->content_model === self::RCDATA || 
  105. $this->content_model === self::CDATA 
  106. ); 
  107. $amp_cond = 
  108. !$escape && 
  109. $this->content_model === self::PCDATA || 
  110. $this->content_model === self::RCDATA 
  111. ); 
  112. $lt_cond = 
  113. $this->content_model === self::PCDATA || 
  114. $this->content_model === self::RCDATA || 
  115. $this->content_model === self::CDATA 
  116. ) && 
  117. !$escape 
  118. ); 
  119. $gt_cond =  
  120. $escape && 
  121. $this->content_model === self::RCDATA || 
  122. $this->content_model === self::CDATA 
  123. ); 
  124.  
  125. if($char === '&' && $amp_cond) { 
  126. /** U+0026 AMPERSAND (&) 
  127. When the content model flag is set to one of the PCDATA or RCDATA 
  128. states and the escape flag is false: switch to the 
  129. character reference data state. Otherwise: treat it as per 
  130. the "anything else" entry below. */ 
  131. $state = 'character reference data'; 
  132.  
  133. } elseif( 
  134. $char === '-' && 
  135. $hyp_cond && 
  136. $lastFourChars === '<!--' 
  137. ) { 
  138. /** 
  139. U+002D HYPHEN-MINUS (-) 
  140. If the content model flag is set to either the RCDATA state or 
  141. the CDATA state, and the escape flag is false, and there are at 
  142. least three characters before this one in the input stream, and the 
  143. last four characters in the input stream, including this one, are 
  144. U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+002D HYPHEN-MINUS,  
  145. and U+002D HYPHEN-MINUS ("<!--"), then set the escape flag to true. */ 
  146. $escape = true; 
  147.  
  148. /** In any case, emit the input character as a character token. Stay 
  149. in the data state. */ 
  150. $this->emitToken(array( 
  151. 'type' => self::CHARACTER,  
  152. 'data' => '-' 
  153. )); 
  154. // We do the "any case" part as part of "anything else". 
  155.  
  156. /** U+003C LESS-THAN SIGN (<) */ 
  157. } elseif($char === '<' && $lt_cond) { 
  158. /** When the content model flag is set to the PCDATA state: switch 
  159. to the tag open state. 
  160.   
  161. When the content model flag is set to either the RCDATA state or 
  162. the CDATA state and the escape flag is false: switch to the tag 
  163. open state. 
  164.   
  165. Otherwise: treat it as per the "anything else" entry below. */ 
  166. $state = 'tag open'; 
  167.  
  168. /** U+003E GREATER-THAN SIGN (>) */ 
  169. } elseif( 
  170. $char === '>' && 
  171. $gt_cond && 
  172. substr($lastFourChars, 1) === '-->' 
  173. ) { 
  174. /** If the content model flag is set to either the RCDATA state or 
  175. the CDATA state, and the escape flag is true, and the last three 
  176. characters in the input stream including this one are U+002D 
  177. HYPHEN-MINUS, U+002D HYPHEN-MINUS, U+003E GREATER-THAN SIGN ("-->"),  
  178. set the escape flag to false. */ 
  179. $escape = false; 
  180.  
  181. /** In any case, emit the input character as a character token. 
  182. Stay in the data state. */ 
  183. $this->emitToken(array( 
  184. 'type' => self::CHARACTER,  
  185. 'data' => '>' 
  186. )); 
  187. // We do the "any case" part as part of "anything else". 
  188.  
  189. } elseif($char === false) { 
  190. /** EOF 
  191. Emit an end-of-file token. */ 
  192. $state = null; 
  193. $this->tree->emitToken(array( 
  194. 'type' => self::EOF 
  195. )); 
  196.  
  197. } elseif($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  198. // Directly after emitting a token you switch back to the "data 
  199. // state". At that point spaceCharacters are important so they are 
  200. // emitted separately. 
  201. $chars = $this->stream->charsWhile(self::WHITESPACE); 
  202. $this->emitToken(array( 
  203. 'type' => self::SPACECHARACTER,  
  204. 'data' => $char . $chars 
  205. )); 
  206. $lastFourChars .= $chars; 
  207. if (strlen($lastFourChars) > 4) $lastFourChars = substr($lastFourChars, -4); 
  208.  
  209. } else { 
  210. /** Anything else 
  211. THIS IS AN OPTIMIZATION: Get as many character that 
  212. otherwise would also be treated as a character token and emit it 
  213. as a single character token. Stay in the data state. */ 
  214.  
  215. $mask = ''; 
  216. if ($hyp_cond) $mask .= '-'; 
  217. if ($amp_cond) $mask .= '&'; 
  218. if ($lt_cond) $mask .= '<'; 
  219. if ($gt_cond) $mask .= '>'; 
  220.  
  221. if ($mask === '') { 
  222. $chars = $this->stream->remainingChars(); 
  223. } else { 
  224. $chars = $this->stream->charsUntil($mask); 
  225.  
  226. $this->emitToken(array( 
  227. 'type' => self::CHARACTER,  
  228. 'data' => $char . $chars 
  229. )); 
  230.  
  231. $lastFourChars .= $chars; 
  232. if (strlen($lastFourChars) > 4) $lastFourChars = substr($lastFourChars, -4); 
  233.  
  234. $state = 'data'; 
  235. break; 
  236.  
  237. case 'character reference data': 
  238. /** (This cannot happen if the content model flag 
  239. is set to the CDATA state.) */ 
  240.  
  241. /** Attempt to consume a character reference, with no 
  242. additional allowed character. */ 
  243. $entity = $this->consumeCharacterReference(); 
  244.  
  245. /** If nothing is returned, emit a U+0026 AMPERSAND 
  246. character token. Otherwise, emit the character token that 
  247. was returned. */ 
  248. // This is all done when consuming the character reference. 
  249. $this->emitToken(array( 
  250. 'type' => self::CHARACTER,  
  251. 'data' => $entity 
  252. )); 
  253.  
  254. /** Finally, switch to the data state. */ 
  255. $state = 'data'; 
  256. break; 
  257.  
  258. case 'tag open': 
  259. $char = $this->stream->char(); 
  260.  
  261. switch($this->content_model) { 
  262. case self::RCDATA: 
  263. case self::CDATA: 
  264. /** Consume the next input character. If it is a 
  265. U+002F SOLIDUS (/) character, switch to the close 
  266. tag open state. Otherwise, emit a U+003C LESS-THAN 
  267. SIGN character token and reconsume the current input 
  268. character in the data state. */ 
  269. // We consumed above. 
  270.  
  271. if($char === '/') { 
  272. $state = 'close tag open'; 
  273.  
  274. } else { 
  275. $this->emitToken(array( 
  276. 'type' => self::CHARACTER,  
  277. 'data' => '<' 
  278. )); 
  279.  
  280. $this->stream->unget(); 
  281.  
  282. $state = 'data'; 
  283. break; 
  284.  
  285. case self::PCDATA: 
  286. /** If the content model flag is set to the PCDATA state 
  287. Consume the next input character: */ 
  288. // We consumed above. 
  289.  
  290. if($char === '!') { 
  291. /** U+0021 EXCLAMATION MARK (!) 
  292. Switch to the markup declaration open state. */ 
  293. $state = 'markup declaration open'; 
  294.  
  295. } elseif($char === '/') { 
  296. /** U+002F SOLIDUS (/) 
  297. Switch to the close tag open state. */ 
  298. $state = 'close tag open'; 
  299.  
  300. } elseif('A' <= $char && $char <= 'Z') { 
  301. /** U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z 
  302. Create a new start tag token, set its tag name to the lowercase 
  303. version of the input character (add 0x0020 to the character's code 
  304. point), then switch to the tag name state. (Don't emit the token 
  305. yet; further details will be filled in before it is emitted.) */ 
  306. $this->token = array( 
  307. 'name' => strtolower($char),  
  308. 'type' => self::STARTTAG,  
  309. 'attr' => array() 
  310. ); 
  311.  
  312. $state = 'tag name'; 
  313.  
  314. } elseif('a' <= $char && $char <= 'z') { 
  315. /** U+0061 LATIN SMALL LETTER A through to U+007A LATIN SMALL LETTER Z 
  316. Create a new start tag token, set its tag name to the input 
  317. character, then switch to the tag name state. (Don't emit 
  318. the token yet; further details will be filled in before it 
  319. is emitted.) */ 
  320. $this->token = array( 
  321. 'name' => $char,  
  322. 'type' => self::STARTTAG,  
  323. 'attr' => array() 
  324. ); 
  325.  
  326. $state = 'tag name'; 
  327.  
  328. } elseif($char === '>') { 
  329. /** U+003E GREATER-THAN SIGN (>) 
  330. Parse error. Emit a U+003C LESS-THAN SIGN character token and a 
  331. U+003E GREATER-THAN SIGN character token. Switch to the data state. */ 
  332. $this->emitToken(array( 
  333. 'type' => self::PARSEERROR,  
  334. 'data' => 'expected-tag-name-but-got-right-bracket' 
  335. )); 
  336. $this->emitToken(array( 
  337. 'type' => self::CHARACTER,  
  338. 'data' => '<>' 
  339. )); 
  340.  
  341. $state = 'data'; 
  342.  
  343. } elseif($char === '?') { 
  344. /** U+003F QUESTION MARK (?) 
  345. Parse error. Switch to the bogus comment state. */ 
  346. $this->emitToken(array( 
  347. 'type' => self::PARSEERROR,  
  348. 'data' => 'expected-tag-name-but-got-question-mark' 
  349. )); 
  350. $this->token = array( 
  351. 'data' => '?',  
  352. 'type' => self::COMMENT 
  353. ); 
  354. $state = 'bogus comment'; 
  355.  
  356. } else { 
  357. /** Anything else 
  358. Parse error. Emit a U+003C LESS-THAN SIGN character token and 
  359. reconsume the current input character in the data state. */ 
  360. $this->emitToken(array( 
  361. 'type' => self::PARSEERROR,  
  362. 'data' => 'expected-tag-name' 
  363. )); 
  364. $this->emitToken(array( 
  365. 'type' => self::CHARACTER,  
  366. 'data' => '<' 
  367. )); 
  368.  
  369. $state = 'data'; 
  370. $this->stream->unget(); 
  371. break; 
  372. break; 
  373.  
  374. case 'close tag open': 
  375. if ( 
  376. $this->content_model === self::RCDATA || 
  377. $this->content_model === self::CDATA 
  378. ) { 
  379. /** If the content model flag is set to the RCDATA or CDATA 
  380. states... */ 
  381. $name = strtolower($this->stream->charsWhile(self::ALPHA)); 
  382. $following = $this->stream->char(); 
  383. $this->stream->unget(); 
  384. if ( 
  385. !$this->token || 
  386. $this->token['name'] !== $name || 
  387. $this->token['name'] === $name && !in_array($following, array("\x09", "\x0A", "\x0C", "\x20", "\x3E", "\x2F", false)) 
  388. ) { 
  389. /** if no start tag token has ever been emitted by this instance 
  390. of the tokenizer (fragment case), or, if the next few 
  391. characters do not match the tag name of the last start tag 
  392. token emitted (compared in an ASCII case-insensitive manner),  
  393. or if they do but they are not immediately followed by one of 
  394. the following characters: 
  395.   
  396. * U+0009 CHARACTER TABULATION 
  397. * U+000A LINE FEED (LF) 
  398. * U+000C FORM FEED (FF) 
  399. * U+0020 SPACE 
  400. * U+003E GREATER-THAN SIGN (>) 
  401. * U+002F SOLIDUS (/) 
  402. * EOF 
  403.   
  404. ...then emit a U+003C LESS-THAN SIGN character token, a 
  405. U+002F SOLIDUS character token, and switch to the data 
  406. state to process the next input character. */ 
  407. // XXX: Probably ought to replace in_array with $following === x ||... 
  408.  
  409. // We also need to emit $name now we've consumed that, as we 
  410. // know it'll just be emitted as a character token. 
  411. $this->emitToken(array( 
  412. 'type' => self::CHARACTER,  
  413. 'data' => '</' . $name 
  414. )); 
  415.  
  416. $state = 'data'; 
  417. } else { 
  418. // This matches what would happen if we actually did the 
  419. // otherwise below (but we can't because we've consumed too 
  420. // much). 
  421.  
  422. // Start the end tag token with the name we already have. 
  423. $this->token = array( 
  424. 'name' => $name,  
  425. 'type' => self::ENDTAG 
  426. ); 
  427.  
  428. // Change to tag name state. 
  429. $state = 'tag name'; 
  430. } elseif ($this->content_model === self::PCDATA) { 
  431. /** Otherwise, if the content model flag is set to the PCDATA 
  432. state [...]: */ 
  433. $char = $this->stream->char(); 
  434.  
  435. if ('A' <= $char && $char <= 'Z') { 
  436. /** U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z 
  437. Create a new end tag token, set its tag name to the lowercase version 
  438. of the input character (add 0x0020 to the character's code point), then 
  439. switch to the tag name state. (Don't emit the token yet; further details 
  440. will be filled in before it is emitted.) */ 
  441. $this->token = array( 
  442. 'name' => strtolower($char),  
  443. 'type' => self::ENDTAG 
  444. ); 
  445.  
  446. $state = 'tag name'; 
  447.  
  448. } elseif ('a' <= $char && $char <= 'z') { 
  449. /** U+0061 LATIN SMALL LETTER A through to U+007A LATIN SMALL LETTER Z 
  450. Create a new end tag token, set its tag name to the 
  451. input character, then switch to the tag name state. 
  452. (Don't emit the token yet; further details will be 
  453. filled in before it is emitted.) */ 
  454. $this->token = array( 
  455. 'name' => $char,  
  456. 'type' => self::ENDTAG 
  457. ); 
  458.  
  459. $state = 'tag name'; 
  460.  
  461. } elseif($char === '>') { 
  462. /** U+003E GREATER-THAN SIGN (>) 
  463. Parse error. Switch to the data state. */ 
  464. $this->emitToken(array( 
  465. 'type' => self::PARSEERROR,  
  466. 'data' => 'expected-closing-tag-but-got-right-bracket' 
  467. )); 
  468. $state = 'data'; 
  469.  
  470. } elseif($char === false) { 
  471. /** EOF 
  472. Parse error. Emit a U+003C LESS-THAN SIGN character token and a U+002F 
  473. SOLIDUS character token. Reconsume the EOF character in the data state. */ 
  474. $this->emitToken(array( 
  475. 'type' => self::PARSEERROR,  
  476. 'data' => 'expected-closing-tag-but-got-eof' 
  477. )); 
  478. $this->emitToken(array( 
  479. 'type' => self::CHARACTER,  
  480. 'data' => '</' 
  481. )); 
  482.  
  483. $this->stream->unget(); 
  484. $state = 'data'; 
  485.  
  486. } else { 
  487. /** Parse error. Switch to the bogus comment state. */ 
  488. $this->emitToken(array( 
  489. 'type' => self::PARSEERROR,  
  490. 'data' => 'expected-closing-tag-but-got-char' 
  491. )); 
  492. $this->token = array( 
  493. 'data' => $char,  
  494. 'type' => self::COMMENT 
  495. ); 
  496. $state = 'bogus comment'; 
  497. break; 
  498.  
  499. case 'tag name': 
  500. /** Consume the next input character: */ 
  501. $char = $this->stream->char(); 
  502.  
  503. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  504. /** U+0009 CHARACTER TABULATION 
  505. U+000A LINE FEED (LF) 
  506. U+000C FORM FEED (FF) 
  507. U+0020 SPACE 
  508. Switch to the before attribute name state. */ 
  509. $state = 'before attribute name'; 
  510.  
  511. } elseif($char === '/') { 
  512. /** U+002F SOLIDUS (/) 
  513. Switch to the self-closing start tag state. */ 
  514. $state = 'self-closing start tag'; 
  515.  
  516. } elseif($char === '>') { 
  517. /** U+003E GREATER-THAN SIGN (>) 
  518. Emit the current tag token. Switch to the data state. */ 
  519. $this->emitToken($this->token); 
  520. $state = 'data'; 
  521.  
  522. } elseif('A' <= $char && $char <= 'Z') { 
  523. /** U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z 
  524. Append the lowercase version of the current input 
  525. character (add 0x0020 to the character's code point) to 
  526. the current tag token's tag name. Stay in the tag name state. */ 
  527. $chars = $this->stream->charsWhile(self::UPPER_ALPHA); 
  528.  
  529. $this->token['name'] .= strtolower($char . $chars); 
  530. $state = 'tag name'; 
  531.  
  532. } elseif($char === false) { 
  533. /** EOF 
  534. Parse error. Reconsume the EOF character in the data state. */ 
  535. $this->emitToken(array( 
  536. 'type' => self::PARSEERROR,  
  537. 'data' => 'eof-in-tag-name' 
  538. )); 
  539.  
  540. $this->stream->unget(); 
  541. $state = 'data'; 
  542.  
  543. } else { 
  544. /** Anything else 
  545. Append the current input character to the current tag token's tag name. 
  546. Stay in the tag name state. */ 
  547. $chars = $this->stream->charsUntil("\t\n\x0C />" . self::UPPER_ALPHA); 
  548.  
  549. $this->token['name'] .= $char . $chars; 
  550. $state = 'tag name'; 
  551. break; 
  552.  
  553. case 'before attribute name': 
  554. /** Consume the next input character: */ 
  555. $char = $this->stream->char(); 
  556.  
  557. // this conditional is optimized, check bottom 
  558. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  559. /** U+0009 CHARACTER TABULATION 
  560. U+000A LINE FEED (LF) 
  561. U+000C FORM FEED (FF) 
  562. U+0020 SPACE 
  563. Stay in the before attribute name state. */ 
  564. $state = 'before attribute name'; 
  565.  
  566. } elseif($char === '/') { 
  567. /** U+002F SOLIDUS (/) 
  568. Switch to the self-closing start tag state. */ 
  569. $state = 'self-closing start tag'; 
  570.  
  571. } elseif($char === '>') { 
  572. /** U+003E GREATER-THAN SIGN (>) 
  573. Emit the current tag token. Switch to the data state. */ 
  574. $this->emitToken($this->token); 
  575. $state = 'data'; 
  576.  
  577. } elseif('A' <= $char && $char <= 'Z') { 
  578. /** U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z 
  579. Start a new attribute in the current tag token. Set that 
  580. attribute's name to the lowercase version of the current 
  581. input character (add 0x0020 to the character's code 
  582. point), and its value to the empty string. Switch to the 
  583. attribute name state.*/ 
  584. $this->token['attr'][] = array( 
  585. 'name' => strtolower($char),  
  586. 'value' => '' 
  587. ); 
  588.  
  589. $state = 'attribute name'; 
  590.  
  591. } elseif($char === false) { 
  592. /** EOF 
  593. Parse error. Reconsume the EOF character in the data state. */ 
  594. $this->emitToken(array( 
  595. 'type' => self::PARSEERROR,  
  596. 'data' => 'expected-attribute-name-but-got-eof' 
  597. )); 
  598.  
  599. $this->stream->unget(); 
  600. $state = 'data'; 
  601.  
  602. } else { 
  603. /** U+0022 QUOTATION MARK (") 
  604. U+0027 APOSTROPHE (') 
  605. U+003C LESS-THAN SIGN (<) 
  606. U+003D EQUALS SIGN (=) 
  607. Parse error. Treat it as per the "anything else" entry 
  608. below. */ 
  609. if($char === '"' || $char === "'" || $char === '<' || $char === '=') { 
  610. $this->emitToken(array( 
  611. 'type' => self::PARSEERROR,  
  612. 'data' => 'invalid-character-in-attribute-name' 
  613. )); 
  614.  
  615. /** Anything else 
  616. Start a new attribute in the current tag token. Set that attribute's 
  617. name to the current input character, and its value to the empty string. 
  618. Switch to the attribute name state. */ 
  619. $this->token['attr'][] = array( 
  620. 'name' => $char,  
  621. 'value' => '' 
  622. ); 
  623.  
  624. $state = 'attribute name'; 
  625. break; 
  626.  
  627. case 'attribute name': 
  628. // Consume the next input character: 
  629. $char = $this->stream->char(); 
  630.  
  631. // this conditional is optimized, check bottom 
  632. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  633. /** U+0009 CHARACTER TABULATION 
  634. U+000A LINE FEED (LF) 
  635. U+000C FORM FEED (FF) 
  636. U+0020 SPACE 
  637. Switch to the after attribute name state. */ 
  638. $state = 'after attribute name'; 
  639.  
  640. } elseif($char === '/') { 
  641. /** U+002F SOLIDUS (/) 
  642. Switch to the self-closing start tag state. */ 
  643. $state = 'self-closing start tag'; 
  644.  
  645. } elseif($char === '=') { 
  646. /** U+003D EQUALS SIGN (=) 
  647. Switch to the before attribute value state. */ 
  648. $state = 'before attribute value'; 
  649.  
  650. } elseif($char === '>') { 
  651. /** U+003E GREATER-THAN SIGN (>) 
  652. Emit the current tag token. Switch to the data state. */ 
  653. $this->emitToken($this->token); 
  654. $state = 'data'; 
  655.  
  656. } elseif('A' <= $char && $char <= 'Z') { 
  657. /** U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z 
  658. Append the lowercase version of the current input 
  659. character (add 0x0020 to the character's code point) to 
  660. the current attribute's name. Stay in the attribute name 
  661. state. */ 
  662. $chars = $this->stream->charsWhile(self::UPPER_ALPHA); 
  663.  
  664. $last = count($this->token['attr']) - 1; 
  665. $this->token['attr'][$last]['name'] .= strtolower($char . $chars); 
  666.  
  667. $state = 'attribute name'; 
  668.  
  669. } elseif($char === false) { 
  670. /** EOF 
  671. Parse error. Reconsume the EOF character in the data state. */ 
  672. $this->emitToken(array( 
  673. 'type' => self::PARSEERROR,  
  674. 'data' => 'eof-in-attribute-name' 
  675. )); 
  676.  
  677. $this->stream->unget(); 
  678. $state = 'data'; 
  679.  
  680. } else { 
  681. /** U+0022 QUOTATION MARK (") 
  682. U+0027 APOSTROPHE (') 
  683. U+003C LESS-THAN SIGN (<) 
  684. Parse error. Treat it as per the "anything else" 
  685. entry below. */ 
  686. if($char === '"' || $char === "'" || $char === '<') { 
  687. $this->emitToken(array( 
  688. 'type' => self::PARSEERROR,  
  689. 'data' => 'invalid-character-in-attribute-name' 
  690. )); 
  691.  
  692. /** Anything else 
  693. Append the current input character to the current attribute's name. 
  694. Stay in the attribute name state. */ 
  695. $chars = $this->stream->charsUntil("\t\n\x0C /=>\"'" . self::UPPER_ALPHA); 
  696.  
  697. $last = count($this->token['attr']) - 1; 
  698. $this->token['attr'][$last]['name'] .= $char . $chars; 
  699.  
  700. $state = 'attribute name'; 
  701.  
  702. /** When the user agent leaves the attribute name state 
  703. (and before emitting the tag token, if appropriate), the 
  704. complete attribute's name must be compared to the other 
  705. attributes on the same token; if there is already an 
  706. attribute on the token with the exact same name, then this 
  707. is a parse error and the new attribute must be dropped, along 
  708. with the value that gets associated with it (if any). */ 
  709. // this might be implemented in the emitToken method 
  710. break; 
  711.  
  712. case 'after attribute name': 
  713. // Consume the next input character: 
  714. $char = $this->stream->char(); 
  715.  
  716. // this is an optimized conditional, check the bottom 
  717. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  718. /** U+0009 CHARACTER TABULATION 
  719. U+000A LINE FEED (LF) 
  720. U+000C FORM FEED (FF) 
  721. U+0020 SPACE 
  722. Stay in the after attribute name state. */ 
  723. $state = 'after attribute name'; 
  724.  
  725. } elseif($char === '/') { 
  726. /** U+002F SOLIDUS (/) 
  727. Switch to the self-closing start tag state. */ 
  728. $state = 'self-closing start tag'; 
  729.  
  730. } elseif($char === '=') { 
  731. /** U+003D EQUALS SIGN (=) 
  732. Switch to the before attribute value state. */ 
  733. $state = 'before attribute value'; 
  734.  
  735. } elseif($char === '>') { 
  736. /** U+003E GREATER-THAN SIGN (>) 
  737. Emit the current tag token. Switch to the data state. */ 
  738. $this->emitToken($this->token); 
  739. $state = 'data'; 
  740.  
  741. } elseif('A' <= $char && $char <= 'Z') { 
  742. /** U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z 
  743. Start a new attribute in the current tag token. Set that 
  744. attribute's name to the lowercase version of the current 
  745. input character (add 0x0020 to the character's code 
  746. point), and its value to the empty string. Switch to the 
  747. attribute name state. */ 
  748. $this->token['attr'][] = array( 
  749. 'name' => strtolower($char),  
  750. 'value' => '' 
  751. ); 
  752.  
  753. $state = 'attribute name'; 
  754.  
  755. } elseif($char === false) { 
  756. /** EOF 
  757. Parse error. Reconsume the EOF character in the data state. */ 
  758. $this->emitToken(array( 
  759. 'type' => self::PARSEERROR,  
  760. 'data' => 'expected-end-of-tag-but-got-eof' 
  761. )); 
  762.  
  763. $this->stream->unget(); 
  764. $state = 'data'; 
  765.  
  766. } else { 
  767. /** U+0022 QUOTATION MARK (") 
  768. U+0027 APOSTROPHE (') 
  769. U+003C LESS-THAN SIGN(<) 
  770. Parse error. Treat it as per the "anything else" 
  771. entry below. */ 
  772. if($char === '"' || $char === "'" || $char === "<") { 
  773. $this->emitToken(array( 
  774. 'type' => self::PARSEERROR,  
  775. 'data' => 'invalid-character-after-attribute-name' 
  776. )); 
  777.  
  778. /** Anything else 
  779. Start a new attribute in the current tag token. Set that attribute's 
  780. name to the current input character, and its value to the empty string. 
  781. Switch to the attribute name state. */ 
  782. $this->token['attr'][] = array( 
  783. 'name' => $char,  
  784. 'value' => '' 
  785. ); 
  786.  
  787. $state = 'attribute name'; 
  788. break; 
  789.  
  790. case 'before attribute value': 
  791. // Consume the next input character: 
  792. $char = $this->stream->char(); 
  793.  
  794. // this is an optimized conditional 
  795. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  796. /** U+0009 CHARACTER TABULATION 
  797. U+000A LINE FEED (LF) 
  798. U+000C FORM FEED (FF) 
  799. U+0020 SPACE 
  800. Stay in the before attribute value state. */ 
  801. $state = 'before attribute value'; 
  802.  
  803. } elseif($char === '"') { 
  804. /** U+0022 QUOTATION MARK (") 
  805. Switch to the attribute value (double-quoted) state. */ 
  806. $state = 'attribute value (double-quoted)'; 
  807.  
  808. } elseif($char === '&') { 
  809. /** U+0026 AMPERSAND (&) 
  810. Switch to the attribute value (unquoted) state and reconsume 
  811. this input character. */ 
  812. $this->stream->unget(); 
  813. $state = 'attribute value (unquoted)'; 
  814.  
  815. } elseif($char === '\'') { 
  816. /** U+0027 APOSTROPHE (') 
  817. Switch to the attribute value (single-quoted) state. */ 
  818. $state = 'attribute value (single-quoted)'; 
  819.  
  820. } elseif($char === '>') { 
  821. /** U+003E GREATER-THAN SIGN (>) 
  822. Parse error. Emit the current tag token. Switch to the data state. */ 
  823. $this->emitToken(array( 
  824. 'type' => self::PARSEERROR,  
  825. 'data' => 'expected-attribute-value-but-got-right-bracket' 
  826. )); 
  827. $this->emitToken($this->token); 
  828. $state = 'data'; 
  829.  
  830. } elseif($char === false) { 
  831. /** EOF 
  832. Parse error. Reconsume the EOF character in the data state. */ 
  833. $this->emitToken(array( 
  834. 'type' => self::PARSEERROR,  
  835. 'data' => 'expected-attribute-value-but-got-eof' 
  836. )); 
  837. $this->stream->unget(); 
  838. $state = 'data'; 
  839.  
  840. } else { 
  841. /** U+003D EQUALS SIGN (=) 
  842. * U+003C LESS-THAN SIGN (<) 
  843. Parse error. Treat it as per the "anything else" entry below. */ 
  844. if($char === '=' || $char === '<') { 
  845. $this->emitToken(array( 
  846. 'type' => self::PARSEERROR,  
  847. 'data' => 'equals-in-unquoted-attribute-value' 
  848. )); 
  849.  
  850. /** Anything else 
  851. Append the current input character to the current attribute's value. 
  852. Switch to the attribute value (unquoted) state. */ 
  853. $last = count($this->token['attr']) - 1; 
  854. $this->token['attr'][$last]['value'] .= $char; 
  855.  
  856. $state = 'attribute value (unquoted)'; 
  857. break; 
  858.  
  859. case 'attribute value (double-quoted)': 
  860. // Consume the next input character: 
  861. $char = $this->stream->char(); 
  862.  
  863. if($char === '"') { 
  864. /** U+0022 QUOTATION MARK (") 
  865. Switch to the after attribute value (quoted) state. */ 
  866. $state = 'after attribute value (quoted)'; 
  867.  
  868. } elseif($char === '&') { 
  869. /** U+0026 AMPERSAND (&) 
  870. Switch to the character reference in attribute value 
  871. state, with the additional allowed character 
  872. being U+0022 QUOTATION MARK ("). */ 
  873. $this->characterReferenceInAttributeValue('"'); 
  874.  
  875. } elseif($char === false) { 
  876. /** EOF 
  877. Parse error. Reconsume the EOF character in the data state. */ 
  878. $this->emitToken(array( 
  879. 'type' => self::PARSEERROR,  
  880. 'data' => 'eof-in-attribute-value-double-quote' 
  881. )); 
  882.  
  883. $this->stream->unget(); 
  884. $state = 'data'; 
  885.  
  886. } else { 
  887. /** Anything else 
  888. Append the current input character to the current attribute's value. 
  889. Stay in the attribute value (double-quoted) state. */ 
  890. $chars = $this->stream->charsUntil('"&'); 
  891.  
  892. $last = count($this->token['attr']) - 1; 
  893. $this->token['attr'][$last]['value'] .= $char . $chars; 
  894.  
  895. $state = 'attribute value (double-quoted)'; 
  896. break; 
  897.  
  898. case 'attribute value (single-quoted)': 
  899. // Consume the next input character: 
  900. $char = $this->stream->char(); 
  901.  
  902. if($char === "'") { 
  903. /** U+0022 QUOTATION MARK (') 
  904. Switch to the after attribute value state. */ 
  905. $state = 'after attribute value (quoted)'; 
  906.  
  907. } elseif($char === '&') { 
  908. /** U+0026 AMPERSAND (&) 
  909. Switch to the entity in attribute value state. */ 
  910. $this->characterReferenceInAttributeValue("'"); 
  911.  
  912. } elseif($char === false) { 
  913. /** EOF 
  914. Parse error. Reconsume the EOF character in the data state. */ 
  915. $this->emitToken(array( 
  916. 'type' => self::PARSEERROR,  
  917. 'data' => 'eof-in-attribute-value-single-quote' 
  918. )); 
  919.  
  920. $this->stream->unget(); 
  921. $state = 'data'; 
  922.  
  923. } else { 
  924. /** Anything else 
  925. Append the current input character to the current attribute's value. 
  926. Stay in the attribute value (single-quoted) state. */ 
  927. $chars = $this->stream->charsUntil("'&"); 
  928.  
  929. $last = count($this->token['attr']) - 1; 
  930. $this->token['attr'][$last]['value'] .= $char . $chars; 
  931.  
  932. $state = 'attribute value (single-quoted)'; 
  933. break; 
  934.  
  935. case 'attribute value (unquoted)': 
  936. // Consume the next input character: 
  937. $char = $this->stream->char(); 
  938.  
  939. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  940. /** U+0009 CHARACTER TABULATION 
  941. U+000A LINE FEED (LF) 
  942. U+000C FORM FEED (FF) 
  943. U+0020 SPACE 
  944. Switch to the before attribute name state. */ 
  945. $state = 'before attribute name'; 
  946.  
  947. } elseif($char === '&') { 
  948. /** U+0026 AMPERSAND (&) 
  949. Switch to the entity in attribute value state, with the  
  950. additional allowed character being U+003E  
  951. GREATER-THAN SIGN (>). */ 
  952. $this->characterReferenceInAttributeValue('>'); 
  953.  
  954. } elseif($char === '>') { 
  955. /** U+003E GREATER-THAN SIGN (>) 
  956. Emit the current tag token. Switch to the data state. */ 
  957. $this->emitToken($this->token); 
  958. $state = 'data'; 
  959.  
  960. } elseif ($char === false) { 
  961. /** EOF 
  962. Parse error. Reconsume the EOF character in the data state. */ 
  963. $this->emitToken(array( 
  964. 'type' => self::PARSEERROR,  
  965. 'data' => 'eof-in-attribute-value-no-quotes' 
  966. )); 
  967. $this->stream->unget(); 
  968. $state = 'data'; 
  969.  
  970. } else { 
  971. /** U+0022 QUOTATION MARK (") 
  972. U+0027 APOSTROPHE (') 
  973. U+003C LESS-THAN SIGN (<) 
  974. U+003D EQUALS SIGN (=) 
  975. Parse error. Treat it as per the "anything else" 
  976. entry below. */ 
  977. if($char === '"' || $char === "'" || $char === '=' || $char == '<') { 
  978. $this->emitToken(array( 
  979. 'type' => self::PARSEERROR,  
  980. 'data' => 'unexpected-character-in-unquoted-attribute-value' 
  981. )); 
  982.  
  983. /** Anything else 
  984. Append the current input character to the current attribute's value. 
  985. Stay in the attribute value (unquoted) state. */ 
  986. $chars = $this->stream->charsUntil("\t\n\x0c &>\"'="); 
  987.  
  988. $last = count($this->token['attr']) - 1; 
  989. $this->token['attr'][$last]['value'] .= $char . $chars; 
  990.  
  991. $state = 'attribute value (unquoted)'; 
  992. break; 
  993.  
  994. case 'after attribute value (quoted)': 
  995. /** Consume the next input character: */ 
  996. $char = $this->stream->char(); 
  997.  
  998. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  999. /** U+0009 CHARACTER TABULATION 
  1000. U+000A LINE FEED (LF) 
  1001. U+000C FORM FEED (FF) 
  1002. U+0020 SPACE 
  1003. Switch to the before attribute name state. */ 
  1004. $state = 'before attribute name'; 
  1005.  
  1006. } elseif ($char === '/') { 
  1007. /** U+002F SOLIDUS (/) 
  1008. Switch to the self-closing start tag state. */ 
  1009. $state = 'self-closing start tag'; 
  1010.  
  1011. } elseif ($char === '>') { 
  1012. /** U+003E GREATER-THAN SIGN (>) 
  1013. Emit the current tag token. Switch to the data state. */ 
  1014. $this->emitToken($this->token); 
  1015. $state = 'data'; 
  1016.  
  1017. } elseif ($char === false) { 
  1018. /** EOF 
  1019. Parse error. Reconsume the EOF character in the data state. */ 
  1020. $this->emitToken(array( 
  1021. 'type' => self::PARSEERROR,  
  1022. 'data' => 'unexpected-EOF-after-attribute-value' 
  1023. )); 
  1024. $this->stream->unget(); 
  1025. $state = 'data'; 
  1026.  
  1027. } else { 
  1028. /** Anything else 
  1029. Parse error. Reconsume the character in the before attribute 
  1030. name state. */ 
  1031. $this->emitToken(array( 
  1032. 'type' => self::PARSEERROR,  
  1033. 'data' => 'unexpected-character-after-attribute-value' 
  1034. )); 
  1035. $this->stream->unget(); 
  1036. $state = 'before attribute name'; 
  1037. break; 
  1038.  
  1039. case 'self-closing start tag': 
  1040. /** Consume the next input character: */ 
  1041. $char = $this->stream->char(); 
  1042.  
  1043. if ($char === '>') { 
  1044. /** U+003E GREATER-THAN SIGN (>) 
  1045. Set the self-closing flag of the current tag token. 
  1046. Emit the current tag token. Switch to the data state. */ 
  1047. // not sure if this is the name we want 
  1048. $this->token['self-closing'] = true; 
  1049. $this->emitToken($this->token); 
  1050. $state = 'data'; 
  1051.  
  1052. } elseif ($char === false) { 
  1053. /** EOF 
  1054. Parse error. Reconsume the EOF character in the data state. */ 
  1055. $this->emitToken(array( 
  1056. 'type' => self::PARSEERROR,  
  1057. 'data' => 'unexpected-eof-after-self-closing' 
  1058. )); 
  1059. $this->stream->unget(); 
  1060. $state = 'data'; 
  1061.  
  1062. } else { 
  1063. /** Anything else 
  1064. Parse error. Reconsume the character in the before attribute name state. */ 
  1065. $this->emitToken(array( 
  1066. 'type' => self::PARSEERROR,  
  1067. 'data' => 'unexpected-character-after-self-closing' 
  1068. )); 
  1069. $this->stream->unget(); 
  1070. $state = 'before attribute name'; 
  1071. break; 
  1072.  
  1073. case 'bogus comment': 
  1074. /** (This can only happen if the content model flag is set to the PCDATA state.) */ 
  1075. /** Consume every character up to the first U+003E GREATER-THAN SIGN 
  1076. character (>) or the end of the file (EOF), whichever comes first. Emit 
  1077. a comment token whose data is the concatenation of all the characters 
  1078. starting from and including the character that caused the state machine 
  1079. to switch into the bogus comment state, up to and including the last 
  1080. consumed character before the U+003E character, if any, or up to the 
  1081. end of the file otherwise. (If the comment was started by the end of 
  1082. the file (EOF), the token is empty.) */ 
  1083. $this->token['data'] .= (string) $this->stream->charsUntil('>'); 
  1084. $this->stream->char(); 
  1085.  
  1086. $this->emitToken($this->token); 
  1087.  
  1088. /** Switch to the data state. */ 
  1089. $state = 'data'; 
  1090. break; 
  1091.  
  1092. case 'markup declaration open': 
  1093. // Consume for below 
  1094. $hyphens = $this->stream->charsWhile('-', 2); 
  1095. if ($hyphens === '-') { 
  1096. $this->stream->unget(); 
  1097. if ($hyphens !== '--') { 
  1098. $alpha = $this->stream->charsWhile(self::ALPHA, 7); 
  1099.  
  1100. /** If the next two characters are both U+002D HYPHEN-MINUS (-) 
  1101. characters, consume those two characters, create a comment token whose 
  1102. data is the empty string, and switch to the comment state. */ 
  1103. if($hyphens === '--') { 
  1104. $state = 'comment start'; 
  1105. $this->token = array( 
  1106. 'data' => '',  
  1107. 'type' => self::COMMENT 
  1108. ); 
  1109.  
  1110. /** Otherwise if the next seven characters are a case-insensitive match 
  1111. for the word "DOCTYPE", then consume those characters and switch to the 
  1112. DOCTYPE state. */ 
  1113. } elseif(strtoupper($alpha) === 'DOCTYPE') { 
  1114. $state = 'DOCTYPE'; 
  1115.  
  1116. // XXX not implemented 
  1117. /** Otherwise, if the insertion mode is "in foreign content" 
  1118. and the current node is not an element in the HTML namespace 
  1119. and the next seven characters are an ASCII case-sensitive 
  1120. match for the string "[CDATA[" (the five uppercase letters 
  1121. "CDATA" with a U+005B LEFT SQUARE BRACKET character before 
  1122. and after), then consume those characters and switch to the 
  1123. CDATA section state (which is unrelated to the content model 
  1124. flag's CDATA state). */ 
  1125.  
  1126. /** Otherwise, is is a parse error. Switch to the bogus comment state. 
  1127. The next character that is consumed, if any, is the first character 
  1128. that will be in the comment. */ 
  1129. } else { 
  1130. $this->emitToken(array( 
  1131. 'type' => self::PARSEERROR,  
  1132. 'data' => 'expected-dashes-or-doctype' 
  1133. )); 
  1134. $this->token = array( 
  1135. 'data' => (string) $alpha,  
  1136. 'type' => self::COMMENT 
  1137. ); 
  1138. $state = 'bogus comment'; 
  1139. break; 
  1140.  
  1141. case 'comment start': 
  1142. /** Consume the next input character: */ 
  1143. $char = $this->stream->char(); 
  1144.  
  1145. if ($char === '-') { 
  1146. /** U+002D HYPHEN-MINUS (-) 
  1147. Switch to the comment start dash state. */ 
  1148. $state = 'comment start dash'; 
  1149. } elseif ($char === '>') { 
  1150. /** U+003E GREATER-THAN SIGN (>) 
  1151. Parse error. Emit the comment token. Switch to the 
  1152. data state. */ 
  1153. $this->emitToken(array( 
  1154. 'type' => self::PARSEERROR,  
  1155. 'data' => 'incorrect-comment' 
  1156. )); 
  1157. $this->emitToken($this->token); 
  1158. $state = 'data'; 
  1159. } elseif ($char === false) { 
  1160. /** EOF 
  1161. Parse error. Emit the comment token. Reconsume the 
  1162. EOF character in the data state. */ 
  1163. $this->emitToken(array( 
  1164. 'type' => self::PARSEERROR,  
  1165. 'data' => 'eof-in-comment' 
  1166. )); 
  1167. $this->emitToken($this->token); 
  1168. $this->stream->unget(); 
  1169. $state = 'data'; 
  1170. } else { 
  1171. /** Anything else 
  1172. Append the input character to the comment token's 
  1173. data. Switch to the comment state. */ 
  1174. $this->token['data'] .= $char; 
  1175. $state = 'comment'; 
  1176. break; 
  1177.  
  1178. case 'comment start dash': 
  1179. /** Consume the next input character: */ 
  1180. $char = $this->stream->char(); 
  1181. if ($char === '-') { 
  1182. /** U+002D HYPHEN-MINUS (-) 
  1183. Switch to the comment end state */ 
  1184. $state = 'comment end'; 
  1185. } elseif ($char === '>') { 
  1186. /** U+003E GREATER-THAN SIGN (>) 
  1187. Parse error. Emit the comment token. Switch to the 
  1188. data state. */ 
  1189. $this->emitToken(array( 
  1190. 'type' => self::PARSEERROR,  
  1191. 'data' => 'incorrect-comment' 
  1192. )); 
  1193. $this->emitToken($this->token); 
  1194. $state = 'data'; 
  1195. } elseif ($char === false) { 
  1196. /** Parse error. Emit the comment token. Reconsume the 
  1197. EOF character in the data state. */ 
  1198. $this->emitToken(array( 
  1199. 'type' => self::PARSEERROR,  
  1200. 'data' => 'eof-in-comment' 
  1201. )); 
  1202. $this->emitToken($this->token); 
  1203. $this->stream->unget(); 
  1204. $state = 'data'; 
  1205. } else { 
  1206. $this->token['data'] .= '-' . $char; 
  1207. $state = 'comment'; 
  1208. break; 
  1209.  
  1210. case 'comment': 
  1211. /** Consume the next input character: */ 
  1212. $char = $this->stream->char(); 
  1213.  
  1214. if($char === '-') { 
  1215. /** U+002D HYPHEN-MINUS (-) 
  1216. Switch to the comment end dash state */ 
  1217. $state = 'comment end dash'; 
  1218.  
  1219. } elseif($char === false) { 
  1220. /** EOF 
  1221. Parse error. Emit the comment token. Reconsume the EOF character 
  1222. in the data state. */ 
  1223. $this->emitToken(array( 
  1224. 'type' => self::PARSEERROR,  
  1225. 'data' => 'eof-in-comment' 
  1226. )); 
  1227. $this->emitToken($this->token); 
  1228. $this->stream->unget(); 
  1229. $state = 'data'; 
  1230.  
  1231. } else { 
  1232. /** Anything else 
  1233. Append the input character to the comment token's data. Stay in 
  1234. the comment state. */ 
  1235. $chars = $this->stream->charsUntil('-'); 
  1236.  
  1237. $this->token['data'] .= $char . $chars; 
  1238. break; 
  1239.  
  1240. case 'comment end dash': 
  1241. /** Consume the next input character: */ 
  1242. $char = $this->stream->char(); 
  1243.  
  1244. if($char === '-') { 
  1245. /** U+002D HYPHEN-MINUS (-) 
  1246. Switch to the comment end state */ 
  1247. $state = 'comment end'; 
  1248.  
  1249. } elseif($char === false) { 
  1250. /** EOF 
  1251. Parse error. Emit the comment token. Reconsume the EOF character 
  1252. in the data state. */ 
  1253. $this->emitToken(array( 
  1254. 'type' => self::PARSEERROR,  
  1255. 'data' => 'eof-in-comment-end-dash' 
  1256. )); 
  1257. $this->emitToken($this->token); 
  1258. $this->stream->unget(); 
  1259. $state = 'data'; 
  1260.  
  1261. } else { 
  1262. /** Anything else 
  1263. Append a U+002D HYPHEN-MINUS (-) character and the input 
  1264. character to the comment token's data. Switch to the comment state. */ 
  1265. $this->token['data'] .= '-'.$char; 
  1266. $state = 'comment'; 
  1267. break; 
  1268.  
  1269. case 'comment end': 
  1270. /** Consume the next input character: */ 
  1271. $char = $this->stream->char(); 
  1272.  
  1273. if($char === '>') { 
  1274. /** U+003E GREATER-THAN SIGN (>) 
  1275. Emit the comment token. Switch to the data state. */ 
  1276. $this->emitToken($this->token); 
  1277. $state = 'data'; 
  1278.  
  1279. } elseif($char === '-') { 
  1280. /** U+002D HYPHEN-MINUS (-) 
  1281. Parse error. Append a U+002D HYPHEN-MINUS (-) character 
  1282. to the comment token's data. Stay in the comment end 
  1283. state. */ 
  1284. $this->emitToken(array( 
  1285. 'type' => self::PARSEERROR,  
  1286. 'data' => 'unexpected-dash-after-double-dash-in-comment' 
  1287. )); 
  1288. $this->token['data'] .= '-'; 
  1289.  
  1290. } elseif($char === "\t" || $char === "\n" || $char === "\x0a" || $char === ' ') { 
  1291. $this->emitToken(array( 
  1292. 'type' => self::PARSEERROR,  
  1293. 'data' => 'unexpected-space-after-double-dash-in-comment' 
  1294. )); 
  1295. $this->token['data'] .= '--' . $char; 
  1296. $state = 'comment end space'; 
  1297.  
  1298. } elseif($char === '!') { 
  1299. $this->emitToken(array( 
  1300. 'type' => self::PARSEERROR,  
  1301. 'data' => 'unexpected-bang-after-double-dash-in-comment' 
  1302. )); 
  1303. $state = 'comment end bang'; 
  1304.  
  1305. } elseif($char === false) { 
  1306. /** EOF 
  1307. Parse error. Emit the comment token. Reconsume the 
  1308. EOF character in the data state. */ 
  1309. $this->emitToken(array( 
  1310. 'type' => self::PARSEERROR,  
  1311. 'data' => 'eof-in-comment-double-dash' 
  1312. )); 
  1313. $this->emitToken($this->token); 
  1314. $this->stream->unget(); 
  1315. $state = 'data'; 
  1316.  
  1317. } else { 
  1318. /** Anything else 
  1319. Parse error. Append two U+002D HYPHEN-MINUS (-) 
  1320. characters and the input character to the comment token's 
  1321. data. Switch to the comment state. */ 
  1322. $this->emitToken(array( 
  1323. 'type' => self::PARSEERROR,  
  1324. 'data' => 'unexpected-char-in-comment' 
  1325. )); 
  1326. $this->token['data'] .= '--'.$char; 
  1327. $state = 'comment'; 
  1328. break; 
  1329.  
  1330. case 'comment end bang': 
  1331. $char = $this->stream->char(); 
  1332. if ($char === '>') { 
  1333. $this->emitToken($this->token); 
  1334. $state = 'data'; 
  1335. } elseif ($char === "-") { 
  1336. $this->token['data'] .= '--!'; 
  1337. $state = 'comment end dash'; 
  1338. } elseif ($char === false) { 
  1339. $this->emitToken(array( 
  1340. 'type' => self::PARSEERROR,  
  1341. 'data' => 'eof-in-comment-end-bang' 
  1342. )); 
  1343. $this->emitToken($this->token); 
  1344. $this->stream->unget(); 
  1345. $state = 'data'; 
  1346. } else { 
  1347. $this->token['data'] .= '--!' . $char; 
  1348. $state = 'comment'; 
  1349. break; 
  1350.  
  1351. case 'comment end space': 
  1352. $char = $this->stream->char(); 
  1353. if ($char === '>') { 
  1354. $this->emitToken($this->token); 
  1355. $state = 'data'; 
  1356. } elseif ($char === '-') { 
  1357. $state = 'comment end dash'; 
  1358. } elseif ($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  1359. $this->token['data'] .= $char; 
  1360. } elseif ($char === false) { 
  1361. $this->emitToken(array( 
  1362. 'type' => self::PARSEERROR,  
  1363. 'data' => 'unexpected-eof-in-comment-end-space',  
  1364. )); 
  1365. $this->emitToken($this->token); 
  1366. $this->stream->unget(); 
  1367. $state = 'data'; 
  1368. } else { 
  1369. $this->token['data'] .= $char; 
  1370. $state = 'comment'; 
  1371. break; 
  1372.  
  1373. case 'DOCTYPE': 
  1374. /** Consume the next input character: */ 
  1375. $char = $this->stream->char(); 
  1376.  
  1377. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  1378. /** U+0009 CHARACTER TABULATION 
  1379. U+000A LINE FEED (LF) 
  1380. U+000C FORM FEED (FF) 
  1381. U+0020 SPACE 
  1382. Switch to the before DOCTYPE name state. */ 
  1383. $state = 'before DOCTYPE name'; 
  1384.  
  1385. } elseif($char === false) { 
  1386. /** EOF 
  1387. Parse error. Create a new DOCTYPE token. Set its 
  1388. force-quirks flag to on. Emit the token. Reconsume the 
  1389. EOF character in the data state. */ 
  1390. $this->emitToken(array( 
  1391. 'type' => self::PARSEERROR,  
  1392. 'data' => 'need-space-after-doctype-but-got-eof' 
  1393. )); 
  1394. $this->emitToken(array( 
  1395. 'name' => '',  
  1396. 'type' => self::DOCTYPE,  
  1397. 'force-quirks' => true,  
  1398. 'error' => true 
  1399. )); 
  1400. $this->stream->unget(); 
  1401. $state = 'data'; 
  1402.  
  1403. } else { 
  1404. /** Anything else 
  1405. Parse error. Reconsume the current character in the 
  1406. before DOCTYPE name state. */ 
  1407. $this->emitToken(array( 
  1408. 'type' => self::PARSEERROR,  
  1409. 'data' => 'need-space-after-doctype' 
  1410. )); 
  1411. $this->stream->unget(); 
  1412. $state = 'before DOCTYPE name'; 
  1413. break; 
  1414.  
  1415. case 'before DOCTYPE name': 
  1416. /** Consume the next input character: */ 
  1417. $char = $this->stream->char(); 
  1418.  
  1419. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  1420. /** U+0009 CHARACTER TABULATION 
  1421. U+000A LINE FEED (LF) 
  1422. U+000C FORM FEED (FF) 
  1423. U+0020 SPACE 
  1424. Stay in the before DOCTYPE name state. */ 
  1425.  
  1426. } elseif($char === '>') { 
  1427. /** U+003E GREATER-THAN SIGN (>) 
  1428. Parse error. Create a new DOCTYPE token. Set its 
  1429. force-quirks flag to on. Emit the token. Switch to the 
  1430. data state. */ 
  1431. $this->emitToken(array( 
  1432. 'type' => self::PARSEERROR,  
  1433. 'data' => 'expected-doctype-name-but-got-right-bracket' 
  1434. )); 
  1435. $this->emitToken(array( 
  1436. 'name' => '',  
  1437. 'type' => self::DOCTYPE,  
  1438. 'force-quirks' => true,  
  1439. 'error' => true 
  1440. )); 
  1441.  
  1442. $state = 'data'; 
  1443.  
  1444. } elseif('A' <= $char && $char <= 'Z') { 
  1445. /** U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z 
  1446. Create a new DOCTYPE token. Set the token's name to the 
  1447. lowercase version of the input character (add 0x0020 to 
  1448. the character's code point). Switch to the DOCTYPE name 
  1449. state. */ 
  1450. $this->token = array( 
  1451. 'name' => strtolower($char),  
  1452. 'type' => self::DOCTYPE,  
  1453. 'error' => true 
  1454. ); 
  1455.  
  1456. $state = 'DOCTYPE name'; 
  1457.  
  1458. } elseif($char === false) { 
  1459. /** EOF 
  1460. Parse error. Create a new DOCTYPE token. Set its 
  1461. force-quirks flag to on. Emit the token. Reconsume the 
  1462. EOF character in the data state. */ 
  1463. $this->emitToken(array( 
  1464. 'type' => self::PARSEERROR,  
  1465. 'data' => 'expected-doctype-name-but-got-eof' 
  1466. )); 
  1467. $this->emitToken(array( 
  1468. 'name' => '',  
  1469. 'type' => self::DOCTYPE,  
  1470. 'force-quirks' => true,  
  1471. 'error' => true 
  1472. )); 
  1473.  
  1474. $this->stream->unget(); 
  1475. $state = 'data'; 
  1476.  
  1477. } else { 
  1478. /** Anything else 
  1479. Create a new DOCTYPE token. Set the token's name to the 
  1480. current input character. Switch to the DOCTYPE name state. */ 
  1481. $this->token = array( 
  1482. 'name' => $char,  
  1483. 'type' => self::DOCTYPE,  
  1484. 'error' => true 
  1485. ); 
  1486.  
  1487. $state = 'DOCTYPE name'; 
  1488. break; 
  1489.  
  1490. case 'DOCTYPE name': 
  1491. /** Consume the next input character: */ 
  1492. $char = $this->stream->char(); 
  1493.  
  1494. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  1495. /** U+0009 CHARACTER TABULATION 
  1496. U+000A LINE FEED (LF) 
  1497. U+000C FORM FEED (FF) 
  1498. U+0020 SPACE 
  1499. Switch to the after DOCTYPE name state. */ 
  1500. $state = 'after DOCTYPE name'; 
  1501.  
  1502. } elseif($char === '>') { 
  1503. /** U+003E GREATER-THAN SIGN (>) 
  1504. Emit the current DOCTYPE token. Switch to the data state. */ 
  1505. $this->emitToken($this->token); 
  1506. $state = 'data'; 
  1507.  
  1508. } elseif('A' <= $char && $char <= 'Z') { 
  1509. /** U+0041 LATIN CAPITAL LETTER A through to U+005A LATIN CAPITAL LETTER Z 
  1510. Append the lowercase version of the input character 
  1511. (add 0x0020 to the character's code point) to the current 
  1512. DOCTYPE token's name. Stay in the DOCTYPE name state. */ 
  1513. $this->token['name'] .= strtolower($char); 
  1514.  
  1515. } elseif($char === false) { 
  1516. /** EOF 
  1517. Parse error. Set the DOCTYPE token's force-quirks flag 
  1518. to on. Emit that DOCTYPE token. Reconsume the EOF 
  1519. character in the data state. */ 
  1520. $this->emitToken(array( 
  1521. 'type' => self::PARSEERROR,  
  1522. 'data' => 'eof-in-doctype-name' 
  1523. )); 
  1524. $this->token['force-quirks'] = true; 
  1525. $this->emitToken($this->token); 
  1526. $this->stream->unget(); 
  1527. $state = 'data'; 
  1528.  
  1529. } else { 
  1530. /** Anything else 
  1531. Append the current input character to the current 
  1532. DOCTYPE token's name. Stay in the DOCTYPE name state. */ 
  1533. $this->token['name'] .= $char; 
  1534.  
  1535. // XXX this is probably some sort of quirks mode designation,  
  1536. // check tree-builder to be sure. In general 'error' needs 
  1537. // to be specc'ified, this probably means removing it at the end 
  1538. $this->token['error'] = ($this->token['name'] === 'HTML') 
  1539. ? false 
  1540. : true; 
  1541. break; 
  1542.  
  1543. case 'after DOCTYPE name': 
  1544. /** Consume the next input character: */ 
  1545. $char = $this->stream->char(); 
  1546.  
  1547. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  1548. /** U+0009 CHARACTER TABULATION 
  1549. U+000A LINE FEED (LF) 
  1550. U+000C FORM FEED (FF) 
  1551. U+0020 SPACE 
  1552. Stay in the after DOCTYPE name state. */ 
  1553.  
  1554. } elseif($char === '>') { 
  1555. /** U+003E GREATER-THAN SIGN (>) 
  1556. Emit the current DOCTYPE token. Switch to the data state. */ 
  1557. $this->emitToken($this->token); 
  1558. $state = 'data'; 
  1559.  
  1560. } elseif($char === false) { 
  1561. /** EOF 
  1562. Parse error. Set the DOCTYPE token's force-quirks flag 
  1563. to on. Emit that DOCTYPE token. Reconsume the EOF 
  1564. character in the data state. */ 
  1565. $this->emitToken(array( 
  1566. 'type' => self::PARSEERROR,  
  1567. 'data' => 'eof-in-doctype' 
  1568. )); 
  1569. $this->token['force-quirks'] = true; 
  1570. $this->emitToken($this->token); 
  1571. $this->stream->unget(); 
  1572. $state = 'data'; 
  1573.  
  1574. } else { 
  1575. /** Anything else */ 
  1576.  
  1577. $nextSix = strtoupper($char . $this->stream->charsWhile(self::ALPHA, 5)); 
  1578. if ($nextSix === 'PUBLIC') { 
  1579. /** If the next six characters are an ASCII 
  1580. case-insensitive match for the word "PUBLIC", then 
  1581. consume those characters and switch to the before 
  1582. DOCTYPE public identifier state. */ 
  1583. $state = 'before DOCTYPE public identifier'; 
  1584.  
  1585. } elseif ($nextSix === 'SYSTEM') { 
  1586. /** Otherwise, if the next six characters are an ASCII 
  1587. case-insensitive match for the word "SYSTEM", then 
  1588. consume those characters and switch to the before 
  1589. DOCTYPE system identifier state. */ 
  1590. $state = 'before DOCTYPE system identifier'; 
  1591.  
  1592. } else { 
  1593. /** Otherwise, this is the parse error. Set the DOCTYPE 
  1594. token's force-quirks flag to on. Switch to the bogus 
  1595. DOCTYPE state. */ 
  1596. $this->emitToken(array( 
  1597. 'type' => self::PARSEERROR,  
  1598. 'data' => 'expected-space-or-right-bracket-in-doctype' 
  1599. )); 
  1600. $this->token['force-quirks'] = true; 
  1601. $this->token['error'] = true; 
  1602. $state = 'bogus DOCTYPE'; 
  1603. break; 
  1604.  
  1605. case 'before DOCTYPE public identifier': 
  1606. /** Consume the next input character: */ 
  1607. $char = $this->stream->char(); 
  1608.  
  1609. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  1610. /** U+0009 CHARACTER TABULATION 
  1611. U+000A LINE FEED (LF) 
  1612. U+000C FORM FEED (FF) 
  1613. U+0020 SPACE 
  1614. Stay in the before DOCTYPE public identifier state. */ 
  1615. } elseif ($char === '"') { 
  1616. /** U+0022 QUOTATION MARK (") 
  1617. Set the DOCTYPE token's public identifier to the empty 
  1618. string (not missing), then switch to the DOCTYPE public 
  1619. identifier (double-quoted) state. */ 
  1620. $this->token['public'] = ''; 
  1621. $state = 'DOCTYPE public identifier (double-quoted)'; 
  1622. } elseif ($char === "'") { 
  1623. /** U+0027 APOSTROPHE (') 
  1624. Set the DOCTYPE token's public identifier to the empty 
  1625. string (not missing), then switch to the DOCTYPE public 
  1626. identifier (single-quoted) state. */ 
  1627. $this->token['public'] = ''; 
  1628. $state = 'DOCTYPE public identifier (single-quoted)'; 
  1629. } elseif ($char === '>') { 
  1630. /** Parse error. Set the DOCTYPE token's force-quirks flag 
  1631. to on. Emit that DOCTYPE token. Switch to the data state. */ 
  1632. $this->emitToken(array( 
  1633. 'type' => self::PARSEERROR,  
  1634. 'data' => 'unexpected-end-of-doctype' 
  1635. )); 
  1636. $this->token['force-quirks'] = true; 
  1637. $this->emitToken($this->token); 
  1638. $state = 'data'; 
  1639. } elseif ($char === false) { 
  1640. /** Parse error. Set the DOCTYPE token's force-quirks 
  1641. flag to on. Emit that DOCTYPE token. Reconsume the EOF 
  1642. character in the data state. */ 
  1643. $this->emitToken(array( 
  1644. 'type' => self::PARSEERROR,  
  1645. 'data' => 'eof-in-doctype' 
  1646. )); 
  1647. $this->token['force-quirks'] = true; 
  1648. $this->emitToken($this->token); 
  1649. $this->stream->unget(); 
  1650. $state = 'data'; 
  1651. } else { 
  1652. /** Parse error. Set the DOCTYPE token's force-quirks flag 
  1653. to on. Switch to the bogus DOCTYPE state. */ 
  1654. $this->emitToken(array( 
  1655. 'type' => self::PARSEERROR,  
  1656. 'data' => 'unexpected-char-in-doctype' 
  1657. )); 
  1658. $this->token['force-quirks'] = true; 
  1659. $state = 'bogus DOCTYPE'; 
  1660. break; 
  1661.  
  1662. case 'DOCTYPE public identifier (double-quoted)': 
  1663. /** Consume the next input character: */ 
  1664. $char = $this->stream->char(); 
  1665.  
  1666. if ($char === '"') { 
  1667. /** U+0022 QUOTATION MARK (") 
  1668. Switch to the after DOCTYPE public identifier state. */ 
  1669. $state = 'after DOCTYPE public identifier'; 
  1670. } elseif ($char === '>') { 
  1671. /** U+003E GREATER-THAN SIGN (>) 
  1672. Parse error. Set the DOCTYPE token's force-quirks flag 
  1673. to on. Emit that DOCTYPE token. Switch to the data state. */ 
  1674. $this->emitToken(array( 
  1675. 'type' => self::PARSEERROR,  
  1676. 'data' => 'unexpected-end-of-doctype' 
  1677. )); 
  1678. $this->token['force-quirks'] = true; 
  1679. $this->emitToken($this->token); 
  1680. $state = 'data'; 
  1681. } elseif ($char === false) { 
  1682. /** EOF 
  1683. Parse error. Set the DOCTYPE token's force-quirks flag 
  1684. to on. Emit that DOCTYPE token. Reconsume the EOF 
  1685. character in the data state. */ 
  1686. $this->emitToken(array( 
  1687. 'type' => self::PARSEERROR,  
  1688. 'data' => 'eof-in-doctype' 
  1689. )); 
  1690. $this->token['force-quirks'] = true; 
  1691. $this->emitToken($this->token); 
  1692. $this->stream->unget(); 
  1693. $state = 'data'; 
  1694. } else { 
  1695. /** Anything else 
  1696. Append the current input character to the current 
  1697. DOCTYPE token's public identifier. Stay in the DOCTYPE 
  1698. public identifier (double-quoted) state. */ 
  1699. $this->token['public'] .= $char; 
  1700. break; 
  1701.  
  1702. case 'DOCTYPE public identifier (single-quoted)': 
  1703. /** Consume the next input character: */ 
  1704. $char = $this->stream->char(); 
  1705.  
  1706. if ($char === "'") { 
  1707. /** U+0027 APOSTROPHE (') 
  1708. Switch to the after DOCTYPE public identifier state. */ 
  1709. $state = 'after DOCTYPE public identifier'; 
  1710. } elseif ($char === '>') { 
  1711. /** U+003E GREATER-THAN SIGN (>) 
  1712. Parse error. Set the DOCTYPE token's force-quirks flag 
  1713. to on. Emit that DOCTYPE token. Switch to the data state. */ 
  1714. $this->emitToken(array( 
  1715. 'type' => self::PARSEERROR,  
  1716. 'data' => 'unexpected-end-of-doctype' 
  1717. )); 
  1718. $this->token['force-quirks'] = true; 
  1719. $this->emitToken($this->token); 
  1720. $state = 'data'; 
  1721. } elseif ($char === false) { 
  1722. /** EOF 
  1723. Parse error. Set the DOCTYPE token's force-quirks flag 
  1724. to on. Emit that DOCTYPE token. Reconsume the EOF 
  1725. character in the data state. */ 
  1726. $this->emitToken(array( 
  1727. 'type' => self::PARSEERROR,  
  1728. 'data' => 'eof-in-doctype' 
  1729. )); 
  1730. $this->token['force-quirks'] = true; 
  1731. $this->emitToken($this->token); 
  1732. $this->stream->unget(); 
  1733. $state = 'data'; 
  1734. } else { 
  1735. /** Anything else 
  1736. Append the current input character to the current 
  1737. DOCTYPE token's public identifier. Stay in the DOCTYPE 
  1738. public identifier (double-quoted) state. */ 
  1739. $this->token['public'] .= $char; 
  1740. break; 
  1741.  
  1742. case 'after DOCTYPE public identifier': 
  1743. /** Consume the next input character: */ 
  1744. $char = $this->stream->char(); 
  1745.  
  1746. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  1747. /** U+0009 CHARACTER TABULATION 
  1748. U+000A LINE FEED (LF) 
  1749. U+000C FORM FEED (FF) 
  1750. U+0020 SPACE 
  1751. Stay in the after DOCTYPE public identifier state. */ 
  1752. } elseif ($char === '"') { 
  1753. /** U+0022 QUOTATION MARK (") 
  1754. Set the DOCTYPE token's system identifier to the 
  1755. empty string (not missing), then switch to the DOCTYPE 
  1756. system identifier (double-quoted) state. */ 
  1757. $this->token['system'] = ''; 
  1758. $state = 'DOCTYPE system identifier (double-quoted)'; 
  1759. } elseif ($char === "'") { 
  1760. /** U+0027 APOSTROPHE (') 
  1761. Set the DOCTYPE token's system identifier to the 
  1762. empty string (not missing), then switch to the DOCTYPE 
  1763. system identifier (single-quoted) state. */ 
  1764. $this->token['system'] = ''; 
  1765. $state = 'DOCTYPE system identifier (single-quoted)'; 
  1766. } elseif ($char === '>') { 
  1767. /** U+003E GREATER-THAN SIGN (>) 
  1768. Emit the current DOCTYPE token. Switch to the data state. */ 
  1769. $this->emitToken($this->token); 
  1770. $state = 'data'; 
  1771. } elseif ($char === false) { 
  1772. /** Parse error. Set the DOCTYPE token's force-quirks 
  1773. flag to on. Emit that DOCTYPE token. Reconsume the EOF 
  1774. character in the data state. */ 
  1775. $this->emitToken(array( 
  1776. 'type' => self::PARSEERROR,  
  1777. 'data' => 'eof-in-doctype' 
  1778. )); 
  1779. $this->token['force-quirks'] = true; 
  1780. $this->emitToken($this->token); 
  1781. $this->stream->unget(); 
  1782. $state = 'data'; 
  1783. } else { 
  1784. /** Anything else 
  1785. Parse error. Set the DOCTYPE token's force-quirks flag 
  1786. to on. Switch to the bogus DOCTYPE state. */ 
  1787. $this->emitToken(array( 
  1788. 'type' => self::PARSEERROR,  
  1789. 'data' => 'unexpected-char-in-doctype' 
  1790. )); 
  1791. $this->token['force-quirks'] = true; 
  1792. $state = 'bogus DOCTYPE'; 
  1793. break; 
  1794.  
  1795. case 'before DOCTYPE system identifier': 
  1796. /** Consume the next input character: */ 
  1797. $char = $this->stream->char(); 
  1798.  
  1799. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  1800. /** U+0009 CHARACTER TABULATION 
  1801. U+000A LINE FEED (LF) 
  1802. U+000C FORM FEED (FF) 
  1803. U+0020 SPACE 
  1804. Stay in the before DOCTYPE system identifier state. */ 
  1805. } elseif ($char === '"') { 
  1806. /** U+0022 QUOTATION MARK (") 
  1807. Set the DOCTYPE token's system identifier to the empty 
  1808. string (not missing), then switch to the DOCTYPE system 
  1809. identifier (double-quoted) state. */ 
  1810. $this->token['system'] = ''; 
  1811. $state = 'DOCTYPE system identifier (double-quoted)'; 
  1812. } elseif ($char === "'") { 
  1813. /** U+0027 APOSTROPHE (') 
  1814. Set the DOCTYPE token's system identifier to the empty 
  1815. string (not missing), then switch to the DOCTYPE system 
  1816. identifier (single-quoted) state. */ 
  1817. $this->token['system'] = ''; 
  1818. $state = 'DOCTYPE system identifier (single-quoted)'; 
  1819. } elseif ($char === '>') { 
  1820. /** Parse error. Set the DOCTYPE token's force-quirks flag 
  1821. to on. Emit that DOCTYPE token. Switch to the data state. */ 
  1822. $this->emitToken(array( 
  1823. 'type' => self::PARSEERROR,  
  1824. 'data' => 'unexpected-char-in-doctype' 
  1825. )); 
  1826. $this->token['force-quirks'] = true; 
  1827. $this->emitToken($this->token); 
  1828. $state = 'data'; 
  1829. } elseif ($char === false) { 
  1830. /** Parse error. Set the DOCTYPE token's force-quirks 
  1831. flag to on. Emit that DOCTYPE token. Reconsume the EOF 
  1832. character in the data state. */ 
  1833. $this->emitToken(array( 
  1834. 'type' => self::PARSEERROR,  
  1835. 'data' => 'eof-in-doctype' 
  1836. )); 
  1837. $this->token['force-quirks'] = true; 
  1838. $this->emitToken($this->token); 
  1839. $this->stream->unget(); 
  1840. $state = 'data'; 
  1841. } else { 
  1842. /** Parse error. Set the DOCTYPE token's force-quirks flag 
  1843. to on. Switch to the bogus DOCTYPE state. */ 
  1844. $this->emitToken(array( 
  1845. 'type' => self::PARSEERROR,  
  1846. 'data' => 'unexpected-char-in-doctype' 
  1847. )); 
  1848. $this->token['force-quirks'] = true; 
  1849. $state = 'bogus DOCTYPE'; 
  1850. break; 
  1851.  
  1852. case 'DOCTYPE system identifier (double-quoted)': 
  1853. /** Consume the next input character: */ 
  1854. $char = $this->stream->char(); 
  1855.  
  1856. if ($char === '"') { 
  1857. /** U+0022 QUOTATION MARK (") 
  1858. Switch to the after DOCTYPE system identifier state. */ 
  1859. $state = 'after DOCTYPE system identifier'; 
  1860. } elseif ($char === '>') { 
  1861. /** U+003E GREATER-THAN SIGN (>) 
  1862. Parse error. Set the DOCTYPE token's force-quirks flag 
  1863. to on. Emit that DOCTYPE token. Switch to the data state. */ 
  1864. $this->emitToken(array( 
  1865. 'type' => self::PARSEERROR,  
  1866. 'data' => 'unexpected-end-of-doctype' 
  1867. )); 
  1868. $this->token['force-quirks'] = true; 
  1869. $this->emitToken($this->token); 
  1870. $state = 'data'; 
  1871. } elseif ($char === false) { 
  1872. /** EOF 
  1873. Parse error. Set the DOCTYPE token's force-quirks flag 
  1874. to on. Emit that DOCTYPE token. Reconsume the EOF 
  1875. character in the data state. */ 
  1876. $this->emitToken(array( 
  1877. 'type' => self::PARSEERROR,  
  1878. 'data' => 'eof-in-doctype' 
  1879. )); 
  1880. $this->token['force-quirks'] = true; 
  1881. $this->emitToken($this->token); 
  1882. $this->stream->unget(); 
  1883. $state = 'data'; 
  1884. } else { 
  1885. /** Anything else 
  1886. Append the current input character to the current 
  1887. DOCTYPE token's system identifier. Stay in the DOCTYPE 
  1888. system identifier (double-quoted) state. */ 
  1889. $this->token['system'] .= $char; 
  1890. break; 
  1891.  
  1892. case 'DOCTYPE system identifier (single-quoted)': 
  1893. /** Consume the next input character: */ 
  1894. $char = $this->stream->char(); 
  1895.  
  1896. if ($char === "'") { 
  1897. /** U+0027 APOSTROPHE (') 
  1898. Switch to the after DOCTYPE system identifier state. */ 
  1899. $state = 'after DOCTYPE system identifier'; 
  1900. } elseif ($char === '>') { 
  1901. /** U+003E GREATER-THAN SIGN (>) 
  1902. Parse error. Set the DOCTYPE token's force-quirks flag 
  1903. to on. Emit that DOCTYPE token. Switch to the data state. */ 
  1904. $this->emitToken(array( 
  1905. 'type' => self::PARSEERROR,  
  1906. 'data' => 'unexpected-end-of-doctype' 
  1907. )); 
  1908. $this->token['force-quirks'] = true; 
  1909. $this->emitToken($this->token); 
  1910. $state = 'data'; 
  1911. } elseif ($char === false) { 
  1912. /** EOF 
  1913. Parse error. Set the DOCTYPE token's force-quirks flag 
  1914. to on. Emit that DOCTYPE token. Reconsume the EOF 
  1915. character in the data state. */ 
  1916. $this->emitToken(array( 
  1917. 'type' => self::PARSEERROR,  
  1918. 'data' => 'eof-in-doctype' 
  1919. )); 
  1920. $this->token['force-quirks'] = true; 
  1921. $this->emitToken($this->token); 
  1922. $this->stream->unget(); 
  1923. $state = 'data'; 
  1924. } else { 
  1925. /** Anything else 
  1926. Append the current input character to the current 
  1927. DOCTYPE token's system identifier. Stay in the DOCTYPE 
  1928. system identifier (double-quoted) state. */ 
  1929. $this->token['system'] .= $char; 
  1930. break; 
  1931.  
  1932. case 'after DOCTYPE system identifier': 
  1933. /** Consume the next input character: */ 
  1934. $char = $this->stream->char(); 
  1935.  
  1936. if($char === "\t" || $char === "\n" || $char === "\x0c" || $char === ' ') { 
  1937. /** U+0009 CHARACTER TABULATION 
  1938. U+000A LINE FEED (LF) 
  1939. U+000C FORM FEED (FF) 
  1940. U+0020 SPACE 
  1941. Stay in the after DOCTYPE system identifier state. */ 
  1942. } elseif ($char === '>') { 
  1943. /** U+003E GREATER-THAN SIGN (>) 
  1944. Emit the current DOCTYPE token. Switch to the data state. */ 
  1945. $this->emitToken($this->token); 
  1946. $state = 'data'; 
  1947. } elseif ($char === false) { 
  1948. /** Parse error. Set the DOCTYPE token's force-quirks 
  1949. flag to on. Emit that DOCTYPE token. Reconsume the EOF 
  1950. character in the data state. */ 
  1951. $this->emitToken(array( 
  1952. 'type' => self::PARSEERROR,  
  1953. 'data' => 'eof-in-doctype' 
  1954. )); 
  1955. $this->token['force-quirks'] = true; 
  1956. $this->emitToken($this->token); 
  1957. $this->stream->unget(); 
  1958. $state = 'data'; 
  1959. } else { 
  1960. /** Anything else 
  1961. Parse error. Switch to the bogus DOCTYPE state. 
  1962. (This does not set the DOCTYPE token's force-quirks 
  1963. flag to on.) */ 
  1964. $this->emitToken(array( 
  1965. 'type' => self::PARSEERROR,  
  1966. 'data' => 'unexpected-char-in-doctype' 
  1967. )); 
  1968. $state = 'bogus DOCTYPE'; 
  1969. break; 
  1970.  
  1971. case 'bogus DOCTYPE': 
  1972. /** Consume the next input character: */ 
  1973. $char = $this->stream->char(); 
  1974.  
  1975. if ($char === '>') { 
  1976. /** U+003E GREATER-THAN SIGN (>) 
  1977. Emit the DOCTYPE token. Switch to the data state. */ 
  1978. $this->emitToken($this->token); 
  1979. $state = 'data'; 
  1980.  
  1981. } elseif($char === false) { 
  1982. /** EOF 
  1983. Emit the DOCTYPE token. Reconsume the EOF character in 
  1984. the data state. */ 
  1985. $this->emitToken($this->token); 
  1986. $this->stream->unget(); 
  1987. $state = 'data'; 
  1988.  
  1989. } else { 
  1990. /** Anything else 
  1991. Stay in the bogus DOCTYPE state. */ 
  1992. break; 
  1993.  
  1994. // case 'cdataSection': 
  1995.  
  1996.  
  1997. /** 
  1998. * Returns a serialized representation of the tree. 
  1999. */ 
  2000. public function save() { 
  2001. return $this->tree->save(); 
  2002.  
  2003. /** 
  2004. * @return HTML5_TreeBuilder The tree 
  2005. */ 
  2006. public function getTree() { 
  2007. return $this->tree; 
  2008.  
  2009. /** 
  2010. * Returns the input stream. 
  2011. */ 
  2012. public function stream() { 
  2013. return $this->stream; 
  2014.  
  2015. private function consumeCharacterReference($allowed = false, $inattr = false) { 
  2016. // This goes quite far against spec, and is far closer to the Python 
  2017. // impl., mainly because we don't do the large unconsuming the spec 
  2018. // requires. 
  2019.  
  2020. // All consumed characters. 
  2021. $chars = $this->stream->char(); 
  2022.  
  2023. /** This section defines how to consume a character 
  2024. reference. This definition is used when parsing character 
  2025. references in text and in attributes. 
  2026.   
  2027. The behavior depends on the identity of the next character 
  2028. (the one immediately after the U+0026 AMPERSAND character): */ 
  2029.  
  2030. if ( 
  2031. $chars[0] === "\x09" || 
  2032. $chars[0] === "\x0A" || 
  2033. $chars[0] === "\x0C" || 
  2034. $chars[0] === "\x20" || 
  2035. $chars[0] === '<' || 
  2036. $chars[0] === '&' || 
  2037. $chars === false || 
  2038. $chars[0] === $allowed 
  2039. ) { 
  2040. /** U+0009 CHARACTER TABULATION 
  2041. U+000A LINE FEED (LF) 
  2042. U+000C FORM FEED (FF) 
  2043. U+0020 SPACE 
  2044. U+003C LESS-THAN SIGN 
  2045. U+0026 AMPERSAND 
  2046. EOF 
  2047. The additional allowed character, if there is one 
  2048. Not a character reference. No characters are consumed,  
  2049. and nothing is returned. (This is not an error, either.) */ 
  2050. // We already consumed, so unconsume. 
  2051. $this->stream->unget(); 
  2052. return '&'; 
  2053. } elseif ($chars[0] === '#') { 
  2054. /** Consume the U+0023 NUMBER SIGN. */ 
  2055. // Um, yeah, we already did that. 
  2056. /** The behavior further depends on the character after 
  2057. the U+0023 NUMBER SIGN: */ 
  2058. $chars .= $this->stream->char(); 
  2059. if (isset($chars[1]) && ($chars[1] === 'x' || $chars[1] === 'X')) { 
  2060. /** U+0078 LATIN SMALL LETTER X 
  2061. U+0058 LATIN CAPITAL LETTER X */ 
  2062. /** Consume the X. */ 
  2063. // Um, yeah, we already did that. 
  2064. /** Follow the steps below, but using the range of 
  2065. characters U+0030 DIGIT ZERO through to U+0039 DIGIT 
  2066. NINE, U+0061 LATIN SMALL LETTER A through to U+0066 
  2067. LATIN SMALL LETTER F, and U+0041 LATIN CAPITAL LETTER 
  2068. A, through to U+0046 LATIN CAPITAL LETTER F (in other 
  2069. words, 0123456789, ABCDEF, abcdef). */ 
  2070. $char_class = self::HEX; 
  2071. /** When it comes to interpreting the 
  2072. number, interpret it as a hexadecimal number. */ 
  2073. $hex = true; 
  2074. } else { 
  2075. /** Anything else */ 
  2076. // Unconsume because we shouldn't have consumed this. 
  2077. $chars = $chars[0]; 
  2078. $this->stream->unget(); 
  2079. /** Follow the steps below, but using the range of 
  2080. characters U+0030 DIGIT ZERO through to U+0039 DIGIT 
  2081. NINE (i.e. just 0123456789). */ 
  2082. $char_class = self::DIGIT; 
  2083. /** When it comes to interpreting the number,  
  2084. interpret it as a decimal number. */ 
  2085. $hex = false; 
  2086.  
  2087. /** Consume as many characters as match the range of characters given above. */ 
  2088. $consumed = $this->stream->charsWhile($char_class); 
  2089. if ($consumed === '' || $consumed === false) { 
  2090. /** If no characters match the range, then don't consume 
  2091. any characters (and unconsume the U+0023 NUMBER SIGN 
  2092. character and, if appropriate, the X character). This 
  2093. is a parse error; nothing is returned. */ 
  2094. $this->emitToken(array( 
  2095. 'type' => self::PARSEERROR,  
  2096. 'data' => 'expected-numeric-entity' 
  2097. )); 
  2098. return '&' . $chars; 
  2099. } else { 
  2100. /** Otherwise, if the next character is a U+003B SEMICOLON,  
  2101. consume that too. If it isn't, there is a parse error. */ 
  2102. if ($this->stream->char() !== ';') { 
  2103. $this->stream->unget(); 
  2104. $this->emitToken(array( 
  2105. 'type' => self::PARSEERROR,  
  2106. 'data' => 'numeric-entity-without-semicolon' 
  2107. )); 
  2108.  
  2109. /** If one or more characters match the range, then take 
  2110. them all and interpret the string of characters as a number 
  2111. (either hexadecimal or decimal as appropriate). */ 
  2112. $codepoint = $hex ? hexdec($consumed) : (int) $consumed; 
  2113.  
  2114. /** If that number is one of the numbers in the first column 
  2115. of the following table, then this is a parse error. Find the 
  2116. row with that number in the first column, and return a 
  2117. character token for the Unicode character given in the 
  2118. second column of that row. */ 
  2119. $new_codepoint = HTML5_Data::getRealCodepoint($codepoint); 
  2120. if ($new_codepoint) { 
  2121. $this->emitToken(array( 
  2122. 'type' => self::PARSEERROR,  
  2123. 'data' => 'illegal-windows-1252-entity' 
  2124. )); 
  2125. return HTML5_Data::utf8chr($new_codepoint); 
  2126. } else { 
  2127. /** Otherwise, if the number is greater than 0x10FFFF, then  
  2128. * this is a parse error. Return a U+FFFD REPLACEMENT  
  2129. * CHARACTER. */ 
  2130. if ($codepoint > 0x10FFFF) { 
  2131. $this->emitToken(array( 
  2132. 'type' => self::PARSEERROR,  
  2133. 'data' => 'overlong-character-entity' // XXX probably not correct 
  2134. )); 
  2135. return "\xEF\xBF\xBD"; 
  2136. /** Otherwise, return a character token for the Unicode  
  2137. * character whose code point is that number. If the  
  2138. * number is in the range 0x0001 to 0x0008, 0x000E to  
  2139. * 0x001F, 0x007F to 0x009F, 0xD800 to 0xDFFF, 0xFDD0 to  
  2140. * 0xFDEF, or is one of 0x000B, 0xFFFE, 0xFFFF, 0x1FFFE,  
  2141. * 0x1FFFF, 0x2FFFE, 0x2FFFF, 0x3FFFE, 0x3FFFF, 0x4FFFE,  
  2142. * 0x4FFFF, 0x5FFFE, 0x5FFFF, 0x6FFFE, 0x6FFFF, 0x7FFFE,  
  2143. * 0x7FFFF, 0x8FFFE, 0x8FFFF, 0x9FFFE, 0x9FFFF, 0xAFFFE,  
  2144. * 0xAFFFF, 0xBFFFE, 0xBFFFF, 0xCFFFE, 0xCFFFF, 0xDFFFE,  
  2145. * 0xDFFFF, 0xEFFFE, 0xEFFFF, 0xFFFFE, 0xFFFFF, 0x10FFFE,  
  2146. * or 0x10FFFF, then this is a parse error. */ 
  2147. // && has higher precedence than || 
  2148. if ( 
  2149. $codepoint >= 0x0000 && $codepoint <= 0x0008 || 
  2150. $codepoint === 0x000B || 
  2151. $codepoint >= 0x000E && $codepoint <= 0x001F || 
  2152. $codepoint >= 0x007F && $codepoint <= 0x009F || 
  2153. $codepoint >= 0xD800 && $codepoint <= 0xDFFF || 
  2154. $codepoint >= 0xFDD0 && $codepoint <= 0xFDEF || 
  2155. ($codepoint & 0xFFFE) === 0xFFFE || 
  2156. $codepoint == 0x10FFFF || $codepoint == 0x10FFFE 
  2157. ) { 
  2158. $this->emitToken(array( 
  2159. 'type' => self::PARSEERROR,  
  2160. 'data' => 'illegal-codepoint-for-numeric-entity' 
  2161. )); 
  2162. return HTML5_Data::utf8chr($codepoint); 
  2163.  
  2164. } else { 
  2165. /** Anything else */ 
  2166.  
  2167. /** Consume the maximum number of characters possible,  
  2168. with the consumed characters matching one of the 
  2169. identifiers in the first column of the named character 
  2170. references table (in a case-sensitive manner). */ 
  2171. // What we actually do here is consume as much as we can while it 
  2172. // matches the start of one of the identifiers in the first column. 
  2173.  
  2174. $refs = HTML5_Data::getNamedCharacterReferences(); 
  2175.  
  2176. // Get the longest string which is the start of an identifier 
  2177. // ($chars) as well as the longest identifier which matches ($id) 
  2178. // and its codepoint ($codepoint). 
  2179. $codepoint = false; 
  2180. $char = $chars; 
  2181. while ($char !== false && isset($refs[$char])) { 
  2182. $refs = $refs[$char]; 
  2183. if (isset($refs['codepoint'])) { 
  2184. $id = $chars; 
  2185. $codepoint = $refs['codepoint']; 
  2186. $chars .= $char = $this->stream->char(); 
  2187.  
  2188. // Unconsume the one character we just took which caused the while 
  2189. // statement to fail. This could be anything and could cause state 
  2190. // changes (as if it matches the while loop it must be 
  2191. // alphanumeric so we can just concat it to whatever we get later). 
  2192. $this->stream->unget(); 
  2193. if ($char !== false) { 
  2194. $chars = substr($chars, 0, -1); 
  2195.  
  2196. /** If no match can be made, then this is a parse error. 
  2197. No characters are consumed, and nothing is returned. */ 
  2198. if (!$codepoint) { 
  2199. $this->emitToken(array( 
  2200. 'type' => self::PARSEERROR,  
  2201. 'data' => 'expected-named-entity' 
  2202. )); 
  2203. return '&' . $chars; 
  2204.  
  2205. /** If the last character matched is not a U+003B SEMICOLON 
  2206. (;), there is a parse error. */ 
  2207. $semicolon = true; 
  2208. if (substr($id, -1) !== ';') { 
  2209. $this->emitToken(array( 
  2210. 'type' => self::PARSEERROR,  
  2211. 'data' => 'named-entity-without-semicolon' 
  2212. )); 
  2213. $semicolon = false; 
  2214.  
  2215. /** If the character reference is being consumed as part of 
  2216. an attribute, and the last character matched is not a 
  2217. U+003B SEMICOLON (;), and the next character is in the 
  2218. range U+0030 DIGIT ZERO to U+0039 DIGIT NINE, U+0041 
  2219. LATIN CAPITAL LETTER A to U+005A LATIN CAPITAL LETTER Z,  
  2220. or U+0061 LATIN SMALL LETTER A to U+007A LATIN SMALL LETTER Z,  
  2221. then, for historical reasons, all the characters that were 
  2222. matched after the U+0026 AMPERSAND (&) must be unconsumed,  
  2223. and nothing is returned. */ 
  2224. if ($inattr && !$semicolon) { 
  2225. // The next character is either the next character in $chars or in the stream. 
  2226. if (strlen($chars) > strlen($id)) { 
  2227. $next = substr($chars, strlen($id), 1); 
  2228. } else { 
  2229. $next = $this->stream->char(); 
  2230. $this->stream->unget(); 
  2231. if ( 
  2232. '0' <= $next && $next <= '9' || 
  2233. 'A' <= $next && $next <= 'Z' || 
  2234. 'a' <= $next && $next <= 'z' 
  2235. ) { 
  2236. return '&' . $chars; 
  2237.  
  2238. /** Otherwise, return a character token for the character 
  2239. corresponding to the character reference name (as given 
  2240. by the second column of the named character references table). */ 
  2241. return HTML5_Data::utf8chr($codepoint) . substr($chars, strlen($id)); 
  2242.  
  2243. private function characterReferenceInAttributeValue($allowed = false) { 
  2244. /** Attempt to consume a character reference. */ 
  2245. $entity = $this->consumeCharacterReference($allowed, true); 
  2246.  
  2247. /** If nothing is returned, append a U+0026 AMPERSAND 
  2248. character to the current attribute's value. 
  2249.   
  2250. Otherwise, append the returned character token to the 
  2251. current attribute's value. */ 
  2252. $char = (!$entity) 
  2253. ? '&' 
  2254. : $entity; 
  2255.  
  2256. $last = count($this->token['attr']) - 1; 
  2257. $this->token['attr'][$last]['value'] .= $char; 
  2258.  
  2259. /** Finally, switch back to the attribute value state that you 
  2260. were in when were switched into this state. */ 
  2261.  
  2262. /** 
  2263. * Emits a token, passing it on to the tree builder. 
  2264. */ 
  2265. protected function emitToken($token, $checkStream = true, $dry = false) { 
  2266. if ($checkStream) { 
  2267. // Emit errors from input stream. 
  2268. while ($this->stream->errors) { 
  2269. $this->emitToken(array_shift($this->stream->errors), false); 
  2270. if($token['type'] === self::ENDTAG && !empty($token['attr'])) { 
  2271. for ($i = 0; $i < count($token['attr']); $i++) { 
  2272. $this->emitToken(array( 
  2273. 'type' => self::PARSEERROR,  
  2274. 'data' => 'attributes-in-end-tag' 
  2275. )); 
  2276. if($token['type'] === self::ENDTAG && !empty($token['self-closing'])) { 
  2277. $this->emitToken(array( 
  2278. 'type' => self::PARSEERROR,  
  2279. 'data' => 'self-closing-flag-on-end-tag',  
  2280. )); 
  2281. if($token['type'] === self::STARTTAG) { 
  2282. // This could be changed to actually pass the tree-builder a hash 
  2283. $hash = array(); 
  2284. foreach ($token['attr'] as $keypair) { 
  2285. if (isset($hash[$keypair['name']])) { 
  2286. $this->emitToken(array( 
  2287. 'type' => self::PARSEERROR,  
  2288. 'data' => 'duplicate-attribute',  
  2289. )); 
  2290. } else { 
  2291. $hash[$keypair['name']] = $keypair['value']; 
  2292.  
  2293. if(!$dry) { 
  2294. // the current structure of attributes is not a terribly good one 
  2295. $this->tree->emitToken($token); 
  2296.  
  2297. if(!$dry && is_int($this->tree->content_model)) { 
  2298. $this->content_model = $this->tree->content_model; 
  2299. $this->tree->content_model = null; 
  2300.  
  2301. } elseif($token['type'] === self::ENDTAG) { 
  2302. $this->content_model = self::PCDATA;