HTMLTokenizer.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  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_EOF
  196. {
  197. EMIT_EOF;
  198. }
  199. ANYTHING_ELSE
  200. {
  201. EMIT_CURRENT_CHARACTER;
  202. }
  203. }
  204. END_STATE
  205. BEGIN_STATE(TagOpen)
  206. {
  207. ON('!')
  208. {
  209. SWITCH_TO(MarkupDeclarationOpen);
  210. }
  211. ON('/')
  212. {
  213. SWITCH_TO(EndTagOpen);
  214. }
  215. ON_ASCII_ALPHA
  216. {
  217. create_new_token(HTMLToken::Type::StartTag);
  218. RECONSUME_IN(TagName);
  219. }
  220. ON('?')
  221. {
  222. TODO();
  223. }
  224. ANYTHING_ELSE
  225. {
  226. TODO();
  227. }
  228. }
  229. END_STATE
  230. BEGIN_STATE(TagName)
  231. {
  232. ON_WHITESPACE
  233. {
  234. SWITCH_TO(BeforeAttributeName);
  235. }
  236. ON('/')
  237. {
  238. SWITCH_TO(SelfClosingStartTag);
  239. }
  240. ON('>')
  241. {
  242. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  243. }
  244. ANYTHING_ELSE
  245. {
  246. m_current_token.m_tag.tag_name.append(current_input_character.value());
  247. continue;
  248. }
  249. }
  250. END_STATE
  251. BEGIN_STATE(EndTagOpen)
  252. {
  253. ON_ASCII_ALPHA
  254. {
  255. create_new_token(HTMLToken::Type::EndTag);
  256. RECONSUME_IN(TagName);
  257. }
  258. }
  259. END_STATE
  260. BEGIN_STATE(MarkupDeclarationOpen)
  261. {
  262. DONT_CONSUME_NEXT_INPUT_CHARACTER;
  263. if (consume_next_if_match("--")) {
  264. create_new_token(HTMLToken::Type::Comment);
  265. SWITCH_TO(CommentStart);
  266. }
  267. if (consume_next_if_match("DOCTYPE", CaseSensitivity::CaseInsensitive)) {
  268. SWITCH_TO(DOCTYPE);
  269. }
  270. }
  271. END_STATE
  272. BEGIN_STATE(DOCTYPE)
  273. {
  274. ON_WHITESPACE
  275. {
  276. SWITCH_TO(BeforeDOCTYPEName);
  277. }
  278. ON('>')
  279. {
  280. RECONSUME_IN(BeforeDOCTYPEName);
  281. }
  282. ON_EOF
  283. {
  284. TODO();
  285. }
  286. ANYTHING_ELSE
  287. {
  288. TODO();
  289. }
  290. }
  291. END_STATE
  292. BEGIN_STATE(BeforeDOCTYPEName)
  293. {
  294. ON_WHITESPACE
  295. {
  296. continue;
  297. }
  298. ON_ASCII_UPPER_ALPHA
  299. {
  300. create_new_token(HTMLToken::Type::DOCTYPE);
  301. m_current_token.m_doctype.name.append(tolower(current_input_character.value()));
  302. SWITCH_TO(DOCTYPEName);
  303. }
  304. ON(0)
  305. {
  306. TODO();
  307. }
  308. ON('>')
  309. {
  310. TODO();
  311. }
  312. ON_EOF
  313. {
  314. TODO();
  315. }
  316. ANYTHING_ELSE
  317. {
  318. create_new_token(HTMLToken::Type::DOCTYPE);
  319. m_current_token.m_doctype.name.append(current_input_character.value());
  320. SWITCH_TO(DOCTYPEName);
  321. }
  322. }
  323. END_STATE
  324. BEGIN_STATE(DOCTYPEName)
  325. {
  326. ON_WHITESPACE
  327. {
  328. SWITCH_TO(AfterDOCTYPEName);
  329. }
  330. ON('>')
  331. {
  332. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  333. }
  334. ON_ASCII_UPPER_ALPHA
  335. {
  336. m_current_token.m_doctype.name.append(tolower(current_input_character.value()));
  337. }
  338. ON(0)
  339. {
  340. TODO();
  341. }
  342. ON_EOF
  343. {
  344. TODO();
  345. }
  346. ANYTHING_ELSE
  347. {
  348. m_current_token.m_doctype.name.append(current_input_character.value());
  349. continue;
  350. }
  351. }
  352. END_STATE
  353. BEGIN_STATE(AfterDOCTYPEName)
  354. {
  355. ON_WHITESPACE
  356. {
  357. continue;
  358. }
  359. ON('>')
  360. {
  361. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  362. }
  363. ON_EOF
  364. {
  365. TODO();
  366. }
  367. ANYTHING_ELSE
  368. {
  369. if (toupper(current_input_character.value()) == 'P' && consume_next_if_match("UBLIC", CaseSensitivity::CaseInsensitive)) {
  370. SWITCH_TO(AfterDOCTYPEPublicKeyword);
  371. }
  372. if (toupper(current_input_character.value()) == 'S' && consume_next_if_match("YSTEM", CaseSensitivity::CaseInsensitive)) {
  373. SWITCH_TO(AfterDOCTYPESystemKeyword);
  374. }
  375. TODO();
  376. }
  377. }
  378. END_STATE
  379. BEGIN_STATE(AfterDOCTYPEPublicKeyword)
  380. {
  381. ON_WHITESPACE
  382. {
  383. SWITCH_TO(BeforeDOCTYPEPublicIdentifier);
  384. }
  385. ON('"')
  386. {
  387. TODO();
  388. }
  389. ON('\'')
  390. {
  391. TODO();
  392. }
  393. ON('>')
  394. {
  395. TODO();
  396. }
  397. ON_EOF
  398. {
  399. TODO();
  400. }
  401. ANYTHING_ELSE
  402. {
  403. TODO();
  404. }
  405. }
  406. END_STATE
  407. BEGIN_STATE(AfterDOCTYPESystemKeyword)
  408. {
  409. ON_WHITESPACE
  410. {
  411. SWITCH_TO(BeforeDOCTYPESystemIdentifier);
  412. }
  413. ON('"')
  414. {
  415. TODO();
  416. }
  417. ON('\'')
  418. {
  419. TODO();
  420. }
  421. ON('>')
  422. {
  423. TODO();
  424. }
  425. ON_EOF
  426. {
  427. TODO();
  428. }
  429. ANYTHING_ELSE
  430. {
  431. TODO();
  432. }
  433. }
  434. END_STATE
  435. BEGIN_STATE(BeforeDOCTYPEPublicIdentifier)
  436. {
  437. ON_WHITESPACE
  438. {
  439. continue;
  440. }
  441. ON('"')
  442. {
  443. m_current_token.m_doctype.public_identifier.clear();
  444. SWITCH_TO(DOCTYPEPublicIdentifierDoubleQuoted);
  445. }
  446. ON('\'')
  447. {
  448. m_current_token.m_doctype.public_identifier.clear();
  449. SWITCH_TO(DOCTYPEPublicIdentifierSingleQuoted);
  450. }
  451. ON('>')
  452. {
  453. TODO();
  454. }
  455. ON_EOF
  456. {
  457. TODO();
  458. }
  459. ANYTHING_ELSE
  460. {
  461. TODO();
  462. }
  463. }
  464. END_STATE
  465. BEGIN_STATE(BeforeDOCTYPESystemIdentifier)
  466. {
  467. ON_WHITESPACE
  468. {
  469. continue;
  470. }
  471. ON('"')
  472. {
  473. m_current_token.m_doctype.system_identifier.clear();
  474. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  475. }
  476. ON('\'')
  477. {
  478. m_current_token.m_doctype.system_identifier.clear();
  479. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  480. }
  481. ON('>')
  482. {
  483. TODO();
  484. }
  485. ON_EOF
  486. {
  487. TODO();
  488. }
  489. ANYTHING_ELSE
  490. {
  491. TODO();
  492. }
  493. }
  494. END_STATE
  495. BEGIN_STATE(DOCTYPEPublicIdentifierDoubleQuoted)
  496. {
  497. ON('"')
  498. {
  499. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  500. }
  501. ON(0)
  502. {
  503. TODO();
  504. }
  505. ON('>')
  506. {
  507. TODO();
  508. }
  509. ON_EOF
  510. {
  511. TODO();
  512. }
  513. ANYTHING_ELSE
  514. {
  515. m_current_token.m_doctype.public_identifier.append(current_input_character.value());
  516. continue;
  517. }
  518. }
  519. END_STATE
  520. BEGIN_STATE(DOCTYPEPublicIdentifierSingleQuoted)
  521. {
  522. ON('\'')
  523. {
  524. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  525. }
  526. ON(0)
  527. {
  528. TODO();
  529. }
  530. ON('>')
  531. {
  532. TODO();
  533. }
  534. ON_EOF
  535. {
  536. TODO();
  537. }
  538. ANYTHING_ELSE
  539. {
  540. m_current_token.m_doctype.public_identifier.append(current_input_character.value());
  541. continue;
  542. }
  543. }
  544. END_STATE
  545. BEGIN_STATE(DOCTYPESystemIdentifierDoubleQuoted)
  546. {
  547. ON('"')
  548. {
  549. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  550. }
  551. ON(0)
  552. {
  553. TODO();
  554. }
  555. ON('>')
  556. {
  557. TODO();
  558. }
  559. ON_EOF
  560. {
  561. TODO();
  562. }
  563. ANYTHING_ELSE
  564. {
  565. m_current_token.m_doctype.system_identifier.append(current_input_character.value());
  566. continue;
  567. }
  568. }
  569. END_STATE
  570. BEGIN_STATE(DOCTYPESystemIdentifierSingleQuoted)
  571. {
  572. ON('\'')
  573. {
  574. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  575. }
  576. ON(0)
  577. {
  578. TODO();
  579. }
  580. ON('>')
  581. {
  582. TODO();
  583. }
  584. ON_EOF
  585. {
  586. TODO();
  587. }
  588. ANYTHING_ELSE
  589. {
  590. m_current_token.m_doctype.system_identifier.append(current_input_character.value());
  591. continue;
  592. }
  593. }
  594. END_STATE
  595. BEGIN_STATE(AfterDOCTYPEPublicIdentifier)
  596. {
  597. ON_WHITESPACE
  598. {
  599. SWITCH_TO(BetweenDOCTYPEPublicAndSystemIdentifiers);
  600. }
  601. ON('>')
  602. {
  603. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  604. }
  605. ON('"')
  606. {
  607. TODO();
  608. }
  609. ON('\'')
  610. {
  611. TODO();
  612. }
  613. ON_EOF
  614. {
  615. TODO();
  616. }
  617. ANYTHING_ELSE
  618. {
  619. TODO();
  620. }
  621. }
  622. END_STATE
  623. BEGIN_STATE(BetweenDOCTYPEPublicAndSystemIdentifiers)
  624. {
  625. ON_WHITESPACE
  626. {
  627. continue;
  628. }
  629. ON('>')
  630. {
  631. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  632. }
  633. ON('"')
  634. {
  635. m_current_token.m_doctype.system_identifier.clear();
  636. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  637. }
  638. ON('\'')
  639. {
  640. m_current_token.m_doctype.system_identifier.clear();
  641. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  642. }
  643. ON_EOF
  644. {
  645. TODO();
  646. }
  647. ANYTHING_ELSE
  648. {
  649. TODO();
  650. }
  651. }
  652. END_STATE
  653. BEGIN_STATE(AfterDOCTYPESystemIdentifier)
  654. {
  655. ON_WHITESPACE
  656. {
  657. continue;
  658. }
  659. ON('>')
  660. {
  661. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  662. }
  663. ON_EOF
  664. {
  665. TODO();
  666. }
  667. ANYTHING_ELSE
  668. {
  669. TODO();
  670. }
  671. }
  672. END_STATE
  673. BEGIN_STATE(BeforeAttributeName)
  674. {
  675. ON_WHITESPACE
  676. {
  677. continue;
  678. }
  679. ON('/')
  680. {
  681. RECONSUME_IN(AfterAttributeName);
  682. }
  683. ON('>')
  684. {
  685. RECONSUME_IN(AfterAttributeName);
  686. }
  687. ON_EOF
  688. {
  689. RECONSUME_IN(AfterAttributeName);
  690. }
  691. ON('=')
  692. {
  693. TODO();
  694. }
  695. ANYTHING_ELSE
  696. {
  697. m_current_token.m_tag.attributes.append(HTMLToken::AttributeBuilder());
  698. RECONSUME_IN(AttributeName);
  699. }
  700. }
  701. END_STATE
  702. BEGIN_STATE(SelfClosingStartTag)
  703. {
  704. ON('>')
  705. {
  706. m_current_token.m_tag.self_closing = true;
  707. SWITCH_TO(Data);
  708. }
  709. ON_EOF
  710. {
  711. TODO();
  712. }
  713. ANYTHING_ELSE
  714. {
  715. TODO();
  716. }
  717. }
  718. END_STATE
  719. BEGIN_STATE(AttributeName)
  720. {
  721. ON_WHITESPACE
  722. {
  723. RECONSUME_IN(AfterAttributeName);
  724. }
  725. ON('/')
  726. {
  727. RECONSUME_IN(AfterAttributeName);
  728. }
  729. ON('>')
  730. {
  731. RECONSUME_IN(AfterAttributeName);
  732. }
  733. ON_EOF
  734. {
  735. RECONSUME_IN(AfterAttributeName);
  736. }
  737. ON('=')
  738. {
  739. SWITCH_TO(BeforeAttributeValue);
  740. }
  741. ANYTHING_ELSE
  742. {
  743. m_current_token.m_tag.attributes.last().name_builder.append(current_input_character.value());
  744. continue;
  745. }
  746. }
  747. END_STATE
  748. BEGIN_STATE(AfterAttributeName)
  749. {
  750. ON_WHITESPACE
  751. {
  752. continue;
  753. }
  754. ON('/')
  755. {
  756. SWITCH_TO(SelfClosingStartTag);
  757. }
  758. ON('=')
  759. {
  760. SWITCH_TO(BeforeAttributeValue);
  761. }
  762. ON('>')
  763. {
  764. SWITCH_TO(Data);
  765. }
  766. ON_EOF
  767. {
  768. TODO();
  769. }
  770. ANYTHING_ELSE
  771. {
  772. m_current_token.m_tag.attributes.append(HTMLToken::AttributeBuilder());
  773. RECONSUME_IN(AttributeName);
  774. }
  775. }
  776. END_STATE
  777. BEGIN_STATE(BeforeAttributeValue)
  778. {
  779. ON_WHITESPACE
  780. {
  781. continue;
  782. }
  783. ON('"')
  784. {
  785. SWITCH_TO(AttributeValueDoubleQuoted);
  786. }
  787. ON('\'')
  788. {
  789. SWITCH_TO(AttributeValueSingleQuoted);
  790. }
  791. ON('>')
  792. {
  793. TODO();
  794. }
  795. ANYTHING_ELSE
  796. {
  797. RECONSUME_IN(AttributeValueUnquoted);
  798. }
  799. }
  800. END_STATE
  801. BEGIN_STATE(AttributeValueDoubleQuoted)
  802. {
  803. ON('"')
  804. {
  805. SWITCH_TO(AfterAttributeValueQuoted);
  806. }
  807. ON('&')
  808. {
  809. m_return_state = State::AttributeValueDoubleQuoted;
  810. SWITCH_TO(CharacterReference);
  811. }
  812. ON(0)
  813. {
  814. TODO();
  815. }
  816. ON_EOF
  817. {
  818. TODO();
  819. }
  820. ANYTHING_ELSE
  821. {
  822. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  823. continue;
  824. }
  825. }
  826. END_STATE
  827. BEGIN_STATE(AttributeValueSingleQuoted)
  828. {
  829. ON('\'')
  830. {
  831. SWITCH_TO(AfterAttributeValueQuoted);
  832. }
  833. ON('&')
  834. {
  835. m_return_state = State::AttributeValueSingleQuoted;
  836. SWITCH_TO(CharacterReference);
  837. }
  838. ON(0)
  839. {
  840. TODO();
  841. }
  842. ON_EOF
  843. {
  844. TODO();
  845. }
  846. ANYTHING_ELSE
  847. {
  848. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  849. continue;
  850. }
  851. }
  852. END_STATE
  853. BEGIN_STATE(AttributeValueUnquoted)
  854. {
  855. ON_WHITESPACE
  856. {
  857. SWITCH_TO(BeforeAttributeName);
  858. }
  859. ON('&')
  860. {
  861. m_return_state = State::AttributeValueUnquoted;
  862. SWITCH_TO(CharacterReference);
  863. }
  864. ON('>')
  865. {
  866. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  867. }
  868. ON(0)
  869. {
  870. TODO();
  871. }
  872. ON_EOF
  873. {
  874. TODO();
  875. }
  876. ANYTHING_ELSE
  877. {
  878. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  879. continue;
  880. }
  881. }
  882. END_STATE
  883. BEGIN_STATE(AfterAttributeValueQuoted)
  884. {
  885. ON_WHITESPACE
  886. {
  887. SWITCH_TO(BeforeAttributeName);
  888. }
  889. ON('/')
  890. {
  891. SWITCH_TO(SelfClosingStartTag);
  892. }
  893. ON('>')
  894. {
  895. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  896. }
  897. ON_EOF
  898. {
  899. TODO();
  900. }
  901. ANYTHING_ELSE
  902. {
  903. TODO();
  904. }
  905. }
  906. END_STATE
  907. BEGIN_STATE(CommentStart)
  908. {
  909. ON('-')
  910. {
  911. SWITCH_TO(CommentStartDash);
  912. }
  913. ON('>')
  914. {
  915. TODO();
  916. }
  917. ANYTHING_ELSE
  918. {
  919. RECONSUME_IN(Comment);
  920. }
  921. }
  922. END_STATE
  923. BEGIN_STATE(CommentStartDash)
  924. {
  925. ON('-')
  926. {
  927. SWITCH_TO(CommentEnd);
  928. }
  929. ON('>')
  930. {
  931. TODO();
  932. }
  933. ON_EOF
  934. {
  935. TODO();
  936. }
  937. ANYTHING_ELSE
  938. {
  939. m_current_token.m_comment_or_character.data.append('-');
  940. RECONSUME_IN(Comment);
  941. }
  942. }
  943. END_STATE
  944. BEGIN_STATE(Comment)
  945. {
  946. ON('<')
  947. {
  948. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  949. SWITCH_TO(CommentLessThanSign);
  950. }
  951. ON('-')
  952. {
  953. SWITCH_TO(CommentEndDash);
  954. }
  955. ON(0)
  956. {
  957. TODO();
  958. }
  959. ON_EOF
  960. {
  961. TODO();
  962. }
  963. ANYTHING_ELSE
  964. {
  965. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  966. continue;
  967. }
  968. }
  969. END_STATE
  970. BEGIN_STATE(CommentEnd)
  971. {
  972. ON('>')
  973. {
  974. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  975. }
  976. ON('!')
  977. {
  978. SWITCH_TO(CommentEndBang);
  979. }
  980. ON('-')
  981. {
  982. m_current_token.m_comment_or_character.data.append('-');
  983. continue;
  984. }
  985. ON_EOF
  986. {
  987. TODO();
  988. }
  989. ANYTHING_ELSE
  990. {
  991. m_current_token.m_comment_or_character.data.append('-');
  992. RECONSUME_IN(Comment);
  993. }
  994. }
  995. END_STATE
  996. BEGIN_STATE(CommentEndBang)
  997. {
  998. ON('-')
  999. {
  1000. m_current_token.m_comment_or_character.data.append("--!");
  1001. SWITCH_TO(CommentEndDash);
  1002. }
  1003. ON('>')
  1004. {
  1005. TODO();
  1006. }
  1007. ON_EOF
  1008. {
  1009. TODO();
  1010. }
  1011. ANYTHING_ELSE
  1012. {
  1013. m_current_token.m_comment_or_character.data.append("--!");
  1014. RECONSUME_IN(Comment);
  1015. }
  1016. }
  1017. END_STATE
  1018. BEGIN_STATE(CommentEndDash)
  1019. {
  1020. ON('-')
  1021. {
  1022. SWITCH_TO(CommentEnd);
  1023. }
  1024. ON_EOF
  1025. {
  1026. TODO();
  1027. }
  1028. ANYTHING_ELSE
  1029. {
  1030. m_current_token.m_comment_or_character.data.append('-');
  1031. RECONSUME_IN(Comment);
  1032. }
  1033. }
  1034. END_STATE
  1035. BEGIN_STATE(CommentLessThanSign)
  1036. {
  1037. ON('!')
  1038. {
  1039. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  1040. SWITCH_TO(CommentLessThanSignBang);
  1041. }
  1042. ON('<')
  1043. {
  1044. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  1045. continue;
  1046. }
  1047. ANYTHING_ELSE
  1048. {
  1049. RECONSUME_IN(Comment);
  1050. }
  1051. }
  1052. END_STATE
  1053. BEGIN_STATE(CommentLessThanSignBang)
  1054. {
  1055. ON('-')
  1056. {
  1057. SWITCH_TO(CommentLessThanSignBangDash);
  1058. }
  1059. ANYTHING_ELSE
  1060. {
  1061. RECONSUME_IN(Comment);
  1062. }
  1063. }
  1064. END_STATE
  1065. BEGIN_STATE(CommentLessThanSignBangDash)
  1066. {
  1067. ON('-')
  1068. {
  1069. SWITCH_TO(CommentLessThanSignBangDashDash);
  1070. }
  1071. ANYTHING_ELSE
  1072. {
  1073. RECONSUME_IN(Comment);
  1074. }
  1075. }
  1076. END_STATE
  1077. BEGIN_STATE(CommentLessThanSignBangDashDash)
  1078. {
  1079. ON('>')
  1080. {
  1081. SWITCH_TO(CommentEnd);
  1082. }
  1083. ANYTHING_ELSE
  1084. {
  1085. TODO();
  1086. }
  1087. }
  1088. END_STATE
  1089. BEGIN_STATE(CharacterReference)
  1090. {
  1091. m_temporary_buffer.clear();
  1092. m_temporary_buffer.append('&');
  1093. ON_ASCII_ALPHANUMERIC
  1094. {
  1095. RECONSUME_IN(NamedCharacterReference);
  1096. }
  1097. ON('#')
  1098. {
  1099. m_temporary_buffer.append(current_input_character.value());
  1100. SWITCH_TO(NumericCharacterReference);
  1101. }
  1102. ANYTHING_ELSE
  1103. {
  1104. RECONSUME_IN_RETURN_STATE;
  1105. }
  1106. }
  1107. END_STATE
  1108. BEGIN_STATE(NamedCharacterReference)
  1109. {
  1110. auto match = HTML::codepoints_from_entity(m_input.substring_view(m_cursor - 1, m_input.length() - m_cursor + 1));
  1111. if (match.has_value()) {
  1112. m_cursor += match.value().entity.length();
  1113. for (auto ch : match.value().entity)
  1114. m_temporary_buffer.append(ch);
  1115. if (consumed_as_part_of_an_attribute() && match.value().entity.ends_with(';')) {
  1116. auto next_codepoint = peek_codepoint(0);
  1117. if (next_codepoint.has_value() && next_codepoint.value() == '=') {
  1118. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1119. SWITCH_TO_RETURN_STATE;
  1120. }
  1121. }
  1122. if (!match.value().entity.ends_with(';')) {
  1123. PARSE_ERROR();
  1124. }
  1125. m_temporary_buffer.clear();
  1126. m_temporary_buffer.append(match.value().codepoints);
  1127. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1128. SWITCH_TO_RETURN_STATE;
  1129. } else {
  1130. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1131. SWITCH_TO(AmbiguousAmpersand);
  1132. }
  1133. }
  1134. END_STATE
  1135. BEGIN_STATE(AmbiguousAmpersand)
  1136. {
  1137. ON_ASCII_ALPHANUMERIC
  1138. {
  1139. if (consumed_as_part_of_an_attribute()) {
  1140. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  1141. continue;
  1142. } else {
  1143. EMIT_CURRENT_CHARACTER;
  1144. }
  1145. }
  1146. ON(';')
  1147. {
  1148. TODO();
  1149. }
  1150. ANYTHING_ELSE
  1151. {
  1152. RECONSUME_IN_RETURN_STATE;
  1153. }
  1154. }
  1155. END_STATE
  1156. BEGIN_STATE(NumericCharacterReference)
  1157. {
  1158. m_character_reference_code = 0;
  1159. ON('X')
  1160. {
  1161. m_temporary_buffer.append(current_input_character.value());
  1162. SWITCH_TO(HexadecimalCharacterReferenceStart);
  1163. }
  1164. ON('x')
  1165. {
  1166. m_temporary_buffer.append(current_input_character.value());
  1167. SWITCH_TO(HexadecimalCharacterReferenceStart);
  1168. }
  1169. ANYTHING_ELSE
  1170. {
  1171. RECONSUME_IN(DecimalCharacterReferenceStart);
  1172. }
  1173. }
  1174. END_STATE
  1175. BEGIN_STATE(HexadecimalCharacterReferenceStart)
  1176. {
  1177. ON_ASCII_HEX_DIGIT
  1178. {
  1179. RECONSUME_IN(HexadecimalCharacterReference);
  1180. }
  1181. ANYTHING_ELSE
  1182. {
  1183. TODO();
  1184. }
  1185. }
  1186. END_STATE
  1187. BEGIN_STATE(DecimalCharacterReferenceStart)
  1188. {
  1189. ON_ASCII_DIGIT
  1190. {
  1191. RECONSUME_IN(DecimalCharacterReference);
  1192. }
  1193. ANYTHING_ELSE
  1194. {
  1195. TODO();
  1196. }
  1197. }
  1198. END_STATE
  1199. BEGIN_STATE(HexadecimalCharacterReference)
  1200. {
  1201. ON_ASCII_DIGIT
  1202. {
  1203. m_character_reference_code *= 16;
  1204. m_character_reference_code += current_input_character.value() - 0x30;
  1205. continue;
  1206. }
  1207. ON_ASCII_UPPER_ALPHA
  1208. {
  1209. m_character_reference_code *= 16;
  1210. m_character_reference_code += current_input_character.value() - 0x37;
  1211. continue;
  1212. }
  1213. ON_ASCII_LOWER_ALPHA
  1214. {
  1215. m_character_reference_code *= 16;
  1216. m_character_reference_code += current_input_character.value() - 0x57;
  1217. continue;
  1218. }
  1219. ON(';')
  1220. {
  1221. SWITCH_TO(NumericCharacterReferenceEnd);
  1222. }
  1223. ANYTHING_ELSE
  1224. {
  1225. TODO();
  1226. }
  1227. }
  1228. END_STATE
  1229. BEGIN_STATE(DecimalCharacterReference)
  1230. {
  1231. ON_ASCII_DIGIT
  1232. {
  1233. m_character_reference_code *= 10;
  1234. m_character_reference_code += current_input_character.value() - 0x30;
  1235. continue;
  1236. }
  1237. ON(';')
  1238. {
  1239. SWITCH_TO(NumericCharacterReferenceEnd);
  1240. }
  1241. ANYTHING_ELSE
  1242. {
  1243. TODO();
  1244. }
  1245. }
  1246. END_STATE
  1247. BEGIN_STATE(NumericCharacterReferenceEnd)
  1248. {
  1249. if (m_character_reference_code == 0) {
  1250. TODO();
  1251. }
  1252. if (m_character_reference_code > 0x10ffff) {
  1253. TODO();
  1254. }
  1255. if (is_surrogate(m_character_reference_code)) {
  1256. TODO();
  1257. }
  1258. if (is_noncharacter(m_character_reference_code)) {
  1259. TODO();
  1260. }
  1261. if (m_character_reference_code == 0xd || (is_control(m_character_reference_code) && !isspace(m_character_reference_code))) {
  1262. TODO();
  1263. }
  1264. if (is_control(m_character_reference_code)) {
  1265. constexpr struct {
  1266. u32 number;
  1267. u32 codepoint;
  1268. } conversion_table[] = {
  1269. { 0x80, 0x20AC },
  1270. { 0x82, 0x201A },
  1271. { 0x83, 0x0192 },
  1272. { 0x84, 0x201E },
  1273. { 0x85, 0x2026 },
  1274. { 0x86, 0x2020 },
  1275. { 0x87, 0x2021 },
  1276. { 0x88, 0x02C6 },
  1277. { 0x89, 0x2030 },
  1278. { 0x8A, 0x0160 },
  1279. { 0x8B, 0x2039 },
  1280. { 0x8C, 0x0152 },
  1281. { 0x8E, 0x017D },
  1282. { 0x91, 0x2018 },
  1283. { 0x92, 0x2019 },
  1284. { 0x93, 0x201C },
  1285. { 0x94, 0x201D },
  1286. { 0x95, 0x2022 },
  1287. { 0x96, 0x2013 },
  1288. { 0x97, 0x2014 },
  1289. { 0x98, 0x02DC },
  1290. { 0x99, 0x2122 },
  1291. { 0x9A, 0x0161 },
  1292. { 0x9B, 0x203A },
  1293. { 0x9C, 0x0153 },
  1294. { 0x9E, 0x017E },
  1295. { 0x9F, 0x0178 },
  1296. };
  1297. for (auto& entry : conversion_table) {
  1298. if (m_character_reference_code == entry.number) {
  1299. m_character_reference_code = entry.codepoint;
  1300. break;
  1301. }
  1302. }
  1303. }
  1304. m_temporary_buffer.clear();
  1305. m_temporary_buffer.append(m_character_reference_code);
  1306. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1307. SWITCH_TO_RETURN_STATE;
  1308. }
  1309. END_STATE
  1310. BEGIN_STATE(RCDATA)
  1311. {
  1312. ON('&')
  1313. {
  1314. m_return_state = State::RCDATA;
  1315. SWITCH_TO(CharacterReference);
  1316. }
  1317. ON('<')
  1318. {
  1319. SWITCH_TO(RCDATALessThanSign);
  1320. }
  1321. ON(0)
  1322. {
  1323. TODO();
  1324. }
  1325. ON_EOF
  1326. {
  1327. EMIT_EOF;
  1328. }
  1329. ANYTHING_ELSE
  1330. {
  1331. EMIT_CURRENT_CHARACTER;
  1332. }
  1333. }
  1334. END_STATE
  1335. BEGIN_STATE(RCDATALessThanSign)
  1336. {
  1337. ON('/')
  1338. {
  1339. m_temporary_buffer.clear();
  1340. SWITCH_TO(RCDATAEndTagOpen);
  1341. }
  1342. ANYTHING_ELSE
  1343. {
  1344. EMIT_CHARACTER('<');
  1345. RECONSUME_IN(RCDATA);
  1346. }
  1347. }
  1348. END_STATE
  1349. BEGIN_STATE(RCDATAEndTagOpen)
  1350. {
  1351. ON_ASCII_ALPHA
  1352. {
  1353. create_new_token(HTMLToken::Type::EndTag);
  1354. RECONSUME_IN(RCDATAEndTagName);
  1355. }
  1356. ANYTHING_ELSE
  1357. {
  1358. // FIXME: Emit a U+003C LESS-THAN SIGN character token and a U+002F SOLIDUS character token. Reconsume in the RCDATA state.
  1359. TODO();
  1360. }
  1361. }
  1362. END_STATE
  1363. BEGIN_STATE(RCDATAEndTagName)
  1364. {
  1365. ON_WHITESPACE
  1366. {
  1367. TODO();
  1368. }
  1369. ON('/')
  1370. {
  1371. TODO();
  1372. }
  1373. ON('>')
  1374. {
  1375. if (!current_end_tag_token_is_appropriate()) {
  1376. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1377. TODO();
  1378. }
  1379. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1380. }
  1381. ON_ASCII_UPPER_ALPHA
  1382. {
  1383. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1384. m_temporary_buffer.append(current_input_character.value());
  1385. continue;
  1386. }
  1387. ON_ASCII_LOWER_ALPHA
  1388. {
  1389. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1390. m_temporary_buffer.append(current_input_character.value());
  1391. continue;
  1392. }
  1393. ANYTHING_ELSE
  1394. {
  1395. TODO();
  1396. }
  1397. }
  1398. END_STATE
  1399. BEGIN_STATE(RAWTEXT)
  1400. {
  1401. ON('<')
  1402. {
  1403. SWITCH_TO(RAWTEXTLessThanSign);
  1404. }
  1405. ON(0)
  1406. {
  1407. TODO();
  1408. }
  1409. ON_EOF
  1410. {
  1411. EMIT_EOF;
  1412. }
  1413. ANYTHING_ELSE
  1414. {
  1415. EMIT_CURRENT_CHARACTER;
  1416. }
  1417. }
  1418. END_STATE
  1419. BEGIN_STATE(RAWTEXTLessThanSign)
  1420. {
  1421. ON('/')
  1422. {
  1423. m_temporary_buffer.clear();
  1424. SWITCH_TO(RAWTEXTEndTagOpen);
  1425. }
  1426. ANYTHING_ELSE
  1427. {
  1428. EMIT_CHARACTER('<');
  1429. RECONSUME_IN(RAWTEXT);
  1430. }
  1431. }
  1432. END_STATE
  1433. BEGIN_STATE(RAWTEXTEndTagOpen)
  1434. {
  1435. ON_ASCII_ALPHA
  1436. {
  1437. create_new_token(HTMLToken::Type::EndTag);
  1438. RECONSUME_IN(RAWTEXTEndTagName);
  1439. }
  1440. ANYTHING_ELSE
  1441. {
  1442. // FIXME: Emit a U+003C LESS-THAN SIGN character token and a U+002F SOLIDUS character token. Reconsume in the RAWTEXT state.
  1443. TODO();
  1444. }
  1445. }
  1446. END_STATE
  1447. BEGIN_STATE(RAWTEXTEndTagName)
  1448. {
  1449. ON_WHITESPACE
  1450. {
  1451. TODO();
  1452. }
  1453. ON('/')
  1454. {
  1455. TODO();
  1456. }
  1457. ON('>')
  1458. {
  1459. if (!current_end_tag_token_is_appropriate()) {
  1460. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1461. TODO();
  1462. }
  1463. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1464. }
  1465. ON_ASCII_UPPER_ALPHA
  1466. {
  1467. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1468. m_temporary_buffer.append(current_input_character.value());
  1469. continue;
  1470. }
  1471. ON_ASCII_LOWER_ALPHA
  1472. {
  1473. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1474. m_temporary_buffer.append(current_input_character.value());
  1475. continue;
  1476. }
  1477. ANYTHING_ELSE
  1478. {
  1479. TODO();
  1480. }
  1481. }
  1482. END_STATE
  1483. BEGIN_STATE(ScriptData)
  1484. {
  1485. ON('<')
  1486. {
  1487. SWITCH_TO(ScriptDataLessThanSign);
  1488. }
  1489. ON(0)
  1490. {
  1491. TODO();
  1492. }
  1493. ON_EOF
  1494. {
  1495. EMIT_EOF;
  1496. }
  1497. ANYTHING_ELSE
  1498. {
  1499. EMIT_CURRENT_CHARACTER;
  1500. }
  1501. }
  1502. END_STATE
  1503. BEGIN_STATE(ScriptDataLessThanSign)
  1504. {
  1505. ON('/')
  1506. {
  1507. m_temporary_buffer.clear();
  1508. SWITCH_TO(ScriptDataEndTagOpen);
  1509. }
  1510. ON('!')
  1511. {
  1512. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1513. m_queued_tokens.enqueue(HTMLToken::make_character('!'));
  1514. SWITCH_TO(ScriptDataEscapeStart);
  1515. }
  1516. ANYTHING_ELSE
  1517. {
  1518. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptData);
  1519. }
  1520. }
  1521. END_STATE
  1522. BEGIN_STATE(ScriptDataEscapeStart)
  1523. {
  1524. ON('-')
  1525. {
  1526. m_queued_tokens.enqueue(HTMLToken::make_character('-'));
  1527. SWITCH_TO(ScriptDataEscapeStartDash);
  1528. }
  1529. ANYTHING_ELSE
  1530. {
  1531. RECONSUME_IN(ScriptData);
  1532. }
  1533. }
  1534. END_STATE
  1535. BEGIN_STATE(ScriptDataEscapeStartDash)
  1536. {
  1537. ON('-')
  1538. {
  1539. m_queued_tokens.enqueue(HTMLToken::make_character('-'));
  1540. SWITCH_TO(ScriptDataEscapedDashDash);
  1541. }
  1542. ANYTHING_ELSE
  1543. {
  1544. RECONSUME_IN(ScriptData);
  1545. }
  1546. }
  1547. END_STATE
  1548. BEGIN_STATE(ScriptDataEscapedDashDash)
  1549. {
  1550. ON('-')
  1551. {
  1552. EMIT_CHARACTER('-');
  1553. }
  1554. ON('<')
  1555. {
  1556. SWITCH_TO(ScriptDataEscapedLessThanSign);
  1557. }
  1558. ON('>')
  1559. {
  1560. m_queued_tokens.enqueue(HTMLToken::make_character('>'));
  1561. SWITCH_TO(ScriptData);
  1562. }
  1563. ON(0)
  1564. {
  1565. TODO();
  1566. }
  1567. ON_EOF
  1568. {
  1569. TODO();
  1570. }
  1571. ANYTHING_ELSE
  1572. {
  1573. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscaped);
  1574. }
  1575. }
  1576. END_STATE
  1577. BEGIN_STATE(ScriptDataEscapedLessThanSign)
  1578. {
  1579. ON('/')
  1580. {
  1581. m_temporary_buffer.clear();
  1582. SWITCH_TO(ScriptDataEscapedEndTagOpen);
  1583. }
  1584. ON_ASCII_ALPHA
  1585. {
  1586. m_temporary_buffer.clear();
  1587. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptDataDoubleEscapeStart);
  1588. }
  1589. ANYTHING_ELSE
  1590. {
  1591. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptDataEscaped);
  1592. }
  1593. }
  1594. END_STATE
  1595. BEGIN_STATE(ScriptDataEscapedEndTagOpen)
  1596. {
  1597. ON_ASCII_ALPHA
  1598. {
  1599. create_new_token(HTMLToken::Type::EndTag);
  1600. RECONSUME_IN(ScriptDataEscapedEndTagName);
  1601. }
  1602. ANYTHING_ELSE
  1603. {
  1604. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1605. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1606. RECONSUME_IN(ScriptDataEscaped);
  1607. }
  1608. }
  1609. END_STATE
  1610. BEGIN_STATE(ScriptDataEscapedEndTagName)
  1611. {
  1612. ON_WHITESPACE
  1613. {
  1614. if (current_end_tag_token_is_appropriate()) {
  1615. SWITCH_TO(BeforeAttributeName);
  1616. } else {
  1617. TODO();
  1618. }
  1619. }
  1620. ON('/')
  1621. {
  1622. if (current_end_tag_token_is_appropriate()) {
  1623. SWITCH_TO(SelfClosingStartTag);
  1624. } else {
  1625. TODO();
  1626. }
  1627. }
  1628. ON('>')
  1629. {
  1630. if (current_end_tag_token_is_appropriate()) {
  1631. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1632. } else {
  1633. TODO();
  1634. }
  1635. }
  1636. ON_ASCII_UPPER_ALPHA
  1637. {
  1638. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1639. m_temporary_buffer.append(current_input_character.value());
  1640. continue;
  1641. }
  1642. ON_ASCII_LOWER_ALPHA
  1643. {
  1644. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1645. m_temporary_buffer.append(current_input_character.value());
  1646. continue;
  1647. }
  1648. ANYTHING_ELSE
  1649. {
  1650. m_queued_tokens.enqueue(HTMLToken::make_character('<'));
  1651. m_queued_tokens.enqueue(HTMLToken::make_character('/'));
  1652. for (auto codepoint : m_temporary_buffer) {
  1653. m_queued_tokens.enqueue(HTMLToken::make_character(codepoint));
  1654. }
  1655. RECONSUME_IN(ScriptDataEscaped);
  1656. }
  1657. }
  1658. END_STATE
  1659. BEGIN_STATE(ScriptDataDoubleEscapeStart)
  1660. {
  1661. TODO();
  1662. }
  1663. END_STATE
  1664. BEGIN_STATE(ScriptDataEscapedDash)
  1665. {
  1666. ON('-')
  1667. {
  1668. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscapedDashDash);
  1669. }
  1670. ON('<')
  1671. {
  1672. SWITCH_TO(ScriptDataEscapedLessThanSign);
  1673. }
  1674. ON(0)
  1675. {
  1676. TODO();
  1677. }
  1678. ON_EOF
  1679. {
  1680. TODO();
  1681. }
  1682. ANYTHING_ELSE
  1683. {
  1684. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscaped);
  1685. }
  1686. }
  1687. END_STATE
  1688. BEGIN_STATE(ScriptDataEscaped)
  1689. {
  1690. ON('-')
  1691. {
  1692. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(ScriptDataEscapedDash);
  1693. }
  1694. ON('<')
  1695. {
  1696. SWITCH_TO(ScriptDataEscapedLessThanSign);
  1697. }
  1698. ON(0)
  1699. {
  1700. TODO();
  1701. }
  1702. ON_EOF
  1703. {
  1704. TODO();
  1705. }
  1706. ANYTHING_ELSE
  1707. {
  1708. EMIT_CURRENT_CHARACTER;
  1709. }
  1710. }
  1711. END_STATE
  1712. BEGIN_STATE(ScriptDataEndTagOpen)
  1713. {
  1714. ON_ASCII_ALPHA
  1715. {
  1716. create_new_token(HTMLToken::Type::EndTag);
  1717. RECONSUME_IN(ScriptDataEndTagName);
  1718. }
  1719. ANYTHING_ELSE
  1720. {
  1721. TODO();
  1722. }
  1723. }
  1724. END_STATE
  1725. BEGIN_STATE(ScriptDataEndTagName)
  1726. {
  1727. ON_WHITESPACE
  1728. {
  1729. if (current_end_tag_token_is_appropriate())
  1730. SWITCH_TO(BeforeAttributeName);
  1731. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1732. TODO();
  1733. }
  1734. ON('/')
  1735. {
  1736. if (current_end_tag_token_is_appropriate())
  1737. SWITCH_TO(SelfClosingStartTag);
  1738. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1739. TODO();
  1740. }
  1741. ON('>')
  1742. {
  1743. if (current_end_tag_token_is_appropriate())
  1744. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1745. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1746. TODO();
  1747. }
  1748. ON_ASCII_UPPER_ALPHA
  1749. {
  1750. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1751. m_temporary_buffer.append(current_input_character.value());
  1752. continue;
  1753. }
  1754. ON_ASCII_LOWER_ALPHA
  1755. {
  1756. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1757. m_temporary_buffer.append(current_input_character.value());
  1758. continue;
  1759. }
  1760. ANYTHING_ELSE
  1761. {
  1762. TODO();
  1763. }
  1764. }
  1765. END_STATE
  1766. default:
  1767. TODO();
  1768. }
  1769. }
  1770. }
  1771. bool HTMLTokenizer::consume_next_if_match(const StringView& string, CaseSensitivity case_sensitivity)
  1772. {
  1773. for (size_t i = 0; i < string.length(); ++i) {
  1774. auto codepoint = peek_codepoint(i);
  1775. if (!codepoint.has_value())
  1776. return false;
  1777. // FIXME: This should be more Unicode-aware.
  1778. if (case_sensitivity == CaseSensitivity::CaseInsensitive) {
  1779. if (codepoint.value() < 0x80) {
  1780. if (tolower(codepoint.value()) != tolower(string[i]))
  1781. return false;
  1782. continue;
  1783. }
  1784. }
  1785. if (codepoint.value() != (u32)string[i])
  1786. return false;
  1787. }
  1788. m_cursor += string.length();
  1789. return true;
  1790. }
  1791. void HTMLTokenizer::create_new_token(HTMLToken::Type type)
  1792. {
  1793. m_current_token = {};
  1794. m_current_token.m_type = type;
  1795. }
  1796. HTMLTokenizer::HTMLTokenizer(const StringView& input, const String& encoding)
  1797. {
  1798. auto* decoder = TextCodec::decoder_for(encoding);
  1799. ASSERT(decoder);
  1800. m_decoded_input = decoder->to_utf8(input);
  1801. m_input = m_decoded_input;
  1802. }
  1803. void HTMLTokenizer::will_switch_to([[maybe_unused]] State new_state)
  1804. {
  1805. #ifdef TOKENIZER_TRACE
  1806. dbg() << "[" << state_name(m_state) << "] Switch to " << state_name(new_state);
  1807. #endif
  1808. }
  1809. void HTMLTokenizer::will_reconsume_in([[maybe_unused]] State new_state)
  1810. {
  1811. #ifdef TOKENIZER_TRACE
  1812. dbg() << "[" << state_name(m_state) << "] Reconsume in " << state_name(new_state);
  1813. #endif
  1814. }
  1815. void HTMLTokenizer::switch_to(Badge<HTMLDocumentParser>, State new_state)
  1816. {
  1817. #ifdef TOKENIZER_TRACE
  1818. dbg() << "[" << state_name(m_state) << "] Parser switches tokenizer state to " << state_name(new_state);
  1819. #endif
  1820. m_state = new_state;
  1821. }
  1822. void HTMLTokenizer::will_emit(HTMLToken& token)
  1823. {
  1824. if (token.is_start_tag())
  1825. m_last_emitted_start_tag = token;
  1826. }
  1827. bool HTMLTokenizer::current_end_tag_token_is_appropriate() const
  1828. {
  1829. ASSERT(m_current_token.is_end_tag());
  1830. if (!m_last_emitted_start_tag.is_start_tag())
  1831. return false;
  1832. return m_current_token.tag_name() == m_last_emitted_start_tag.tag_name();
  1833. }
  1834. bool HTMLTokenizer::consumed_as_part_of_an_attribute() const
  1835. {
  1836. return m_return_state == State::AttributeValueUnquoted || m_return_state == State::AttributeValueSingleQuoted || m_return_state == State::AttributeValueDoubleQuoted;
  1837. }
  1838. }