HTMLTokenizer.cpp 110 KB

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