HTMLTokenizer.cpp 110 KB

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