Tokenizer.php 113 KB

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