HTMLTokenizer.cpp 107 KB

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