HTMLTokenizer.cpp 58 KB

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