HTMLTokenizer.cpp 108 KB

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