HTMLTokenizer.cpp 110 KB

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