PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/html5lib/library/HTML5/Tokenizer.php

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