HTMLTokenizer.cpp 111 KB

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