HTMLTokenizer.cpp 110 KB

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