HTMLTokenizer.cpp 109 KB

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