HTMLTokenizer.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibTextCodec/Decoder.h>
  27. #include <LibWeb/Parser/Entities.h>
  28. #include <LibWeb/Parser/HTMLToken.h>
  29. #include <LibWeb/Parser/HTMLTokenizer.h>
  30. #include <ctype.h>
  31. #pragma GCC diagnostic ignored "-Wunused-label"
  32. //#define TOKENIZER_TRACE
  33. #ifdef TOKENIZER_TRACE
  34. # define PARSE_ERROR() \
  35. do { \
  36. dbg() << "Parse error (tokenization)" << __PRETTY_FUNCTION__ << " @ " << __LINE__; \
  37. } while (0)
  38. #else
  39. # define PARSE_ERROR()
  40. #endif
  41. #define CONSUME_NEXT_INPUT_CHARACTER \
  42. current_input_character = next_codepoint();
  43. #define SWITCH_TO(new_state) \
  44. do { \
  45. will_switch_to(State::new_state); \
  46. m_state = State::new_state; \
  47. CONSUME_NEXT_INPUT_CHARACTER; \
  48. goto new_state; \
  49. } while (0)
  50. #define RECONSUME_IN(new_state) \
  51. do { \
  52. will_reconsume_in(State::new_state); \
  53. m_state = State::new_state; \
  54. goto new_state; \
  55. } while (0)
  56. #define SWITCH_TO_RETURN_STATE \
  57. do { \
  58. will_switch_to(m_return_state); \
  59. m_state = m_return_state; \
  60. goto _StartOfFunction; \
  61. } while (0)
  62. #define RECONSUME_IN_RETURN_STATE \
  63. do { \
  64. will_reconsume_in(m_return_state); \
  65. m_state = m_return_state; \
  66. goto _StartOfFunction; \
  67. } while (0)
  68. #define SWITCH_TO_AND_EMIT_CURRENT_TOKEN(new_state) \
  69. do { \
  70. will_switch_to(State::new_state); \
  71. m_state = State::new_state; \
  72. will_emit(m_current_token); \
  73. m_queued_tokens.enqueue(m_current_token); \
  74. return m_queued_tokens.dequeue(); \
  75. } while (0)
  76. #define EMIT_CHARACTER_AND_RECONSUME_IN(codepoint, new_state) \
  77. do { \
  78. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint)); \
  79. will_reconsume_in(State::new_state); \
  80. m_state = State::new_state; \
  81. goto new_state; \
  82. } while (0)
  83. #define FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE \
  84. do { \
  85. for (auto codepoint : m_temporary_buffer) { \
  86. if (consumed_as_part_of_an_attribute()) { \
  87. m_current_token.m_tag.attributes.last().value_builder.append_codepoint(codepoint); \
  88. } else { \
  89. create_new_token(HTMLToken::Type::Character); \
  90. m_current_token.m_comment_or_character.data.append_codepoint(codepoint); \
  91. m_queued_tokens.enqueue(m_current_token); \
  92. } \
  93. } \
  94. } while (0)
  95. #define DONT_CONSUME_NEXT_INPUT_CHARACTER \
  96. do { \
  97. m_utf8_iterator = m_prev_utf8_iterator; \
  98. } while (0)
  99. #define ON(codepoint) \
  100. if (current_input_character.has_value() && current_input_character.value() == codepoint)
  101. #define ON_EOF \
  102. if (!current_input_character.has_value())
  103. #define ON_ASCII_ALPHA \
  104. if (current_input_character.has_value() && isalpha(current_input_character.value()))
  105. #define ON_ASCII_ALPHANUMERIC \
  106. if (current_input_character.has_value() && isalnum(current_input_character.value()))
  107. #define ON_ASCII_UPPER_ALPHA \
  108. if (current_input_character.has_value() && current_input_character.value() >= 'A' && current_input_character.value() <= 'Z')
  109. #define ON_ASCII_LOWER_ALPHA \
  110. if (current_input_character.has_value() && current_input_character.value() >= 'a' && current_input_character.value() <= 'z')
  111. #define ON_ASCII_DIGIT \
  112. if (current_input_character.has_value() && isxdigit(current_input_character.value()))
  113. #define ON_ASCII_HEX_DIGIT \
  114. if (current_input_character.has_value() && isxdigit(current_input_character.value()))
  115. #define ON_WHITESPACE \
  116. if (current_input_character.has_value() && (current_input_character.value() == '\t' || current_input_character.value() == '\n' || current_input_character.value() == '\f' || current_input_character.value() == ' '))
  117. #define ANYTHING_ELSE if (1)
  118. #define EMIT_EOF \
  119. do { \
  120. if (m_has_emitted_eof) \
  121. return {}; \
  122. m_has_emitted_eof = true; \
  123. create_new_token(HTMLToken::Type::EndOfFile); \
  124. will_emit(m_current_token); \
  125. m_queued_tokens.enqueue(m_current_token); \
  126. return m_queued_tokens.dequeue(); \
  127. } while (0)
  128. #define EMIT_CURRENT_TOKEN \
  129. do { \
  130. will_emit(m_current_token); \
  131. m_queued_tokens.enqueue(m_current_token); \
  132. return m_queued_tokens.dequeue(); \
  133. } while (0)
  134. #define EMIT_CHARACTER(codepoint) \
  135. do { \
  136. create_new_token(HTMLToken::Type::Character); \
  137. m_current_token.m_comment_or_character.data.append_codepoint(codepoint); \
  138. m_queued_tokens.enqueue(m_current_token); \
  139. return m_queued_tokens.dequeue(); \
  140. } while (0)
  141. #define EMIT_CURRENT_CHARACTER \
  142. EMIT_CHARACTER(current_input_character.value());
  143. #define BEGIN_STATE(state) \
  144. state: \
  145. case State::state: { \
  146. { \
  147. {
  148. #define END_STATE \
  149. ASSERT_NOT_REACHED(); \
  150. break; \
  151. } \
  152. } \
  153. }
  154. static inline bool is_surrogate(u32 codepoint)
  155. {
  156. return (codepoint & 0xfffff800) == 0xd800;
  157. }
  158. static inline bool is_noncharacter(u32 codepoint)
  159. {
  160. return codepoint >= 0xfdd0 && (codepoint <= 0xfdef || (codepoint & 0xfffe) == 0xfffe) && codepoint <= 0x10ffff;
  161. }
  162. static inline bool is_c0_control(u32 codepoint)
  163. {
  164. return codepoint <= 0x1f;
  165. }
  166. static inline bool is_control(u32 codepoint)
  167. {
  168. return is_c0_control(codepoint) || (codepoint >= 0x7f && codepoint <= 0x9f);
  169. }
  170. namespace Web {
  171. Optional<u32> HTMLTokenizer::next_codepoint()
  172. {
  173. if (m_utf8_iterator == m_utf8_view.end())
  174. return {};
  175. m_prev_utf8_iterator = m_utf8_iterator;
  176. ++m_utf8_iterator;
  177. #ifdef TOKENIZER_TRACE
  178. dbg() << "(Tokenizer) Next codepoint: " << (char)*m_prev_utf8_iterator;
  179. #endif
  180. return *m_prev_utf8_iterator;
  181. }
  182. Optional<u32> HTMLTokenizer::peek_codepoint(size_t offset) const
  183. {
  184. auto it = m_utf8_iterator;
  185. for (size_t i = 0; i < offset && it != m_utf8_view.end(); ++i)
  186. ++it;
  187. if (it == m_utf8_view.end())
  188. return {};
  189. return *it;
  190. }
  191. Optional<HTMLToken> HTMLTokenizer::next_token()
  192. {
  193. _StartOfFunction:
  194. if (!m_queued_tokens.is_empty())
  195. return m_queued_tokens.dequeue();
  196. for (;;) {
  197. auto current_input_character = next_codepoint();
  198. switch (m_state) {
  199. BEGIN_STATE(Data)
  200. {
  201. ON('&')
  202. {
  203. m_return_state = State::Data;
  204. SWITCH_TO(CharacterReference);
  205. }
  206. ON('<')
  207. {
  208. SWITCH_TO(TagOpen);
  209. }
  210. ON(0)
  211. {
  212. PARSE_ERROR();
  213. EMIT_CURRENT_CHARACTER;
  214. }
  215. ON_EOF
  216. {
  217. EMIT_EOF;
  218. }
  219. ANYTHING_ELSE
  220. {
  221. EMIT_CURRENT_CHARACTER;
  222. }
  223. }
  224. END_STATE
  225. BEGIN_STATE(TagOpen)
  226. {
  227. ON('!')
  228. {
  229. SWITCH_TO(MarkupDeclarationOpen);
  230. }
  231. ON('/')
  232. {
  233. SWITCH_TO(EndTagOpen);
  234. }
  235. ON_ASCII_ALPHA
  236. {
  237. create_new_token(HTMLToken::Type::StartTag);
  238. RECONSUME_IN(TagName);
  239. }
  240. ON('?')
  241. {
  242. PARSE_ERROR();
  243. create_new_token(HTMLToken::Type::Comment);
  244. RECONSUME_IN(BogusComment);
  245. }
  246. ON_EOF
  247. {
  248. TODO();
  249. }
  250. ANYTHING_ELSE
  251. {
  252. PARSE_ERROR();
  253. EMIT_CHARACTER('<');
  254. RECONSUME_IN(Data);
  255. }
  256. }
  257. END_STATE
  258. BEGIN_STATE(TagName)
  259. {
  260. ON_WHITESPACE
  261. {
  262. SWITCH_TO(BeforeAttributeName);
  263. }
  264. ON('/')
  265. {
  266. SWITCH_TO(SelfClosingStartTag);
  267. }
  268. ON('>')
  269. {
  270. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  271. }
  272. ON_ASCII_UPPER_ALPHA
  273. {
  274. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  275. continue;
  276. }
  277. ON(0)
  278. {
  279. PARSE_ERROR();
  280. m_current_token.m_tag.tag_name.append_codepoint(0xFFFD);
  281. continue;
  282. }
  283. ON_EOF
  284. {
  285. PARSE_ERROR();
  286. EMIT_EOF;
  287. }
  288. ANYTHING_ELSE
  289. {
  290. m_current_token.m_tag.tag_name.append_codepoint(current_input_character.value());
  291. continue;
  292. }
  293. }
  294. END_STATE
  295. BEGIN_STATE(EndTagOpen)
  296. {
  297. ON_ASCII_ALPHA
  298. {
  299. create_new_token(HTMLToken::Type::EndTag);
  300. RECONSUME_IN(TagName);
  301. }
  302. ON('>')
  303. {
  304. PARSE_ERROR();
  305. SWITCH_TO(Data);
  306. }
  307. ON_EOF
  308. {
  309. PARSE_ERROR();
  310. // FIXME: Emit a U+003C LESS-THAN SIGN character token, a U+002F SOLIDUS character token and an end-of-file token.
  311. continue;
  312. }
  313. ANYTHING_ELSE
  314. {
  315. PARSE_ERROR();
  316. create_new_token(HTMLToken::Type::Comment);
  317. RECONSUME_IN(BogusComment);
  318. }
  319. }
  320. END_STATE
  321. BEGIN_STATE(MarkupDeclarationOpen)
  322. {
  323. DONT_CONSUME_NEXT_INPUT_CHARACTER;
  324. if (consume_next_if_match("--")) {
  325. create_new_token(HTMLToken::Type::Comment);
  326. SWITCH_TO(CommentStart);
  327. }
  328. if (consume_next_if_match("DOCTYPE", CaseSensitivity::CaseInsensitive)) {
  329. SWITCH_TO(DOCTYPE);
  330. }
  331. if (consume_next_if_match("[CDATA[")) {
  332. TODO();
  333. }
  334. ANYTHING_ELSE
  335. {
  336. PARSE_ERROR();
  337. create_new_token(HTMLToken::Type::Comment);
  338. SWITCH_TO(BogusComment);
  339. }
  340. }
  341. END_STATE
  342. BEGIN_STATE(BogusComment)
  343. {
  344. ON('>')
  345. {
  346. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  347. }
  348. ON_EOF
  349. {
  350. m_queued_tokens.enqueue(m_current_token);
  351. EMIT_EOF;
  352. }
  353. ON(0)
  354. {
  355. PARSE_ERROR();
  356. m_current_token.m_comment_or_character.data.append_codepoint(0xFFFD);
  357. continue;
  358. }
  359. ANYTHING_ELSE
  360. {
  361. m_current_token.m_comment_or_character.data.append_codepoint(current_input_character.value());
  362. continue;
  363. }
  364. }
  365. END_STATE
  366. BEGIN_STATE(DOCTYPE)
  367. {
  368. ON_WHITESPACE
  369. {
  370. SWITCH_TO(BeforeDOCTYPEName);
  371. }
  372. ON('>')
  373. {
  374. RECONSUME_IN(BeforeDOCTYPEName);
  375. }
  376. ON_EOF
  377. {
  378. TODO();
  379. }
  380. ANYTHING_ELSE
  381. {
  382. TODO();
  383. }
  384. }
  385. END_STATE
  386. BEGIN_STATE(BeforeDOCTYPEName)
  387. {
  388. ON_WHITESPACE
  389. {
  390. continue;
  391. }
  392. ON_ASCII_UPPER_ALPHA
  393. {
  394. create_new_token(HTMLToken::Type::DOCTYPE);
  395. m_current_token.m_doctype.name.append(tolower(current_input_character.value()));
  396. SWITCH_TO(DOCTYPEName);
  397. }
  398. ON(0)
  399. {
  400. TODO();
  401. }
  402. ON('>')
  403. {
  404. TODO();
  405. }
  406. ON_EOF
  407. {
  408. TODO();
  409. }
  410. ANYTHING_ELSE
  411. {
  412. create_new_token(HTMLToken::Type::DOCTYPE);
  413. m_current_token.m_doctype.name.append_codepoint(current_input_character.value());
  414. SWITCH_TO(DOCTYPEName);
  415. }
  416. }
  417. END_STATE
  418. BEGIN_STATE(DOCTYPEName)
  419. {
  420. ON_WHITESPACE
  421. {
  422. SWITCH_TO(AfterDOCTYPEName);
  423. }
  424. ON('>')
  425. {
  426. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  427. }
  428. ON_ASCII_UPPER_ALPHA
  429. {
  430. m_current_token.m_doctype.name.append(tolower(current_input_character.value()));
  431. }
  432. ON(0)
  433. {
  434. TODO();
  435. }
  436. ON_EOF
  437. {
  438. TODO();
  439. }
  440. ANYTHING_ELSE
  441. {
  442. m_current_token.m_doctype.name.append_codepoint(current_input_character.value());
  443. continue;
  444. }
  445. }
  446. END_STATE
  447. BEGIN_STATE(AfterDOCTYPEName)
  448. {
  449. ON_WHITESPACE
  450. {
  451. continue;
  452. }
  453. ON('>')
  454. {
  455. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  456. }
  457. ON_EOF
  458. {
  459. TODO();
  460. }
  461. ANYTHING_ELSE
  462. {
  463. if (toupper(current_input_character.value()) == 'P' && consume_next_if_match("UBLIC", CaseSensitivity::CaseInsensitive)) {
  464. SWITCH_TO(AfterDOCTYPEPublicKeyword);
  465. }
  466. if (toupper(current_input_character.value()) == 'S' && consume_next_if_match("YSTEM", CaseSensitivity::CaseInsensitive)) {
  467. SWITCH_TO(AfterDOCTYPESystemKeyword);
  468. }
  469. TODO();
  470. }
  471. }
  472. END_STATE
  473. BEGIN_STATE(AfterDOCTYPEPublicKeyword)
  474. {
  475. ON_WHITESPACE
  476. {
  477. SWITCH_TO(BeforeDOCTYPEPublicIdentifier);
  478. }
  479. ON('"')
  480. {
  481. TODO();
  482. }
  483. ON('\'')
  484. {
  485. TODO();
  486. }
  487. ON('>')
  488. {
  489. TODO();
  490. }
  491. ON_EOF
  492. {
  493. TODO();
  494. }
  495. ANYTHING_ELSE
  496. {
  497. TODO();
  498. }
  499. }
  500. END_STATE
  501. BEGIN_STATE(AfterDOCTYPESystemKeyword)
  502. {
  503. ON_WHITESPACE
  504. {
  505. SWITCH_TO(BeforeDOCTYPESystemIdentifier);
  506. }
  507. ON('"')
  508. {
  509. TODO();
  510. }
  511. ON('\'')
  512. {
  513. TODO();
  514. }
  515. ON('>')
  516. {
  517. TODO();
  518. }
  519. ON_EOF
  520. {
  521. TODO();
  522. }
  523. ANYTHING_ELSE
  524. {
  525. TODO();
  526. }
  527. }
  528. END_STATE
  529. BEGIN_STATE(BeforeDOCTYPEPublicIdentifier)
  530. {
  531. ON_WHITESPACE
  532. {
  533. continue;
  534. }
  535. ON('"')
  536. {
  537. m_current_token.m_doctype.public_identifier.clear();
  538. SWITCH_TO(DOCTYPEPublicIdentifierDoubleQuoted);
  539. }
  540. ON('\'')
  541. {
  542. m_current_token.m_doctype.public_identifier.clear();
  543. SWITCH_TO(DOCTYPEPublicIdentifierSingleQuoted);
  544. }
  545. ON('>')
  546. {
  547. TODO();
  548. }
  549. ON_EOF
  550. {
  551. TODO();
  552. }
  553. ANYTHING_ELSE
  554. {
  555. TODO();
  556. }
  557. }
  558. END_STATE
  559. BEGIN_STATE(BeforeDOCTYPESystemIdentifier)
  560. {
  561. ON_WHITESPACE
  562. {
  563. continue;
  564. }
  565. ON('"')
  566. {
  567. m_current_token.m_doctype.system_identifier.clear();
  568. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  569. }
  570. ON('\'')
  571. {
  572. m_current_token.m_doctype.system_identifier.clear();
  573. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  574. }
  575. ON('>')
  576. {
  577. TODO();
  578. }
  579. ON_EOF
  580. {
  581. TODO();
  582. }
  583. ANYTHING_ELSE
  584. {
  585. TODO();
  586. }
  587. }
  588. END_STATE
  589. BEGIN_STATE(DOCTYPEPublicIdentifierDoubleQuoted)
  590. {
  591. ON('"')
  592. {
  593. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  594. }
  595. ON(0)
  596. {
  597. TODO();
  598. }
  599. ON('>')
  600. {
  601. TODO();
  602. }
  603. ON_EOF
  604. {
  605. TODO();
  606. }
  607. ANYTHING_ELSE
  608. {
  609. m_current_token.m_doctype.public_identifier.append_codepoint(current_input_character.value());
  610. continue;
  611. }
  612. }
  613. END_STATE
  614. BEGIN_STATE(DOCTYPEPublicIdentifierSingleQuoted)
  615. {
  616. ON('\'')
  617. {
  618. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  619. }
  620. ON(0)
  621. {
  622. TODO();
  623. }
  624. ON('>')
  625. {
  626. TODO();
  627. }
  628. ON_EOF
  629. {
  630. TODO();
  631. }
  632. ANYTHING_ELSE
  633. {
  634. m_current_token.m_doctype.public_identifier.append_codepoint(current_input_character.value());
  635. continue;
  636. }
  637. }
  638. END_STATE
  639. BEGIN_STATE(DOCTYPESystemIdentifierDoubleQuoted)
  640. {
  641. ON('"')
  642. {
  643. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  644. }
  645. ON(0)
  646. {
  647. TODO();
  648. }
  649. ON('>')
  650. {
  651. TODO();
  652. }
  653. ON_EOF
  654. {
  655. TODO();
  656. }
  657. ANYTHING_ELSE
  658. {
  659. m_current_token.m_doctype.system_identifier.append_codepoint(current_input_character.value());
  660. continue;
  661. }
  662. }
  663. END_STATE
  664. BEGIN_STATE(DOCTYPESystemIdentifierSingleQuoted)
  665. {
  666. ON('\'')
  667. {
  668. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  669. }
  670. ON(0)
  671. {
  672. TODO();
  673. }
  674. ON('>')
  675. {
  676. TODO();
  677. }
  678. ON_EOF
  679. {
  680. TODO();
  681. }
  682. ANYTHING_ELSE
  683. {
  684. m_current_token.m_doctype.system_identifier.append_codepoint(current_input_character.value());
  685. continue;
  686. }
  687. }
  688. END_STATE
  689. BEGIN_STATE(AfterDOCTYPEPublicIdentifier)
  690. {
  691. ON_WHITESPACE
  692. {
  693. SWITCH_TO(BetweenDOCTYPEPublicAndSystemIdentifiers);
  694. }
  695. ON('>')
  696. {
  697. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  698. }
  699. ON('"')
  700. {
  701. TODO();
  702. }
  703. ON('\'')
  704. {
  705. TODO();
  706. }
  707. ON_EOF
  708. {
  709. TODO();
  710. }
  711. ANYTHING_ELSE
  712. {
  713. TODO();
  714. }
  715. }
  716. END_STATE
  717. BEGIN_STATE(BetweenDOCTYPEPublicAndSystemIdentifiers)
  718. {
  719. ON_WHITESPACE
  720. {
  721. continue;
  722. }
  723. ON('>')
  724. {
  725. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  726. }
  727. ON('"')
  728. {
  729. m_current_token.m_doctype.system_identifier.clear();
  730. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  731. }
  732. ON('\'')
  733. {
  734. m_current_token.m_doctype.system_identifier.clear();
  735. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  736. }
  737. ON_EOF
  738. {
  739. TODO();
  740. }
  741. ANYTHING_ELSE
  742. {
  743. TODO();
  744. }
  745. }
  746. END_STATE
  747. BEGIN_STATE(AfterDOCTYPESystemIdentifier)
  748. {
  749. ON_WHITESPACE
  750. {
  751. continue;
  752. }
  753. ON('>')
  754. {
  755. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  756. }
  757. ON_EOF
  758. {
  759. TODO();
  760. }
  761. ANYTHING_ELSE
  762. {
  763. TODO();
  764. }
  765. }
  766. END_STATE
  767. BEGIN_STATE(BeforeAttributeName)
  768. {
  769. ON_WHITESPACE
  770. {
  771. continue;
  772. }
  773. ON('/')
  774. {
  775. RECONSUME_IN(AfterAttributeName);
  776. }
  777. ON('>')
  778. {
  779. RECONSUME_IN(AfterAttributeName);
  780. }
  781. ON_EOF
  782. {
  783. RECONSUME_IN(AfterAttributeName);
  784. }
  785. ON('=')
  786. {
  787. TODO();
  788. }
  789. ANYTHING_ELSE
  790. {
  791. m_current_token.m_tag.attributes.append(HTMLToken::AttributeBuilder());
  792. RECONSUME_IN(AttributeName);
  793. }
  794. }
  795. END_STATE
  796. BEGIN_STATE(SelfClosingStartTag)
  797. {
  798. ON('>')
  799. {
  800. m_current_token.m_tag.self_closing = true;
  801. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  802. }
  803. ON_EOF
  804. {
  805. PARSE_ERROR();
  806. EMIT_EOF;
  807. }
  808. ANYTHING_ELSE
  809. {
  810. PARSE_ERROR();
  811. RECONSUME_IN(BeforeAttributeName);
  812. }
  813. }
  814. END_STATE
  815. BEGIN_STATE(AttributeName)
  816. {
  817. ON_WHITESPACE
  818. {
  819. RECONSUME_IN(AfterAttributeName);
  820. }
  821. ON('/')
  822. {
  823. RECONSUME_IN(AfterAttributeName);
  824. }
  825. ON('>')
  826. {
  827. RECONSUME_IN(AfterAttributeName);
  828. }
  829. ON_EOF
  830. {
  831. RECONSUME_IN(AfterAttributeName);
  832. }
  833. ON('=')
  834. {
  835. SWITCH_TO(BeforeAttributeValue);
  836. }
  837. ANYTHING_ELSE
  838. {
  839. m_current_token.m_tag.attributes.last().name_builder.append_codepoint(current_input_character.value());
  840. continue;
  841. }
  842. }
  843. END_STATE
  844. BEGIN_STATE(AfterAttributeName)
  845. {
  846. ON_WHITESPACE
  847. {
  848. continue;
  849. }
  850. ON('/')
  851. {
  852. SWITCH_TO(SelfClosingStartTag);
  853. }
  854. ON('=')
  855. {
  856. SWITCH_TO(BeforeAttributeValue);
  857. }
  858. ON('>')
  859. {
  860. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  861. }
  862. ON_EOF
  863. {
  864. TODO();
  865. }
  866. ANYTHING_ELSE
  867. {
  868. m_current_token.m_tag.attributes.append(HTMLToken::AttributeBuilder());
  869. RECONSUME_IN(AttributeName);
  870. }
  871. }
  872. END_STATE
  873. BEGIN_STATE(BeforeAttributeValue)
  874. {
  875. ON_WHITESPACE
  876. {
  877. continue;
  878. }
  879. ON('"')
  880. {
  881. SWITCH_TO(AttributeValueDoubleQuoted);
  882. }
  883. ON('\'')
  884. {
  885. SWITCH_TO(AttributeValueSingleQuoted);
  886. }
  887. ON('>')
  888. {
  889. PARSE_ERROR();
  890. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  891. }
  892. ANYTHING_ELSE
  893. {
  894. RECONSUME_IN(AttributeValueUnquoted);
  895. }
  896. }
  897. END_STATE
  898. BEGIN_STATE(AttributeValueDoubleQuoted)
  899. {
  900. ON('"')
  901. {
  902. SWITCH_TO(AfterAttributeValueQuoted);
  903. }
  904. ON('&')
  905. {
  906. m_return_state = State::AttributeValueDoubleQuoted;
  907. SWITCH_TO(CharacterReference);
  908. }
  909. ON(0)
  910. {
  911. TODO();
  912. }
  913. ON_EOF
  914. {
  915. TODO();
  916. }
  917. ANYTHING_ELSE
  918. {
  919. m_current_token.m_tag.attributes.last().value_builder.append_codepoint(current_input_character.value());
  920. continue;
  921. }
  922. }
  923. END_STATE
  924. BEGIN_STATE(AttributeValueSingleQuoted)
  925. {
  926. ON('\'')
  927. {
  928. SWITCH_TO(AfterAttributeValueQuoted);
  929. }
  930. ON('&')
  931. {
  932. m_return_state = State::AttributeValueSingleQuoted;
  933. SWITCH_TO(CharacterReference);
  934. }
  935. ON(0)
  936. {
  937. TODO();
  938. }
  939. ON_EOF
  940. {
  941. TODO();
  942. }
  943. ANYTHING_ELSE
  944. {
  945. m_current_token.m_tag.attributes.last().value_builder.append_codepoint(current_input_character.value());
  946. continue;
  947. }
  948. }
  949. END_STATE
  950. BEGIN_STATE(AttributeValueUnquoted)
  951. {
  952. ON_WHITESPACE
  953. {
  954. SWITCH_TO(BeforeAttributeName);
  955. }
  956. ON('&')
  957. {
  958. m_return_state = State::AttributeValueUnquoted;
  959. SWITCH_TO(CharacterReference);
  960. }
  961. ON('>')
  962. {
  963. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  964. }
  965. ON(0)
  966. {
  967. TODO();
  968. }
  969. ON_EOF
  970. {
  971. TODO();
  972. }
  973. ANYTHING_ELSE
  974. {
  975. m_current_token.m_tag.attributes.last().value_builder.append_codepoint(current_input_character.value());
  976. continue;
  977. }
  978. }
  979. END_STATE
  980. BEGIN_STATE(AfterAttributeValueQuoted)
  981. {
  982. ON_WHITESPACE
  983. {
  984. SWITCH_TO(BeforeAttributeName);
  985. }
  986. ON('/')
  987. {
  988. SWITCH_TO(SelfClosingStartTag);
  989. }
  990. ON('>')
  991. {
  992. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  993. }
  994. ON_EOF
  995. {
  996. PARSE_ERROR();
  997. EMIT_EOF;
  998. }
  999. ANYTHING_ELSE
  1000. {
  1001. PARSE_ERROR();
  1002. RECONSUME_IN(BeforeAttributeName);
  1003. }
  1004. }
  1005. END_STATE
  1006. BEGIN_STATE(CommentStart)
  1007. {
  1008. ON('-')
  1009. {
  1010. SWITCH_TO(CommentStartDash);
  1011. }
  1012. ON('>')
  1013. {
  1014. TODO();
  1015. }
  1016. ANYTHING_ELSE
  1017. {
  1018. RECONSUME_IN(Comment);
  1019. }
  1020. }
  1021. END_STATE
  1022. BEGIN_STATE(CommentStartDash)
  1023. {
  1024. ON('-')
  1025. {
  1026. SWITCH_TO(CommentEnd);
  1027. }
  1028. ON('>')
  1029. {
  1030. TODO();
  1031. }
  1032. ON_EOF
  1033. {
  1034. TODO();
  1035. }
  1036. ANYTHING_ELSE
  1037. {
  1038. m_current_token.m_comment_or_character.data.append('-');
  1039. RECONSUME_IN(Comment);
  1040. }
  1041. }
  1042. END_STATE
  1043. BEGIN_STATE(Comment)
  1044. {
  1045. ON('<')
  1046. {
  1047. m_current_token.m_comment_or_character.data.append_codepoint(current_input_character.value());
  1048. SWITCH_TO(CommentLessThanSign);
  1049. }
  1050. ON('-')
  1051. {
  1052. SWITCH_TO(CommentEndDash);
  1053. }
  1054. ON(0)
  1055. {
  1056. TODO();
  1057. }
  1058. ON_EOF
  1059. {
  1060. TODO();
  1061. }
  1062. ANYTHING_ELSE
  1063. {
  1064. m_current_token.m_comment_or_character.data.append_codepoint(current_input_character.value());
  1065. continue;
  1066. }
  1067. }
  1068. END_STATE
  1069. BEGIN_STATE(CommentEnd)
  1070. {
  1071. ON('>')
  1072. {
  1073. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1074. }
  1075. ON('!')
  1076. {
  1077. SWITCH_TO(CommentEndBang);
  1078. }
  1079. ON('-')
  1080. {
  1081. m_current_token.m_comment_or_character.data.append('-');
  1082. continue;
  1083. }
  1084. ON_EOF
  1085. {
  1086. TODO();
  1087. }
  1088. ANYTHING_ELSE
  1089. {
  1090. m_current_token.m_comment_or_character.data.append('-');
  1091. RECONSUME_IN(Comment);
  1092. }
  1093. }
  1094. END_STATE
  1095. BEGIN_STATE(CommentEndBang)
  1096. {
  1097. ON('-')
  1098. {
  1099. m_current_token.m_comment_or_character.data.append("--!");
  1100. SWITCH_TO(CommentEndDash);
  1101. }
  1102. ON('>')
  1103. {
  1104. TODO();
  1105. }
  1106. ON_EOF
  1107. {
  1108. TODO();
  1109. }
  1110. ANYTHING_ELSE
  1111. {
  1112. m_current_token.m_comment_or_character.data.append("--!");
  1113. RECONSUME_IN(Comment);
  1114. }
  1115. }
  1116. END_STATE
  1117. BEGIN_STATE(CommentEndDash)
  1118. {
  1119. ON('-')
  1120. {
  1121. SWITCH_TO(CommentEnd);
  1122. }
  1123. ON_EOF
  1124. {
  1125. TODO();
  1126. }
  1127. ANYTHING_ELSE
  1128. {
  1129. m_current_token.m_comment_or_character.data.append('-');
  1130. RECONSUME_IN(Comment);
  1131. }
  1132. }
  1133. END_STATE
  1134. BEGIN_STATE(CommentLessThanSign)
  1135. {
  1136. ON('!')
  1137. {
  1138. m_current_token.m_comment_or_character.data.append_codepoint(current_input_character.value());
  1139. SWITCH_TO(CommentLessThanSignBang);
  1140. }
  1141. ON('<')
  1142. {
  1143. m_current_token.m_comment_or_character.data.append_codepoint(current_input_character.value());
  1144. continue;
  1145. }
  1146. ANYTHING_ELSE
  1147. {
  1148. RECONSUME_IN(Comment);
  1149. }
  1150. }
  1151. END_STATE
  1152. BEGIN_STATE(CommentLessThanSignBang)
  1153. {
  1154. ON('-')
  1155. {
  1156. SWITCH_TO(CommentLessThanSignBangDash);
  1157. }
  1158. ANYTHING_ELSE
  1159. {
  1160. RECONSUME_IN(Comment);
  1161. }
  1162. }
  1163. END_STATE
  1164. BEGIN_STATE(CommentLessThanSignBangDash)
  1165. {
  1166. ON('-')
  1167. {
  1168. SWITCH_TO(CommentLessThanSignBangDashDash);
  1169. }
  1170. ANYTHING_ELSE
  1171. {
  1172. RECONSUME_IN(Comment);
  1173. }
  1174. }
  1175. END_STATE
  1176. BEGIN_STATE(CommentLessThanSignBangDashDash)
  1177. {
  1178. ON('>')
  1179. {
  1180. SWITCH_TO(CommentEnd);
  1181. }
  1182. ANYTHING_ELSE
  1183. {
  1184. PARSE_ERROR();
  1185. RECONSUME_IN(CommentEnd);
  1186. }
  1187. }
  1188. END_STATE
  1189. BEGIN_STATE(CharacterReference)
  1190. {
  1191. m_temporary_buffer.clear();
  1192. m_temporary_buffer.append('&');
  1193. ON_ASCII_ALPHANUMERIC
  1194. {
  1195. RECONSUME_IN(NamedCharacterReference);
  1196. }
  1197. ON('#')
  1198. {
  1199. m_temporary_buffer.append(current_input_character.value());
  1200. SWITCH_TO(NumericCharacterReference);
  1201. }
  1202. ANYTHING_ELSE
  1203. {
  1204. RECONSUME_IN_RETURN_STATE;
  1205. }
  1206. }
  1207. END_STATE
  1208. BEGIN_STATE(NamedCharacterReference)
  1209. {
  1210. size_t byte_offset = m_utf8_view.byte_offset_of(m_prev_utf8_iterator);
  1211. auto match = HTML::codepoints_from_entity(m_decoded_input.substring_view(byte_offset, m_decoded_input.length() - byte_offset - 1));
  1212. if (match.has_value()) {
  1213. for (size_t i = 0; i < match.value().entity.length(); ++i) {
  1214. m_prev_utf8_iterator = m_utf8_iterator;
  1215. ++m_utf8_iterator;
  1216. }
  1217. for (auto ch : match.value().entity)
  1218. m_temporary_buffer.append(ch);
  1219. if (consumed_as_part_of_an_attribute() && match.value().entity.ends_with(';')) {
  1220. auto next_codepoint = peek_codepoint(0);
  1221. if (next_codepoint.has_value() && next_codepoint.value() == '=') {
  1222. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1223. SWITCH_TO_RETURN_STATE;
  1224. }
  1225. }
  1226. if (!match.value().entity.ends_with(';')) {
  1227. PARSE_ERROR();
  1228. }
  1229. m_temporary_buffer.clear();
  1230. m_temporary_buffer.append(match.value().codepoints);
  1231. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1232. SWITCH_TO_RETURN_STATE;
  1233. } else {
  1234. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1235. SWITCH_TO(AmbiguousAmpersand);
  1236. }
  1237. }
  1238. END_STATE
  1239. BEGIN_STATE(AmbiguousAmpersand)
  1240. {
  1241. ON_ASCII_ALPHANUMERIC
  1242. {
  1243. if (consumed_as_part_of_an_attribute()) {
  1244. m_current_token.m_tag.attributes.last().value_builder.append_codepoint(current_input_character.value());
  1245. continue;
  1246. } else {
  1247. EMIT_CURRENT_CHARACTER;
  1248. }
  1249. }
  1250. ON(';')
  1251. {
  1252. TODO();
  1253. }
  1254. ANYTHING_ELSE
  1255. {
  1256. RECONSUME_IN_RETURN_STATE;
  1257. }
  1258. }
  1259. END_STATE
  1260. BEGIN_STATE(NumericCharacterReference)
  1261. {
  1262. m_character_reference_code = 0;
  1263. ON('X')
  1264. {
  1265. m_temporary_buffer.append(current_input_character.value());
  1266. SWITCH_TO(HexadecimalCharacterReferenceStart);
  1267. }
  1268. ON('x')
  1269. {
  1270. m_temporary_buffer.append(current_input_character.value());
  1271. SWITCH_TO(HexadecimalCharacterReferenceStart);
  1272. }
  1273. ANYTHING_ELSE
  1274. {
  1275. RECONSUME_IN(DecimalCharacterReferenceStart);
  1276. }
  1277. }
  1278. END_STATE
  1279. BEGIN_STATE(HexadecimalCharacterReferenceStart)
  1280. {
  1281. ON_ASCII_HEX_DIGIT
  1282. {
  1283. RECONSUME_IN(HexadecimalCharacterReference);
  1284. }
  1285. ANYTHING_ELSE
  1286. {
  1287. TODO();
  1288. }
  1289. }
  1290. END_STATE
  1291. BEGIN_STATE(DecimalCharacterReferenceStart)
  1292. {
  1293. ON_ASCII_DIGIT
  1294. {
  1295. RECONSUME_IN(DecimalCharacterReference);
  1296. }
  1297. ANYTHING_ELSE
  1298. {
  1299. TODO();
  1300. }
  1301. }
  1302. END_STATE
  1303. BEGIN_STATE(HexadecimalCharacterReference)
  1304. {
  1305. ON_ASCII_DIGIT
  1306. {
  1307. m_character_reference_code *= 16;
  1308. m_character_reference_code += current_input_character.value() - 0x30;
  1309. continue;
  1310. }
  1311. ON_ASCII_UPPER_ALPHA
  1312. {
  1313. m_character_reference_code *= 16;
  1314. m_character_reference_code += current_input_character.value() - 0x37;
  1315. continue;
  1316. }
  1317. ON_ASCII_LOWER_ALPHA
  1318. {
  1319. m_character_reference_code *= 16;
  1320. m_character_reference_code += current_input_character.value() - 0x57;
  1321. continue;
  1322. }
  1323. ON(';')
  1324. {
  1325. SWITCH_TO(NumericCharacterReferenceEnd);
  1326. }
  1327. ANYTHING_ELSE
  1328. {
  1329. TODO();
  1330. }
  1331. }
  1332. END_STATE
  1333. BEGIN_STATE(DecimalCharacterReference)
  1334. {
  1335. ON_ASCII_DIGIT
  1336. {
  1337. m_character_reference_code *= 10;
  1338. m_character_reference_code += current_input_character.value() - 0x30;
  1339. continue;
  1340. }
  1341. ON(';')
  1342. {
  1343. SWITCH_TO(NumericCharacterReferenceEnd);
  1344. }
  1345. ANYTHING_ELSE
  1346. {
  1347. TODO();
  1348. }
  1349. }
  1350. END_STATE
  1351. BEGIN_STATE(NumericCharacterReferenceEnd)
  1352. {
  1353. DONT_CONSUME_NEXT_INPUT_CHARACTER;
  1354. if (m_character_reference_code == 0) {
  1355. TODO();
  1356. }
  1357. if (m_character_reference_code > 0x10ffff) {
  1358. TODO();
  1359. }
  1360. if (is_surrogate(m_character_reference_code)) {
  1361. TODO();
  1362. }
  1363. if (is_noncharacter(m_character_reference_code)) {
  1364. TODO();
  1365. }
  1366. if (m_character_reference_code == 0xd || (is_control(m_character_reference_code) && !isspace(m_character_reference_code))) {
  1367. TODO();
  1368. }
  1369. if (is_control(m_character_reference_code)) {
  1370. constexpr struct {
  1371. u32 number;
  1372. u32 codepoint;
  1373. } conversion_table[] = {
  1374. { 0x80, 0x20AC },
  1375. { 0x82, 0x201A },
  1376. { 0x83, 0x0192 },
  1377. { 0x84, 0x201E },
  1378. { 0x85, 0x2026 },
  1379. { 0x86, 0x2020 },
  1380. { 0x87, 0x2021 },
  1381. { 0x88, 0x02C6 },
  1382. { 0x89, 0x2030 },
  1383. { 0x8A, 0x0160 },
  1384. { 0x8B, 0x2039 },
  1385. { 0x8C, 0x0152 },
  1386. { 0x8E, 0x017D },
  1387. { 0x91, 0x2018 },
  1388. { 0x92, 0x2019 },
  1389. { 0x93, 0x201C },
  1390. { 0x94, 0x201D },
  1391. { 0x95, 0x2022 },
  1392. { 0x96, 0x2013 },
  1393. { 0x97, 0x2014 },
  1394. { 0x98, 0x02DC },
  1395. { 0x99, 0x2122 },
  1396. { 0x9A, 0x0161 },
  1397. { 0x9B, 0x203A },
  1398. { 0x9C, 0x0153 },
  1399. { 0x9E, 0x017E },
  1400. { 0x9F, 0x0178 },
  1401. };
  1402. for (auto& entry : conversion_table) {
  1403. if (m_character_reference_code == entry.number) {
  1404. m_character_reference_code = entry.codepoint;
  1405. break;
  1406. }
  1407. }
  1408. }
  1409. m_temporary_buffer.clear();
  1410. m_temporary_buffer.append(m_character_reference_code);
  1411. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1412. SWITCH_TO_RETURN_STATE;
  1413. }
  1414. END_STATE
  1415. BEGIN_STATE(RCDATA)
  1416. {
  1417. ON('&')
  1418. {
  1419. m_return_state = State::RCDATA;
  1420. SWITCH_TO(CharacterReference);
  1421. }
  1422. ON('<')
  1423. {
  1424. SWITCH_TO(RCDATALessThanSign);
  1425. }
  1426. ON(0)
  1427. {
  1428. PARSE_ERROR();
  1429. EMIT_CHARACTER(0xFFFD);
  1430. }
  1431. ON_EOF
  1432. {
  1433. EMIT_EOF;
  1434. }
  1435. ANYTHING_ELSE
  1436. {
  1437. EMIT_CURRENT_CHARACTER;
  1438. }
  1439. }
  1440. END_STATE
  1441. BEGIN_STATE(RCDATALessThanSign)
  1442. {
  1443. ON('/')
  1444. {
  1445. m_temporary_buffer.clear();
  1446. SWITCH_TO(RCDATAEndTagOpen);
  1447. }
  1448. ANYTHING_ELSE
  1449. {
  1450. EMIT_CHARACTER('<');
  1451. RECONSUME_IN(RCDATA);
  1452. }
  1453. }
  1454. END_STATE
  1455. BEGIN_STATE(RCDATAEndTagOpen)
  1456. {
  1457. ON_ASCII_ALPHA
  1458. {
  1459. create_new_token(HTMLToken::Type::EndTag);
  1460. RECONSUME_IN(RCDATAEndTagName);
  1461. }
  1462. ANYTHING_ELSE
  1463. {
  1464. // FIXME: Emit a U+003C LESS-THAN SIGN character token and a U+002F SOLIDUS character token. Reconsume in the RCDATA state.
  1465. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1466. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1467. RECONSUME_IN(RCDATA);
  1468. }
  1469. }
  1470. END_STATE
  1471. BEGIN_STATE(RCDATAEndTagName)
  1472. {
  1473. ON_WHITESPACE
  1474. {
  1475. if (!current_end_tag_token_is_appropriate()) {
  1476. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1477. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1478. for (auto codepoint : m_temporary_buffer)
  1479. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1480. RECONSUME_IN(RCDATA);
  1481. }
  1482. SWITCH_TO(BeforeAttributeName);
  1483. }
  1484. ON('/')
  1485. {
  1486. if (!current_end_tag_token_is_appropriate()) {
  1487. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1488. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1489. for (auto codepoint : m_temporary_buffer)
  1490. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1491. RECONSUME_IN(RCDATA);
  1492. }
  1493. SWITCH_TO(SelfClosingStartTag);
  1494. }
  1495. ON('>')
  1496. {
  1497. if (!current_end_tag_token_is_appropriate()) {
  1498. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1499. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1500. for (auto codepoint : m_temporary_buffer)
  1501. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1502. RECONSUME_IN(RCDATA);
  1503. }
  1504. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1505. }
  1506. ON_ASCII_UPPER_ALPHA
  1507. {
  1508. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1509. m_temporary_buffer.append(current_input_character.value());
  1510. continue;
  1511. }
  1512. ON_ASCII_LOWER_ALPHA
  1513. {
  1514. m_current_token.m_tag.tag_name.append_codepoint(current_input_character.value());
  1515. m_temporary_buffer.append(current_input_character.value());
  1516. continue;
  1517. }
  1518. ANYTHING_ELSE
  1519. {
  1520. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1521. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1522. for (auto codepoint : m_temporary_buffer)
  1523. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1524. RECONSUME_IN(RCDATA);
  1525. }
  1526. }
  1527. END_STATE
  1528. BEGIN_STATE(RAWTEXT)
  1529. {
  1530. ON('<')
  1531. {
  1532. SWITCH_TO(RAWTEXTLessThanSign);
  1533. }
  1534. ON(0)
  1535. {
  1536. PARSE_ERROR();
  1537. EMIT_CHARACTER(0xFFFD);
  1538. }
  1539. ON_EOF
  1540. {
  1541. EMIT_EOF;
  1542. }
  1543. ANYTHING_ELSE
  1544. {
  1545. EMIT_CURRENT_CHARACTER;
  1546. }
  1547. }
  1548. END_STATE
  1549. BEGIN_STATE(RAWTEXTLessThanSign)
  1550. {
  1551. ON('/')
  1552. {
  1553. m_temporary_buffer.clear();
  1554. SWITCH_TO(RAWTEXTEndTagOpen);
  1555. }
  1556. ANYTHING_ELSE
  1557. {
  1558. EMIT_CHARACTER('<');
  1559. RECONSUME_IN(RAWTEXT);
  1560. }
  1561. }
  1562. END_STATE
  1563. BEGIN_STATE(RAWTEXTEndTagOpen)
  1564. {
  1565. ON_ASCII_ALPHA
  1566. {
  1567. create_new_token(HTMLToken::Type::EndTag);
  1568. RECONSUME_IN(RAWTEXTEndTagName);
  1569. }
  1570. ANYTHING_ELSE
  1571. {
  1572. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1573. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1574. RECONSUME_IN(RAWTEXT);
  1575. }
  1576. }
  1577. END_STATE
  1578. BEGIN_STATE(RAWTEXTEndTagName)
  1579. {
  1580. ON_WHITESPACE
  1581. {
  1582. if (!current_end_tag_token_is_appropriate()) {
  1583. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1584. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1585. for (auto codepoint : m_temporary_buffer)
  1586. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1587. RECONSUME_IN(RAWTEXT);
  1588. }
  1589. SWITCH_TO(BeforeAttributeName);
  1590. }
  1591. ON('/')
  1592. {
  1593. if (!current_end_tag_token_is_appropriate()) {
  1594. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1595. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1596. for (auto codepoint : m_temporary_buffer)
  1597. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1598. RECONSUME_IN(RAWTEXT);
  1599. }
  1600. SWITCH_TO(SelfClosingStartTag);
  1601. }
  1602. ON('>')
  1603. {
  1604. if (!current_end_tag_token_is_appropriate()) {
  1605. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1606. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1607. for (auto codepoint : m_temporary_buffer)
  1608. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1609. RECONSUME_IN(RAWTEXT);
  1610. }
  1611. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1612. }
  1613. ON_ASCII_UPPER_ALPHA
  1614. {
  1615. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1616. m_temporary_buffer.append(current_input_character.value());
  1617. continue;
  1618. }
  1619. ON_ASCII_LOWER_ALPHA
  1620. {
  1621. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1622. m_temporary_buffer.append(current_input_character.value());
  1623. continue;
  1624. }
  1625. ANYTHING_ELSE
  1626. {
  1627. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1628. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1629. for (auto codepoint : m_temporary_buffer)
  1630. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1631. RECONSUME_IN(RAWTEXT);
  1632. }
  1633. }
  1634. END_STATE
  1635. BEGIN_STATE(ScriptData)
  1636. {
  1637. ON('<')
  1638. {
  1639. SWITCH_TO(ScriptDataLessThanSign);
  1640. }
  1641. ON(0)
  1642. {
  1643. PARSE_ERROR();
  1644. EMIT_CHARACTER(0xFFFD);
  1645. }
  1646. ON_EOF
  1647. {
  1648. EMIT_EOF;
  1649. }
  1650. ANYTHING_ELSE
  1651. {
  1652. EMIT_CURRENT_CHARACTER;
  1653. }
  1654. }
  1655. END_STATE
  1656. BEGIN_STATE(PLAINTEXT)
  1657. {
  1658. ON(0)
  1659. {
  1660. PARSE_ERROR();
  1661. EMIT_CHARACTER(0xFFFD);
  1662. }
  1663. ON_EOF
  1664. {
  1665. EMIT_EOF;
  1666. }
  1667. ANYTHING_ELSE
  1668. {
  1669. EMIT_CURRENT_CHARACTER;
  1670. }
  1671. }
  1672. END_STATE
  1673. BEGIN_STATE(ScriptDataLessThanSign)
  1674. {
  1675. ON('/')
  1676. {
  1677. m_temporary_buffer.clear();
  1678. SWITCH_TO(ScriptDataEndTagOpen);
  1679. }
  1680. ON('!')
  1681. {
  1682. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1683. m_queued_tokens.enqueue(HTMLToken::make_character('!'));
  1684. SWITCH_TO(ScriptDataEscapeStart);
  1685. }
  1686. ANYTHING_ELSE
  1687. {
  1688. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptData);
  1689. }
  1690. }
  1691. END_STATE
  1692. BEGIN_STATE(ScriptDataEscapeStart)
  1693. {
  1694. ON('-')
  1695. {
  1696. m_queued_tokens.enqueue(HTMLToken::make_character('-'));
  1697. SWITCH_TO(ScriptDataEscapeStartDash);
  1698. }
  1699. ANYTHING_ELSE
  1700. {
  1701. RECONSUME_IN(ScriptData);
  1702. }
  1703. }
  1704. END_STATE
  1705. BEGIN_STATE(ScriptDataEscapeStartDash)
  1706. {
  1707. ON('-')
  1708. {
  1709. m_queued_tokens.enqueue(HTMLToken::make_character('-'));
  1710. SWITCH_TO(ScriptDataEscapedDashDash);
  1711. }
  1712. ANYTHING_ELSE
  1713. {
  1714. RECONSUME_IN(ScriptData);
  1715. }
  1716. }
  1717. END_STATE
  1718. BEGIN_STATE(ScriptDataEscapedDashDash)
  1719. {
  1720. ON('-')
  1721. {
  1722. EMIT_CHARACTER('-');
  1723. }
  1724. ON('<')
  1725. {
  1726. SWITCH_TO(ScriptDataEscapedLessThanSign);
  1727. }
  1728. ON('>')
  1729. {
  1730. m_queued_tokens.enqueue(HTMLToken::make_character('>'));
  1731. SWITCH_TO(ScriptData);
  1732. }
  1733. ON(0)
  1734. {
  1735. TODO();
  1736. }
  1737. ON_EOF
  1738. {
  1739. TODO();
  1740. }
  1741. ANYTHING_ELSE
  1742. {
  1743. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscaped);
  1744. }
  1745. }
  1746. END_STATE
  1747. BEGIN_STATE(ScriptDataEscapedLessThanSign)
  1748. {
  1749. ON('/')
  1750. {
  1751. m_temporary_buffer.clear();
  1752. SWITCH_TO(ScriptDataEscapedEndTagOpen);
  1753. }
  1754. ON_ASCII_ALPHA
  1755. {
  1756. m_temporary_buffer.clear();
  1757. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptDataDoubleEscapeStart);
  1758. }
  1759. ANYTHING_ELSE
  1760. {
  1761. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptDataEscaped);
  1762. }
  1763. }
  1764. END_STATE
  1765. BEGIN_STATE(ScriptDataEscapedEndTagOpen)
  1766. {
  1767. ON_ASCII_ALPHA
  1768. {
  1769. create_new_token(HTMLToken::Type::EndTag);
  1770. RECONSUME_IN(ScriptDataEscapedEndTagName);
  1771. }
  1772. ANYTHING_ELSE
  1773. {
  1774. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1775. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1776. RECONSUME_IN(ScriptDataEscaped);
  1777. }
  1778. }
  1779. END_STATE
  1780. BEGIN_STATE(ScriptDataEscapedEndTagName)
  1781. {
  1782. ON_WHITESPACE
  1783. {
  1784. if (current_end_tag_token_is_appropriate()) {
  1785. SWITCH_TO(BeforeAttributeName);
  1786. } else {
  1787. TODO();
  1788. }
  1789. }
  1790. ON('/')
  1791. {
  1792. if (current_end_tag_token_is_appropriate()) {
  1793. SWITCH_TO(SelfClosingStartTag);
  1794. } else {
  1795. TODO();
  1796. }
  1797. }
  1798. ON('>')
  1799. {
  1800. if (current_end_tag_token_is_appropriate()) {
  1801. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1802. } else {
  1803. TODO();
  1804. }
  1805. }
  1806. ON_ASCII_UPPER_ALPHA
  1807. {
  1808. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1809. m_temporary_buffer.append(current_input_character.value());
  1810. continue;
  1811. }
  1812. ON_ASCII_LOWER_ALPHA
  1813. {
  1814. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1815. m_temporary_buffer.append(current_input_character.value());
  1816. continue;
  1817. }
  1818. ANYTHING_ELSE
  1819. {
  1820. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1821. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1822. for (auto codepoint : m_temporary_buffer) {
  1823. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1824. }
  1825. RECONSUME_IN(ScriptDataEscaped);
  1826. }
  1827. }
  1828. END_STATE
  1829. BEGIN_STATE(ScriptDataDoubleEscapeStart)
  1830. {
  1831. TODO();
  1832. }
  1833. END_STATE
  1834. BEGIN_STATE(ScriptDataEscapedDash)
  1835. {
  1836. ON('-')
  1837. {
  1838. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscapedDashDash);
  1839. }
  1840. ON('<')
  1841. {
  1842. SWITCH_TO(ScriptDataEscapedLessThanSign);
  1843. }
  1844. ON(0)
  1845. {
  1846. TODO();
  1847. }
  1848. ON_EOF
  1849. {
  1850. TODO();
  1851. }
  1852. ANYTHING_ELSE
  1853. {
  1854. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscaped);
  1855. }
  1856. }
  1857. END_STATE
  1858. BEGIN_STATE(ScriptDataEscaped)
  1859. {
  1860. ON('-')
  1861. {
  1862. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscapedDash);
  1863. }
  1864. ON('<')
  1865. {
  1866. SWITCH_TO(ScriptDataEscapedLessThanSign);
  1867. }
  1868. ON(0)
  1869. {
  1870. TODO();
  1871. }
  1872. ON_EOF
  1873. {
  1874. TODO();
  1875. }
  1876. ANYTHING_ELSE
  1877. {
  1878. EMIT_CURRENT_CHARACTER;
  1879. }
  1880. }
  1881. END_STATE
  1882. BEGIN_STATE(ScriptDataEndTagOpen)
  1883. {
  1884. ON_ASCII_ALPHA
  1885. {
  1886. create_new_token(HTMLToken::Type::EndTag);
  1887. RECONSUME_IN(ScriptDataEndTagName);
  1888. }
  1889. ANYTHING_ELSE
  1890. {
  1891. TODO();
  1892. }
  1893. }
  1894. END_STATE
  1895. BEGIN_STATE(ScriptDataEndTagName)
  1896. {
  1897. ON_WHITESPACE
  1898. {
  1899. if (current_end_tag_token_is_appropriate())
  1900. SWITCH_TO(BeforeAttributeName);
  1901. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1902. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1903. for (auto codepoint : m_temporary_buffer)
  1904. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1905. RECONSUME_IN(ScriptData);
  1906. }
  1907. ON('/')
  1908. {
  1909. if (current_end_tag_token_is_appropriate())
  1910. SWITCH_TO(SelfClosingStartTag);
  1911. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1912. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1913. for (auto codepoint : m_temporary_buffer)
  1914. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1915. RECONSUME_IN(ScriptData);
  1916. }
  1917. ON('>')
  1918. {
  1919. if (current_end_tag_token_is_appropriate())
  1920. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1921. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1922. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1923. for (auto codepoint : m_temporary_buffer)
  1924. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1925. RECONSUME_IN(ScriptData);
  1926. }
  1927. ON_ASCII_UPPER_ALPHA
  1928. {
  1929. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1930. m_temporary_buffer.append(current_input_character.value());
  1931. continue;
  1932. }
  1933. ON_ASCII_LOWER_ALPHA
  1934. {
  1935. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1936. m_temporary_buffer.append(current_input_character.value());
  1937. continue;
  1938. }
  1939. ANYTHING_ELSE
  1940. {
  1941. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1942. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1943. for (auto codepoint : m_temporary_buffer)
  1944. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1945. RECONSUME_IN(ScriptData);
  1946. }
  1947. }
  1948. END_STATE
  1949. default:
  1950. TODO();
  1951. }
  1952. }
  1953. }
  1954. bool HTMLTokenizer::consume_next_if_match(const StringView& string, CaseSensitivity case_sensitivity)
  1955. {
  1956. for (size_t i = 0; i < string.length(); ++i) {
  1957. auto codepoint = peek_codepoint(i);
  1958. if (!codepoint.has_value())
  1959. return false;
  1960. // FIXME: This should be more Unicode-aware.
  1961. if (case_sensitivity == CaseSensitivity::CaseInsensitive) {
  1962. if (codepoint.value() < 0x80) {
  1963. if (tolower(codepoint.value()) != tolower(string[i]))
  1964. return false;
  1965. continue;
  1966. }
  1967. }
  1968. if (codepoint.value() != (u32)string[i])
  1969. return false;
  1970. }
  1971. for (size_t i = 0; i < string.length(); ++i) {
  1972. m_prev_utf8_iterator = m_utf8_iterator;
  1973. ++m_utf8_iterator;
  1974. }
  1975. return true;
  1976. }
  1977. void HTMLTokenizer::create_new_token(HTMLToken::Type type)
  1978. {
  1979. m_current_token = {};
  1980. m_current_token.m_type = type;
  1981. }
  1982. HTMLTokenizer::HTMLTokenizer(const StringView& input, const String& encoding)
  1983. {
  1984. auto* decoder = TextCodec::decoder_for(encoding);
  1985. ASSERT(decoder);
  1986. m_decoded_input = decoder->to_utf8(input);
  1987. m_utf8_view = Utf8View(m_decoded_input);
  1988. m_utf8_iterator = m_utf8_view.begin();
  1989. }
  1990. void HTMLTokenizer::will_switch_to([[maybe_unused]] State new_state)
  1991. {
  1992. #ifdef TOKENIZER_TRACE
  1993. dbg() << "[" << state_name(m_state) << "] Switch to " << state_name(new_state);
  1994. #endif
  1995. }
  1996. void HTMLTokenizer::will_reconsume_in([[maybe_unused]] State new_state)
  1997. {
  1998. #ifdef TOKENIZER_TRACE
  1999. dbg() << "[" << state_name(m_state) << "] Reconsume in " << state_name(new_state);
  2000. #endif
  2001. }
  2002. void HTMLTokenizer::switch_to(Badge<HTMLDocumentParser>, State new_state)
  2003. {
  2004. #ifdef TOKENIZER_TRACE
  2005. dbg() << "[" << state_name(m_state) << "] Parser switches tokenizer state to " << state_name(new_state);
  2006. #endif
  2007. m_state = new_state;
  2008. }
  2009. void HTMLTokenizer::will_emit(HTMLToken& token)
  2010. {
  2011. if (token.is_start_tag())
  2012. m_last_emitted_start_tag = token;
  2013. }
  2014. bool HTMLTokenizer::current_end_tag_token_is_appropriate() const
  2015. {
  2016. ASSERT(m_current_token.is_end_tag());
  2017. if (!m_last_emitted_start_tag.is_start_tag())
  2018. return false;
  2019. return m_current_token.tag_name() == m_last_emitted_start_tag.tag_name();
  2020. }
  2021. bool HTMLTokenizer::consumed_as_part_of_an_attribute() const
  2022. {
  2023. return m_return_state == State::AttributeValueUnquoted || m_return_state == State::AttributeValueSingleQuoted || m_return_state == State::AttributeValueDoubleQuoted;
  2024. }
  2025. }