HTMLTokenizer.cpp 111 KB

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