HTMLTokenizer.cpp 66 KB

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