HTMLTokenizer.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibTextCodec/Decoder.h>
  27. #include <LibWeb/Parser/Entities.h>
  28. #include <LibWeb/Parser/HTMLToken.h>
  29. #include <LibWeb/Parser/HTMLTokenizer.h>
  30. #include <ctype.h>
  31. #pragma GCC diagnostic ignored "-Wunused-label"
  32. //#define TOKENIZER_TRACE
  33. #define PARSE_ERROR() \
  34. do { \
  35. dbg() << "Parse error (tokenization)" << __PRETTY_FUNCTION__ << " @ " << __LINE__; \
  36. } while (0)
  37. #define CONSUME_NEXT_INPUT_CHARACTER \
  38. current_input_character = next_codepoint();
  39. #define SWITCH_TO(new_state) \
  40. do { \
  41. will_switch_to(State::new_state); \
  42. m_state = State::new_state; \
  43. CONSUME_NEXT_INPUT_CHARACTER; \
  44. goto new_state; \
  45. } while (0)
  46. #define RECONSUME_IN(new_state) \
  47. do { \
  48. will_reconsume_in(State::new_state); \
  49. m_state = State::new_state; \
  50. goto new_state; \
  51. } while (0)
  52. #define SWITCH_TO_RETURN_STATE \
  53. do { \
  54. will_switch_to(m_return_state); \
  55. m_state = m_return_state; \
  56. goto _StartOfFunction; \
  57. } while (0)
  58. #define RECONSUME_IN_RETURN_STATE \
  59. do { \
  60. will_reconsume_in(m_return_state); \
  61. m_state = m_return_state; \
  62. goto _StartOfFunction; \
  63. } while (0)
  64. #define SWITCH_TO_AND_EMIT_CURRENT_TOKEN(new_state) \
  65. do { \
  66. will_switch_to(State::new_state); \
  67. m_state = State::new_state; \
  68. will_emit(m_current_token); \
  69. m_queued_tokens.enqueue(m_current_token); \
  70. return m_queued_tokens.dequeue(); \
  71. } while (0)
  72. #define EMIT_CHARACTER_AND_RECONSUME_IN(codepoint, new_state) \
  73. do { \
  74. m_queued_tokens.enqueue(m_current_token); \
  75. will_reconsume_in(State::new_state); \
  76. m_state = State::new_state; \
  77. goto new_state; \
  78. } while (0)
  79. #define FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE \
  80. do { \
  81. for (auto codepoint : m_temporary_buffer) { \
  82. if (consumed_as_part_of_an_attribute()) { \
  83. m_current_token.m_tag.attributes.last().value_builder.append(codepoint); \
  84. } else { \
  85. create_new_token(HTMLToken::Type::Character); \
  86. m_current_token.m_comment_or_character.data.append(codepoint); \
  87. m_queued_tokens.enqueue(m_current_token); \
  88. } \
  89. } \
  90. } while (0)
  91. #define DONT_CONSUME_NEXT_INPUT_CHARACTER --m_cursor;
  92. #define ON(codepoint) \
  93. if (current_input_character.has_value() && current_input_character.value() == codepoint)
  94. #define ON_EOF \
  95. if (!current_input_character.has_value())
  96. #define ON_ASCII_ALPHA \
  97. if (current_input_character.has_value() && isalpha(current_input_character.value()))
  98. #define ON_ASCII_ALPHANUMERIC \
  99. if (current_input_character.has_value() && isalnum(current_input_character.value()))
  100. #define ON_ASCII_UPPER_ALPHA \
  101. if (current_input_character.has_value() && current_input_character.value() >= 'A' && current_input_character.value() <= 'Z')
  102. #define ON_ASCII_LOWER_ALPHA \
  103. if (current_input_character.has_value() && current_input_character.value() >= 'a' && current_input_character.value() <= 'z')
  104. #define ON_ASCII_DIGIT \
  105. if (current_input_character.has_value() && isxdigit(current_input_character.value()))
  106. #define ON_ASCII_HEX_DIGIT \
  107. if (current_input_character.has_value() && isxdigit(current_input_character.value()))
  108. #define ON_WHITESPACE \
  109. if (current_input_character.has_value() && (current_input_character.value() == '\t' || current_input_character.value() == '\n' || current_input_character.value() == '\f' || current_input_character.value() == ' '))
  110. #define ANYTHING_ELSE if (1)
  111. #define EMIT_EOF \
  112. do { \
  113. if (m_has_emitted_eof) \
  114. return {}; \
  115. m_has_emitted_eof = true; \
  116. create_new_token(HTMLToken::Type::EndOfFile); \
  117. will_emit(m_current_token); \
  118. m_queued_tokens.enqueue(m_current_token); \
  119. return m_queued_tokens.dequeue(); \
  120. } while (0)
  121. #define EMIT_CURRENT_TOKEN \
  122. do { \
  123. will_emit(m_current_token); \
  124. m_queued_tokens.enqueue(m_current_token); \
  125. return m_queued_tokens.dequeue(); \
  126. } while (0)
  127. #define EMIT_CHARACTER(codepoint) \
  128. do { \
  129. create_new_token(HTMLToken::Type::Character); \
  130. m_current_token.m_comment_or_character.data.append(codepoint); \
  131. m_queued_tokens.enqueue(m_current_token); \
  132. return m_queued_tokens.dequeue(); \
  133. } while (0)
  134. #define EMIT_CURRENT_CHARACTER \
  135. EMIT_CHARACTER(current_input_character.value());
  136. #define BEGIN_STATE(state) \
  137. state: \
  138. case State::state: { \
  139. { \
  140. {
  141. #define END_STATE \
  142. ASSERT_NOT_REACHED(); \
  143. break; \
  144. } \
  145. } \
  146. }
  147. static inline bool is_surrogate(u32 codepoint)
  148. {
  149. return (codepoint & 0xfffff800) == 0xd800;
  150. }
  151. static inline bool is_noncharacter(u32 codepoint)
  152. {
  153. return codepoint >= 0xfdd0 && (codepoint <= 0xfdef || (codepoint & 0xfffe) == 0xfffe) && codepoint <= 0x10ffff;
  154. }
  155. static inline bool is_c0_control(u32 codepoint)
  156. {
  157. return codepoint <= 0x1f;
  158. }
  159. static inline bool is_control(u32 codepoint)
  160. {
  161. return is_c0_control(codepoint) || (codepoint >= 0x7f && codepoint <= 0x9f);
  162. }
  163. namespace Web {
  164. Optional<u32> HTMLTokenizer::next_codepoint()
  165. {
  166. if (m_cursor >= m_input.length())
  167. return {};
  168. return m_input[m_cursor++];
  169. }
  170. Optional<u32> HTMLTokenizer::peek_codepoint(size_t offset) const
  171. {
  172. if ((m_cursor + offset) >= m_input.length())
  173. return {};
  174. return m_input[m_cursor + offset];
  175. }
  176. Optional<HTMLToken> HTMLTokenizer::next_token()
  177. {
  178. _StartOfFunction:
  179. if (!m_queued_tokens.is_empty())
  180. return m_queued_tokens.dequeue();
  181. for (;;) {
  182. auto current_input_character = next_codepoint();
  183. switch (m_state) {
  184. BEGIN_STATE(Data)
  185. {
  186. ON('&')
  187. {
  188. m_return_state = State::Data;
  189. SWITCH_TO(CharacterReference);
  190. }
  191. ON('<')
  192. {
  193. SWITCH_TO(TagOpen);
  194. }
  195. ON(0)
  196. {
  197. PARSE_ERROR();
  198. EMIT_CURRENT_CHARACTER;
  199. }
  200. ON_EOF
  201. {
  202. EMIT_EOF;
  203. }
  204. ANYTHING_ELSE
  205. {
  206. EMIT_CURRENT_CHARACTER;
  207. }
  208. }
  209. END_STATE
  210. BEGIN_STATE(TagOpen)
  211. {
  212. ON('!')
  213. {
  214. SWITCH_TO(MarkupDeclarationOpen);
  215. }
  216. ON('/')
  217. {
  218. SWITCH_TO(EndTagOpen);
  219. }
  220. ON_ASCII_ALPHA
  221. {
  222. create_new_token(HTMLToken::Type::StartTag);
  223. RECONSUME_IN(TagName);
  224. }
  225. ON('?')
  226. {
  227. PARSE_ERROR();
  228. create_new_token(HTMLToken::Type::Comment);
  229. RECONSUME_IN(BogusComment);
  230. }
  231. ON_EOF
  232. {
  233. TODO();
  234. }
  235. ANYTHING_ELSE
  236. {
  237. PARSE_ERROR();
  238. EMIT_CHARACTER('<');
  239. RECONSUME_IN(Data);
  240. }
  241. }
  242. END_STATE
  243. BEGIN_STATE(TagName)
  244. {
  245. ON_WHITESPACE
  246. {
  247. SWITCH_TO(BeforeAttributeName);
  248. }
  249. ON('/')
  250. {
  251. SWITCH_TO(SelfClosingStartTag);
  252. }
  253. ON('>')
  254. {
  255. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  256. }
  257. ON_ASCII_UPPER_ALPHA
  258. {
  259. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  260. continue;
  261. }
  262. ON(0)
  263. {
  264. PARSE_ERROR();
  265. m_current_token.m_tag.tag_name.append("\uFFFD");
  266. continue;
  267. }
  268. ON_EOF
  269. {
  270. PARSE_ERROR();
  271. EMIT_EOF;
  272. }
  273. ANYTHING_ELSE
  274. {
  275. m_current_token.m_tag.tag_name.append(current_input_character.value());
  276. continue;
  277. }
  278. }
  279. END_STATE
  280. BEGIN_STATE(EndTagOpen)
  281. {
  282. ON_ASCII_ALPHA
  283. {
  284. create_new_token(HTMLToken::Type::EndTag);
  285. RECONSUME_IN(TagName);
  286. }
  287. ON('>')
  288. {
  289. PARSE_ERROR();
  290. SWITCH_TO(Data);
  291. }
  292. ON_EOF
  293. {
  294. PARSE_ERROR();
  295. // FIXME: Emit a U+003C LESS-THAN SIGN character token, a U+002F SOLIDUS character token and an end-of-file token.
  296. continue;
  297. }
  298. ANYTHING_ELSE
  299. {
  300. PARSE_ERROR();
  301. create_new_token(HTMLToken::Type::Comment);
  302. RECONSUME_IN(BogusComment);
  303. }
  304. }
  305. END_STATE
  306. BEGIN_STATE(MarkupDeclarationOpen)
  307. {
  308. DONT_CONSUME_NEXT_INPUT_CHARACTER;
  309. if (consume_next_if_match("--")) {
  310. create_new_token(HTMLToken::Type::Comment);
  311. SWITCH_TO(CommentStart);
  312. }
  313. if (consume_next_if_match("DOCTYPE", CaseSensitivity::CaseInsensitive)) {
  314. SWITCH_TO(DOCTYPE);
  315. }
  316. }
  317. END_STATE
  318. BEGIN_STATE(BogusComment)
  319. {
  320. ON('>')
  321. {
  322. TODO();
  323. }
  324. ON_EOF
  325. {
  326. TODO();
  327. }
  328. ON(0)
  329. {
  330. TODO();
  331. }
  332. ANYTHING_ELSE
  333. {
  334. TODO();
  335. }
  336. }
  337. END_STATE
  338. BEGIN_STATE(DOCTYPE)
  339. {
  340. ON_WHITESPACE
  341. {
  342. SWITCH_TO(BeforeDOCTYPEName);
  343. }
  344. ON('>')
  345. {
  346. RECONSUME_IN(BeforeDOCTYPEName);
  347. }
  348. ON_EOF
  349. {
  350. TODO();
  351. }
  352. ANYTHING_ELSE
  353. {
  354. TODO();
  355. }
  356. }
  357. END_STATE
  358. BEGIN_STATE(BeforeDOCTYPEName)
  359. {
  360. ON_WHITESPACE
  361. {
  362. continue;
  363. }
  364. ON_ASCII_UPPER_ALPHA
  365. {
  366. create_new_token(HTMLToken::Type::DOCTYPE);
  367. m_current_token.m_doctype.name.append(tolower(current_input_character.value()));
  368. SWITCH_TO(DOCTYPEName);
  369. }
  370. ON(0)
  371. {
  372. TODO();
  373. }
  374. ON('>')
  375. {
  376. TODO();
  377. }
  378. ON_EOF
  379. {
  380. TODO();
  381. }
  382. ANYTHING_ELSE
  383. {
  384. create_new_token(HTMLToken::Type::DOCTYPE);
  385. m_current_token.m_doctype.name.append(current_input_character.value());
  386. SWITCH_TO(DOCTYPEName);
  387. }
  388. }
  389. END_STATE
  390. BEGIN_STATE(DOCTYPEName)
  391. {
  392. ON_WHITESPACE
  393. {
  394. SWITCH_TO(AfterDOCTYPEName);
  395. }
  396. ON('>')
  397. {
  398. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  399. }
  400. ON_ASCII_UPPER_ALPHA
  401. {
  402. m_current_token.m_doctype.name.append(tolower(current_input_character.value()));
  403. }
  404. ON(0)
  405. {
  406. TODO();
  407. }
  408. ON_EOF
  409. {
  410. TODO();
  411. }
  412. ANYTHING_ELSE
  413. {
  414. m_current_token.m_doctype.name.append(current_input_character.value());
  415. continue;
  416. }
  417. }
  418. END_STATE
  419. BEGIN_STATE(AfterDOCTYPEName)
  420. {
  421. ON_WHITESPACE
  422. {
  423. continue;
  424. }
  425. ON('>')
  426. {
  427. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  428. }
  429. ON_EOF
  430. {
  431. TODO();
  432. }
  433. ANYTHING_ELSE
  434. {
  435. if (toupper(current_input_character.value()) == 'P' && consume_next_if_match("UBLIC", CaseSensitivity::CaseInsensitive)) {
  436. SWITCH_TO(AfterDOCTYPEPublicKeyword);
  437. }
  438. if (toupper(current_input_character.value()) == 'S' && consume_next_if_match("YSTEM", CaseSensitivity::CaseInsensitive)) {
  439. SWITCH_TO(AfterDOCTYPESystemKeyword);
  440. }
  441. TODO();
  442. }
  443. }
  444. END_STATE
  445. BEGIN_STATE(AfterDOCTYPEPublicKeyword)
  446. {
  447. ON_WHITESPACE
  448. {
  449. SWITCH_TO(BeforeDOCTYPEPublicIdentifier);
  450. }
  451. ON('"')
  452. {
  453. TODO();
  454. }
  455. ON('\'')
  456. {
  457. TODO();
  458. }
  459. ON('>')
  460. {
  461. TODO();
  462. }
  463. ON_EOF
  464. {
  465. TODO();
  466. }
  467. ANYTHING_ELSE
  468. {
  469. TODO();
  470. }
  471. }
  472. END_STATE
  473. BEGIN_STATE(AfterDOCTYPESystemKeyword)
  474. {
  475. ON_WHITESPACE
  476. {
  477. SWITCH_TO(BeforeDOCTYPESystemIdentifier);
  478. }
  479. ON('"')
  480. {
  481. TODO();
  482. }
  483. ON('\'')
  484. {
  485. TODO();
  486. }
  487. ON('>')
  488. {
  489. TODO();
  490. }
  491. ON_EOF
  492. {
  493. TODO();
  494. }
  495. ANYTHING_ELSE
  496. {
  497. TODO();
  498. }
  499. }
  500. END_STATE
  501. BEGIN_STATE(BeforeDOCTYPEPublicIdentifier)
  502. {
  503. ON_WHITESPACE
  504. {
  505. continue;
  506. }
  507. ON('"')
  508. {
  509. m_current_token.m_doctype.public_identifier.clear();
  510. SWITCH_TO(DOCTYPEPublicIdentifierDoubleQuoted);
  511. }
  512. ON('\'')
  513. {
  514. m_current_token.m_doctype.public_identifier.clear();
  515. SWITCH_TO(DOCTYPEPublicIdentifierSingleQuoted);
  516. }
  517. ON('>')
  518. {
  519. TODO();
  520. }
  521. ON_EOF
  522. {
  523. TODO();
  524. }
  525. ANYTHING_ELSE
  526. {
  527. TODO();
  528. }
  529. }
  530. END_STATE
  531. BEGIN_STATE(BeforeDOCTYPESystemIdentifier)
  532. {
  533. ON_WHITESPACE
  534. {
  535. continue;
  536. }
  537. ON('"')
  538. {
  539. m_current_token.m_doctype.system_identifier.clear();
  540. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  541. }
  542. ON('\'')
  543. {
  544. m_current_token.m_doctype.system_identifier.clear();
  545. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  546. }
  547. ON('>')
  548. {
  549. TODO();
  550. }
  551. ON_EOF
  552. {
  553. TODO();
  554. }
  555. ANYTHING_ELSE
  556. {
  557. TODO();
  558. }
  559. }
  560. END_STATE
  561. BEGIN_STATE(DOCTYPEPublicIdentifierDoubleQuoted)
  562. {
  563. ON('"')
  564. {
  565. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  566. }
  567. ON(0)
  568. {
  569. TODO();
  570. }
  571. ON('>')
  572. {
  573. TODO();
  574. }
  575. ON_EOF
  576. {
  577. TODO();
  578. }
  579. ANYTHING_ELSE
  580. {
  581. m_current_token.m_doctype.public_identifier.append(current_input_character.value());
  582. continue;
  583. }
  584. }
  585. END_STATE
  586. BEGIN_STATE(DOCTYPEPublicIdentifierSingleQuoted)
  587. {
  588. ON('\'')
  589. {
  590. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  591. }
  592. ON(0)
  593. {
  594. TODO();
  595. }
  596. ON('>')
  597. {
  598. TODO();
  599. }
  600. ON_EOF
  601. {
  602. TODO();
  603. }
  604. ANYTHING_ELSE
  605. {
  606. m_current_token.m_doctype.public_identifier.append(current_input_character.value());
  607. continue;
  608. }
  609. }
  610. END_STATE
  611. BEGIN_STATE(DOCTYPESystemIdentifierDoubleQuoted)
  612. {
  613. ON('"')
  614. {
  615. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  616. }
  617. ON(0)
  618. {
  619. TODO();
  620. }
  621. ON('>')
  622. {
  623. TODO();
  624. }
  625. ON_EOF
  626. {
  627. TODO();
  628. }
  629. ANYTHING_ELSE
  630. {
  631. m_current_token.m_doctype.system_identifier.append(current_input_character.value());
  632. continue;
  633. }
  634. }
  635. END_STATE
  636. BEGIN_STATE(DOCTYPESystemIdentifierSingleQuoted)
  637. {
  638. ON('\'')
  639. {
  640. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  641. }
  642. ON(0)
  643. {
  644. TODO();
  645. }
  646. ON('>')
  647. {
  648. TODO();
  649. }
  650. ON_EOF
  651. {
  652. TODO();
  653. }
  654. ANYTHING_ELSE
  655. {
  656. m_current_token.m_doctype.system_identifier.append(current_input_character.value());
  657. continue;
  658. }
  659. }
  660. END_STATE
  661. BEGIN_STATE(AfterDOCTYPEPublicIdentifier)
  662. {
  663. ON_WHITESPACE
  664. {
  665. SWITCH_TO(BetweenDOCTYPEPublicAndSystemIdentifiers);
  666. }
  667. ON('>')
  668. {
  669. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  670. }
  671. ON('"')
  672. {
  673. TODO();
  674. }
  675. ON('\'')
  676. {
  677. TODO();
  678. }
  679. ON_EOF
  680. {
  681. TODO();
  682. }
  683. ANYTHING_ELSE
  684. {
  685. TODO();
  686. }
  687. }
  688. END_STATE
  689. BEGIN_STATE(BetweenDOCTYPEPublicAndSystemIdentifiers)
  690. {
  691. ON_WHITESPACE
  692. {
  693. continue;
  694. }
  695. ON('>')
  696. {
  697. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  698. }
  699. ON('"')
  700. {
  701. m_current_token.m_doctype.system_identifier.clear();
  702. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  703. }
  704. ON('\'')
  705. {
  706. m_current_token.m_doctype.system_identifier.clear();
  707. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  708. }
  709. ON_EOF
  710. {
  711. TODO();
  712. }
  713. ANYTHING_ELSE
  714. {
  715. TODO();
  716. }
  717. }
  718. END_STATE
  719. BEGIN_STATE(AfterDOCTYPESystemIdentifier)
  720. {
  721. ON_WHITESPACE
  722. {
  723. continue;
  724. }
  725. ON('>')
  726. {
  727. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  728. }
  729. ON_EOF
  730. {
  731. TODO();
  732. }
  733. ANYTHING_ELSE
  734. {
  735. TODO();
  736. }
  737. }
  738. END_STATE
  739. BEGIN_STATE(BeforeAttributeName)
  740. {
  741. ON_WHITESPACE
  742. {
  743. continue;
  744. }
  745. ON('/')
  746. {
  747. RECONSUME_IN(AfterAttributeName);
  748. }
  749. ON('>')
  750. {
  751. RECONSUME_IN(AfterAttributeName);
  752. }
  753. ON_EOF
  754. {
  755. RECONSUME_IN(AfterAttributeName);
  756. }
  757. ON('=')
  758. {
  759. TODO();
  760. }
  761. ANYTHING_ELSE
  762. {
  763. m_current_token.m_tag.attributes.append(HTMLToken::AttributeBuilder());
  764. RECONSUME_IN(AttributeName);
  765. }
  766. }
  767. END_STATE
  768. BEGIN_STATE(SelfClosingStartTag)
  769. {
  770. ON('>')
  771. {
  772. m_current_token.m_tag.self_closing = true;
  773. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  774. }
  775. ON_EOF
  776. {
  777. TODO();
  778. }
  779. ANYTHING_ELSE
  780. {
  781. TODO();
  782. }
  783. }
  784. END_STATE
  785. BEGIN_STATE(AttributeName)
  786. {
  787. ON_WHITESPACE
  788. {
  789. RECONSUME_IN(AfterAttributeName);
  790. }
  791. ON('/')
  792. {
  793. RECONSUME_IN(AfterAttributeName);
  794. }
  795. ON('>')
  796. {
  797. RECONSUME_IN(AfterAttributeName);
  798. }
  799. ON_EOF
  800. {
  801. RECONSUME_IN(AfterAttributeName);
  802. }
  803. ON('=')
  804. {
  805. SWITCH_TO(BeforeAttributeValue);
  806. }
  807. ANYTHING_ELSE
  808. {
  809. m_current_token.m_tag.attributes.last().name_builder.append(current_input_character.value());
  810. continue;
  811. }
  812. }
  813. END_STATE
  814. BEGIN_STATE(AfterAttributeName)
  815. {
  816. ON_WHITESPACE
  817. {
  818. continue;
  819. }
  820. ON('/')
  821. {
  822. SWITCH_TO(SelfClosingStartTag);
  823. }
  824. ON('=')
  825. {
  826. SWITCH_TO(BeforeAttributeValue);
  827. }
  828. ON('>')
  829. {
  830. SWITCH_TO(Data);
  831. }
  832. ON_EOF
  833. {
  834. TODO();
  835. }
  836. ANYTHING_ELSE
  837. {
  838. m_current_token.m_tag.attributes.append(HTMLToken::AttributeBuilder());
  839. RECONSUME_IN(AttributeName);
  840. }
  841. }
  842. END_STATE
  843. BEGIN_STATE(BeforeAttributeValue)
  844. {
  845. ON_WHITESPACE
  846. {
  847. continue;
  848. }
  849. ON('"')
  850. {
  851. SWITCH_TO(AttributeValueDoubleQuoted);
  852. }
  853. ON('\'')
  854. {
  855. SWITCH_TO(AttributeValueSingleQuoted);
  856. }
  857. ON('>')
  858. {
  859. TODO();
  860. }
  861. ANYTHING_ELSE
  862. {
  863. RECONSUME_IN(AttributeValueUnquoted);
  864. }
  865. }
  866. END_STATE
  867. BEGIN_STATE(AttributeValueDoubleQuoted)
  868. {
  869. ON('"')
  870. {
  871. SWITCH_TO(AfterAttributeValueQuoted);
  872. }
  873. ON('&')
  874. {
  875. m_return_state = State::AttributeValueDoubleQuoted;
  876. SWITCH_TO(CharacterReference);
  877. }
  878. ON(0)
  879. {
  880. TODO();
  881. }
  882. ON_EOF
  883. {
  884. TODO();
  885. }
  886. ANYTHING_ELSE
  887. {
  888. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  889. continue;
  890. }
  891. }
  892. END_STATE
  893. BEGIN_STATE(AttributeValueSingleQuoted)
  894. {
  895. ON('\'')
  896. {
  897. SWITCH_TO(AfterAttributeValueQuoted);
  898. }
  899. ON('&')
  900. {
  901. m_return_state = State::AttributeValueSingleQuoted;
  902. SWITCH_TO(CharacterReference);
  903. }
  904. ON(0)
  905. {
  906. TODO();
  907. }
  908. ON_EOF
  909. {
  910. TODO();
  911. }
  912. ANYTHING_ELSE
  913. {
  914. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  915. continue;
  916. }
  917. }
  918. END_STATE
  919. BEGIN_STATE(AttributeValueUnquoted)
  920. {
  921. ON_WHITESPACE
  922. {
  923. SWITCH_TO(BeforeAttributeName);
  924. }
  925. ON('&')
  926. {
  927. m_return_state = State::AttributeValueUnquoted;
  928. SWITCH_TO(CharacterReference);
  929. }
  930. ON('>')
  931. {
  932. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  933. }
  934. ON(0)
  935. {
  936. TODO();
  937. }
  938. ON_EOF
  939. {
  940. TODO();
  941. }
  942. ANYTHING_ELSE
  943. {
  944. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  945. continue;
  946. }
  947. }
  948. END_STATE
  949. BEGIN_STATE(AfterAttributeValueQuoted)
  950. {
  951. ON_WHITESPACE
  952. {
  953. SWITCH_TO(BeforeAttributeName);
  954. }
  955. ON('/')
  956. {
  957. SWITCH_TO(SelfClosingStartTag);
  958. }
  959. ON('>')
  960. {
  961. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  962. }
  963. ON_EOF
  964. {
  965. PARSE_ERROR();
  966. EMIT_EOF;
  967. }
  968. ANYTHING_ELSE
  969. {
  970. PARSE_ERROR();
  971. RECONSUME_IN(BeforeAttributeName);
  972. }
  973. }
  974. END_STATE
  975. BEGIN_STATE(CommentStart)
  976. {
  977. ON('-')
  978. {
  979. SWITCH_TO(CommentStartDash);
  980. }
  981. ON('>')
  982. {
  983. TODO();
  984. }
  985. ANYTHING_ELSE
  986. {
  987. RECONSUME_IN(Comment);
  988. }
  989. }
  990. END_STATE
  991. BEGIN_STATE(CommentStartDash)
  992. {
  993. ON('-')
  994. {
  995. SWITCH_TO(CommentEnd);
  996. }
  997. ON('>')
  998. {
  999. TODO();
  1000. }
  1001. ON_EOF
  1002. {
  1003. TODO();
  1004. }
  1005. ANYTHING_ELSE
  1006. {
  1007. m_current_token.m_comment_or_character.data.append('-');
  1008. RECONSUME_IN(Comment);
  1009. }
  1010. }
  1011. END_STATE
  1012. BEGIN_STATE(Comment)
  1013. {
  1014. ON('<')
  1015. {
  1016. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  1017. SWITCH_TO(CommentLessThanSign);
  1018. }
  1019. ON('-')
  1020. {
  1021. SWITCH_TO(CommentEndDash);
  1022. }
  1023. ON(0)
  1024. {
  1025. TODO();
  1026. }
  1027. ON_EOF
  1028. {
  1029. TODO();
  1030. }
  1031. ANYTHING_ELSE
  1032. {
  1033. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  1034. continue;
  1035. }
  1036. }
  1037. END_STATE
  1038. BEGIN_STATE(CommentEnd)
  1039. {
  1040. ON('>')
  1041. {
  1042. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1043. }
  1044. ON('!')
  1045. {
  1046. SWITCH_TO(CommentEndBang);
  1047. }
  1048. ON('-')
  1049. {
  1050. m_current_token.m_comment_or_character.data.append('-');
  1051. continue;
  1052. }
  1053. ON_EOF
  1054. {
  1055. TODO();
  1056. }
  1057. ANYTHING_ELSE
  1058. {
  1059. m_current_token.m_comment_or_character.data.append('-');
  1060. RECONSUME_IN(Comment);
  1061. }
  1062. }
  1063. END_STATE
  1064. BEGIN_STATE(CommentEndBang)
  1065. {
  1066. ON('-')
  1067. {
  1068. m_current_token.m_comment_or_character.data.append("--!");
  1069. SWITCH_TO(CommentEndDash);
  1070. }
  1071. ON('>')
  1072. {
  1073. TODO();
  1074. }
  1075. ON_EOF
  1076. {
  1077. TODO();
  1078. }
  1079. ANYTHING_ELSE
  1080. {
  1081. m_current_token.m_comment_or_character.data.append("--!");
  1082. RECONSUME_IN(Comment);
  1083. }
  1084. }
  1085. END_STATE
  1086. BEGIN_STATE(CommentEndDash)
  1087. {
  1088. ON('-')
  1089. {
  1090. SWITCH_TO(CommentEnd);
  1091. }
  1092. ON_EOF
  1093. {
  1094. TODO();
  1095. }
  1096. ANYTHING_ELSE
  1097. {
  1098. m_current_token.m_comment_or_character.data.append('-');
  1099. RECONSUME_IN(Comment);
  1100. }
  1101. }
  1102. END_STATE
  1103. BEGIN_STATE(CommentLessThanSign)
  1104. {
  1105. ON('!')
  1106. {
  1107. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  1108. SWITCH_TO(CommentLessThanSignBang);
  1109. }
  1110. ON('<')
  1111. {
  1112. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  1113. continue;
  1114. }
  1115. ANYTHING_ELSE
  1116. {
  1117. RECONSUME_IN(Comment);
  1118. }
  1119. }
  1120. END_STATE
  1121. BEGIN_STATE(CommentLessThanSignBang)
  1122. {
  1123. ON('-')
  1124. {
  1125. SWITCH_TO(CommentLessThanSignBangDash);
  1126. }
  1127. ANYTHING_ELSE
  1128. {
  1129. RECONSUME_IN(Comment);
  1130. }
  1131. }
  1132. END_STATE
  1133. BEGIN_STATE(CommentLessThanSignBangDash)
  1134. {
  1135. ON('-')
  1136. {
  1137. SWITCH_TO(CommentLessThanSignBangDashDash);
  1138. }
  1139. ANYTHING_ELSE
  1140. {
  1141. RECONSUME_IN(Comment);
  1142. }
  1143. }
  1144. END_STATE
  1145. BEGIN_STATE(CommentLessThanSignBangDashDash)
  1146. {
  1147. ON('>')
  1148. {
  1149. SWITCH_TO(CommentEnd);
  1150. }
  1151. ANYTHING_ELSE
  1152. {
  1153. TODO();
  1154. }
  1155. }
  1156. END_STATE
  1157. BEGIN_STATE(CharacterReference)
  1158. {
  1159. m_temporary_buffer.clear();
  1160. m_temporary_buffer.append('&');
  1161. ON_ASCII_ALPHANUMERIC
  1162. {
  1163. RECONSUME_IN(NamedCharacterReference);
  1164. }
  1165. ON('#')
  1166. {
  1167. m_temporary_buffer.append(current_input_character.value());
  1168. SWITCH_TO(NumericCharacterReference);
  1169. }
  1170. ANYTHING_ELSE
  1171. {
  1172. RECONSUME_IN_RETURN_STATE;
  1173. }
  1174. }
  1175. END_STATE
  1176. BEGIN_STATE(NamedCharacterReference)
  1177. {
  1178. auto match = HTML::codepoints_from_entity(m_input.substring_view(m_cursor - 1, m_input.length() - m_cursor + 1));
  1179. if (match.has_value()) {
  1180. m_cursor += match.value().entity.length();
  1181. for (auto ch : match.value().entity)
  1182. m_temporary_buffer.append(ch);
  1183. if (consumed_as_part_of_an_attribute() && match.value().entity.ends_with(';')) {
  1184. auto next_codepoint = peek_codepoint(0);
  1185. if (next_codepoint.has_value() && next_codepoint.value() == '=') {
  1186. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1187. SWITCH_TO_RETURN_STATE;
  1188. }
  1189. }
  1190. if (!match.value().entity.ends_with(';')) {
  1191. PARSE_ERROR();
  1192. }
  1193. m_temporary_buffer.clear();
  1194. m_temporary_buffer.append(match.value().codepoints);
  1195. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1196. SWITCH_TO_RETURN_STATE;
  1197. } else {
  1198. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1199. SWITCH_TO(AmbiguousAmpersand);
  1200. }
  1201. }
  1202. END_STATE
  1203. BEGIN_STATE(AmbiguousAmpersand)
  1204. {
  1205. ON_ASCII_ALPHANUMERIC
  1206. {
  1207. if (consumed_as_part_of_an_attribute()) {
  1208. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  1209. continue;
  1210. } else {
  1211. EMIT_CURRENT_CHARACTER;
  1212. }
  1213. }
  1214. ON(';')
  1215. {
  1216. TODO();
  1217. }
  1218. ANYTHING_ELSE
  1219. {
  1220. RECONSUME_IN_RETURN_STATE;
  1221. }
  1222. }
  1223. END_STATE
  1224. BEGIN_STATE(NumericCharacterReference)
  1225. {
  1226. m_character_reference_code = 0;
  1227. ON('X')
  1228. {
  1229. m_temporary_buffer.append(current_input_character.value());
  1230. SWITCH_TO(HexadecimalCharacterReferenceStart);
  1231. }
  1232. ON('x')
  1233. {
  1234. m_temporary_buffer.append(current_input_character.value());
  1235. SWITCH_TO(HexadecimalCharacterReferenceStart);
  1236. }
  1237. ANYTHING_ELSE
  1238. {
  1239. RECONSUME_IN(DecimalCharacterReferenceStart);
  1240. }
  1241. }
  1242. END_STATE
  1243. BEGIN_STATE(HexadecimalCharacterReferenceStart)
  1244. {
  1245. ON_ASCII_HEX_DIGIT
  1246. {
  1247. RECONSUME_IN(HexadecimalCharacterReference);
  1248. }
  1249. ANYTHING_ELSE
  1250. {
  1251. TODO();
  1252. }
  1253. }
  1254. END_STATE
  1255. BEGIN_STATE(DecimalCharacterReferenceStart)
  1256. {
  1257. ON_ASCII_DIGIT
  1258. {
  1259. RECONSUME_IN(DecimalCharacterReference);
  1260. }
  1261. ANYTHING_ELSE
  1262. {
  1263. TODO();
  1264. }
  1265. }
  1266. END_STATE
  1267. BEGIN_STATE(HexadecimalCharacterReference)
  1268. {
  1269. ON_ASCII_DIGIT
  1270. {
  1271. m_character_reference_code *= 16;
  1272. m_character_reference_code += current_input_character.value() - 0x30;
  1273. continue;
  1274. }
  1275. ON_ASCII_UPPER_ALPHA
  1276. {
  1277. m_character_reference_code *= 16;
  1278. m_character_reference_code += current_input_character.value() - 0x37;
  1279. continue;
  1280. }
  1281. ON_ASCII_LOWER_ALPHA
  1282. {
  1283. m_character_reference_code *= 16;
  1284. m_character_reference_code += current_input_character.value() - 0x57;
  1285. continue;
  1286. }
  1287. ON(';')
  1288. {
  1289. SWITCH_TO(NumericCharacterReferenceEnd);
  1290. }
  1291. ANYTHING_ELSE
  1292. {
  1293. TODO();
  1294. }
  1295. }
  1296. END_STATE
  1297. BEGIN_STATE(DecimalCharacterReference)
  1298. {
  1299. ON_ASCII_DIGIT
  1300. {
  1301. m_character_reference_code *= 10;
  1302. m_character_reference_code += current_input_character.value() - 0x30;
  1303. continue;
  1304. }
  1305. ON(';')
  1306. {
  1307. SWITCH_TO(NumericCharacterReferenceEnd);
  1308. }
  1309. ANYTHING_ELSE
  1310. {
  1311. TODO();
  1312. }
  1313. }
  1314. END_STATE
  1315. BEGIN_STATE(NumericCharacterReferenceEnd)
  1316. {
  1317. if (m_character_reference_code == 0) {
  1318. TODO();
  1319. }
  1320. if (m_character_reference_code > 0x10ffff) {
  1321. TODO();
  1322. }
  1323. if (is_surrogate(m_character_reference_code)) {
  1324. TODO();
  1325. }
  1326. if (is_noncharacter(m_character_reference_code)) {
  1327. TODO();
  1328. }
  1329. if (m_character_reference_code == 0xd || (is_control(m_character_reference_code) && !isspace(m_character_reference_code))) {
  1330. TODO();
  1331. }
  1332. if (is_control(m_character_reference_code)) {
  1333. constexpr struct {
  1334. u32 number;
  1335. u32 codepoint;
  1336. } conversion_table[] = {
  1337. { 0x80, 0x20AC },
  1338. { 0x82, 0x201A },
  1339. { 0x83, 0x0192 },
  1340. { 0x84, 0x201E },
  1341. { 0x85, 0x2026 },
  1342. { 0x86, 0x2020 },
  1343. { 0x87, 0x2021 },
  1344. { 0x88, 0x02C6 },
  1345. { 0x89, 0x2030 },
  1346. { 0x8A, 0x0160 },
  1347. { 0x8B, 0x2039 },
  1348. { 0x8C, 0x0152 },
  1349. { 0x8E, 0x017D },
  1350. { 0x91, 0x2018 },
  1351. { 0x92, 0x2019 },
  1352. { 0x93, 0x201C },
  1353. { 0x94, 0x201D },
  1354. { 0x95, 0x2022 },
  1355. { 0x96, 0x2013 },
  1356. { 0x97, 0x2014 },
  1357. { 0x98, 0x02DC },
  1358. { 0x99, 0x2122 },
  1359. { 0x9A, 0x0161 },
  1360. { 0x9B, 0x203A },
  1361. { 0x9C, 0x0153 },
  1362. { 0x9E, 0x017E },
  1363. { 0x9F, 0x0178 },
  1364. };
  1365. for (auto& entry : conversion_table) {
  1366. if (m_character_reference_code == entry.number) {
  1367. m_character_reference_code = entry.codepoint;
  1368. break;
  1369. }
  1370. }
  1371. }
  1372. m_temporary_buffer.clear();
  1373. m_temporary_buffer.append(m_character_reference_code);
  1374. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1375. SWITCH_TO_RETURN_STATE;
  1376. }
  1377. END_STATE
  1378. BEGIN_STATE(RCDATA)
  1379. {
  1380. ON('&')
  1381. {
  1382. m_return_state = State::RCDATA;
  1383. SWITCH_TO(CharacterReference);
  1384. }
  1385. ON('<')
  1386. {
  1387. SWITCH_TO(RCDATALessThanSign);
  1388. }
  1389. ON(0)
  1390. {
  1391. PARSE_ERROR();
  1392. EMIT_CHARACTER("\uFFFD");
  1393. }
  1394. ON_EOF
  1395. {
  1396. EMIT_EOF;
  1397. }
  1398. ANYTHING_ELSE
  1399. {
  1400. EMIT_CURRENT_CHARACTER;
  1401. }
  1402. }
  1403. END_STATE
  1404. BEGIN_STATE(RCDATALessThanSign)
  1405. {
  1406. ON('/')
  1407. {
  1408. m_temporary_buffer.clear();
  1409. SWITCH_TO(RCDATAEndTagOpen);
  1410. }
  1411. ANYTHING_ELSE
  1412. {
  1413. EMIT_CHARACTER('<');
  1414. RECONSUME_IN(RCDATA);
  1415. }
  1416. }
  1417. END_STATE
  1418. BEGIN_STATE(RCDATAEndTagOpen)
  1419. {
  1420. ON_ASCII_ALPHA
  1421. {
  1422. create_new_token(HTMLToken::Type::EndTag);
  1423. RECONSUME_IN(RCDATAEndTagName);
  1424. }
  1425. ANYTHING_ELSE
  1426. {
  1427. // FIXME: Emit a U+003C LESS-THAN SIGN character token and a U+002F SOLIDUS character token. Reconsume in the RCDATA state.
  1428. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1429. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1430. RECONSUME_IN(RCDATA);
  1431. }
  1432. }
  1433. END_STATE
  1434. BEGIN_STATE(RCDATAEndTagName)
  1435. {
  1436. ON_WHITESPACE
  1437. {
  1438. if (!current_end_tag_token_is_appropriate()) {
  1439. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1440. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1441. for (auto codepoint : m_temporary_buffer)
  1442. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1443. RECONSUME_IN(RCDATA);
  1444. }
  1445. SWITCH_TO(BeforeAttributeName);
  1446. }
  1447. ON('/')
  1448. {
  1449. if (!current_end_tag_token_is_appropriate()) {
  1450. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1451. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1452. for (auto codepoint : m_temporary_buffer)
  1453. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1454. RECONSUME_IN(RCDATA);
  1455. }
  1456. SWITCH_TO(SelfClosingStartTag);
  1457. }
  1458. ON('>')
  1459. {
  1460. if (!current_end_tag_token_is_appropriate()) {
  1461. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1462. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1463. for (auto codepoint : m_temporary_buffer)
  1464. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1465. RECONSUME_IN(RCDATA);
  1466. }
  1467. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1468. }
  1469. ON_ASCII_UPPER_ALPHA
  1470. {
  1471. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1472. m_temporary_buffer.append(current_input_character.value());
  1473. continue;
  1474. }
  1475. ON_ASCII_LOWER_ALPHA
  1476. {
  1477. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1478. m_temporary_buffer.append(current_input_character.value());
  1479. continue;
  1480. }
  1481. ANYTHING_ELSE
  1482. {
  1483. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1484. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1485. for (auto codepoint : m_temporary_buffer)
  1486. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1487. RECONSUME_IN(RCDATA);
  1488. }
  1489. }
  1490. END_STATE
  1491. BEGIN_STATE(RAWTEXT)
  1492. {
  1493. ON('<')
  1494. {
  1495. SWITCH_TO(RAWTEXTLessThanSign);
  1496. }
  1497. ON(0)
  1498. {
  1499. PARSE_ERROR();
  1500. EMIT_CHARACTER("\uFFFD");
  1501. }
  1502. ON_EOF
  1503. {
  1504. EMIT_EOF;
  1505. }
  1506. ANYTHING_ELSE
  1507. {
  1508. EMIT_CURRENT_CHARACTER;
  1509. }
  1510. }
  1511. END_STATE
  1512. BEGIN_STATE(RAWTEXTLessThanSign)
  1513. {
  1514. ON('/')
  1515. {
  1516. m_temporary_buffer.clear();
  1517. SWITCH_TO(RAWTEXTEndTagOpen);
  1518. }
  1519. ANYTHING_ELSE
  1520. {
  1521. EMIT_CHARACTER('<');
  1522. RECONSUME_IN(RAWTEXT);
  1523. }
  1524. }
  1525. END_STATE
  1526. BEGIN_STATE(RAWTEXTEndTagOpen)
  1527. {
  1528. ON_ASCII_ALPHA
  1529. {
  1530. create_new_token(HTMLToken::Type::EndTag);
  1531. RECONSUME_IN(RAWTEXTEndTagName);
  1532. }
  1533. ANYTHING_ELSE
  1534. {
  1535. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1536. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1537. RECONSUME_IN(RAWTEXT);
  1538. }
  1539. }
  1540. END_STATE
  1541. BEGIN_STATE(RAWTEXTEndTagName)
  1542. {
  1543. ON_WHITESPACE
  1544. {
  1545. if (!current_end_tag_token_is_appropriate()) {
  1546. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1547. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1548. for (auto codepoint : m_temporary_buffer)
  1549. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1550. RECONSUME_IN(RAWTEXT);
  1551. }
  1552. SWITCH_TO(BeforeAttributeName);
  1553. }
  1554. ON('/')
  1555. {
  1556. if (!current_end_tag_token_is_appropriate()) {
  1557. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1558. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1559. for (auto codepoint : m_temporary_buffer)
  1560. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1561. RECONSUME_IN(RAWTEXT);
  1562. }
  1563. SWITCH_TO(SelfClosingStartTag);
  1564. }
  1565. ON('>')
  1566. {
  1567. if (!current_end_tag_token_is_appropriate()) {
  1568. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1569. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1570. for (auto codepoint : m_temporary_buffer)
  1571. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1572. RECONSUME_IN(RAWTEXT);
  1573. }
  1574. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1575. }
  1576. ON_ASCII_UPPER_ALPHA
  1577. {
  1578. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1579. m_temporary_buffer.append(current_input_character.value());
  1580. continue;
  1581. }
  1582. ON_ASCII_LOWER_ALPHA
  1583. {
  1584. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1585. m_temporary_buffer.append(current_input_character.value());
  1586. continue;
  1587. }
  1588. ANYTHING_ELSE
  1589. {
  1590. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1591. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1592. for (auto codepoint : m_temporary_buffer)
  1593. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1594. RECONSUME_IN(RAWTEXT);
  1595. }
  1596. }
  1597. END_STATE
  1598. BEGIN_STATE(ScriptData)
  1599. {
  1600. ON('<')
  1601. {
  1602. SWITCH_TO(ScriptDataLessThanSign);
  1603. }
  1604. ON(0)
  1605. {
  1606. PARSE_ERROR();
  1607. EMIT_CHARACTER("\uFFFD");
  1608. }
  1609. ON_EOF
  1610. {
  1611. EMIT_EOF;
  1612. }
  1613. ANYTHING_ELSE
  1614. {
  1615. EMIT_CURRENT_CHARACTER;
  1616. }
  1617. }
  1618. END_STATE
  1619. BEGIN_STATE(PLAINTEXT)
  1620. {
  1621. ON(0)
  1622. {
  1623. PARSE_ERROR();
  1624. EMIT_CHARACTER("\uFFFD");
  1625. }
  1626. ON_EOF
  1627. {
  1628. EMIT_EOF;
  1629. }
  1630. ANYTHING_ELSE
  1631. {
  1632. EMIT_CURRENT_CHARACTER;
  1633. }
  1634. }
  1635. END_STATE
  1636. BEGIN_STATE(ScriptDataLessThanSign)
  1637. {
  1638. ON('/')
  1639. {
  1640. m_temporary_buffer.clear();
  1641. SWITCH_TO(ScriptDataEndTagOpen);
  1642. }
  1643. ON('!')
  1644. {
  1645. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1646. m_queued_tokens.enqueue(HTMLToken::make_character('!'));
  1647. SWITCH_TO(ScriptDataEscapeStart);
  1648. }
  1649. ANYTHING_ELSE
  1650. {
  1651. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptData);
  1652. }
  1653. }
  1654. END_STATE
  1655. BEGIN_STATE(ScriptDataEscapeStart)
  1656. {
  1657. ON('-')
  1658. {
  1659. m_queued_tokens.enqueue(HTMLToken::make_character('-'));
  1660. SWITCH_TO(ScriptDataEscapeStartDash);
  1661. }
  1662. ANYTHING_ELSE
  1663. {
  1664. RECONSUME_IN(ScriptData);
  1665. }
  1666. }
  1667. END_STATE
  1668. BEGIN_STATE(ScriptDataEscapeStartDash)
  1669. {
  1670. ON('-')
  1671. {
  1672. m_queued_tokens.enqueue(HTMLToken::make_character('-'));
  1673. SWITCH_TO(ScriptDataEscapedDashDash);
  1674. }
  1675. ANYTHING_ELSE
  1676. {
  1677. RECONSUME_IN(ScriptData);
  1678. }
  1679. }
  1680. END_STATE
  1681. BEGIN_STATE(ScriptDataEscapedDashDash)
  1682. {
  1683. ON('-')
  1684. {
  1685. EMIT_CHARACTER('-');
  1686. }
  1687. ON('<')
  1688. {
  1689. SWITCH_TO(ScriptDataEscapedLessThanSign);
  1690. }
  1691. ON('>')
  1692. {
  1693. m_queued_tokens.enqueue(HTMLToken::make_character('>'));
  1694. SWITCH_TO(ScriptData);
  1695. }
  1696. ON(0)
  1697. {
  1698. TODO();
  1699. }
  1700. ON_EOF
  1701. {
  1702. TODO();
  1703. }
  1704. ANYTHING_ELSE
  1705. {
  1706. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscaped);
  1707. }
  1708. }
  1709. END_STATE
  1710. BEGIN_STATE(ScriptDataEscapedLessThanSign)
  1711. {
  1712. ON('/')
  1713. {
  1714. m_temporary_buffer.clear();
  1715. SWITCH_TO(ScriptDataEscapedEndTagOpen);
  1716. }
  1717. ON_ASCII_ALPHA
  1718. {
  1719. m_temporary_buffer.clear();
  1720. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptDataDoubleEscapeStart);
  1721. }
  1722. ANYTHING_ELSE
  1723. {
  1724. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptDataEscaped);
  1725. }
  1726. }
  1727. END_STATE
  1728. BEGIN_STATE(ScriptDataEscapedEndTagOpen)
  1729. {
  1730. ON_ASCII_ALPHA
  1731. {
  1732. create_new_token(HTMLToken::Type::EndTag);
  1733. RECONSUME_IN(ScriptDataEscapedEndTagName);
  1734. }
  1735. ANYTHING_ELSE
  1736. {
  1737. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1738. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1739. RECONSUME_IN(ScriptDataEscaped);
  1740. }
  1741. }
  1742. END_STATE
  1743. BEGIN_STATE(ScriptDataEscapedEndTagName)
  1744. {
  1745. ON_WHITESPACE
  1746. {
  1747. if (current_end_tag_token_is_appropriate()) {
  1748. SWITCH_TO(BeforeAttributeName);
  1749. } else {
  1750. TODO();
  1751. }
  1752. }
  1753. ON('/')
  1754. {
  1755. if (current_end_tag_token_is_appropriate()) {
  1756. SWITCH_TO(SelfClosingStartTag);
  1757. } else {
  1758. TODO();
  1759. }
  1760. }
  1761. ON('>')
  1762. {
  1763. if (current_end_tag_token_is_appropriate()) {
  1764. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1765. } else {
  1766. TODO();
  1767. }
  1768. }
  1769. ON_ASCII_UPPER_ALPHA
  1770. {
  1771. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1772. m_temporary_buffer.append(current_input_character.value());
  1773. continue;
  1774. }
  1775. ON_ASCII_LOWER_ALPHA
  1776. {
  1777. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1778. m_temporary_buffer.append(current_input_character.value());
  1779. continue;
  1780. }
  1781. ANYTHING_ELSE
  1782. {
  1783. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1784. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1785. for (auto codepoint : m_temporary_buffer) {
  1786. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1787. }
  1788. RECONSUME_IN(ScriptDataEscaped);
  1789. }
  1790. }
  1791. END_STATE
  1792. BEGIN_STATE(ScriptDataDoubleEscapeStart)
  1793. {
  1794. TODO();
  1795. }
  1796. END_STATE
  1797. BEGIN_STATE(ScriptDataEscapedDash)
  1798. {
  1799. ON('-')
  1800. {
  1801. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscapedDashDash);
  1802. }
  1803. ON('<')
  1804. {
  1805. SWITCH_TO(ScriptDataEscapedLessThanSign);
  1806. }
  1807. ON(0)
  1808. {
  1809. TODO();
  1810. }
  1811. ON_EOF
  1812. {
  1813. TODO();
  1814. }
  1815. ANYTHING_ELSE
  1816. {
  1817. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscaped);
  1818. }
  1819. }
  1820. END_STATE
  1821. BEGIN_STATE(ScriptDataEscaped)
  1822. {
  1823. ON('-')
  1824. {
  1825. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscapedDash);
  1826. }
  1827. ON('<')
  1828. {
  1829. SWITCH_TO(ScriptDataEscapedLessThanSign);
  1830. }
  1831. ON(0)
  1832. {
  1833. TODO();
  1834. }
  1835. ON_EOF
  1836. {
  1837. TODO();
  1838. }
  1839. ANYTHING_ELSE
  1840. {
  1841. EMIT_CURRENT_CHARACTER;
  1842. }
  1843. }
  1844. END_STATE
  1845. BEGIN_STATE(ScriptDataEndTagOpen)
  1846. {
  1847. ON_ASCII_ALPHA
  1848. {
  1849. create_new_token(HTMLToken::Type::EndTag);
  1850. RECONSUME_IN(ScriptDataEndTagName);
  1851. }
  1852. ANYTHING_ELSE
  1853. {
  1854. TODO();
  1855. }
  1856. }
  1857. END_STATE
  1858. BEGIN_STATE(ScriptDataEndTagName)
  1859. {
  1860. ON_WHITESPACE
  1861. {
  1862. if (current_end_tag_token_is_appropriate())
  1863. SWITCH_TO(BeforeAttributeName);
  1864. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1865. TODO();
  1866. }
  1867. ON('/')
  1868. {
  1869. if (current_end_tag_token_is_appropriate())
  1870. SWITCH_TO(SelfClosingStartTag);
  1871. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1872. TODO();
  1873. }
  1874. ON('>')
  1875. {
  1876. if (current_end_tag_token_is_appropriate())
  1877. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1878. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1879. TODO();
  1880. }
  1881. ON_ASCII_UPPER_ALPHA
  1882. {
  1883. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1884. m_temporary_buffer.append(current_input_character.value());
  1885. continue;
  1886. }
  1887. ON_ASCII_LOWER_ALPHA
  1888. {
  1889. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1890. m_temporary_buffer.append(current_input_character.value());
  1891. continue;
  1892. }
  1893. ANYTHING_ELSE
  1894. {
  1895. TODO();
  1896. }
  1897. }
  1898. END_STATE
  1899. default:
  1900. TODO();
  1901. }
  1902. }
  1903. }
  1904. bool HTMLTokenizer::consume_next_if_match(const StringView& string, CaseSensitivity case_sensitivity)
  1905. {
  1906. for (size_t i = 0; i < string.length(); ++i) {
  1907. auto codepoint = peek_codepoint(i);
  1908. if (!codepoint.has_value())
  1909. return false;
  1910. // FIXME: This should be more Unicode-aware.
  1911. if (case_sensitivity == CaseSensitivity::CaseInsensitive) {
  1912. if (codepoint.value() < 0x80) {
  1913. if (tolower(codepoint.value()) != tolower(string[i]))
  1914. return false;
  1915. continue;
  1916. }
  1917. }
  1918. if (codepoint.value() != (u32)string[i])
  1919. return false;
  1920. }
  1921. m_cursor += string.length();
  1922. return true;
  1923. }
  1924. void HTMLTokenizer::create_new_token(HTMLToken::Type type)
  1925. {
  1926. m_current_token = {};
  1927. m_current_token.m_type = type;
  1928. }
  1929. HTMLTokenizer::HTMLTokenizer(const StringView& input, const String& encoding)
  1930. {
  1931. auto* decoder = TextCodec::decoder_for(encoding);
  1932. ASSERT(decoder);
  1933. m_decoded_input = decoder->to_utf8(input);
  1934. m_input = m_decoded_input;
  1935. }
  1936. void HTMLTokenizer::will_switch_to([[maybe_unused]] State new_state)
  1937. {
  1938. #ifdef TOKENIZER_TRACE
  1939. dbg() << "[" << state_name(m_state) << "] Switch to " << state_name(new_state);
  1940. #endif
  1941. }
  1942. void HTMLTokenizer::will_reconsume_in([[maybe_unused]] State new_state)
  1943. {
  1944. #ifdef TOKENIZER_TRACE
  1945. dbg() << "[" << state_name(m_state) << "] Reconsume in " << state_name(new_state);
  1946. #endif
  1947. }
  1948. void HTMLTokenizer::switch_to(Badge<HTMLDocumentParser>, State new_state)
  1949. {
  1950. #ifdef TOKENIZER_TRACE
  1951. dbg() << "[" << state_name(m_state) << "] Parser switches tokenizer state to " << state_name(new_state);
  1952. #endif
  1953. m_state = new_state;
  1954. }
  1955. void HTMLTokenizer::will_emit(HTMLToken& token)
  1956. {
  1957. if (token.is_start_tag())
  1958. m_last_emitted_start_tag = token;
  1959. }
  1960. bool HTMLTokenizer::current_end_tag_token_is_appropriate() const
  1961. {
  1962. ASSERT(m_current_token.is_end_tag());
  1963. if (!m_last_emitted_start_tag.is_start_tag())
  1964. return false;
  1965. return m_current_token.tag_name() == m_last_emitted_start_tag.tag_name();
  1966. }
  1967. bool HTMLTokenizer::consumed_as_part_of_an_attribute() const
  1968. {
  1969. return m_return_state == State::AttributeValueUnquoted || m_return_state == State::AttributeValueSingleQuoted || m_return_state == State::AttributeValueDoubleQuoted;
  1970. }
  1971. }