PageRenderTime 62ms CodeModel.GetById 28ms 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

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

  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

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