HTMLTokenizer.cpp 111 KB

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