HTMLTokenizer.cpp 96 KB

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