HTMLTokenizer.cpp 108 KB

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