HTMLTokenizer.cpp 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <AK/Debug.h>
  8. #include <AK/SourceLocation.h>
  9. #include <LibTextCodec/Decoder.h>
  10. #include <LibWeb/HTML/Parser/Entities.h>
  11. #include <LibWeb/HTML/Parser/HTMLToken.h>
  12. #include <LibWeb/HTML/Parser/HTMLTokenizer.h>
  13. #include <string.h>
  14. namespace Web::HTML {
  15. #pragma GCC diagnostic ignored "-Wunused-label"
  16. #define CONSUME_NEXT_INPUT_CHARACTER \
  17. current_input_character = next_code_point();
  18. #define SWITCH_TO(new_state) \
  19. do { \
  20. VERIFY(m_current_builder.is_empty()); \
  21. SWITCH_TO_WITH_UNCLEAN_BUILDER(new_state); \
  22. } while (0)
  23. #define SWITCH_TO_WITH_UNCLEAN_BUILDER(new_state) \
  24. do { \
  25. will_switch_to(State::new_state); \
  26. m_state = State::new_state; \
  27. CONSUME_NEXT_INPUT_CHARACTER; \
  28. goto new_state; \
  29. } while (0)
  30. #define RECONSUME_IN(new_state) \
  31. do { \
  32. will_reconsume_in(State::new_state); \
  33. m_state = State::new_state; \
  34. goto new_state; \
  35. } while (0)
  36. #define SWITCH_TO_RETURN_STATE \
  37. do { \
  38. will_switch_to(m_return_state); \
  39. m_state = m_return_state; \
  40. goto _StartOfFunction; \
  41. } while (0)
  42. #define RECONSUME_IN_RETURN_STATE \
  43. do { \
  44. will_reconsume_in(m_return_state); \
  45. m_state = m_return_state; \
  46. if (current_input_character.has_value()) \
  47. restore_to(m_prev_utf8_iterator); \
  48. goto _StartOfFunction; \
  49. } while (0)
  50. #define SWITCH_TO_AND_EMIT_CURRENT_TOKEN(new_state) \
  51. do { \
  52. VERIFY(m_current_builder.is_empty()); \
  53. will_switch_to(State::new_state); \
  54. m_state = State::new_state; \
  55. will_emit(m_current_token); \
  56. m_queued_tokens.enqueue(move(m_current_token)); \
  57. return m_queued_tokens.dequeue(); \
  58. } while (0)
  59. #define EMIT_CHARACTER_AND_RECONSUME_IN(code_point, new_state) \
  60. do { \
  61. m_queued_tokens.enqueue(HTMLToken::make_character(code_point)); \
  62. will_reconsume_in(State::new_state); \
  63. m_state = State::new_state; \
  64. goto new_state; \
  65. } while (0)
  66. #define FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE \
  67. do { \
  68. for (auto code_point : m_temporary_buffer) { \
  69. if (consumed_as_part_of_an_attribute()) { \
  70. m_current_builder.append_code_point(code_point); \
  71. } else { \
  72. create_new_token(HTMLToken::Type::Character); \
  73. m_current_token.set_code_point(code_point); \
  74. m_queued_tokens.enqueue(move(m_current_token)); \
  75. } \
  76. } \
  77. } while (0)
  78. #define DONT_CONSUME_NEXT_INPUT_CHARACTER \
  79. do { \
  80. restore_to(m_prev_utf8_iterator); \
  81. } while (0)
  82. #define ON(code_point) \
  83. if (current_input_character.has_value() && current_input_character.value() == code_point)
  84. #define ON_EOF \
  85. if (!current_input_character.has_value())
  86. #define ON_ASCII_ALPHA \
  87. if (current_input_character.has_value() && is_ascii_alpha(current_input_character.value()))
  88. #define ON_ASCII_ALPHANUMERIC \
  89. if (current_input_character.has_value() && is_ascii_alphanumeric(current_input_character.value()))
  90. #define ON_ASCII_UPPER_ALPHA \
  91. if (current_input_character.has_value() && is_ascii_upper_alpha(current_input_character.value()))
  92. #define ON_ASCII_LOWER_ALPHA \
  93. if (current_input_character.has_value() && is_ascii_lower_alpha(current_input_character.value()))
  94. #define ON_ASCII_DIGIT \
  95. if (current_input_character.has_value() && is_ascii_digit(current_input_character.value()))
  96. #define ON_ASCII_HEX_DIGIT \
  97. if (current_input_character.has_value() && is_ascii_hex_digit(current_input_character.value()))
  98. #define ON_WHITESPACE \
  99. if (current_input_character.has_value() && is_ascii(current_input_character.value()) && "\t\n\f "sv.contains(current_input_character.value()))
  100. #define ANYTHING_ELSE if (1)
  101. #define EMIT_EOF \
  102. do { \
  103. if (m_has_emitted_eof) \
  104. return {}; \
  105. m_has_emitted_eof = true; \
  106. create_new_token(HTMLToken::Type::EndOfFile); \
  107. will_emit(m_current_token); \
  108. m_queued_tokens.enqueue(move(m_current_token)); \
  109. return m_queued_tokens.dequeue(); \
  110. } while (0)
  111. #define EMIT_CURRENT_TOKEN \
  112. do { \
  113. VERIFY(m_current_builder.is_empty()); \
  114. will_emit(m_current_token); \
  115. m_queued_tokens.enqueue(move(m_current_token)); \
  116. return m_queued_tokens.dequeue(); \
  117. } while (0)
  118. #define EMIT_CHARACTER(code_point) \
  119. do { \
  120. create_new_token(HTMLToken::Type::Character); \
  121. m_current_token.set_code_point(code_point); \
  122. m_queued_tokens.enqueue(move(m_current_token)); \
  123. return m_queued_tokens.dequeue(); \
  124. } while (0)
  125. #define EMIT_CURRENT_CHARACTER \
  126. EMIT_CHARACTER(current_input_character.value());
  127. #define SWITCH_TO_AND_EMIT_CHARACTER(code_point, new_state) \
  128. do { \
  129. will_switch_to(State::new_state); \
  130. m_state = State::new_state; \
  131. EMIT_CHARACTER(code_point); \
  132. } while (0)
  133. #define SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(new_state) \
  134. SWITCH_TO_AND_EMIT_CHARACTER(current_input_character.value(), new_state)
  135. #define BEGIN_STATE(state) \
  136. state: \
  137. case State::state: { \
  138. { \
  139. {
  140. #define END_STATE \
  141. VERIFY_NOT_REACHED(); \
  142. break; \
  143. } \
  144. } \
  145. }
  146. static inline void log_parse_error(SourceLocation const& location = SourceLocation::current())
  147. {
  148. dbgln_if(TOKENIZER_TRACE_DEBUG, "Parse error (tokenization) {}", location);
  149. }
  150. Optional<u32> HTMLTokenizer::next_code_point()
  151. {
  152. if (m_utf8_iterator == m_utf8_view.end())
  153. return {};
  154. skip(1);
  155. dbgln_if(TOKENIZER_TRACE_DEBUG, "(Tokenizer) Next code_point: {}", (char)*m_prev_utf8_iterator);
  156. return *m_prev_utf8_iterator;
  157. }
  158. void HTMLTokenizer::skip(size_t count)
  159. {
  160. m_source_positions.append(m_source_positions.last());
  161. for (size_t i = 0; i < count; ++i) {
  162. m_prev_utf8_iterator = m_utf8_iterator;
  163. auto code_point = *m_utf8_iterator;
  164. if (code_point == '\n') {
  165. m_source_positions.last().column = 0;
  166. m_source_positions.last().line++;
  167. } else {
  168. m_source_positions.last().column++;
  169. }
  170. ++m_utf8_iterator;
  171. }
  172. }
  173. Optional<u32> HTMLTokenizer::peek_code_point(size_t offset) const
  174. {
  175. auto it = m_utf8_iterator;
  176. for (size_t i = 0; i < offset && it != m_utf8_view.end(); ++i)
  177. ++it;
  178. if (it == m_utf8_view.end())
  179. return {};
  180. return *it;
  181. }
  182. HTMLToken::Position HTMLTokenizer::nth_last_position(size_t n)
  183. {
  184. if (n + 1 > m_source_positions.size()) {
  185. dbgln_if(TOKENIZER_TRACE_DEBUG, "(Tokenizer::nth_last_position) Invalid position requested: {}th-last of {}. Returning (0-0).", n, m_source_positions.size());
  186. return HTMLToken::Position { 0, 0 };
  187. };
  188. return m_source_positions.at(m_source_positions.size() - 1 - n);
  189. }
  190. Optional<HTMLToken> HTMLTokenizer::next_token()
  191. {
  192. {
  193. auto last_position = m_source_positions.last();
  194. m_source_positions.clear();
  195. m_source_positions.append(move(last_position));
  196. }
  197. _StartOfFunction:
  198. if (!m_queued_tokens.is_empty())
  199. return m_queued_tokens.dequeue();
  200. for (;;) {
  201. auto current_input_character = next_code_point();
  202. switch (m_state) {
  203. BEGIN_STATE(Data)
  204. {
  205. ON('&')
  206. {
  207. m_return_state = State::Data;
  208. SWITCH_TO(CharacterReference);
  209. }
  210. ON('<')
  211. {
  212. SWITCH_TO(TagOpen);
  213. }
  214. ON(0)
  215. {
  216. log_parse_error();
  217. EMIT_CURRENT_CHARACTER;
  218. }
  219. ON_EOF
  220. {
  221. EMIT_EOF;
  222. }
  223. ANYTHING_ELSE
  224. {
  225. EMIT_CURRENT_CHARACTER;
  226. }
  227. }
  228. END_STATE
  229. BEGIN_STATE(TagOpen)
  230. {
  231. ON('!')
  232. {
  233. SWITCH_TO(MarkupDeclarationOpen);
  234. }
  235. ON('/')
  236. {
  237. SWITCH_TO(EndTagOpen);
  238. }
  239. ON_ASCII_ALPHA
  240. {
  241. create_new_token(HTMLToken::Type::StartTag);
  242. RECONSUME_IN(TagName);
  243. }
  244. ON('?')
  245. {
  246. log_parse_error();
  247. create_new_token(HTMLToken::Type::Comment);
  248. m_current_token.set_start_position({}, nth_last_position(2));
  249. RECONSUME_IN(BogusComment);
  250. }
  251. ON_EOF
  252. {
  253. log_parse_error();
  254. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  255. EMIT_EOF;
  256. }
  257. ANYTHING_ELSE
  258. {
  259. log_parse_error();
  260. EMIT_CHARACTER_AND_RECONSUME_IN('<', Data);
  261. }
  262. }
  263. END_STATE
  264. BEGIN_STATE(TagName)
  265. {
  266. ON_WHITESPACE
  267. {
  268. m_current_token.set_tag_name(consume_current_builder());
  269. m_current_token.set_end_position({}, nth_last_position(1));
  270. SWITCH_TO(BeforeAttributeName);
  271. }
  272. ON('/')
  273. {
  274. m_current_token.set_tag_name(consume_current_builder());
  275. m_current_token.set_end_position({}, nth_last_position(0));
  276. SWITCH_TO(SelfClosingStartTag);
  277. }
  278. ON('>')
  279. {
  280. m_current_token.set_tag_name(consume_current_builder());
  281. m_current_token.set_end_position({}, nth_last_position(1));
  282. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  283. }
  284. ON_ASCII_UPPER_ALPHA
  285. {
  286. m_current_builder.append_code_point(to_ascii_lowercase(current_input_character.value()));
  287. m_current_token.set_end_position({}, nth_last_position(0));
  288. continue;
  289. }
  290. ON(0)
  291. {
  292. log_parse_error();
  293. m_current_builder.append_code_point(0xFFFD);
  294. m_current_token.set_end_position({}, nth_last_position(0));
  295. continue;
  296. }
  297. ON_EOF
  298. {
  299. log_parse_error();
  300. m_current_token.set_end_position({}, nth_last_position(0));
  301. EMIT_EOF;
  302. }
  303. ANYTHING_ELSE
  304. {
  305. m_current_builder.append_code_point(current_input_character.value());
  306. m_current_token.set_end_position({}, nth_last_position(0));
  307. continue;
  308. }
  309. }
  310. END_STATE
  311. BEGIN_STATE(EndTagOpen)
  312. {
  313. ON_ASCII_ALPHA
  314. {
  315. create_new_token(HTMLToken::Type::EndTag);
  316. RECONSUME_IN(TagName);
  317. }
  318. ON('>')
  319. {
  320. log_parse_error();
  321. SWITCH_TO(Data);
  322. }
  323. ON_EOF
  324. {
  325. log_parse_error();
  326. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  327. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  328. EMIT_EOF;
  329. }
  330. ANYTHING_ELSE
  331. {
  332. log_parse_error();
  333. create_new_token(HTMLToken::Type::Comment);
  334. RECONSUME_IN(BogusComment);
  335. }
  336. }
  337. END_STATE
  338. BEGIN_STATE(MarkupDeclarationOpen)
  339. {
  340. DONT_CONSUME_NEXT_INPUT_CHARACTER;
  341. if (consume_next_if_match("--")) {
  342. create_new_token(HTMLToken::Type::Comment);
  343. m_current_token.set_start_position({}, nth_last_position(4));
  344. SWITCH_TO(CommentStart);
  345. }
  346. if (consume_next_if_match("DOCTYPE", CaseSensitivity::CaseInsensitive)) {
  347. SWITCH_TO(DOCTYPE);
  348. }
  349. if (consume_next_if_match("[CDATA[")) {
  350. TODO();
  351. }
  352. ANYTHING_ELSE
  353. {
  354. log_parse_error();
  355. create_new_token(HTMLToken::Type::Comment);
  356. SWITCH_TO(BogusComment);
  357. }
  358. }
  359. END_STATE
  360. BEGIN_STATE(BogusComment)
  361. {
  362. ON('>')
  363. {
  364. m_current_token.set_comment(consume_current_builder());
  365. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  366. }
  367. ON_EOF
  368. {
  369. m_queued_tokens.enqueue(move(m_current_token));
  370. EMIT_EOF;
  371. }
  372. ON(0)
  373. {
  374. log_parse_error();
  375. m_current_builder.append_code_point(0xFFFD);
  376. continue;
  377. }
  378. ANYTHING_ELSE
  379. {
  380. m_current_builder.append_code_point(current_input_character.value());
  381. continue;
  382. }
  383. }
  384. END_STATE
  385. BEGIN_STATE(DOCTYPE)
  386. {
  387. ON_WHITESPACE
  388. {
  389. SWITCH_TO(BeforeDOCTYPEName);
  390. }
  391. ON('>')
  392. {
  393. RECONSUME_IN(BeforeDOCTYPEName);
  394. }
  395. ON_EOF
  396. {
  397. log_parse_error();
  398. create_new_token(HTMLToken::Type::DOCTYPE);
  399. m_current_token.ensure_doctype_data().force_quirks = true;
  400. m_queued_tokens.enqueue(move(m_current_token));
  401. EMIT_EOF;
  402. }
  403. ANYTHING_ELSE
  404. {
  405. log_parse_error();
  406. RECONSUME_IN(BeforeDOCTYPEName);
  407. }
  408. }
  409. END_STATE
  410. BEGIN_STATE(BeforeDOCTYPEName)
  411. {
  412. ON_WHITESPACE
  413. {
  414. continue;
  415. }
  416. ON_ASCII_UPPER_ALPHA
  417. {
  418. create_new_token(HTMLToken::Type::DOCTYPE);
  419. m_current_builder.append_code_point(to_ascii_lowercase(current_input_character.value()));
  420. m_current_token.ensure_doctype_data().missing_name = false;
  421. SWITCH_TO_WITH_UNCLEAN_BUILDER(DOCTYPEName);
  422. }
  423. ON(0)
  424. {
  425. log_parse_error();
  426. create_new_token(HTMLToken::Type::DOCTYPE);
  427. m_current_builder.append_code_point(0xFFFD);
  428. m_current_token.ensure_doctype_data().missing_name = false;
  429. SWITCH_TO_WITH_UNCLEAN_BUILDER(DOCTYPEName);
  430. }
  431. ON('>')
  432. {
  433. log_parse_error();
  434. create_new_token(HTMLToken::Type::DOCTYPE);
  435. m_current_token.ensure_doctype_data().force_quirks = true;
  436. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  437. }
  438. ON_EOF
  439. {
  440. log_parse_error();
  441. create_new_token(HTMLToken::Type::DOCTYPE);
  442. m_current_token.ensure_doctype_data().force_quirks = true;
  443. m_queued_tokens.enqueue(move(m_current_token));
  444. EMIT_EOF;
  445. }
  446. ANYTHING_ELSE
  447. {
  448. create_new_token(HTMLToken::Type::DOCTYPE);
  449. m_current_builder.append_code_point(current_input_character.value());
  450. m_current_token.ensure_doctype_data().missing_name = false;
  451. SWITCH_TO_WITH_UNCLEAN_BUILDER(DOCTYPEName);
  452. }
  453. }
  454. END_STATE
  455. BEGIN_STATE(DOCTYPEName)
  456. {
  457. ON_WHITESPACE
  458. {
  459. m_current_token.ensure_doctype_data().name = consume_current_builder();
  460. SWITCH_TO(AfterDOCTYPEName);
  461. }
  462. ON('>')
  463. {
  464. m_current_token.ensure_doctype_data().name = consume_current_builder();
  465. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  466. }
  467. ON_ASCII_UPPER_ALPHA
  468. {
  469. m_current_builder.append_code_point(to_ascii_lowercase(current_input_character.value()));
  470. continue;
  471. }
  472. ON(0)
  473. {
  474. log_parse_error();
  475. m_current_builder.append_code_point(0xFFFD);
  476. continue;
  477. }
  478. ON_EOF
  479. {
  480. log_parse_error();
  481. m_current_token.ensure_doctype_data().force_quirks = true;
  482. m_queued_tokens.enqueue(move(m_current_token));
  483. EMIT_EOF;
  484. }
  485. ANYTHING_ELSE
  486. {
  487. m_current_builder.append_code_point(current_input_character.value());
  488. continue;
  489. }
  490. }
  491. END_STATE
  492. BEGIN_STATE(AfterDOCTYPEName)
  493. {
  494. ON_WHITESPACE
  495. {
  496. continue;
  497. }
  498. ON('>')
  499. {
  500. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  501. }
  502. ON_EOF
  503. {
  504. log_parse_error();
  505. m_current_token.ensure_doctype_data().force_quirks = true;
  506. m_queued_tokens.enqueue(move(m_current_token));
  507. EMIT_EOF;
  508. }
  509. ANYTHING_ELSE
  510. {
  511. if (to_ascii_uppercase(current_input_character.value()) == 'P' && consume_next_if_match("UBLIC", CaseSensitivity::CaseInsensitive)) {
  512. SWITCH_TO(AfterDOCTYPEPublicKeyword);
  513. }
  514. if (to_ascii_uppercase(current_input_character.value()) == 'S' && consume_next_if_match("YSTEM", CaseSensitivity::CaseInsensitive)) {
  515. SWITCH_TO(AfterDOCTYPESystemKeyword);
  516. }
  517. log_parse_error();
  518. m_current_token.ensure_doctype_data().force_quirks = true;
  519. RECONSUME_IN(BogusDOCTYPE);
  520. }
  521. }
  522. END_STATE
  523. BEGIN_STATE(AfterDOCTYPEPublicKeyword)
  524. {
  525. ON_WHITESPACE
  526. {
  527. SWITCH_TO(BeforeDOCTYPEPublicIdentifier);
  528. }
  529. ON('"')
  530. {
  531. log_parse_error();
  532. m_current_token.ensure_doctype_data().missing_public_identifier = false;
  533. SWITCH_TO(DOCTYPEPublicIdentifierDoubleQuoted);
  534. }
  535. ON('\'')
  536. {
  537. log_parse_error();
  538. m_current_token.ensure_doctype_data().missing_public_identifier = false;
  539. SWITCH_TO(DOCTYPEPublicIdentifierSingleQuoted);
  540. }
  541. ON('>')
  542. {
  543. log_parse_error();
  544. m_current_token.ensure_doctype_data().force_quirks = true;
  545. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  546. }
  547. ON_EOF
  548. {
  549. log_parse_error();
  550. m_current_token.ensure_doctype_data().force_quirks = true;
  551. m_queued_tokens.enqueue(move(m_current_token));
  552. EMIT_EOF;
  553. }
  554. ANYTHING_ELSE
  555. {
  556. log_parse_error();
  557. m_current_token.ensure_doctype_data().force_quirks = true;
  558. RECONSUME_IN(BogusDOCTYPE);
  559. }
  560. }
  561. END_STATE
  562. BEGIN_STATE(AfterDOCTYPESystemKeyword)
  563. {
  564. ON_WHITESPACE
  565. {
  566. SWITCH_TO(BeforeDOCTYPESystemIdentifier);
  567. }
  568. ON('"')
  569. {
  570. log_parse_error();
  571. m_current_token.ensure_doctype_data().system_identifier = {};
  572. m_current_token.ensure_doctype_data().missing_system_identifier = false;
  573. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  574. }
  575. ON('\'')
  576. {
  577. log_parse_error();
  578. m_current_token.ensure_doctype_data().system_identifier = {};
  579. m_current_token.ensure_doctype_data().missing_system_identifier = false;
  580. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  581. }
  582. ON('>')
  583. {
  584. log_parse_error();
  585. m_current_token.ensure_doctype_data().force_quirks = true;
  586. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  587. }
  588. ON_EOF
  589. {
  590. log_parse_error();
  591. m_current_token.ensure_doctype_data().force_quirks = true;
  592. m_queued_tokens.enqueue(move(m_current_token));
  593. EMIT_EOF;
  594. }
  595. ANYTHING_ELSE
  596. {
  597. log_parse_error();
  598. m_current_token.ensure_doctype_data().force_quirks = true;
  599. RECONSUME_IN(BogusDOCTYPE);
  600. }
  601. }
  602. END_STATE
  603. BEGIN_STATE(BeforeDOCTYPEPublicIdentifier)
  604. {
  605. ON_WHITESPACE
  606. {
  607. continue;
  608. }
  609. ON('"')
  610. {
  611. m_current_token.ensure_doctype_data().missing_public_identifier = false;
  612. SWITCH_TO(DOCTYPEPublicIdentifierDoubleQuoted);
  613. }
  614. ON('\'')
  615. {
  616. m_current_token.ensure_doctype_data().missing_public_identifier = false;
  617. SWITCH_TO(DOCTYPEPublicIdentifierSingleQuoted);
  618. }
  619. ON('>')
  620. {
  621. log_parse_error();
  622. m_current_token.ensure_doctype_data().force_quirks = true;
  623. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  624. }
  625. ON_EOF
  626. {
  627. log_parse_error();
  628. m_current_token.ensure_doctype_data().force_quirks = true;
  629. m_queued_tokens.enqueue(move(m_current_token));
  630. EMIT_EOF;
  631. }
  632. ANYTHING_ELSE
  633. {
  634. log_parse_error();
  635. m_current_token.ensure_doctype_data().force_quirks = true;
  636. RECONSUME_IN(BogusDOCTYPE);
  637. }
  638. }
  639. END_STATE
  640. BEGIN_STATE(BeforeDOCTYPESystemIdentifier)
  641. {
  642. ON_WHITESPACE
  643. {
  644. continue;
  645. }
  646. ON('"')
  647. {
  648. m_current_token.ensure_doctype_data().missing_system_identifier = false;
  649. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  650. }
  651. ON('\'')
  652. {
  653. m_current_token.ensure_doctype_data().missing_system_identifier = false;
  654. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  655. }
  656. ON('>')
  657. {
  658. log_parse_error();
  659. m_current_token.ensure_doctype_data().force_quirks = true;
  660. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  661. }
  662. ON_EOF
  663. {
  664. log_parse_error();
  665. m_current_token.ensure_doctype_data().force_quirks = true;
  666. m_queued_tokens.enqueue(move(m_current_token));
  667. EMIT_EOF;
  668. }
  669. ANYTHING_ELSE
  670. {
  671. log_parse_error();
  672. m_current_token.ensure_doctype_data().force_quirks = true;
  673. RECONSUME_IN(BogusDOCTYPE);
  674. }
  675. }
  676. END_STATE
  677. BEGIN_STATE(DOCTYPEPublicIdentifierDoubleQuoted)
  678. {
  679. ON('"')
  680. {
  681. m_current_token.ensure_doctype_data().public_identifier = consume_current_builder();
  682. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  683. }
  684. ON(0)
  685. {
  686. log_parse_error();
  687. m_current_builder.append_code_point(0xFFFD);
  688. continue;
  689. }
  690. ON('>')
  691. {
  692. log_parse_error();
  693. m_current_token.ensure_doctype_data().public_identifier = consume_current_builder();
  694. m_current_token.ensure_doctype_data().force_quirks = true;
  695. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  696. }
  697. ON_EOF
  698. {
  699. log_parse_error();
  700. m_current_token.ensure_doctype_data().force_quirks = true;
  701. m_queued_tokens.enqueue(move(m_current_token));
  702. EMIT_EOF;
  703. }
  704. ANYTHING_ELSE
  705. {
  706. m_current_builder.append_code_point(current_input_character.value());
  707. continue;
  708. }
  709. }
  710. END_STATE
  711. BEGIN_STATE(DOCTYPEPublicIdentifierSingleQuoted)
  712. {
  713. ON('\'')
  714. {
  715. m_current_token.ensure_doctype_data().public_identifier = consume_current_builder();
  716. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  717. }
  718. ON(0)
  719. {
  720. log_parse_error();
  721. m_current_builder.append_code_point(0xFFFD);
  722. continue;
  723. }
  724. ON('>')
  725. {
  726. log_parse_error();
  727. m_current_token.ensure_doctype_data().public_identifier = consume_current_builder();
  728. m_current_token.ensure_doctype_data().force_quirks = true;
  729. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  730. }
  731. ON_EOF
  732. {
  733. log_parse_error();
  734. m_current_token.ensure_doctype_data().force_quirks = true;
  735. m_queued_tokens.enqueue(move(m_current_token));
  736. EMIT_EOF;
  737. }
  738. ANYTHING_ELSE
  739. {
  740. m_current_builder.append_code_point(current_input_character.value());
  741. continue;
  742. }
  743. }
  744. END_STATE
  745. BEGIN_STATE(DOCTYPESystemIdentifierDoubleQuoted)
  746. {
  747. ON('"')
  748. {
  749. m_current_token.ensure_doctype_data().public_identifier = consume_current_builder();
  750. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  751. }
  752. ON(0)
  753. {
  754. log_parse_error();
  755. m_current_builder.append_code_point(0xFFFD);
  756. continue;
  757. }
  758. ON('>')
  759. {
  760. log_parse_error();
  761. m_current_token.ensure_doctype_data().public_identifier = consume_current_builder();
  762. m_current_token.ensure_doctype_data().force_quirks = true;
  763. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  764. }
  765. ON_EOF
  766. {
  767. log_parse_error();
  768. m_current_token.ensure_doctype_data().force_quirks = true;
  769. m_queued_tokens.enqueue(move(m_current_token));
  770. EMIT_EOF;
  771. }
  772. ANYTHING_ELSE
  773. {
  774. m_current_builder.append_code_point(current_input_character.value());
  775. continue;
  776. }
  777. }
  778. END_STATE
  779. BEGIN_STATE(DOCTYPESystemIdentifierSingleQuoted)
  780. {
  781. ON('\'')
  782. {
  783. m_current_token.ensure_doctype_data().system_identifier = consume_current_builder();
  784. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  785. }
  786. ON(0)
  787. {
  788. log_parse_error();
  789. m_current_builder.append_code_point(0xFFFD);
  790. continue;
  791. }
  792. ON('>')
  793. {
  794. log_parse_error();
  795. m_current_token.ensure_doctype_data().system_identifier = consume_current_builder();
  796. m_current_token.ensure_doctype_data().force_quirks = true;
  797. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  798. }
  799. ON_EOF
  800. {
  801. log_parse_error();
  802. m_current_token.ensure_doctype_data().force_quirks = true;
  803. m_queued_tokens.enqueue(move(m_current_token));
  804. EMIT_EOF;
  805. }
  806. ANYTHING_ELSE
  807. {
  808. m_current_builder.append_code_point(current_input_character.value());
  809. continue;
  810. }
  811. }
  812. END_STATE
  813. BEGIN_STATE(AfterDOCTYPEPublicIdentifier)
  814. {
  815. ON_WHITESPACE
  816. {
  817. SWITCH_TO(BetweenDOCTYPEPublicAndSystemIdentifiers);
  818. }
  819. ON('>')
  820. {
  821. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  822. }
  823. ON('"')
  824. {
  825. log_parse_error();
  826. m_current_token.ensure_doctype_data().missing_system_identifier = false;
  827. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  828. }
  829. ON('\'')
  830. {
  831. log_parse_error();
  832. m_current_token.ensure_doctype_data().missing_system_identifier = false;
  833. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  834. }
  835. ON_EOF
  836. {
  837. log_parse_error();
  838. m_current_token.ensure_doctype_data().force_quirks = true;
  839. m_queued_tokens.enqueue(move(m_current_token));
  840. EMIT_EOF;
  841. }
  842. ANYTHING_ELSE
  843. {
  844. log_parse_error();
  845. m_current_token.ensure_doctype_data().force_quirks = true;
  846. RECONSUME_IN(BogusDOCTYPE);
  847. }
  848. }
  849. END_STATE
  850. BEGIN_STATE(BetweenDOCTYPEPublicAndSystemIdentifiers)
  851. {
  852. ON_WHITESPACE
  853. {
  854. continue;
  855. }
  856. ON('>')
  857. {
  858. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  859. }
  860. ON('"')
  861. {
  862. m_current_token.ensure_doctype_data().missing_system_identifier = false;
  863. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  864. }
  865. ON('\'')
  866. {
  867. m_current_token.ensure_doctype_data().missing_system_identifier = false;
  868. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  869. }
  870. ON_EOF
  871. {
  872. log_parse_error();
  873. m_current_token.ensure_doctype_data().force_quirks = true;
  874. m_queued_tokens.enqueue(move(m_current_token));
  875. EMIT_EOF;
  876. }
  877. ANYTHING_ELSE
  878. {
  879. log_parse_error();
  880. m_current_token.ensure_doctype_data().force_quirks = true;
  881. RECONSUME_IN(BogusDOCTYPE);
  882. }
  883. }
  884. END_STATE
  885. BEGIN_STATE(AfterDOCTYPESystemIdentifier)
  886. {
  887. ON_WHITESPACE
  888. {
  889. continue;
  890. }
  891. ON('>')
  892. {
  893. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  894. }
  895. ON_EOF
  896. {
  897. log_parse_error();
  898. m_current_token.ensure_doctype_data().force_quirks = true;
  899. m_queued_tokens.enqueue(move(m_current_token));
  900. EMIT_EOF;
  901. }
  902. ANYTHING_ELSE
  903. {
  904. log_parse_error();
  905. RECONSUME_IN(BogusDOCTYPE);
  906. }
  907. }
  908. END_STATE
  909. BEGIN_STATE(BogusDOCTYPE)
  910. {
  911. ON('>')
  912. {
  913. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  914. }
  915. ON(0)
  916. {
  917. log_parse_error();
  918. continue;
  919. }
  920. ON_EOF
  921. {
  922. m_queued_tokens.enqueue(move(m_current_token));
  923. EMIT_EOF;
  924. }
  925. ANYTHING_ELSE
  926. {
  927. continue;
  928. }
  929. }
  930. END_STATE
  931. BEGIN_STATE(BeforeAttributeName)
  932. {
  933. ON_WHITESPACE
  934. {
  935. continue;
  936. }
  937. ON('/')
  938. {
  939. if (m_current_token.has_attributes())
  940. m_current_token.last_attribute().name_end_position = nth_last_position(1);
  941. RECONSUME_IN(AfterAttributeName);
  942. }
  943. ON('>')
  944. {
  945. RECONSUME_IN(AfterAttributeName);
  946. }
  947. ON_EOF
  948. {
  949. RECONSUME_IN(AfterAttributeName);
  950. }
  951. ON('=')
  952. {
  953. log_parse_error();
  954. HTMLToken::Attribute new_attribute;
  955. new_attribute.name_start_position = nth_last_position(1);
  956. m_current_builder.append_code_point(current_input_character.value());
  957. m_current_token.add_attribute(move(new_attribute));
  958. SWITCH_TO_WITH_UNCLEAN_BUILDER(AttributeName);
  959. }
  960. ANYTHING_ELSE
  961. {
  962. HTMLToken::Attribute new_attribute;
  963. new_attribute.name_start_position = nth_last_position(1);
  964. m_current_token.add_attribute(move(new_attribute));
  965. RECONSUME_IN(AttributeName);
  966. }
  967. }
  968. END_STATE
  969. BEGIN_STATE(SelfClosingStartTag)
  970. {
  971. ON('>')
  972. {
  973. m_current_token.set_self_closing(true);
  974. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  975. }
  976. ON_EOF
  977. {
  978. log_parse_error();
  979. EMIT_EOF;
  980. }
  981. ANYTHING_ELSE
  982. {
  983. log_parse_error();
  984. RECONSUME_IN(BeforeAttributeName);
  985. }
  986. }
  987. END_STATE
  988. BEGIN_STATE(AttributeName)
  989. {
  990. ON_WHITESPACE
  991. {
  992. m_current_token.last_attribute().local_name = consume_current_builder();
  993. RECONSUME_IN(AfterAttributeName);
  994. }
  995. ON('/')
  996. {
  997. m_current_token.last_attribute().local_name = consume_current_builder();
  998. RECONSUME_IN(AfterAttributeName);
  999. }
  1000. ON('>')
  1001. {
  1002. m_current_token.last_attribute().local_name = consume_current_builder();
  1003. RECONSUME_IN(AfterAttributeName);
  1004. }
  1005. ON_EOF
  1006. {
  1007. m_current_token.last_attribute().local_name = consume_current_builder();
  1008. RECONSUME_IN(AfterAttributeName);
  1009. }
  1010. ON('=')
  1011. {
  1012. m_current_token.last_attribute().name_end_position = nth_last_position(1);
  1013. m_current_token.last_attribute().local_name = consume_current_builder();
  1014. SWITCH_TO(BeforeAttributeValue);
  1015. }
  1016. ON_ASCII_UPPER_ALPHA
  1017. {
  1018. m_current_builder.append_code_point(to_ascii_lowercase(current_input_character.value()));
  1019. continue;
  1020. }
  1021. ON(0)
  1022. {
  1023. log_parse_error();
  1024. m_current_builder.append_code_point(0xFFFD);
  1025. continue;
  1026. }
  1027. ON('"')
  1028. {
  1029. log_parse_error();
  1030. goto AnythingElseAttributeName;
  1031. }
  1032. ON('\'')
  1033. {
  1034. log_parse_error();
  1035. goto AnythingElseAttributeName;
  1036. }
  1037. ON('<')
  1038. {
  1039. log_parse_error();
  1040. goto AnythingElseAttributeName;
  1041. }
  1042. ANYTHING_ELSE
  1043. {
  1044. AnythingElseAttributeName:
  1045. m_current_builder.append_code_point(current_input_character.value());
  1046. continue;
  1047. }
  1048. }
  1049. END_STATE
  1050. BEGIN_STATE(AfterAttributeName)
  1051. {
  1052. ON_WHITESPACE
  1053. {
  1054. continue;
  1055. }
  1056. ON('/')
  1057. {
  1058. SWITCH_TO(SelfClosingStartTag);
  1059. }
  1060. ON('=')
  1061. {
  1062. m_current_token.last_attribute().name_end_position = nth_last_position(1);
  1063. SWITCH_TO(BeforeAttributeValue);
  1064. }
  1065. ON('>')
  1066. {
  1067. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1068. }
  1069. ON_EOF
  1070. {
  1071. log_parse_error();
  1072. EMIT_EOF;
  1073. }
  1074. ANYTHING_ELSE
  1075. {
  1076. m_current_token.add_attribute({});
  1077. m_current_token.last_attribute().name_start_position = m_source_positions.last();
  1078. RECONSUME_IN(AttributeName);
  1079. }
  1080. }
  1081. END_STATE
  1082. BEGIN_STATE(BeforeAttributeValue)
  1083. {
  1084. m_current_token.last_attribute().value_start_position = nth_last_position(1);
  1085. ON_WHITESPACE
  1086. {
  1087. continue;
  1088. }
  1089. ON('"')
  1090. {
  1091. SWITCH_TO(AttributeValueDoubleQuoted);
  1092. }
  1093. ON('\'')
  1094. {
  1095. SWITCH_TO(AttributeValueSingleQuoted);
  1096. }
  1097. ON('>')
  1098. {
  1099. log_parse_error();
  1100. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1101. }
  1102. ANYTHING_ELSE
  1103. {
  1104. RECONSUME_IN(AttributeValueUnquoted);
  1105. }
  1106. }
  1107. END_STATE
  1108. BEGIN_STATE(AttributeValueDoubleQuoted)
  1109. {
  1110. ON('"')
  1111. {
  1112. m_current_token.last_attribute().value = consume_current_builder();
  1113. SWITCH_TO(AfterAttributeValueQuoted);
  1114. }
  1115. ON('&')
  1116. {
  1117. m_return_state = State::AttributeValueDoubleQuoted;
  1118. SWITCH_TO_WITH_UNCLEAN_BUILDER(CharacterReference);
  1119. }
  1120. ON(0)
  1121. {
  1122. log_parse_error();
  1123. m_current_builder.append_code_point(0xFFFD);
  1124. continue;
  1125. }
  1126. ON_EOF
  1127. {
  1128. log_parse_error();
  1129. EMIT_EOF;
  1130. }
  1131. ANYTHING_ELSE
  1132. {
  1133. m_current_builder.append_code_point(current_input_character.value());
  1134. continue;
  1135. }
  1136. }
  1137. END_STATE
  1138. BEGIN_STATE(AttributeValueSingleQuoted)
  1139. {
  1140. ON('\'')
  1141. {
  1142. m_current_token.last_attribute().value = consume_current_builder();
  1143. SWITCH_TO(AfterAttributeValueQuoted);
  1144. }
  1145. ON('&')
  1146. {
  1147. m_return_state = State::AttributeValueSingleQuoted;
  1148. SWITCH_TO_WITH_UNCLEAN_BUILDER(CharacterReference);
  1149. }
  1150. ON(0)
  1151. {
  1152. log_parse_error();
  1153. m_current_builder.append_code_point(0xFFFD);
  1154. continue;
  1155. }
  1156. ON_EOF
  1157. {
  1158. log_parse_error();
  1159. EMIT_EOF;
  1160. }
  1161. ANYTHING_ELSE
  1162. {
  1163. m_current_builder.append_code_point(current_input_character.value());
  1164. continue;
  1165. }
  1166. }
  1167. END_STATE
  1168. BEGIN_STATE(AttributeValueUnquoted)
  1169. {
  1170. ON_WHITESPACE
  1171. {
  1172. m_current_token.last_attribute().value = consume_current_builder();
  1173. m_current_token.last_attribute().value_end_position = nth_last_position(2);
  1174. SWITCH_TO(BeforeAttributeName);
  1175. }
  1176. ON('&')
  1177. {
  1178. m_return_state = State::AttributeValueUnquoted;
  1179. SWITCH_TO_WITH_UNCLEAN_BUILDER(CharacterReference);
  1180. }
  1181. ON('>')
  1182. {
  1183. m_current_token.last_attribute().value = consume_current_builder();
  1184. m_current_token.last_attribute().value_end_position = nth_last_position(1);
  1185. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1186. }
  1187. ON(0)
  1188. {
  1189. log_parse_error();
  1190. m_current_builder.append_code_point(0xFFFD);
  1191. continue;
  1192. }
  1193. ON('"')
  1194. {
  1195. log_parse_error();
  1196. goto AnythingElseAttributeValueUnquoted;
  1197. }
  1198. ON('\'')
  1199. {
  1200. log_parse_error();
  1201. goto AnythingElseAttributeValueUnquoted;
  1202. }
  1203. ON('<')
  1204. {
  1205. log_parse_error();
  1206. goto AnythingElseAttributeValueUnquoted;
  1207. }
  1208. ON('=')
  1209. {
  1210. log_parse_error();
  1211. goto AnythingElseAttributeValueUnquoted;
  1212. }
  1213. ON('`')
  1214. {
  1215. log_parse_error();
  1216. goto AnythingElseAttributeValueUnquoted;
  1217. }
  1218. ON_EOF
  1219. {
  1220. log_parse_error();
  1221. EMIT_EOF;
  1222. }
  1223. ANYTHING_ELSE
  1224. {
  1225. AnythingElseAttributeValueUnquoted:
  1226. m_current_builder.append_code_point(current_input_character.value());
  1227. continue;
  1228. }
  1229. }
  1230. END_STATE
  1231. BEGIN_STATE(AfterAttributeValueQuoted)
  1232. {
  1233. m_current_token.last_attribute().value_end_position = nth_last_position(1);
  1234. ON_WHITESPACE
  1235. {
  1236. SWITCH_TO(BeforeAttributeName);
  1237. }
  1238. ON('/')
  1239. {
  1240. SWITCH_TO(SelfClosingStartTag);
  1241. }
  1242. ON('>')
  1243. {
  1244. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1245. }
  1246. ON_EOF
  1247. {
  1248. log_parse_error();
  1249. EMIT_EOF;
  1250. }
  1251. ANYTHING_ELSE
  1252. {
  1253. log_parse_error();
  1254. RECONSUME_IN(BeforeAttributeName);
  1255. }
  1256. }
  1257. END_STATE
  1258. BEGIN_STATE(CommentStart)
  1259. {
  1260. ON('-')
  1261. {
  1262. SWITCH_TO(CommentStartDash);
  1263. }
  1264. ON('>')
  1265. {
  1266. log_parse_error();
  1267. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1268. }
  1269. ANYTHING_ELSE
  1270. {
  1271. RECONSUME_IN(Comment);
  1272. }
  1273. }
  1274. END_STATE
  1275. BEGIN_STATE(CommentStartDash)
  1276. {
  1277. ON('-')
  1278. {
  1279. SWITCH_TO(CommentEnd);
  1280. }
  1281. ON('>')
  1282. {
  1283. log_parse_error();
  1284. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1285. }
  1286. ON_EOF
  1287. {
  1288. log_parse_error();
  1289. EMIT_CURRENT_TOKEN;
  1290. EMIT_EOF;
  1291. }
  1292. ANYTHING_ELSE
  1293. {
  1294. m_current_builder.append('-');
  1295. RECONSUME_IN(Comment);
  1296. }
  1297. }
  1298. END_STATE
  1299. BEGIN_STATE(Comment)
  1300. {
  1301. ON('<')
  1302. {
  1303. m_current_builder.append_code_point(current_input_character.value());
  1304. SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentLessThanSign);
  1305. }
  1306. ON('-')
  1307. {
  1308. SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentEndDash);
  1309. }
  1310. ON(0)
  1311. {
  1312. log_parse_error();
  1313. m_current_builder.append_code_point(0xFFFD);
  1314. continue;
  1315. }
  1316. ON_EOF
  1317. {
  1318. log_parse_error();
  1319. m_current_token.set_comment(consume_current_builder());
  1320. EMIT_CURRENT_TOKEN;
  1321. EMIT_EOF;
  1322. }
  1323. ANYTHING_ELSE
  1324. {
  1325. m_current_builder.append_code_point(current_input_character.value());
  1326. continue;
  1327. }
  1328. }
  1329. END_STATE
  1330. BEGIN_STATE(CommentEnd)
  1331. {
  1332. ON('>')
  1333. {
  1334. m_current_token.set_comment(consume_current_builder());
  1335. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1336. }
  1337. ON('!')
  1338. {
  1339. SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentEndBang);
  1340. }
  1341. ON('-')
  1342. {
  1343. m_current_builder.append('-');
  1344. continue;
  1345. }
  1346. ON_EOF
  1347. {
  1348. log_parse_error();
  1349. m_current_token.set_comment(consume_current_builder());
  1350. EMIT_CURRENT_TOKEN;
  1351. EMIT_EOF;
  1352. }
  1353. ANYTHING_ELSE
  1354. {
  1355. m_current_builder.append('-');
  1356. RECONSUME_IN(Comment);
  1357. }
  1358. }
  1359. END_STATE
  1360. BEGIN_STATE(CommentEndBang)
  1361. {
  1362. ON('-')
  1363. {
  1364. m_current_builder.append("--!");
  1365. SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentEndDash);
  1366. }
  1367. ON('>')
  1368. {
  1369. log_parse_error();
  1370. m_current_token.set_comment(consume_current_builder());
  1371. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1372. }
  1373. ON_EOF
  1374. {
  1375. log_parse_error();
  1376. m_current_token.set_comment(consume_current_builder());
  1377. EMIT_CURRENT_TOKEN;
  1378. EMIT_EOF;
  1379. }
  1380. ANYTHING_ELSE
  1381. {
  1382. m_current_builder.append("--!");
  1383. RECONSUME_IN(Comment);
  1384. }
  1385. }
  1386. END_STATE
  1387. BEGIN_STATE(CommentEndDash)
  1388. {
  1389. ON('-')
  1390. {
  1391. SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentEnd);
  1392. }
  1393. ON_EOF
  1394. {
  1395. log_parse_error();
  1396. m_current_token.set_comment(consume_current_builder());
  1397. EMIT_CURRENT_TOKEN;
  1398. EMIT_EOF;
  1399. }
  1400. ANYTHING_ELSE
  1401. {
  1402. m_current_builder.append('-');
  1403. RECONSUME_IN(Comment);
  1404. }
  1405. }
  1406. END_STATE
  1407. BEGIN_STATE(CommentLessThanSign)
  1408. {
  1409. ON('!')
  1410. {
  1411. m_current_builder.append_code_point(current_input_character.value());
  1412. SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentLessThanSignBang);
  1413. }
  1414. ON('<')
  1415. {
  1416. m_current_builder.append_code_point(current_input_character.value());
  1417. continue;
  1418. }
  1419. ANYTHING_ELSE
  1420. {
  1421. RECONSUME_IN(Comment);
  1422. }
  1423. }
  1424. END_STATE
  1425. BEGIN_STATE(CommentLessThanSignBang)
  1426. {
  1427. ON('-')
  1428. {
  1429. SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentLessThanSignBangDash);
  1430. }
  1431. ANYTHING_ELSE
  1432. {
  1433. RECONSUME_IN(Comment);
  1434. }
  1435. }
  1436. END_STATE
  1437. BEGIN_STATE(CommentLessThanSignBangDash)
  1438. {
  1439. ON('-')
  1440. {
  1441. SWITCH_TO_WITH_UNCLEAN_BUILDER(CommentLessThanSignBangDashDash);
  1442. }
  1443. ANYTHING_ELSE
  1444. {
  1445. RECONSUME_IN(CommentEndDash);
  1446. }
  1447. }
  1448. END_STATE
  1449. BEGIN_STATE(CommentLessThanSignBangDashDash)
  1450. {
  1451. ON('>')
  1452. {
  1453. RECONSUME_IN(CommentEnd);
  1454. }
  1455. ON_EOF
  1456. {
  1457. RECONSUME_IN(CommentEnd);
  1458. }
  1459. ANYTHING_ELSE
  1460. {
  1461. log_parse_error();
  1462. RECONSUME_IN(CommentEnd);
  1463. }
  1464. }
  1465. END_STATE
  1466. BEGIN_STATE(CharacterReference)
  1467. {
  1468. m_temporary_buffer.clear();
  1469. m_temporary_buffer.append('&');
  1470. ON_ASCII_ALPHANUMERIC
  1471. {
  1472. RECONSUME_IN(NamedCharacterReference);
  1473. }
  1474. ON('#')
  1475. {
  1476. m_temporary_buffer.append(current_input_character.value());
  1477. SWITCH_TO_WITH_UNCLEAN_BUILDER(NumericCharacterReference);
  1478. }
  1479. ANYTHING_ELSE
  1480. {
  1481. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1482. RECONSUME_IN_RETURN_STATE;
  1483. }
  1484. }
  1485. END_STATE
  1486. BEGIN_STATE(NamedCharacterReference)
  1487. {
  1488. size_t byte_offset = m_utf8_view.byte_offset_of(m_prev_utf8_iterator);
  1489. auto match = HTML::code_points_from_entity(m_decoded_input.substring_view(byte_offset, m_decoded_input.length() - byte_offset - 1));
  1490. if (match.has_value()) {
  1491. skip(match->entity.length() - 1);
  1492. for (auto ch : match.value().entity)
  1493. m_temporary_buffer.append(ch);
  1494. if (consumed_as_part_of_an_attribute() && !match.value().entity.ends_with(';')) {
  1495. auto next_code_point = peek_code_point(0);
  1496. if (next_code_point.has_value() && (next_code_point.value() == '=' || is_ascii_alphanumeric(next_code_point.value()))) {
  1497. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1498. SWITCH_TO_RETURN_STATE;
  1499. }
  1500. }
  1501. if (!match.value().entity.ends_with(';')) {
  1502. log_parse_error();
  1503. }
  1504. m_temporary_buffer = match.value().code_points;
  1505. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1506. SWITCH_TO_RETURN_STATE;
  1507. } else {
  1508. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1509. // FIXME: This should be SWITCH_TO, but we always lose the first character on this path, so just reconsume it.
  1510. // I can't wrap my head around how to do it as the spec says.
  1511. RECONSUME_IN(AmbiguousAmpersand);
  1512. }
  1513. }
  1514. END_STATE
  1515. BEGIN_STATE(AmbiguousAmpersand)
  1516. {
  1517. ON_ASCII_ALPHANUMERIC
  1518. {
  1519. if (consumed_as_part_of_an_attribute()) {
  1520. m_current_builder.append_code_point(current_input_character.value());
  1521. continue;
  1522. } else {
  1523. EMIT_CURRENT_CHARACTER;
  1524. }
  1525. }
  1526. ON(';')
  1527. {
  1528. log_parse_error();
  1529. RECONSUME_IN_RETURN_STATE;
  1530. }
  1531. ANYTHING_ELSE
  1532. {
  1533. RECONSUME_IN_RETURN_STATE;
  1534. }
  1535. }
  1536. END_STATE
  1537. BEGIN_STATE(NumericCharacterReference)
  1538. {
  1539. m_character_reference_code = 0;
  1540. ON('X')
  1541. {
  1542. m_temporary_buffer.append(current_input_character.value());
  1543. SWITCH_TO_WITH_UNCLEAN_BUILDER(HexadecimalCharacterReferenceStart);
  1544. }
  1545. ON('x')
  1546. {
  1547. m_temporary_buffer.append(current_input_character.value());
  1548. SWITCH_TO_WITH_UNCLEAN_BUILDER(HexadecimalCharacterReferenceStart);
  1549. }
  1550. ANYTHING_ELSE
  1551. {
  1552. RECONSUME_IN(DecimalCharacterReferenceStart);
  1553. }
  1554. }
  1555. END_STATE
  1556. BEGIN_STATE(HexadecimalCharacterReferenceStart)
  1557. {
  1558. ON_ASCII_HEX_DIGIT
  1559. {
  1560. RECONSUME_IN(HexadecimalCharacterReference);
  1561. }
  1562. ANYTHING_ELSE
  1563. {
  1564. log_parse_error();
  1565. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1566. RECONSUME_IN_RETURN_STATE;
  1567. }
  1568. }
  1569. END_STATE
  1570. BEGIN_STATE(DecimalCharacterReferenceStart)
  1571. {
  1572. ON_ASCII_DIGIT
  1573. {
  1574. RECONSUME_IN(DecimalCharacterReference);
  1575. }
  1576. ANYTHING_ELSE
  1577. {
  1578. log_parse_error();
  1579. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1580. RECONSUME_IN_RETURN_STATE;
  1581. }
  1582. }
  1583. END_STATE
  1584. BEGIN_STATE(HexadecimalCharacterReference)
  1585. {
  1586. ON_ASCII_DIGIT
  1587. {
  1588. m_character_reference_code *= 16;
  1589. m_character_reference_code += current_input_character.value() - 0x30;
  1590. continue;
  1591. }
  1592. ON_ASCII_UPPER_ALPHA
  1593. {
  1594. m_character_reference_code *= 16;
  1595. m_character_reference_code += current_input_character.value() - 0x37;
  1596. continue;
  1597. }
  1598. ON_ASCII_LOWER_ALPHA
  1599. {
  1600. m_character_reference_code *= 16;
  1601. m_character_reference_code += current_input_character.value() - 0x57;
  1602. continue;
  1603. }
  1604. ON(';')
  1605. {
  1606. SWITCH_TO_WITH_UNCLEAN_BUILDER(NumericCharacterReferenceEnd);
  1607. }
  1608. ANYTHING_ELSE
  1609. {
  1610. log_parse_error();
  1611. RECONSUME_IN(NumericCharacterReferenceEnd);
  1612. }
  1613. }
  1614. END_STATE
  1615. BEGIN_STATE(DecimalCharacterReference)
  1616. {
  1617. ON_ASCII_DIGIT
  1618. {
  1619. m_character_reference_code *= 10;
  1620. m_character_reference_code += current_input_character.value() - 0x30;
  1621. continue;
  1622. }
  1623. ON(';')
  1624. {
  1625. SWITCH_TO_WITH_UNCLEAN_BUILDER(NumericCharacterReferenceEnd);
  1626. }
  1627. ANYTHING_ELSE
  1628. {
  1629. log_parse_error();
  1630. RECONSUME_IN(NumericCharacterReferenceEnd);
  1631. }
  1632. }
  1633. END_STATE
  1634. BEGIN_STATE(NumericCharacterReferenceEnd)
  1635. {
  1636. DONT_CONSUME_NEXT_INPUT_CHARACTER;
  1637. if (m_character_reference_code == 0) {
  1638. log_parse_error();
  1639. m_character_reference_code = 0xFFFD;
  1640. }
  1641. if (m_character_reference_code > 0x10ffff) {
  1642. log_parse_error();
  1643. m_character_reference_code = 0xFFFD;
  1644. }
  1645. if (is_unicode_surrogate(m_character_reference_code)) {
  1646. log_parse_error();
  1647. m_character_reference_code = 0xFFFD;
  1648. }
  1649. if (is_unicode_noncharacter(m_character_reference_code)) {
  1650. log_parse_error();
  1651. }
  1652. if (m_character_reference_code == 0xd || (is_unicode_control(m_character_reference_code) && !is_ascii_space(m_character_reference_code))) {
  1653. log_parse_error();
  1654. constexpr struct {
  1655. u32 number;
  1656. u32 code_point;
  1657. } conversion_table[] = {
  1658. { 0x80, 0x20AC },
  1659. { 0x82, 0x201A },
  1660. { 0x83, 0x0192 },
  1661. { 0x84, 0x201E },
  1662. { 0x85, 0x2026 },
  1663. { 0x86, 0x2020 },
  1664. { 0x87, 0x2021 },
  1665. { 0x88, 0x02C6 },
  1666. { 0x89, 0x2030 },
  1667. { 0x8A, 0x0160 },
  1668. { 0x8B, 0x2039 },
  1669. { 0x8C, 0x0152 },
  1670. { 0x8E, 0x017D },
  1671. { 0x91, 0x2018 },
  1672. { 0x92, 0x2019 },
  1673. { 0x93, 0x201C },
  1674. { 0x94, 0x201D },
  1675. { 0x95, 0x2022 },
  1676. { 0x96, 0x2013 },
  1677. { 0x97, 0x2014 },
  1678. { 0x98, 0x02DC },
  1679. { 0x99, 0x2122 },
  1680. { 0x9A, 0x0161 },
  1681. { 0x9B, 0x203A },
  1682. { 0x9C, 0x0153 },
  1683. { 0x9E, 0x017E },
  1684. { 0x9F, 0x0178 },
  1685. };
  1686. for (auto& entry : conversion_table) {
  1687. if (m_character_reference_code == entry.number) {
  1688. m_character_reference_code = entry.code_point;
  1689. break;
  1690. }
  1691. }
  1692. }
  1693. m_temporary_buffer.clear();
  1694. m_temporary_buffer.append(m_character_reference_code);
  1695. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1696. SWITCH_TO_RETURN_STATE;
  1697. }
  1698. END_STATE
  1699. BEGIN_STATE(RCDATA)
  1700. {
  1701. ON('&')
  1702. {
  1703. m_return_state = State::RCDATA;
  1704. SWITCH_TO(CharacterReference);
  1705. }
  1706. ON('<')
  1707. {
  1708. SWITCH_TO(RCDATALessThanSign);
  1709. }
  1710. ON(0)
  1711. {
  1712. log_parse_error();
  1713. EMIT_CHARACTER(0xFFFD);
  1714. }
  1715. ON_EOF
  1716. {
  1717. EMIT_EOF;
  1718. }
  1719. ANYTHING_ELSE
  1720. {
  1721. EMIT_CURRENT_CHARACTER;
  1722. }
  1723. }
  1724. END_STATE
  1725. BEGIN_STATE(RCDATALessThanSign)
  1726. {
  1727. ON('/')
  1728. {
  1729. m_temporary_buffer.clear();
  1730. SWITCH_TO(RCDATAEndTagOpen);
  1731. }
  1732. ANYTHING_ELSE
  1733. {
  1734. EMIT_CHARACTER_AND_RECONSUME_IN('<', RCDATA);
  1735. }
  1736. }
  1737. END_STATE
  1738. BEGIN_STATE(RCDATAEndTagOpen)
  1739. {
  1740. ON_ASCII_ALPHA
  1741. {
  1742. create_new_token(HTMLToken::Type::EndTag);
  1743. RECONSUME_IN(RCDATAEndTagName);
  1744. }
  1745. ANYTHING_ELSE
  1746. {
  1747. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1748. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1749. RECONSUME_IN(RCDATA);
  1750. }
  1751. }
  1752. END_STATE
  1753. BEGIN_STATE(RCDATAEndTagName)
  1754. {
  1755. ON_WHITESPACE
  1756. {
  1757. m_current_token.set_tag_name(consume_current_builder());
  1758. if (!current_end_tag_token_is_appropriate()) {
  1759. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1760. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1761. for (auto code_point : m_temporary_buffer)
  1762. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  1763. RECONSUME_IN(RCDATA);
  1764. }
  1765. SWITCH_TO(BeforeAttributeName);
  1766. }
  1767. ON('/')
  1768. {
  1769. m_current_token.set_tag_name(consume_current_builder());
  1770. if (!current_end_tag_token_is_appropriate()) {
  1771. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1772. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1773. for (auto code_point : m_temporary_buffer)
  1774. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  1775. RECONSUME_IN(RCDATA);
  1776. }
  1777. SWITCH_TO(SelfClosingStartTag);
  1778. }
  1779. ON('>')
  1780. {
  1781. m_current_token.set_tag_name(consume_current_builder());
  1782. if (!current_end_tag_token_is_appropriate()) {
  1783. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1784. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1785. for (auto code_point : m_temporary_buffer)
  1786. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  1787. RECONSUME_IN(RCDATA);
  1788. }
  1789. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1790. }
  1791. ON_ASCII_UPPER_ALPHA
  1792. {
  1793. m_current_builder.append_code_point(to_ascii_lowercase(current_input_character.value()));
  1794. m_temporary_buffer.append(current_input_character.value());
  1795. continue;
  1796. }
  1797. ON_ASCII_LOWER_ALPHA
  1798. {
  1799. m_current_builder.append_code_point(current_input_character.value());
  1800. m_temporary_buffer.append(current_input_character.value());
  1801. continue;
  1802. }
  1803. ANYTHING_ELSE
  1804. {
  1805. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1806. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1807. // NOTE: The spec doesn't mention this, but it seems that m_current_token (an end tag) is just dropped in this case.
  1808. m_current_builder.clear();
  1809. for (auto code_point : m_temporary_buffer)
  1810. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  1811. RECONSUME_IN(RCDATA);
  1812. }
  1813. }
  1814. END_STATE
  1815. BEGIN_STATE(RAWTEXT)
  1816. {
  1817. ON('<')
  1818. {
  1819. SWITCH_TO(RAWTEXTLessThanSign);
  1820. }
  1821. ON(0)
  1822. {
  1823. log_parse_error();
  1824. EMIT_CHARACTER(0xFFFD);
  1825. }
  1826. ON_EOF
  1827. {
  1828. EMIT_EOF;
  1829. }
  1830. ANYTHING_ELSE
  1831. {
  1832. EMIT_CURRENT_CHARACTER;
  1833. }
  1834. }
  1835. END_STATE
  1836. BEGIN_STATE(RAWTEXTLessThanSign)
  1837. {
  1838. ON('/')
  1839. {
  1840. m_temporary_buffer.clear();
  1841. SWITCH_TO(RAWTEXTEndTagOpen);
  1842. }
  1843. ANYTHING_ELSE
  1844. {
  1845. EMIT_CHARACTER_AND_RECONSUME_IN('<', RAWTEXT);
  1846. }
  1847. }
  1848. END_STATE
  1849. BEGIN_STATE(RAWTEXTEndTagOpen)
  1850. {
  1851. ON_ASCII_ALPHA
  1852. {
  1853. create_new_token(HTMLToken::Type::EndTag);
  1854. RECONSUME_IN(RAWTEXTEndTagName);
  1855. }
  1856. ANYTHING_ELSE
  1857. {
  1858. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1859. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1860. RECONSUME_IN(RAWTEXT);
  1861. }
  1862. }
  1863. END_STATE
  1864. BEGIN_STATE(RAWTEXTEndTagName)
  1865. {
  1866. ON_WHITESPACE
  1867. {
  1868. m_current_token.set_tag_name(consume_current_builder());
  1869. if (!current_end_tag_token_is_appropriate()) {
  1870. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1871. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1872. for (auto code_point : m_temporary_buffer)
  1873. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  1874. RECONSUME_IN(RAWTEXT);
  1875. }
  1876. SWITCH_TO(BeforeAttributeName);
  1877. }
  1878. ON('/')
  1879. {
  1880. m_current_token.set_tag_name(consume_current_builder());
  1881. if (!current_end_tag_token_is_appropriate()) {
  1882. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1883. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1884. for (auto code_point : m_temporary_buffer)
  1885. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  1886. RECONSUME_IN(RAWTEXT);
  1887. }
  1888. SWITCH_TO(SelfClosingStartTag);
  1889. }
  1890. ON('>')
  1891. {
  1892. m_current_token.set_tag_name(consume_current_builder());
  1893. if (!current_end_tag_token_is_appropriate()) {
  1894. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1895. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1896. for (auto code_point : m_temporary_buffer)
  1897. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  1898. RECONSUME_IN(RAWTEXT);
  1899. }
  1900. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1901. }
  1902. ON_ASCII_UPPER_ALPHA
  1903. {
  1904. m_current_builder.append_code_point(to_ascii_lowercase(current_input_character.value()));
  1905. m_temporary_buffer.append(current_input_character.value());
  1906. continue;
  1907. }
  1908. ON_ASCII_LOWER_ALPHA
  1909. {
  1910. m_current_builder.append(current_input_character.value());
  1911. m_temporary_buffer.append(current_input_character.value());
  1912. continue;
  1913. }
  1914. ANYTHING_ELSE
  1915. {
  1916. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1917. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1918. // NOTE: The spec doesn't mention this, but it seems that m_current_token (an end tag) is just dropped in this case.
  1919. m_current_builder.clear();
  1920. for (auto code_point : m_temporary_buffer)
  1921. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  1922. RECONSUME_IN(RAWTEXT);
  1923. }
  1924. }
  1925. END_STATE
  1926. BEGIN_STATE(ScriptData)
  1927. {
  1928. ON('<')
  1929. {
  1930. SWITCH_TO(ScriptDataLessThanSign);
  1931. }
  1932. ON(0)
  1933. {
  1934. log_parse_error();
  1935. EMIT_CHARACTER(0xFFFD);
  1936. }
  1937. ON_EOF
  1938. {
  1939. EMIT_EOF;
  1940. }
  1941. ANYTHING_ELSE
  1942. {
  1943. EMIT_CURRENT_CHARACTER;
  1944. }
  1945. }
  1946. END_STATE
  1947. BEGIN_STATE(PLAINTEXT)
  1948. {
  1949. ON(0)
  1950. {
  1951. log_parse_error();
  1952. EMIT_CHARACTER(0xFFFD);
  1953. }
  1954. ON_EOF
  1955. {
  1956. EMIT_EOF;
  1957. }
  1958. ANYTHING_ELSE
  1959. {
  1960. EMIT_CURRENT_CHARACTER;
  1961. }
  1962. }
  1963. END_STATE
  1964. BEGIN_STATE(ScriptDataLessThanSign)
  1965. {
  1966. ON('/')
  1967. {
  1968. m_temporary_buffer.clear();
  1969. SWITCH_TO(ScriptDataEndTagOpen);
  1970. }
  1971. ON('!')
  1972. {
  1973. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1974. m_queued_tokens.enqueue(HTMLToken::make_character('!'));
  1975. SWITCH_TO(ScriptDataEscapeStart);
  1976. }
  1977. ANYTHING_ELSE
  1978. {
  1979. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptData);
  1980. }
  1981. }
  1982. END_STATE
  1983. BEGIN_STATE(ScriptDataEscapeStart)
  1984. {
  1985. ON('-')
  1986. {
  1987. SWITCH_TO_AND_EMIT_CHARACTER('-', ScriptDataEscapeStartDash);
  1988. }
  1989. ANYTHING_ELSE
  1990. {
  1991. RECONSUME_IN(ScriptData);
  1992. }
  1993. }
  1994. END_STATE
  1995. BEGIN_STATE(ScriptDataEscapeStartDash)
  1996. {
  1997. ON('-')
  1998. {
  1999. SWITCH_TO_AND_EMIT_CHARACTER('-', ScriptDataEscapedDashDash);
  2000. }
  2001. ANYTHING_ELSE
  2002. {
  2003. RECONSUME_IN(ScriptData);
  2004. }
  2005. }
  2006. END_STATE
  2007. BEGIN_STATE(ScriptDataEscapedDashDash)
  2008. {
  2009. ON('-')
  2010. {
  2011. EMIT_CHARACTER('-');
  2012. }
  2013. ON('<')
  2014. {
  2015. SWITCH_TO(ScriptDataEscapedLessThanSign);
  2016. }
  2017. ON('>')
  2018. {
  2019. SWITCH_TO_AND_EMIT_CHARACTER('>', ScriptData);
  2020. }
  2021. ON(0)
  2022. {
  2023. log_parse_error();
  2024. SWITCH_TO_AND_EMIT_CHARACTER(0xFFFD, ScriptDataEscaped);
  2025. }
  2026. ON_EOF
  2027. {
  2028. log_parse_error();
  2029. EMIT_EOF;
  2030. }
  2031. ANYTHING_ELSE
  2032. {
  2033. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataEscaped);
  2034. }
  2035. }
  2036. END_STATE
  2037. BEGIN_STATE(ScriptDataEscapedLessThanSign)
  2038. {
  2039. ON('/')
  2040. {
  2041. m_temporary_buffer.clear();
  2042. SWITCH_TO(ScriptDataEscapedEndTagOpen);
  2043. }
  2044. ON_ASCII_ALPHA
  2045. {
  2046. m_temporary_buffer.clear();
  2047. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptDataDoubleEscapeStart);
  2048. }
  2049. ANYTHING_ELSE
  2050. {
  2051. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptDataEscaped);
  2052. }
  2053. }
  2054. END_STATE
  2055. BEGIN_STATE(ScriptDataEscapedEndTagOpen)
  2056. {
  2057. ON_ASCII_ALPHA
  2058. {
  2059. create_new_token(HTMLToken::Type::EndTag);
  2060. RECONSUME_IN(ScriptDataEscapedEndTagName);
  2061. }
  2062. ANYTHING_ELSE
  2063. {
  2064. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  2065. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  2066. RECONSUME_IN(ScriptDataEscaped);
  2067. }
  2068. }
  2069. END_STATE
  2070. BEGIN_STATE(ScriptDataEscapedEndTagName)
  2071. {
  2072. ON_WHITESPACE
  2073. {
  2074. m_current_token.set_tag_name(consume_current_builder());
  2075. if (current_end_tag_token_is_appropriate())
  2076. SWITCH_TO(BeforeAttributeName);
  2077. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  2078. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  2079. // NOTE: The spec doesn't mention this, but it seems that m_current_token (an end tag) is just dropped in this case.
  2080. m_current_builder.clear();
  2081. for (auto code_point : m_temporary_buffer) {
  2082. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  2083. }
  2084. RECONSUME_IN(ScriptDataEscaped);
  2085. }
  2086. ON('/')
  2087. {
  2088. m_current_token.set_tag_name(consume_current_builder());
  2089. if (current_end_tag_token_is_appropriate())
  2090. SWITCH_TO(SelfClosingStartTag);
  2091. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  2092. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  2093. // NOTE: The spec doesn't mention this, but it seems that m_current_token (an end tag) is just dropped in this case.
  2094. m_current_builder.clear();
  2095. for (auto code_point : m_temporary_buffer) {
  2096. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  2097. }
  2098. RECONSUME_IN(ScriptDataEscaped);
  2099. }
  2100. ON('>')
  2101. {
  2102. m_current_token.set_tag_name(consume_current_builder());
  2103. if (current_end_tag_token_is_appropriate())
  2104. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  2105. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  2106. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  2107. // NOTE: The spec doesn't mention this, but it seems that m_current_token (an end tag) is just dropped in this case.
  2108. m_current_builder.clear();
  2109. for (auto code_point : m_temporary_buffer) {
  2110. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  2111. }
  2112. RECONSUME_IN(ScriptDataEscaped);
  2113. }
  2114. ON_ASCII_UPPER_ALPHA
  2115. {
  2116. m_current_builder.append_code_point(to_ascii_lowercase(current_input_character.value()));
  2117. m_temporary_buffer.append(current_input_character.value());
  2118. continue;
  2119. }
  2120. ON_ASCII_LOWER_ALPHA
  2121. {
  2122. m_current_builder.append(current_input_character.value());
  2123. m_temporary_buffer.append(current_input_character.value());
  2124. continue;
  2125. }
  2126. ANYTHING_ELSE
  2127. {
  2128. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  2129. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  2130. // NOTE: The spec doesn't mention this, but it seems that m_current_token (an end tag) is just dropped in this case.
  2131. m_current_builder.clear();
  2132. for (auto code_point : m_temporary_buffer) {
  2133. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  2134. }
  2135. RECONSUME_IN(ScriptDataEscaped);
  2136. }
  2137. }
  2138. END_STATE
  2139. BEGIN_STATE(ScriptDataDoubleEscapeStart)
  2140. {
  2141. auto temporary_buffer_equal_to_script = [this]() -> bool {
  2142. if (m_temporary_buffer.size() != 6)
  2143. return false;
  2144. // FIXME: Is there a better way of doing this?
  2145. return m_temporary_buffer[0] == 's' && m_temporary_buffer[1] == 'c' && m_temporary_buffer[2] == 'r' && m_temporary_buffer[3] == 'i' && m_temporary_buffer[4] == 'p' && m_temporary_buffer[5] == 't';
  2146. };
  2147. ON_WHITESPACE
  2148. {
  2149. if (temporary_buffer_equal_to_script())
  2150. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataDoubleEscaped);
  2151. else
  2152. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataEscaped);
  2153. }
  2154. ON('/')
  2155. {
  2156. if (temporary_buffer_equal_to_script())
  2157. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataDoubleEscaped);
  2158. else
  2159. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataEscaped);
  2160. }
  2161. ON('>')
  2162. {
  2163. if (temporary_buffer_equal_to_script())
  2164. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataDoubleEscaped);
  2165. else
  2166. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataEscaped);
  2167. }
  2168. ON_ASCII_UPPER_ALPHA
  2169. {
  2170. m_temporary_buffer.append(to_ascii_lowercase(current_input_character.value()));
  2171. EMIT_CURRENT_CHARACTER;
  2172. }
  2173. ON_ASCII_LOWER_ALPHA
  2174. {
  2175. m_temporary_buffer.append(current_input_character.value());
  2176. EMIT_CURRENT_CHARACTER;
  2177. }
  2178. ANYTHING_ELSE
  2179. {
  2180. RECONSUME_IN(ScriptDataEscaped);
  2181. }
  2182. }
  2183. END_STATE
  2184. BEGIN_STATE(ScriptDataDoubleEscaped)
  2185. {
  2186. ON('-')
  2187. {
  2188. SWITCH_TO_AND_EMIT_CHARACTER('-', ScriptDataDoubleEscapedDash);
  2189. }
  2190. ON('<')
  2191. {
  2192. SWITCH_TO_AND_EMIT_CHARACTER('<', ScriptDataDoubleEscapedLessThanSign);
  2193. }
  2194. ON(0)
  2195. {
  2196. log_parse_error();
  2197. EMIT_CHARACTER(0xFFFD);
  2198. }
  2199. ON_EOF
  2200. {
  2201. log_parse_error();
  2202. EMIT_EOF;
  2203. }
  2204. ANYTHING_ELSE
  2205. {
  2206. EMIT_CURRENT_CHARACTER;
  2207. }
  2208. }
  2209. END_STATE
  2210. BEGIN_STATE(ScriptDataDoubleEscapedDash)
  2211. {
  2212. ON('-')
  2213. {
  2214. SWITCH_TO_AND_EMIT_CHARACTER('-', ScriptDataDoubleEscapedDashDash);
  2215. }
  2216. ON('<')
  2217. {
  2218. SWITCH_TO_AND_EMIT_CHARACTER('<', ScriptDataDoubleEscapedLessThanSign);
  2219. }
  2220. ON(0)
  2221. {
  2222. log_parse_error();
  2223. SWITCH_TO_AND_EMIT_CHARACTER(0xFFFD, ScriptDataDoubleEscaped);
  2224. }
  2225. ON_EOF
  2226. {
  2227. log_parse_error();
  2228. EMIT_EOF;
  2229. }
  2230. ANYTHING_ELSE
  2231. {
  2232. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataDoubleEscaped);
  2233. }
  2234. }
  2235. END_STATE
  2236. BEGIN_STATE(ScriptDataDoubleEscapedDashDash)
  2237. {
  2238. ON('-')
  2239. {
  2240. EMIT_CHARACTER('-');
  2241. }
  2242. ON('<')
  2243. {
  2244. SWITCH_TO_AND_EMIT_CHARACTER('<', ScriptDataDoubleEscapedLessThanSign);
  2245. }
  2246. ON('>')
  2247. {
  2248. SWITCH_TO_AND_EMIT_CHARACTER('>', ScriptData);
  2249. }
  2250. ON(0)
  2251. {
  2252. log_parse_error();
  2253. SWITCH_TO_AND_EMIT_CHARACTER(0xFFFD, ScriptDataDoubleEscaped);
  2254. }
  2255. ON_EOF
  2256. {
  2257. log_parse_error();
  2258. EMIT_EOF;
  2259. }
  2260. ANYTHING_ELSE
  2261. {
  2262. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataDoubleEscaped);
  2263. }
  2264. }
  2265. END_STATE
  2266. BEGIN_STATE(ScriptDataDoubleEscapedLessThanSign)
  2267. {
  2268. ON('/')
  2269. {
  2270. m_temporary_buffer.clear();
  2271. SWITCH_TO_AND_EMIT_CHARACTER('/', ScriptDataDoubleEscapeEnd);
  2272. }
  2273. ANYTHING_ELSE
  2274. {
  2275. RECONSUME_IN(ScriptDataDoubleEscaped);
  2276. }
  2277. }
  2278. END_STATE
  2279. BEGIN_STATE(ScriptDataDoubleEscapeEnd)
  2280. {
  2281. auto temporary_buffer_equal_to_script = [this]() -> bool {
  2282. if (m_temporary_buffer.size() != 6)
  2283. return false;
  2284. // FIXME: Is there a better way of doing this?
  2285. return m_temporary_buffer[0] == 's' && m_temporary_buffer[1] == 'c' && m_temporary_buffer[2] == 'r' && m_temporary_buffer[3] == 'i' && m_temporary_buffer[4] == 'p' && m_temporary_buffer[5] == 't';
  2286. };
  2287. ON_WHITESPACE
  2288. {
  2289. if (temporary_buffer_equal_to_script())
  2290. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataEscaped);
  2291. else
  2292. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataDoubleEscaped);
  2293. }
  2294. ON('/')
  2295. {
  2296. if (temporary_buffer_equal_to_script())
  2297. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataEscaped);
  2298. else
  2299. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataDoubleEscaped);
  2300. }
  2301. ON('>')
  2302. {
  2303. if (temporary_buffer_equal_to_script())
  2304. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataEscaped);
  2305. else
  2306. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataDoubleEscaped);
  2307. }
  2308. ON_ASCII_UPPER_ALPHA
  2309. {
  2310. m_temporary_buffer.append(to_ascii_lowercase(current_input_character.value()));
  2311. EMIT_CURRENT_CHARACTER;
  2312. }
  2313. ON_ASCII_LOWER_ALPHA
  2314. {
  2315. m_temporary_buffer.append(current_input_character.value());
  2316. EMIT_CURRENT_CHARACTER;
  2317. }
  2318. ANYTHING_ELSE
  2319. {
  2320. RECONSUME_IN(ScriptDataDoubleEscaped);
  2321. }
  2322. }
  2323. END_STATE
  2324. BEGIN_STATE(ScriptDataEscapedDash)
  2325. {
  2326. ON('-')
  2327. {
  2328. SWITCH_TO_AND_EMIT_CHARACTER('-', ScriptDataEscapedDashDash);
  2329. }
  2330. ON('<')
  2331. {
  2332. SWITCH_TO(ScriptDataEscapedLessThanSign);
  2333. }
  2334. ON(0)
  2335. {
  2336. log_parse_error();
  2337. SWITCH_TO_AND_EMIT_CHARACTER(0xFFFD, ScriptDataEscaped);
  2338. }
  2339. ON_EOF
  2340. {
  2341. log_parse_error();
  2342. EMIT_EOF;
  2343. }
  2344. ANYTHING_ELSE
  2345. {
  2346. SWITCH_TO_AND_EMIT_CURRENT_CHARACTER(ScriptDataEscaped);
  2347. }
  2348. }
  2349. END_STATE
  2350. BEGIN_STATE(ScriptDataEscaped)
  2351. {
  2352. ON('-')
  2353. {
  2354. SWITCH_TO_AND_EMIT_CHARACTER('-', ScriptDataEscapedDash);
  2355. }
  2356. ON('<')
  2357. {
  2358. SWITCH_TO(ScriptDataEscapedLessThanSign);
  2359. }
  2360. ON(0)
  2361. {
  2362. log_parse_error();
  2363. EMIT_CHARACTER(0xFFFD);
  2364. }
  2365. ON_EOF
  2366. {
  2367. log_parse_error();
  2368. EMIT_EOF;
  2369. }
  2370. ANYTHING_ELSE
  2371. {
  2372. EMIT_CURRENT_CHARACTER;
  2373. }
  2374. }
  2375. END_STATE
  2376. BEGIN_STATE(ScriptDataEndTagOpen)
  2377. {
  2378. ON_ASCII_ALPHA
  2379. {
  2380. create_new_token(HTMLToken::Type::EndTag);
  2381. RECONSUME_IN(ScriptDataEndTagName);
  2382. }
  2383. ANYTHING_ELSE
  2384. {
  2385. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  2386. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  2387. RECONSUME_IN(ScriptData);
  2388. }
  2389. }
  2390. END_STATE
  2391. BEGIN_STATE(ScriptDataEndTagName)
  2392. {
  2393. ON_WHITESPACE
  2394. {
  2395. m_current_token.set_tag_name(consume_current_builder());
  2396. if (current_end_tag_token_is_appropriate())
  2397. SWITCH_TO(BeforeAttributeName);
  2398. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  2399. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  2400. // NOTE: The spec doesn't mention this, but it seems that m_current_token (an end tag) is just dropped in this case.
  2401. m_current_builder.clear();
  2402. for (auto code_point : m_temporary_buffer)
  2403. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  2404. RECONSUME_IN(ScriptData);
  2405. }
  2406. ON('/')
  2407. {
  2408. m_current_token.set_tag_name(consume_current_builder());
  2409. if (current_end_tag_token_is_appropriate())
  2410. SWITCH_TO(SelfClosingStartTag);
  2411. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  2412. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  2413. // NOTE: The spec doesn't mention this, but it seems that m_current_token (an end tag) is just dropped in this case.
  2414. m_current_builder.clear();
  2415. for (auto code_point : m_temporary_buffer)
  2416. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  2417. RECONSUME_IN(ScriptData);
  2418. }
  2419. ON('>')
  2420. {
  2421. m_current_token.set_tag_name(consume_current_builder());
  2422. if (current_end_tag_token_is_appropriate())
  2423. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  2424. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  2425. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  2426. // NOTE: The spec doesn't mention this, but it seems that m_current_token (an end tag) is just dropped in this case.
  2427. m_current_builder.clear();
  2428. for (auto code_point : m_temporary_buffer)
  2429. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  2430. RECONSUME_IN(ScriptData);
  2431. }
  2432. ON_ASCII_UPPER_ALPHA
  2433. {
  2434. m_current_builder.append_code_point(to_ascii_lowercase(current_input_character.value()));
  2435. m_temporary_buffer.append(current_input_character.value());
  2436. continue;
  2437. }
  2438. ON_ASCII_LOWER_ALPHA
  2439. {
  2440. m_current_builder.append(current_input_character.value());
  2441. m_temporary_buffer.append(current_input_character.value());
  2442. continue;
  2443. }
  2444. ANYTHING_ELSE
  2445. {
  2446. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  2447. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  2448. // NOTE: The spec doesn't mention this, but it seems that m_current_token (an end tag) is just dropped in this case.
  2449. m_current_builder.clear();
  2450. for (auto code_point : m_temporary_buffer)
  2451. m_queued_tokens.enqueue(HTMLToken::make_character(code_point));
  2452. RECONSUME_IN(ScriptData);
  2453. }
  2454. }
  2455. END_STATE
  2456. BEGIN_STATE(CDATASection)
  2457. {
  2458. ON(']')
  2459. {
  2460. SWITCH_TO(CDATASectionBracket);
  2461. }
  2462. ON_EOF
  2463. {
  2464. log_parse_error();
  2465. EMIT_EOF;
  2466. }
  2467. ANYTHING_ELSE
  2468. {
  2469. EMIT_CURRENT_CHARACTER;
  2470. }
  2471. }
  2472. END_STATE
  2473. BEGIN_STATE(CDATASectionBracket)
  2474. {
  2475. ON(']')
  2476. {
  2477. SWITCH_TO(CDATASectionEnd);
  2478. }
  2479. ANYTHING_ELSE
  2480. {
  2481. EMIT_CHARACTER_AND_RECONSUME_IN(']', CDATASection);
  2482. }
  2483. }
  2484. END_STATE
  2485. BEGIN_STATE(CDATASectionEnd)
  2486. {
  2487. ON(']')
  2488. {
  2489. EMIT_CHARACTER(']');
  2490. }
  2491. ON('>')
  2492. {
  2493. SWITCH_TO(Data);
  2494. }
  2495. ANYTHING_ELSE
  2496. {
  2497. m_queued_tokens.enqueue(HTMLToken::make_character(']'));
  2498. m_queued_tokens.enqueue(HTMLToken::make_character(']'));
  2499. RECONSUME_IN(CDATASection);
  2500. }
  2501. }
  2502. END_STATE
  2503. default:
  2504. TODO();
  2505. }
  2506. }
  2507. }
  2508. bool HTMLTokenizer::consume_next_if_match(StringView const& string, CaseSensitivity case_sensitivity)
  2509. {
  2510. for (size_t i = 0; i < string.length(); ++i) {
  2511. auto code_point = peek_code_point(i);
  2512. if (!code_point.has_value())
  2513. return false;
  2514. // FIXME: This should be more Unicode-aware.
  2515. if (case_sensitivity == CaseSensitivity::CaseInsensitive) {
  2516. if (code_point.value() < 0x80) {
  2517. if (to_ascii_lowercase(code_point.value()) != to_ascii_lowercase(string[i]))
  2518. return false;
  2519. continue;
  2520. }
  2521. }
  2522. if (code_point.value() != (u32)string[i])
  2523. return false;
  2524. }
  2525. skip(string.length());
  2526. return true;
  2527. }
  2528. void HTMLTokenizer::create_new_token(HTMLToken::Type type)
  2529. {
  2530. m_current_token = { type };
  2531. size_t offset = 0;
  2532. switch (type) {
  2533. case HTMLToken::Type::StartTag:
  2534. offset = 1;
  2535. break;
  2536. case HTMLToken::Type::EndTag:
  2537. offset = 2;
  2538. break;
  2539. default:
  2540. break;
  2541. }
  2542. m_current_token.set_start_position({}, nth_last_position(offset));
  2543. }
  2544. HTMLTokenizer::HTMLTokenizer(StringView const& input, String const& encoding)
  2545. {
  2546. auto* decoder = TextCodec::decoder_for(encoding);
  2547. VERIFY(decoder);
  2548. m_decoded_input = decoder->to_utf8(input);
  2549. m_utf8_view = Utf8View(m_decoded_input);
  2550. m_utf8_iterator = m_utf8_view.begin();
  2551. m_source_positions.empend(0u, 0u);
  2552. }
  2553. void HTMLTokenizer::will_switch_to([[maybe_unused]] State new_state)
  2554. {
  2555. dbgln_if(TOKENIZER_TRACE_DEBUG, "[{}] Switch to {}", state_name(m_state), state_name(new_state));
  2556. }
  2557. void HTMLTokenizer::will_reconsume_in([[maybe_unused]] State new_state)
  2558. {
  2559. dbgln_if(TOKENIZER_TRACE_DEBUG, "[{}] Reconsume in {}", state_name(m_state), state_name(new_state));
  2560. }
  2561. void HTMLTokenizer::switch_to(Badge<HTMLParser>, State new_state)
  2562. {
  2563. dbgln_if(TOKENIZER_TRACE_DEBUG, "[{}] Parser switches tokenizer state to {}", state_name(m_state), state_name(new_state));
  2564. m_state = new_state;
  2565. }
  2566. void HTMLTokenizer::will_emit(HTMLToken& token)
  2567. {
  2568. if (token.is_start_tag())
  2569. m_last_emitted_start_tag_name = token.tag_name();
  2570. token.set_end_position({}, nth_last_position(0));
  2571. }
  2572. bool HTMLTokenizer::current_end_tag_token_is_appropriate() const
  2573. {
  2574. VERIFY(m_current_token.is_end_tag());
  2575. if (!m_last_emitted_start_tag_name.has_value())
  2576. return false;
  2577. return m_current_token.tag_name() == m_last_emitted_start_tag_name.value();
  2578. }
  2579. bool HTMLTokenizer::consumed_as_part_of_an_attribute() const
  2580. {
  2581. return m_return_state == State::AttributeValueUnquoted || m_return_state == State::AttributeValueSingleQuoted || m_return_state == State::AttributeValueDoubleQuoted;
  2582. }
  2583. void HTMLTokenizer::restore_to(Utf8CodePointIterator const& new_iterator)
  2584. {
  2585. if (new_iterator != m_prev_utf8_iterator) {
  2586. auto diff = m_prev_utf8_iterator - new_iterator;
  2587. if (diff > 0) {
  2588. for (ssize_t i = 0; i < diff; ++i)
  2589. m_source_positions.take_last();
  2590. } else {
  2591. // Going forwards...?
  2592. TODO();
  2593. }
  2594. }
  2595. m_utf8_iterator = new_iterator;
  2596. }
  2597. String HTMLTokenizer::consume_current_builder()
  2598. {
  2599. auto string = m_current_builder.to_string();
  2600. m_current_builder.clear();
  2601. return string;
  2602. }
  2603. }