HTMLTokenizer.cpp 62 KB

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