HTMLTokenizer.cpp 96 KB

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