HTMLTokenizer.cpp 110 KB

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