HTMLTokenizer.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  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 <LibWeb/Parser/HTMLToken.h>
  27. #include <LibWeb/Parser/HTMLTokenizer.h>
  28. #include <ctype.h>
  29. #pragma GCC diagnostic ignored "-Wunused-label"
  30. //#define TOKENIZER_TRACE
  31. #define CONSUME_NEXT_INPUT_CHARACTER \
  32. current_input_character = next_codepoint();
  33. #define TODO() \
  34. do { \
  35. dbg() << "[TODO: " << state_name(m_state) << "] '" << (char)current_input_character.value() << "'"; \
  36. ASSERT_NOT_REACHED(); \
  37. } while (0)
  38. #define SWITCH_TO(new_state) \
  39. do { \
  40. will_switch_to(State::new_state); \
  41. m_state = State::new_state; \
  42. CONSUME_NEXT_INPUT_CHARACTER; \
  43. goto new_state; \
  44. } while (0)
  45. #define RECONSUME_IN(new_state) \
  46. do { \
  47. will_reconsume_in(State::new_state); \
  48. m_state = State::new_state; \
  49. goto new_state; \
  50. } while (0)
  51. #define SWITCH_TO_RETURN_STATE \
  52. do { \
  53. will_switch_to(m_return_state); \
  54. m_state = m_return_state; \
  55. goto _StartOfFunction; \
  56. } while (0)
  57. #define RECONSUME_IN_RETURN_STATE \
  58. do { \
  59. will_reconsume_in(m_return_state); \
  60. m_state = m_return_state; \
  61. goto _StartOfFunction; \
  62. } while (0)
  63. #define SWITCH_TO_AND_EMIT_CURRENT_TOKEN(new_state) \
  64. do { \
  65. will_switch_to(State::new_state); \
  66. m_state = State::new_state; \
  67. will_emit(m_current_token); \
  68. m_queued_tokens.enqueue(m_current_token); \
  69. return m_queued_tokens.dequeue(); \
  70. } while (0)
  71. #define EMIT_CHARACTER_AND_RECONSUME_IN(codepoint, new_state) \
  72. do { \
  73. m_queued_tokens.enqueue(m_current_token); \
  74. will_reconsume_in(State::new_state); \
  75. m_state = State::new_state; \
  76. goto new_state; \
  77. } while (0)
  78. #define FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE \
  79. do { \
  80. for (auto codepoint : m_temporary_buffer) { \
  81. if (consumed_as_part_of_an_attribute()) { \
  82. m_current_token.m_tag.attributes.last().value_builder.append(codepoint); \
  83. } else { \
  84. create_new_token(HTMLToken::Type::Character); \
  85. m_current_token.m_comment_or_character.data.append(codepoint); \
  86. m_queued_tokens.enqueue(m_current_token); \
  87. } \
  88. } \
  89. } while (0)
  90. #define DONT_CONSUME_NEXT_INPUT_CHARACTER --m_cursor;
  91. #define ON(codepoint) \
  92. if (current_input_character.has_value() && current_input_character.value() == codepoint)
  93. #define ON_EOF \
  94. if (!current_input_character.has_value())
  95. #define ON_ASCII_ALPHA \
  96. if (current_input_character.has_value() && isalpha(current_input_character.value()))
  97. #define ON_ASCII_ALPHANUMERIC \
  98. if (current_input_character.has_value() && isalnum(current_input_character.value()))
  99. #define ON_ASCII_UPPER_ALPHA \
  100. if (current_input_character.has_value() && current_input_character.value() >= 'A' && current_input_character.value() <= 'Z')
  101. #define ON_ASCII_LOWER_ALPHA \
  102. if (current_input_character.has_value() && current_input_character.value() >= 'a' && current_input_character.value() <= 'z')
  103. #define ON_ASCII_DIGIT \
  104. if (current_input_character.has_value() && isxdigit(current_input_character.value()))
  105. #define ON_ASCII_HEX_DIGIT \
  106. if (current_input_character.has_value() && isxdigit(current_input_character.value()))
  107. #define ON_WHITESPACE \
  108. 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() == ' '))
  109. #define ANYTHING_ELSE if (1)
  110. #define EMIT_EOF \
  111. do { \
  112. if (m_has_emitted_eof) \
  113. return {}; \
  114. m_has_emitted_eof = true; \
  115. create_new_token(HTMLToken::Type::EndOfFile); \
  116. will_emit(m_current_token); \
  117. m_queued_tokens.enqueue(m_current_token); \
  118. return m_queued_tokens.dequeue(); \
  119. } while (0)
  120. #define EMIT_CURRENT_TOKEN \
  121. do { \
  122. will_emit(m_current_token); \
  123. m_queued_tokens.enqueue(m_current_token); \
  124. return m_queued_tokens.dequeue(); \
  125. } while (0)
  126. #define EMIT_CHARACTER(codepoint) \
  127. do { \
  128. create_new_token(HTMLToken::Type::Character); \
  129. m_current_token.m_comment_or_character.data.append(codepoint); \
  130. m_queued_tokens.enqueue(m_current_token); \
  131. return m_queued_tokens.dequeue(); \
  132. } while (0)
  133. #define EMIT_CURRENT_CHARACTER \
  134. EMIT_CHARACTER(current_input_character.value());
  135. #define BEGIN_STATE(state) \
  136. state: \
  137. case State::state: { \
  138. { \
  139. {
  140. #define END_STATE \
  141. ASSERT_NOT_REACHED(); \
  142. break; \
  143. } \
  144. } \
  145. }
  146. static inline bool is_surrogate(u32 codepoint)
  147. {
  148. return (codepoint & 0xfffff800) == 0xd800;
  149. }
  150. static inline bool is_noncharacter(u32 codepoint)
  151. {
  152. return codepoint >= 0xfdd0 && (codepoint <= 0xfdef || (codepoint & 0xfffe) == 0xfffe) && codepoint <= 0x10ffff;
  153. }
  154. static inline bool is_c0_control(u32 codepoint)
  155. {
  156. return codepoint <= 0x1f;
  157. }
  158. static inline bool is_control(u32 codepoint)
  159. {
  160. return is_c0_control(codepoint) || (codepoint >= 0x7f && codepoint <= 0x9f);
  161. }
  162. namespace Web {
  163. Optional<u32> HTMLTokenizer::next_codepoint()
  164. {
  165. if (m_cursor >= m_input.length())
  166. return {};
  167. return m_input[m_cursor++];
  168. }
  169. Optional<u32> HTMLTokenizer::peek_codepoint(size_t offset) const
  170. {
  171. if ((m_cursor + offset) >= m_input.length())
  172. return {};
  173. return m_input[m_cursor + offset];
  174. }
  175. Optional<HTMLToken> HTMLTokenizer::next_token()
  176. {
  177. _StartOfFunction:
  178. if (!m_queued_tokens.is_empty())
  179. return m_queued_tokens.dequeue();
  180. for (;;) {
  181. auto current_input_character = next_codepoint();
  182. switch (m_state) {
  183. BEGIN_STATE(Data)
  184. {
  185. ON('&')
  186. {
  187. m_return_state = State::Data;
  188. SWITCH_TO(CharacterReference);
  189. }
  190. ON('<')
  191. {
  192. SWITCH_TO(TagOpen);
  193. }
  194. ON_EOF
  195. {
  196. EMIT_EOF;
  197. }
  198. ANYTHING_ELSE
  199. {
  200. EMIT_CURRENT_CHARACTER;
  201. }
  202. }
  203. END_STATE
  204. BEGIN_STATE(TagOpen)
  205. {
  206. ON('!')
  207. {
  208. SWITCH_TO(MarkupDeclarationOpen);
  209. }
  210. ON('/')
  211. {
  212. SWITCH_TO(EndTagOpen);
  213. }
  214. ON_ASCII_ALPHA
  215. {
  216. create_new_token(HTMLToken::Type::StartTag);
  217. RECONSUME_IN(TagName);
  218. }
  219. ON('?')
  220. {
  221. TODO();
  222. }
  223. ANYTHING_ELSE
  224. {
  225. TODO();
  226. }
  227. }
  228. END_STATE
  229. BEGIN_STATE(TagName)
  230. {
  231. ON_WHITESPACE
  232. {
  233. SWITCH_TO(BeforeAttributeName);
  234. }
  235. ON('/')
  236. {
  237. SWITCH_TO(SelfClosingStartTag);
  238. }
  239. ON('>')
  240. {
  241. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  242. }
  243. ANYTHING_ELSE
  244. {
  245. m_current_token.m_tag.tag_name.append(current_input_character.value());
  246. continue;
  247. }
  248. }
  249. END_STATE
  250. BEGIN_STATE(EndTagOpen)
  251. {
  252. ON_ASCII_ALPHA
  253. {
  254. create_new_token(HTMLToken::Type::EndTag);
  255. RECONSUME_IN(TagName);
  256. }
  257. }
  258. END_STATE
  259. BEGIN_STATE(MarkupDeclarationOpen)
  260. {
  261. DONT_CONSUME_NEXT_INPUT_CHARACTER;
  262. if (consume_next_if_match("--")) {
  263. create_new_token(HTMLToken::Type::Comment);
  264. SWITCH_TO(CommentStart);
  265. }
  266. if (consume_next_if_match("DOCTYPE", CaseSensitivity::CaseInsensitive)) {
  267. SWITCH_TO(DOCTYPE);
  268. }
  269. }
  270. END_STATE
  271. BEGIN_STATE(DOCTYPE)
  272. {
  273. ON_WHITESPACE
  274. {
  275. SWITCH_TO(BeforeDOCTYPEName);
  276. }
  277. ON('>')
  278. {
  279. RECONSUME_IN(BeforeDOCTYPEName);
  280. }
  281. ON_EOF
  282. {
  283. TODO();
  284. }
  285. ANYTHING_ELSE
  286. {
  287. TODO();
  288. }
  289. }
  290. END_STATE
  291. BEGIN_STATE(BeforeDOCTYPEName)
  292. {
  293. ON_WHITESPACE
  294. {
  295. continue;
  296. }
  297. ON_ASCII_UPPER_ALPHA
  298. {
  299. create_new_token(HTMLToken::Type::DOCTYPE);
  300. m_current_token.m_doctype.name.append(tolower(current_input_character.value()));
  301. SWITCH_TO(DOCTYPEName);
  302. }
  303. ON(0)
  304. {
  305. TODO();
  306. }
  307. ON('>')
  308. {
  309. TODO();
  310. }
  311. ON_EOF
  312. {
  313. TODO();
  314. }
  315. ANYTHING_ELSE
  316. {
  317. create_new_token(HTMLToken::Type::DOCTYPE);
  318. m_current_token.m_doctype.name.append(current_input_character.value());
  319. SWITCH_TO(DOCTYPEName);
  320. }
  321. }
  322. END_STATE
  323. BEGIN_STATE(DOCTYPEName)
  324. {
  325. ON_WHITESPACE
  326. {
  327. SWITCH_TO(AfterDOCTYPEName);
  328. }
  329. ON('>')
  330. {
  331. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  332. }
  333. ON_ASCII_UPPER_ALPHA
  334. {
  335. m_current_token.m_doctype.name.append(tolower(current_input_character.value()));
  336. }
  337. ON(0)
  338. {
  339. TODO();
  340. }
  341. ON_EOF
  342. {
  343. TODO();
  344. }
  345. ANYTHING_ELSE
  346. {
  347. m_current_token.m_doctype.name.append(current_input_character.value());
  348. continue;
  349. }
  350. }
  351. END_STATE
  352. BEGIN_STATE(AfterDOCTYPEName)
  353. {
  354. ON_WHITESPACE
  355. {
  356. continue;
  357. }
  358. ON('>')
  359. {
  360. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  361. }
  362. ON_EOF
  363. {
  364. TODO();
  365. }
  366. ANYTHING_ELSE
  367. {
  368. if (toupper(current_input_character.value()) == 'P' && consume_next_if_match("UBLIC", CaseSensitivity::CaseInsensitive)) {
  369. SWITCH_TO(AfterDOCTYPEPublicKeyword);
  370. }
  371. if (toupper(current_input_character.value()) == 'S' && consume_next_if_match("YSTEM", CaseSensitivity::CaseInsensitive)) {
  372. SWITCH_TO(AfterDOCTYPESystemKeyword);
  373. }
  374. TODO();
  375. }
  376. }
  377. END_STATE
  378. BEGIN_STATE(AfterDOCTYPEPublicKeyword)
  379. {
  380. ON_WHITESPACE
  381. {
  382. SWITCH_TO(BeforeDOCTYPEPublicIdentifier);
  383. }
  384. ON('"')
  385. {
  386. TODO();
  387. }
  388. ON('\'')
  389. {
  390. TODO();
  391. }
  392. ON('>')
  393. {
  394. TODO();
  395. }
  396. ON_EOF
  397. {
  398. TODO();
  399. }
  400. ANYTHING_ELSE
  401. {
  402. TODO();
  403. }
  404. }
  405. END_STATE
  406. BEGIN_STATE(AfterDOCTYPESystemKeyword)
  407. {
  408. ON_WHITESPACE
  409. {
  410. SWITCH_TO(BeforeDOCTYPESystemIdentifier);
  411. }
  412. ON('"')
  413. {
  414. TODO();
  415. }
  416. ON('\'')
  417. {
  418. TODO();
  419. }
  420. ON('>')
  421. {
  422. TODO();
  423. }
  424. ON_EOF
  425. {
  426. TODO();
  427. }
  428. ANYTHING_ELSE
  429. {
  430. TODO();
  431. }
  432. }
  433. END_STATE
  434. BEGIN_STATE(BeforeDOCTYPEPublicIdentifier)
  435. {
  436. ON_WHITESPACE
  437. {
  438. continue;
  439. }
  440. ON('"')
  441. {
  442. m_current_token.m_doctype.public_identifier.clear();
  443. SWITCH_TO(DOCTYPEPublicIdentifierDoubleQuoted);
  444. }
  445. ON('\'')
  446. {
  447. m_current_token.m_doctype.public_identifier.clear();
  448. SWITCH_TO(DOCTYPEPublicIdentifierSingleQuoted);
  449. }
  450. ON('>')
  451. {
  452. TODO();
  453. }
  454. ON_EOF
  455. {
  456. TODO();
  457. }
  458. ANYTHING_ELSE
  459. {
  460. TODO();
  461. }
  462. }
  463. END_STATE
  464. BEGIN_STATE(BeforeDOCTYPESystemIdentifier)
  465. {
  466. ON_WHITESPACE
  467. {
  468. continue;
  469. }
  470. ON('"')
  471. {
  472. m_current_token.m_doctype.system_identifier.clear();
  473. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  474. }
  475. ON('\'')
  476. {
  477. m_current_token.m_doctype.system_identifier.clear();
  478. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  479. }
  480. ON('>')
  481. {
  482. TODO();
  483. }
  484. ON_EOF
  485. {
  486. TODO();
  487. }
  488. ANYTHING_ELSE
  489. {
  490. TODO();
  491. }
  492. }
  493. END_STATE
  494. BEGIN_STATE(DOCTYPEPublicIdentifierDoubleQuoted)
  495. {
  496. ON('"')
  497. {
  498. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  499. }
  500. ON(0)
  501. {
  502. TODO();
  503. }
  504. ON('>')
  505. {
  506. TODO();
  507. }
  508. ON_EOF
  509. {
  510. TODO();
  511. }
  512. ANYTHING_ELSE
  513. {
  514. m_current_token.m_doctype.public_identifier.append(current_input_character.value());
  515. continue;
  516. }
  517. }
  518. END_STATE
  519. BEGIN_STATE(DOCTYPEPublicIdentifierSingleQuoted)
  520. {
  521. ON('\'')
  522. {
  523. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  524. }
  525. ON(0)
  526. {
  527. TODO();
  528. }
  529. ON('>')
  530. {
  531. TODO();
  532. }
  533. ON_EOF
  534. {
  535. TODO();
  536. }
  537. ANYTHING_ELSE
  538. {
  539. m_current_token.m_doctype.public_identifier.append(current_input_character.value());
  540. continue;
  541. }
  542. }
  543. END_STATE
  544. BEGIN_STATE(DOCTYPESystemIdentifierDoubleQuoted)
  545. {
  546. ON('"')
  547. {
  548. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  549. }
  550. ON(0)
  551. {
  552. TODO();
  553. }
  554. ON('>')
  555. {
  556. TODO();
  557. }
  558. ON_EOF
  559. {
  560. TODO();
  561. }
  562. ANYTHING_ELSE
  563. {
  564. m_current_token.m_doctype.system_identifier.append(current_input_character.value());
  565. continue;
  566. }
  567. }
  568. END_STATE
  569. BEGIN_STATE(DOCTYPESystemIdentifierSingleQuoted)
  570. {
  571. ON('\'')
  572. {
  573. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  574. }
  575. ON(0)
  576. {
  577. TODO();
  578. }
  579. ON('>')
  580. {
  581. TODO();
  582. }
  583. ON_EOF
  584. {
  585. TODO();
  586. }
  587. ANYTHING_ELSE
  588. {
  589. m_current_token.m_doctype.system_identifier.append(current_input_character.value());
  590. continue;
  591. }
  592. }
  593. END_STATE
  594. BEGIN_STATE(AfterDOCTYPEPublicIdentifier)
  595. {
  596. ON_WHITESPACE
  597. {
  598. SWITCH_TO(BetweenDOCTYPEPublicAndSystemIdentifiers);
  599. }
  600. ON('>')
  601. {
  602. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  603. }
  604. ON('"')
  605. {
  606. TODO();
  607. }
  608. ON('\'')
  609. {
  610. TODO();
  611. }
  612. ON_EOF
  613. {
  614. TODO();
  615. }
  616. ANYTHING_ELSE
  617. {
  618. TODO();
  619. }
  620. }
  621. END_STATE
  622. BEGIN_STATE(BetweenDOCTYPEPublicAndSystemIdentifiers)
  623. {
  624. ON_WHITESPACE
  625. {
  626. continue;
  627. }
  628. ON('>')
  629. {
  630. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  631. }
  632. ON('"')
  633. {
  634. m_current_token.m_doctype.system_identifier.clear();
  635. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  636. }
  637. ON('\'')
  638. {
  639. m_current_token.m_doctype.system_identifier.clear();
  640. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  641. }
  642. ON_EOF
  643. {
  644. TODO();
  645. }
  646. ANYTHING_ELSE
  647. {
  648. TODO();
  649. }
  650. }
  651. END_STATE
  652. BEGIN_STATE(AfterDOCTYPESystemIdentifier)
  653. {
  654. ON_WHITESPACE
  655. {
  656. continue;
  657. }
  658. ON('>')
  659. {
  660. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  661. }
  662. ON_EOF
  663. {
  664. TODO();
  665. }
  666. ANYTHING_ELSE
  667. {
  668. TODO();
  669. }
  670. }
  671. END_STATE
  672. BEGIN_STATE(BeforeAttributeName)
  673. {
  674. ON_WHITESPACE
  675. {
  676. continue;
  677. }
  678. ON('/')
  679. {
  680. RECONSUME_IN(AfterAttributeName);
  681. }
  682. ON('>')
  683. {
  684. RECONSUME_IN(AfterAttributeName);
  685. }
  686. ON_EOF
  687. {
  688. RECONSUME_IN(AfterAttributeName);
  689. }
  690. ON('=')
  691. {
  692. TODO();
  693. }
  694. ANYTHING_ELSE
  695. {
  696. m_current_token.m_tag.attributes.append(HTMLToken::AttributeBuilder());
  697. RECONSUME_IN(AttributeName);
  698. }
  699. }
  700. END_STATE
  701. BEGIN_STATE(SelfClosingStartTag)
  702. {
  703. ON('>')
  704. {
  705. m_current_token.m_tag.self_closing = true;
  706. SWITCH_TO(Data);
  707. }
  708. ON_EOF
  709. {
  710. TODO();
  711. }
  712. ANYTHING_ELSE
  713. {
  714. TODO();
  715. }
  716. }
  717. END_STATE
  718. BEGIN_STATE(AttributeName)
  719. {
  720. ON_WHITESPACE
  721. {
  722. RECONSUME_IN(AfterAttributeName);
  723. }
  724. ON('/')
  725. {
  726. RECONSUME_IN(AfterAttributeName);
  727. }
  728. ON('>')
  729. {
  730. RECONSUME_IN(AfterAttributeName);
  731. }
  732. ON_EOF
  733. {
  734. RECONSUME_IN(AfterAttributeName);
  735. }
  736. ON('=')
  737. {
  738. SWITCH_TO(BeforeAttributeValue);
  739. }
  740. ANYTHING_ELSE
  741. {
  742. m_current_token.m_tag.attributes.last().name_builder.append(current_input_character.value());
  743. continue;
  744. }
  745. }
  746. END_STATE
  747. BEGIN_STATE(AfterAttributeName)
  748. {
  749. }
  750. END_STATE
  751. BEGIN_STATE(BeforeAttributeValue)
  752. {
  753. ON_WHITESPACE
  754. {
  755. continue;
  756. }
  757. ON('"')
  758. {
  759. SWITCH_TO(AttributeValueDoubleQuoted);
  760. }
  761. ON('\'')
  762. {
  763. SWITCH_TO(AttributeValueSingleQuoted);
  764. }
  765. ON('>')
  766. {
  767. TODO();
  768. }
  769. ANYTHING_ELSE
  770. {
  771. RECONSUME_IN(AttributeValueUnquoted);
  772. }
  773. }
  774. END_STATE
  775. BEGIN_STATE(AttributeValueDoubleQuoted)
  776. {
  777. ON('"')
  778. {
  779. SWITCH_TO(AfterAttributeValueQuoted);
  780. }
  781. ON('&')
  782. {
  783. m_return_state = State::AttributeValueDoubleQuoted;
  784. SWITCH_TO(CharacterReference);
  785. }
  786. ON(0)
  787. {
  788. TODO();
  789. }
  790. ON_EOF
  791. {
  792. TODO();
  793. }
  794. ANYTHING_ELSE
  795. {
  796. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  797. continue;
  798. }
  799. }
  800. END_STATE
  801. BEGIN_STATE(AttributeValueSingleQuoted)
  802. {
  803. ON('\'')
  804. {
  805. SWITCH_TO(AfterAttributeValueQuoted);
  806. }
  807. ON('&')
  808. {
  809. m_return_state = State::AttributeValueSingleQuoted;
  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(AttributeValueUnquoted)
  828. {
  829. ON_WHITESPACE
  830. {
  831. SWITCH_TO(BeforeAttributeName);
  832. }
  833. ON('&')
  834. {
  835. m_return_state = State::AttributeValueUnquoted;
  836. SWITCH_TO(CharacterReference);
  837. }
  838. ON('>')
  839. {
  840. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  841. }
  842. ON(0)
  843. {
  844. TODO();
  845. }
  846. ON_EOF
  847. {
  848. TODO();
  849. }
  850. ANYTHING_ELSE
  851. {
  852. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  853. continue;
  854. }
  855. }
  856. END_STATE
  857. BEGIN_STATE(AfterAttributeValueQuoted)
  858. {
  859. ON_WHITESPACE
  860. {
  861. SWITCH_TO(BeforeAttributeName);
  862. }
  863. ON('/')
  864. {
  865. SWITCH_TO(SelfClosingStartTag);
  866. }
  867. ON('>')
  868. {
  869. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  870. }
  871. ON_EOF
  872. {
  873. TODO();
  874. }
  875. ANYTHING_ELSE
  876. {
  877. TODO();
  878. }
  879. }
  880. END_STATE
  881. BEGIN_STATE(CommentStart)
  882. {
  883. ON('-')
  884. {
  885. SWITCH_TO(CommentStartDash);
  886. }
  887. ON('>')
  888. {
  889. TODO();
  890. }
  891. ANYTHING_ELSE
  892. {
  893. RECONSUME_IN(Comment);
  894. }
  895. }
  896. END_STATE
  897. BEGIN_STATE(CommentStartDash)
  898. {
  899. ON('-')
  900. {
  901. SWITCH_TO(CommentEnd);
  902. }
  903. ON('>')
  904. {
  905. TODO();
  906. }
  907. ON_EOF
  908. {
  909. TODO();
  910. }
  911. ANYTHING_ELSE
  912. {
  913. m_current_token.m_comment_or_character.data.append('-');
  914. RECONSUME_IN(Comment);
  915. }
  916. }
  917. END_STATE
  918. BEGIN_STATE(Comment)
  919. {
  920. ON('<')
  921. {
  922. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  923. SWITCH_TO(CommentLessThanSign);
  924. }
  925. ON('-')
  926. {
  927. SWITCH_TO(CommentEndDash);
  928. }
  929. ON(0)
  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(current_input_character.value());
  940. continue;
  941. }
  942. }
  943. END_STATE
  944. BEGIN_STATE(CommentEnd)
  945. {
  946. ON('>')
  947. {
  948. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  949. }
  950. ON('!')
  951. {
  952. SWITCH_TO(CommentEndBang);
  953. }
  954. ON('-')
  955. {
  956. m_current_token.m_comment_or_character.data.append('-');
  957. continue;
  958. }
  959. ON_EOF
  960. {
  961. TODO();
  962. }
  963. ANYTHING_ELSE
  964. {
  965. m_current_token.m_comment_or_character.data.append('-');
  966. RECONSUME_IN(Comment);
  967. }
  968. }
  969. END_STATE
  970. BEGIN_STATE(CommentEndBang)
  971. {
  972. ON('-')
  973. {
  974. m_current_token.m_comment_or_character.data.append("--!");
  975. SWITCH_TO(CommentEndDash);
  976. }
  977. ON('>')
  978. {
  979. TODO();
  980. }
  981. ON_EOF
  982. {
  983. TODO();
  984. }
  985. ANYTHING_ELSE
  986. {
  987. m_current_token.m_comment_or_character.data.append("--!");
  988. RECONSUME_IN(Comment);
  989. }
  990. }
  991. END_STATE
  992. BEGIN_STATE(CommentEndDash)
  993. {
  994. ON('-')
  995. {
  996. SWITCH_TO(CommentEnd);
  997. }
  998. ON_EOF
  999. {
  1000. TODO();
  1001. }
  1002. ANYTHING_ELSE
  1003. {
  1004. m_current_token.m_comment_or_character.data.append('-');
  1005. RECONSUME_IN(Comment);
  1006. }
  1007. }
  1008. END_STATE
  1009. BEGIN_STATE(CommentLessThanSign)
  1010. {
  1011. ON('!')
  1012. {
  1013. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  1014. SWITCH_TO(CommentLessThanSignBang);
  1015. }
  1016. ON('<')
  1017. {
  1018. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  1019. continue;
  1020. }
  1021. ANYTHING_ELSE
  1022. {
  1023. RECONSUME_IN(Comment);
  1024. }
  1025. }
  1026. END_STATE
  1027. BEGIN_STATE(CommentLessThanSignBang)
  1028. {
  1029. ON('-')
  1030. {
  1031. SWITCH_TO(CommentLessThanSignBangDash);
  1032. }
  1033. ANYTHING_ELSE
  1034. {
  1035. RECONSUME_IN(Comment);
  1036. }
  1037. }
  1038. END_STATE
  1039. BEGIN_STATE(CommentLessThanSignBangDash)
  1040. {
  1041. ON('-')
  1042. {
  1043. SWITCH_TO(CommentLessThanSignBangDashDash);
  1044. }
  1045. ANYTHING_ELSE
  1046. {
  1047. RECONSUME_IN(Comment);
  1048. }
  1049. }
  1050. END_STATE
  1051. BEGIN_STATE(CommentLessThanSignBangDashDash)
  1052. {
  1053. ON('>')
  1054. {
  1055. SWITCH_TO(CommentEnd);
  1056. }
  1057. ANYTHING_ELSE
  1058. {
  1059. TODO();
  1060. }
  1061. }
  1062. END_STATE
  1063. BEGIN_STATE(CharacterReference)
  1064. {
  1065. m_temporary_buffer.clear();
  1066. m_temporary_buffer.append('&');
  1067. ON_ASCII_ALPHANUMERIC
  1068. {
  1069. RECONSUME_IN(NamedCharacterReference);
  1070. }
  1071. ON('#')
  1072. {
  1073. m_temporary_buffer.append(current_input_character.value());
  1074. SWITCH_TO(NumericCharacterReference);
  1075. }
  1076. ANYTHING_ELSE
  1077. {
  1078. RECONSUME_IN_RETURN_STATE;
  1079. }
  1080. }
  1081. END_STATE
  1082. BEGIN_STATE(NamedCharacterReference)
  1083. {
  1084. // FIXME:This is not the right way to implement this state!!
  1085. ON(';')
  1086. {
  1087. SWITCH_TO_RETURN_STATE;
  1088. }
  1089. ANYTHING_ELSE
  1090. {
  1091. dbg() << "NamedCharacterReference: '" << (char)current_input_character.value() << "'";
  1092. continue;
  1093. }
  1094. }
  1095. END_STATE
  1096. BEGIN_STATE(NumericCharacterReference)
  1097. {
  1098. m_character_reference_code = 0;
  1099. ON('X')
  1100. {
  1101. m_temporary_buffer.append(current_input_character.value());
  1102. SWITCH_TO(HexadecimalCharacterReferenceStart);
  1103. }
  1104. ON('x')
  1105. {
  1106. m_temporary_buffer.append(current_input_character.value());
  1107. SWITCH_TO(HexadecimalCharacterReferenceStart);
  1108. }
  1109. ANYTHING_ELSE
  1110. {
  1111. RECONSUME_IN(DecimalCharacterReferenceStart);
  1112. }
  1113. }
  1114. END_STATE
  1115. BEGIN_STATE(HexadecimalCharacterReferenceStart)
  1116. {
  1117. ON_ASCII_HEX_DIGIT
  1118. {
  1119. RECONSUME_IN(HexadecimalCharacterReference);
  1120. }
  1121. ANYTHING_ELSE
  1122. {
  1123. TODO();
  1124. }
  1125. }
  1126. END_STATE
  1127. BEGIN_STATE(DecimalCharacterReferenceStart)
  1128. {
  1129. ON_ASCII_DIGIT
  1130. {
  1131. RECONSUME_IN(DecimalCharacterReference);
  1132. }
  1133. ANYTHING_ELSE
  1134. {
  1135. TODO();
  1136. }
  1137. }
  1138. END_STATE
  1139. BEGIN_STATE(HexadecimalCharacterReference)
  1140. {
  1141. ON_ASCII_DIGIT
  1142. {
  1143. m_character_reference_code *= 16;
  1144. m_character_reference_code += current_input_character.value() - 0x30;
  1145. continue;
  1146. }
  1147. ON_ASCII_UPPER_ALPHA
  1148. {
  1149. m_character_reference_code *= 16;
  1150. m_character_reference_code += current_input_character.value() - 0x37;
  1151. continue;
  1152. }
  1153. ON_ASCII_LOWER_ALPHA
  1154. {
  1155. m_character_reference_code *= 16;
  1156. m_character_reference_code += current_input_character.value() - 0x57;
  1157. continue;
  1158. }
  1159. ON(';')
  1160. {
  1161. SWITCH_TO(NumericCharacterReferenceEnd);
  1162. }
  1163. ANYTHING_ELSE
  1164. {
  1165. TODO();
  1166. }
  1167. }
  1168. END_STATE
  1169. BEGIN_STATE(DecimalCharacterReference)
  1170. {
  1171. ON_ASCII_DIGIT
  1172. {
  1173. m_character_reference_code *= 10;
  1174. m_character_reference_code += current_input_character.value() - 0x30;
  1175. continue;
  1176. }
  1177. ON(';')
  1178. {
  1179. SWITCH_TO(NumericCharacterReferenceEnd);
  1180. }
  1181. ANYTHING_ELSE
  1182. {
  1183. TODO();
  1184. }
  1185. }
  1186. END_STATE
  1187. BEGIN_STATE(NumericCharacterReferenceEnd)
  1188. {
  1189. if (m_character_reference_code == 0) {
  1190. TODO();
  1191. }
  1192. if (m_character_reference_code > 0x10ffff) {
  1193. TODO();
  1194. }
  1195. if (is_surrogate(m_character_reference_code)) {
  1196. TODO();
  1197. }
  1198. if (is_noncharacter(m_character_reference_code)) {
  1199. TODO();
  1200. }
  1201. if (m_character_reference_code == 0xd || (is_control(m_character_reference_code) && !isspace(m_character_reference_code))) {
  1202. TODO();
  1203. }
  1204. if (is_control(m_character_reference_code)) {
  1205. constexpr struct {
  1206. u32 number;
  1207. u32 codepoint;
  1208. } conversion_table[] = {
  1209. { 0x80, 0x20AC },
  1210. { 0x82, 0x201A },
  1211. { 0x83, 0x0192 },
  1212. { 0x84, 0x201E },
  1213. { 0x85, 0x2026 },
  1214. { 0x86, 0x2020 },
  1215. { 0x87, 0x2021 },
  1216. { 0x88, 0x02C6 },
  1217. { 0x89, 0x2030 },
  1218. { 0x8A, 0x0160 },
  1219. { 0x8B, 0x2039 },
  1220. { 0x8C, 0x0152 },
  1221. { 0x8E, 0x017D },
  1222. { 0x91, 0x2018 },
  1223. { 0x92, 0x2019 },
  1224. { 0x93, 0x201C },
  1225. { 0x94, 0x201D },
  1226. { 0x95, 0x2022 },
  1227. { 0x96, 0x2013 },
  1228. { 0x97, 0x2014 },
  1229. { 0x98, 0x02DC },
  1230. { 0x99, 0x2122 },
  1231. { 0x9A, 0x0161 },
  1232. { 0x9B, 0x203A },
  1233. { 0x9C, 0x0153 },
  1234. { 0x9E, 0x017E },
  1235. { 0x9F, 0x0178 },
  1236. };
  1237. for (auto& entry : conversion_table) {
  1238. if (m_character_reference_code == entry.number) {
  1239. m_character_reference_code = entry.codepoint;
  1240. break;
  1241. }
  1242. }
  1243. }
  1244. m_temporary_buffer.clear();
  1245. m_temporary_buffer.append(m_character_reference_code);
  1246. FLUSH_CODEPOINTS_CONSUMED_AS_A_CHARACTER_REFERENCE;
  1247. SWITCH_TO_RETURN_STATE;
  1248. }
  1249. END_STATE
  1250. BEGIN_STATE(RCDATA)
  1251. {
  1252. ON('&')
  1253. {
  1254. m_return_state = State::RCDATA;
  1255. SWITCH_TO(CharacterReference);
  1256. }
  1257. ON('<')
  1258. {
  1259. SWITCH_TO(RCDATALessThanSign);
  1260. }
  1261. ON(0)
  1262. {
  1263. TODO();
  1264. }
  1265. ON_EOF
  1266. {
  1267. EMIT_EOF;
  1268. }
  1269. ANYTHING_ELSE
  1270. {
  1271. EMIT_CURRENT_CHARACTER;
  1272. }
  1273. }
  1274. END_STATE
  1275. BEGIN_STATE(RCDATALessThanSign)
  1276. {
  1277. ON('/')
  1278. {
  1279. m_temporary_buffer.clear();
  1280. SWITCH_TO(RCDATAEndTagOpen);
  1281. }
  1282. ANYTHING_ELSE
  1283. {
  1284. EMIT_CHARACTER('<');
  1285. RECONSUME_IN(RCDATA);
  1286. }
  1287. }
  1288. END_STATE
  1289. BEGIN_STATE(RCDATAEndTagOpen)
  1290. {
  1291. ON_ASCII_ALPHA
  1292. {
  1293. create_new_token(HTMLToken::Type::EndTag);
  1294. RECONSUME_IN(RCDATAEndTagName);
  1295. }
  1296. ANYTHING_ELSE
  1297. {
  1298. // FIXME: Emit a U+003C LESS-THAN SIGN character token and a U+002F SOLIDUS character token. Reconsume in the RCDATA state.
  1299. TODO();
  1300. }
  1301. }
  1302. END_STATE
  1303. BEGIN_STATE(RCDATAEndTagName)
  1304. {
  1305. ON_WHITESPACE
  1306. {
  1307. TODO();
  1308. }
  1309. ON('/')
  1310. {
  1311. TODO();
  1312. }
  1313. ON('>')
  1314. {
  1315. if (!current_end_tag_token_is_appropriate()) {
  1316. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1317. TODO();
  1318. }
  1319. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1320. }
  1321. ON_ASCII_UPPER_ALPHA
  1322. {
  1323. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1324. m_temporary_buffer.append(current_input_character.value());
  1325. continue;
  1326. }
  1327. ON_ASCII_LOWER_ALPHA
  1328. {
  1329. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1330. m_temporary_buffer.append(current_input_character.value());
  1331. continue;
  1332. }
  1333. ANYTHING_ELSE
  1334. {
  1335. TODO();
  1336. }
  1337. }
  1338. END_STATE
  1339. BEGIN_STATE(RAWTEXT)
  1340. {
  1341. ON('<')
  1342. {
  1343. SWITCH_TO(RAWTEXTLessThanSign);
  1344. }
  1345. ON(0)
  1346. {
  1347. TODO();
  1348. }
  1349. ON_EOF
  1350. {
  1351. EMIT_EOF;
  1352. }
  1353. ANYTHING_ELSE
  1354. {
  1355. EMIT_CURRENT_CHARACTER;
  1356. }
  1357. }
  1358. END_STATE
  1359. BEGIN_STATE(RAWTEXTLessThanSign)
  1360. {
  1361. ON('/')
  1362. {
  1363. m_temporary_buffer.clear();
  1364. SWITCH_TO(RAWTEXTEndTagOpen);
  1365. }
  1366. ANYTHING_ELSE
  1367. {
  1368. EMIT_CHARACTER('<');
  1369. RECONSUME_IN(RAWTEXT);
  1370. }
  1371. }
  1372. END_STATE
  1373. BEGIN_STATE(RAWTEXTEndTagOpen)
  1374. {
  1375. ON_ASCII_ALPHA
  1376. {
  1377. create_new_token(HTMLToken::Type::EndTag);
  1378. RECONSUME_IN(RAWTEXTEndTagName);
  1379. }
  1380. ANYTHING_ELSE
  1381. {
  1382. // FIXME: Emit a U+003C LESS-THAN SIGN character token and a U+002F SOLIDUS character token. Reconsume in the RAWTEXT state.
  1383. TODO();
  1384. }
  1385. }
  1386. END_STATE
  1387. BEGIN_STATE(RAWTEXTEndTagName)
  1388. {
  1389. ON_WHITESPACE
  1390. {
  1391. TODO();
  1392. }
  1393. ON('/')
  1394. {
  1395. TODO();
  1396. }
  1397. ON('>')
  1398. {
  1399. if (!current_end_tag_token_is_appropriate()) {
  1400. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1401. TODO();
  1402. }
  1403. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1404. }
  1405. ON_ASCII_UPPER_ALPHA
  1406. {
  1407. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1408. m_temporary_buffer.append(current_input_character.value());
  1409. continue;
  1410. }
  1411. ON_ASCII_LOWER_ALPHA
  1412. {
  1413. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1414. m_temporary_buffer.append(current_input_character.value());
  1415. continue;
  1416. }
  1417. ANYTHING_ELSE
  1418. {
  1419. TODO();
  1420. }
  1421. }
  1422. END_STATE
  1423. BEGIN_STATE(ScriptData)
  1424. {
  1425. ON('<')
  1426. {
  1427. SWITCH_TO(ScriptDataLessThanSign);
  1428. }
  1429. ON(0)
  1430. {
  1431. TODO();
  1432. }
  1433. ON_EOF
  1434. {
  1435. EMIT_EOF;
  1436. }
  1437. ANYTHING_ELSE
  1438. {
  1439. EMIT_CURRENT_CHARACTER;
  1440. }
  1441. }
  1442. END_STATE
  1443. BEGIN_STATE(ScriptDataLessThanSign)
  1444. {
  1445. ON('/')
  1446. {
  1447. m_temporary_buffer.clear();
  1448. SWITCH_TO(ScriptDataEndTagOpen);
  1449. }
  1450. ON('!')
  1451. {
  1452. TODO();
  1453. }
  1454. ANYTHING_ELSE
  1455. {
  1456. EMIT_CHARACTER_AND_RECONSUME_IN('<', ScriptData);
  1457. }
  1458. }
  1459. END_STATE
  1460. BEGIN_STATE(ScriptDataEndTagOpen)
  1461. {
  1462. ON_ASCII_ALPHA
  1463. {
  1464. create_new_token(HTMLToken::Type::EndTag);
  1465. RECONSUME_IN(ScriptDataEndTagName);
  1466. }
  1467. ANYTHING_ELSE
  1468. {
  1469. TODO();
  1470. }
  1471. }
  1472. END_STATE
  1473. BEGIN_STATE(ScriptDataEndTagName)
  1474. {
  1475. ON_WHITESPACE
  1476. {
  1477. if (current_end_tag_token_is_appropriate())
  1478. SWITCH_TO(BeforeAttributeName);
  1479. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1480. TODO();
  1481. }
  1482. ON('/')
  1483. {
  1484. if (current_end_tag_token_is_appropriate())
  1485. SWITCH_TO(SelfClosingStartTag);
  1486. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1487. TODO();
  1488. }
  1489. ON('>')
  1490. {
  1491. if (current_end_tag_token_is_appropriate())
  1492. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1493. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1494. TODO();
  1495. }
  1496. ON_ASCII_UPPER_ALPHA
  1497. {
  1498. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1499. m_temporary_buffer.append(current_input_character.value());
  1500. continue;
  1501. }
  1502. ON_ASCII_LOWER_ALPHA
  1503. {
  1504. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1505. m_temporary_buffer.append(current_input_character.value());
  1506. continue;
  1507. }
  1508. ANYTHING_ELSE
  1509. {
  1510. TODO();
  1511. }
  1512. }
  1513. END_STATE
  1514. default:
  1515. TODO();
  1516. }
  1517. }
  1518. }
  1519. bool HTMLTokenizer::consume_next_if_match(const StringView& string, CaseSensitivity case_sensitivity)
  1520. {
  1521. for (size_t i = 0; i < string.length(); ++i) {
  1522. auto codepoint = peek_codepoint(i);
  1523. if (!codepoint.has_value())
  1524. return false;
  1525. // FIXME: This should be more Unicode-aware.
  1526. if (case_sensitivity == CaseSensitivity::CaseInsensitive) {
  1527. if (codepoint.value() < 0x80) {
  1528. if (tolower(codepoint.value()) != tolower(string[i]))
  1529. return false;
  1530. continue;
  1531. }
  1532. }
  1533. if (codepoint.value() != (u32)string[i])
  1534. return false;
  1535. }
  1536. m_cursor += string.length();
  1537. return true;
  1538. }
  1539. void HTMLTokenizer::create_new_token(HTMLToken::Type type)
  1540. {
  1541. m_current_token = {};
  1542. m_current_token.m_type = type;
  1543. }
  1544. HTMLTokenizer::HTMLTokenizer(const StringView& input)
  1545. : m_input(input)
  1546. {
  1547. }
  1548. void HTMLTokenizer::will_switch_to([[maybe_unused]] State new_state)
  1549. {
  1550. #ifdef TOKENIZER_TRACE
  1551. dbg() << "[" << state_name(m_state) << "] Switch to " << state_name(new_state);
  1552. #endif
  1553. }
  1554. void HTMLTokenizer::will_reconsume_in([[maybe_unused]] State new_state)
  1555. {
  1556. #ifdef TOKENIZER_TRACE
  1557. dbg() << "[" << state_name(m_state) << "] Reconsume in " << state_name(new_state);
  1558. #endif
  1559. }
  1560. void HTMLTokenizer::switch_to(Badge<HTMLDocumentParser>, State new_state)
  1561. {
  1562. #ifdef TOKENIZER_TRACE
  1563. dbg() << "[" << state_name(m_state) << "] Parser switches tokenizer state to " << state_name(new_state);
  1564. #endif
  1565. m_state = new_state;
  1566. }
  1567. void HTMLTokenizer::will_emit(HTMLToken& token)
  1568. {
  1569. if (token.is_start_tag())
  1570. m_last_emitted_start_tag = token;
  1571. }
  1572. bool HTMLTokenizer::current_end_tag_token_is_appropriate() const
  1573. {
  1574. ASSERT(m_current_token.is_end_tag());
  1575. if (!m_last_emitted_start_tag.is_start_tag())
  1576. return false;
  1577. return m_current_token.tag_name() == m_last_emitted_start_tag.tag_name();
  1578. }
  1579. bool HTMLTokenizer::consumed_as_part_of_an_attribute() const
  1580. {
  1581. return m_return_state == State::AttributeValueUnquoted || m_return_state == State::AttributeValueSingleQuoted || m_return_state == State::AttributeValueDoubleQuoted;
  1582. }
  1583. }