HTMLTokenizer.cpp 110 KB

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