HTMLTokenizer.cpp 110 KB

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