HTMLTokenizer.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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 TODO() \
  32. do { \
  33. dbg() << "[TODO: " << state_name(m_state) << "] '" << (char)current_input_character.value() << "'"; \
  34. ASSERT_NOT_REACHED(); \
  35. } while (0)
  36. #define SWITCH_TO(new_state) \
  37. do { \
  38. will_switch_to(State::new_state); \
  39. m_state = State::new_state; \
  40. current_input_character = next_codepoint(); \
  41. goto new_state; \
  42. } while (0)
  43. #define RECONSUME_IN(new_state) \
  44. do { \
  45. will_reconsume_in(State::new_state); \
  46. m_state = State::new_state; \
  47. goto new_state; \
  48. } while (0)
  49. #define SWITCH_TO_AND_EMIT_CURRENT_TOKEN(new_state) \
  50. do { \
  51. will_switch_to(State::new_state); \
  52. m_state = State::new_state; \
  53. will_emit(m_current_token); \
  54. return m_current_token; \
  55. } while (0)
  56. #define DONT_CONSUME_NEXT_INPUT_CHARACTER --m_cursor;
  57. #define ON(codepoint) \
  58. if (current_input_character.has_value() && current_input_character.value() == codepoint)
  59. #define ON_EOF \
  60. if (!current_input_character.has_value())
  61. #define ON_ASCII_ALPHA \
  62. if (current_input_character.has_value() && isalpha(current_input_character.value()))
  63. #define ON_ASCII_UPPER_ALPHA \
  64. if (current_input_character.has_value() && current_input_character.value() >= 'A' && current_input_character.value() <= 'Z')
  65. #define ON_ASCII_LOWER_ALPHA \
  66. if (current_input_character.has_value() && current_input_character.value() >= 'a' && current_input_character.value() <= 'z')
  67. #define ON_WHITESPACE \
  68. 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() == ' '))
  69. #define ANYTHING_ELSE if (1)
  70. #define EMIT_EOF \
  71. do { \
  72. if (m_has_emitted_eof) \
  73. return {}; \
  74. m_has_emitted_eof = true; \
  75. create_new_token(HTMLToken::Type::EndOfFile); \
  76. will_emit(m_current_token); \
  77. return m_current_token; \
  78. } while (0)
  79. #define EMIT_CURRENT_TOKEN \
  80. do { \
  81. will_emit(m_current_token); \
  82. return m_current_token; \
  83. } while (0)
  84. #define EMIT_CHARACTER(codepoint) \
  85. do { \
  86. create_new_token(HTMLToken::Type::Character); \
  87. m_current_token.m_comment_or_character.data.append(codepoint); \
  88. will_emit(m_current_token); \
  89. return m_current_token; \
  90. } while (0)
  91. #define EMIT_CURRENT_CHARACTER \
  92. EMIT_CHARACTER(current_input_character.value());
  93. #define BEGIN_STATE(state) \
  94. state: \
  95. case State::state: { \
  96. { \
  97. {
  98. #define END_STATE \
  99. ASSERT_NOT_REACHED(); \
  100. break; \
  101. } \
  102. } \
  103. }
  104. namespace Web {
  105. Optional<u32> HTMLTokenizer::next_codepoint()
  106. {
  107. if (m_cursor >= m_input.length())
  108. return {};
  109. return m_input[m_cursor++];
  110. }
  111. Optional<u32> HTMLTokenizer::peek_codepoint(size_t offset) const
  112. {
  113. if ((m_cursor + offset) >= m_input.length())
  114. return {};
  115. return m_input[m_cursor + offset];
  116. }
  117. Optional<HTMLToken> HTMLTokenizer::next_token()
  118. {
  119. for (;;) {
  120. auto current_input_character = next_codepoint();
  121. switch (m_state) {
  122. BEGIN_STATE(Data)
  123. {
  124. ON('&')
  125. {
  126. m_return_state = State::Data;
  127. SWITCH_TO(CharacterReference);
  128. }
  129. ON('<')
  130. {
  131. SWITCH_TO(TagOpen);
  132. }
  133. ON_EOF
  134. {
  135. EMIT_EOF;
  136. }
  137. ANYTHING_ELSE
  138. {
  139. EMIT_CURRENT_CHARACTER;
  140. }
  141. }
  142. END_STATE
  143. BEGIN_STATE(TagOpen)
  144. {
  145. ON('!')
  146. {
  147. SWITCH_TO(MarkupDeclarationOpen);
  148. }
  149. ON('/')
  150. {
  151. SWITCH_TO(EndTagOpen);
  152. }
  153. ON_ASCII_ALPHA
  154. {
  155. create_new_token(HTMLToken::Type::StartTag);
  156. RECONSUME_IN(TagName);
  157. }
  158. ON('?')
  159. {
  160. TODO();
  161. }
  162. ANYTHING_ELSE
  163. {
  164. TODO();
  165. }
  166. }
  167. END_STATE
  168. BEGIN_STATE(TagName)
  169. {
  170. ON_WHITESPACE
  171. {
  172. SWITCH_TO(BeforeAttributeName);
  173. }
  174. ON('/')
  175. {
  176. SWITCH_TO(SelfClosingStartTag);
  177. }
  178. ON('>')
  179. {
  180. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  181. }
  182. ANYTHING_ELSE
  183. {
  184. m_current_token.m_tag.tag_name.append(current_input_character.value());
  185. continue;
  186. }
  187. }
  188. END_STATE
  189. BEGIN_STATE(EndTagOpen)
  190. {
  191. ON_ASCII_ALPHA
  192. {
  193. create_new_token(HTMLToken::Type::EndTag);
  194. RECONSUME_IN(TagName);
  195. }
  196. }
  197. END_STATE
  198. BEGIN_STATE(MarkupDeclarationOpen)
  199. {
  200. DONT_CONSUME_NEXT_INPUT_CHARACTER;
  201. if (consume_next_if_match("--")) {
  202. create_new_token(HTMLToken::Type::Comment);
  203. SWITCH_TO(CommentStart);
  204. }
  205. if (consume_next_if_match("DOCTYPE", CaseSensitivity::CaseInsensitive)) {
  206. SWITCH_TO(DOCTYPE);
  207. }
  208. }
  209. END_STATE
  210. BEGIN_STATE(DOCTYPE)
  211. {
  212. ON_WHITESPACE
  213. {
  214. SWITCH_TO(BeforeDOCTYPEName);
  215. }
  216. ON('>')
  217. {
  218. RECONSUME_IN(BeforeDOCTYPEName);
  219. }
  220. ON_EOF
  221. {
  222. TODO();
  223. }
  224. ANYTHING_ELSE
  225. {
  226. TODO();
  227. }
  228. }
  229. END_STATE
  230. BEGIN_STATE(BeforeDOCTYPEName)
  231. {
  232. ON_WHITESPACE
  233. {
  234. continue;
  235. }
  236. ON_ASCII_UPPER_ALPHA
  237. {
  238. create_new_token(HTMLToken::Type::DOCTYPE);
  239. m_current_token.m_doctype.name.append(tolower(current_input_character.value()));
  240. SWITCH_TO(DOCTYPEName);
  241. }
  242. ON(0)
  243. {
  244. TODO();
  245. }
  246. ON('>')
  247. {
  248. TODO();
  249. }
  250. ON_EOF
  251. {
  252. TODO();
  253. }
  254. ANYTHING_ELSE
  255. {
  256. create_new_token(HTMLToken::Type::DOCTYPE);
  257. m_current_token.m_doctype.name.append(current_input_character.value());
  258. SWITCH_TO(DOCTYPEName);
  259. }
  260. }
  261. END_STATE
  262. BEGIN_STATE(DOCTYPEName)
  263. {
  264. ON_WHITESPACE
  265. {
  266. SWITCH_TO(AfterDOCTYPEName);
  267. }
  268. ON('>')
  269. {
  270. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  271. }
  272. ON_ASCII_UPPER_ALPHA
  273. {
  274. m_current_token.m_doctype.name.append(tolower(current_input_character.value()));
  275. }
  276. ON(0)
  277. {
  278. TODO();
  279. }
  280. ON_EOF
  281. {
  282. TODO();
  283. }
  284. ANYTHING_ELSE
  285. {
  286. m_current_token.m_doctype.name.append(current_input_character.value());
  287. continue;
  288. }
  289. }
  290. END_STATE
  291. BEGIN_STATE(AfterDOCTYPEName)
  292. {
  293. ON_WHITESPACE
  294. {
  295. continue;
  296. }
  297. ON('>')
  298. {
  299. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  300. }
  301. ON_EOF
  302. {
  303. TODO();
  304. }
  305. ANYTHING_ELSE
  306. {
  307. if (toupper(current_input_character.value()) == 'P' && consume_next_if_match("UBLIC", CaseSensitivity::CaseInsensitive)) {
  308. SWITCH_TO(AfterDOCTYPEPublicKeyword);
  309. }
  310. if (toupper(current_input_character.value()) == 'S' && consume_next_if_match("YSTEM", CaseSensitivity::CaseInsensitive)) {
  311. SWITCH_TO(AfterDOCTYPESystemKeyword);
  312. }
  313. TODO();
  314. }
  315. }
  316. END_STATE
  317. BEGIN_STATE(AfterDOCTYPEPublicKeyword)
  318. {
  319. ON_WHITESPACE
  320. {
  321. SWITCH_TO(BeforeDOCTYPEPublicIdentifier);
  322. }
  323. ON('"')
  324. {
  325. TODO();
  326. }
  327. ON('\'')
  328. {
  329. TODO();
  330. }
  331. ON('>')
  332. {
  333. TODO();
  334. }
  335. ON_EOF
  336. {
  337. TODO();
  338. }
  339. ANYTHING_ELSE
  340. {
  341. TODO();
  342. }
  343. }
  344. END_STATE
  345. BEGIN_STATE(AfterDOCTYPESystemKeyword)
  346. {
  347. ON_WHITESPACE
  348. {
  349. SWITCH_TO(BeforeDOCTYPESystemIdentifier);
  350. }
  351. ON('"')
  352. {
  353. TODO();
  354. }
  355. ON('\'')
  356. {
  357. TODO();
  358. }
  359. ON('>')
  360. {
  361. TODO();
  362. }
  363. ON_EOF
  364. {
  365. TODO();
  366. }
  367. ANYTHING_ELSE
  368. {
  369. TODO();
  370. }
  371. }
  372. END_STATE
  373. BEGIN_STATE(BeforeDOCTYPEPublicIdentifier)
  374. {
  375. ON_WHITESPACE
  376. {
  377. continue;
  378. }
  379. ON('"')
  380. {
  381. m_current_token.m_doctype.public_identifier.clear();
  382. SWITCH_TO(DOCTYPEPublicIdentifierDoubleQuoted);
  383. }
  384. ON('\'')
  385. {
  386. m_current_token.m_doctype.public_identifier.clear();
  387. SWITCH_TO(DOCTYPEPublicIdentifierSingleQuoted);
  388. }
  389. ON('>')
  390. {
  391. TODO();
  392. }
  393. ON_EOF
  394. {
  395. TODO();
  396. }
  397. ANYTHING_ELSE
  398. {
  399. TODO();
  400. }
  401. }
  402. END_STATE
  403. BEGIN_STATE(BeforeDOCTYPESystemIdentifier)
  404. {
  405. ON_WHITESPACE
  406. {
  407. continue;
  408. }
  409. ON('"')
  410. {
  411. m_current_token.m_doctype.system_identifier.clear();
  412. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  413. }
  414. ON('\'')
  415. {
  416. m_current_token.m_doctype.system_identifier.clear();
  417. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  418. }
  419. ON('>')
  420. {
  421. TODO();
  422. }
  423. ON_EOF
  424. {
  425. TODO();
  426. }
  427. ANYTHING_ELSE
  428. {
  429. TODO();
  430. }
  431. }
  432. END_STATE
  433. BEGIN_STATE(DOCTYPEPublicIdentifierDoubleQuoted)
  434. {
  435. ON('"')
  436. {
  437. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  438. }
  439. ON(0)
  440. {
  441. TODO();
  442. }
  443. ON('>')
  444. {
  445. TODO();
  446. }
  447. ON_EOF
  448. {
  449. TODO();
  450. }
  451. ANYTHING_ELSE
  452. {
  453. m_current_token.m_doctype.public_identifier.append(current_input_character.value());
  454. continue;
  455. }
  456. }
  457. END_STATE
  458. BEGIN_STATE(DOCTYPEPublicIdentifierSingleQuoted)
  459. {
  460. ON('\'')
  461. {
  462. SWITCH_TO(AfterDOCTYPEPublicIdentifier);
  463. }
  464. ON(0)
  465. {
  466. TODO();
  467. }
  468. ON('>')
  469. {
  470. TODO();
  471. }
  472. ON_EOF
  473. {
  474. TODO();
  475. }
  476. ANYTHING_ELSE
  477. {
  478. m_current_token.m_doctype.public_identifier.append(current_input_character.value());
  479. continue;
  480. }
  481. }
  482. END_STATE
  483. BEGIN_STATE(DOCTYPESystemIdentifierDoubleQuoted)
  484. {
  485. ON('"')
  486. {
  487. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  488. }
  489. ON(0)
  490. {
  491. TODO();
  492. }
  493. ON('>')
  494. {
  495. TODO();
  496. }
  497. ON_EOF
  498. {
  499. TODO();
  500. }
  501. ANYTHING_ELSE
  502. {
  503. m_current_token.m_doctype.system_identifier.append(current_input_character.value());
  504. continue;
  505. }
  506. }
  507. END_STATE
  508. BEGIN_STATE(DOCTYPESystemIdentifierSingleQuoted)
  509. {
  510. ON('\'')
  511. {
  512. SWITCH_TO(AfterDOCTYPESystemIdentifier);
  513. }
  514. ON(0)
  515. {
  516. TODO();
  517. }
  518. ON('>')
  519. {
  520. TODO();
  521. }
  522. ON_EOF
  523. {
  524. TODO();
  525. }
  526. ANYTHING_ELSE
  527. {
  528. m_current_token.m_doctype.system_identifier.append(current_input_character.value());
  529. continue;
  530. }
  531. }
  532. END_STATE
  533. BEGIN_STATE(AfterDOCTYPEPublicIdentifier)
  534. {
  535. ON_WHITESPACE
  536. {
  537. SWITCH_TO(BetweenDOCTYPEPublicAndSystemIdentifiers);
  538. }
  539. ON('>')
  540. {
  541. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  542. }
  543. ON('"')
  544. {
  545. TODO();
  546. }
  547. ON('\'')
  548. {
  549. TODO();
  550. }
  551. ON_EOF
  552. {
  553. TODO();
  554. }
  555. ANYTHING_ELSE
  556. {
  557. TODO();
  558. }
  559. }
  560. END_STATE
  561. BEGIN_STATE(BetweenDOCTYPEPublicAndSystemIdentifiers)
  562. {
  563. ON_WHITESPACE
  564. {
  565. continue;
  566. }
  567. ON('>')
  568. {
  569. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  570. }
  571. ON('"')
  572. {
  573. m_current_token.m_doctype.system_identifier.clear();
  574. SWITCH_TO(DOCTYPESystemIdentifierDoubleQuoted);
  575. }
  576. ON('\'')
  577. {
  578. m_current_token.m_doctype.system_identifier.clear();
  579. SWITCH_TO(DOCTYPESystemIdentifierSingleQuoted);
  580. }
  581. ON_EOF
  582. {
  583. TODO();
  584. }
  585. ANYTHING_ELSE
  586. {
  587. TODO();
  588. }
  589. }
  590. END_STATE
  591. BEGIN_STATE(AfterDOCTYPESystemIdentifier)
  592. {
  593. ON_WHITESPACE
  594. {
  595. continue;
  596. }
  597. ON('>')
  598. {
  599. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  600. }
  601. ON_EOF
  602. {
  603. TODO();
  604. }
  605. ANYTHING_ELSE
  606. {
  607. TODO();
  608. }
  609. }
  610. END_STATE
  611. BEGIN_STATE(BeforeAttributeName)
  612. {
  613. ON_WHITESPACE
  614. {
  615. continue;
  616. }
  617. ON('/')
  618. {
  619. RECONSUME_IN(AfterAttributeName);
  620. }
  621. ON('>')
  622. {
  623. RECONSUME_IN(AfterAttributeName);
  624. }
  625. ON_EOF
  626. {
  627. RECONSUME_IN(AfterAttributeName);
  628. }
  629. ON('=')
  630. {
  631. TODO();
  632. }
  633. ANYTHING_ELSE
  634. {
  635. m_current_token.m_tag.attributes.append(HTMLToken::AttributeBuilder());
  636. RECONSUME_IN(AttributeName);
  637. }
  638. }
  639. END_STATE
  640. BEGIN_STATE(SelfClosingStartTag)
  641. {
  642. }
  643. END_STATE
  644. BEGIN_STATE(AttributeName)
  645. {
  646. ON_WHITESPACE
  647. {
  648. RECONSUME_IN(AfterAttributeName);
  649. }
  650. ON('/')
  651. {
  652. RECONSUME_IN(AfterAttributeName);
  653. }
  654. ON('>')
  655. {
  656. RECONSUME_IN(AfterAttributeName);
  657. }
  658. ON_EOF
  659. {
  660. RECONSUME_IN(AfterAttributeName);
  661. }
  662. ON('=')
  663. {
  664. SWITCH_TO(BeforeAttributeValue);
  665. }
  666. ANYTHING_ELSE
  667. {
  668. m_current_token.m_tag.attributes.last().name_builder.append(current_input_character.value());
  669. continue;
  670. }
  671. }
  672. END_STATE
  673. BEGIN_STATE(AfterAttributeName)
  674. {
  675. }
  676. END_STATE
  677. BEGIN_STATE(BeforeAttributeValue)
  678. {
  679. ON_WHITESPACE
  680. {
  681. continue;
  682. }
  683. ON('"')
  684. {
  685. SWITCH_TO(AttributeValueDoubleQuoted);
  686. }
  687. ON('\'')
  688. {
  689. SWITCH_TO(AttributeValueSingleQuoted);
  690. }
  691. ON('>')
  692. {
  693. TODO();
  694. }
  695. ANYTHING_ELSE
  696. {
  697. RECONSUME_IN(AttributeValueUnquoted);
  698. }
  699. }
  700. END_STATE
  701. BEGIN_STATE(AttributeValueDoubleQuoted)
  702. {
  703. ON('"')
  704. {
  705. SWITCH_TO(AfterAttributeValueQuoted);
  706. }
  707. ON('&')
  708. {
  709. m_return_state = State::AttributeValueDoubleQuoted;
  710. SWITCH_TO(CharacterReference);
  711. }
  712. ON(0)
  713. {
  714. TODO();
  715. }
  716. ON_EOF
  717. {
  718. TODO();
  719. }
  720. ANYTHING_ELSE
  721. {
  722. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  723. continue;
  724. }
  725. }
  726. END_STATE
  727. BEGIN_STATE(AttributeValueSingleQuoted)
  728. {
  729. ON('\'')
  730. {
  731. SWITCH_TO(AfterAttributeValueQuoted);
  732. }
  733. ON('&')
  734. {
  735. m_return_state = State::AttributeValueSingleQuoted;
  736. SWITCH_TO(CharacterReference);
  737. }
  738. ON(0)
  739. {
  740. TODO();
  741. }
  742. ON_EOF
  743. {
  744. TODO();
  745. }
  746. ANYTHING_ELSE
  747. {
  748. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  749. continue;
  750. }
  751. }
  752. END_STATE
  753. BEGIN_STATE(AttributeValueUnquoted)
  754. {
  755. ON_WHITESPACE
  756. {
  757. SWITCH_TO(BeforeAttributeName);
  758. }
  759. ON('&')
  760. {
  761. m_return_state = State::AttributeValueUnquoted;
  762. SWITCH_TO(CharacterReference);
  763. }
  764. ON('>')
  765. {
  766. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  767. }
  768. ON(0)
  769. {
  770. TODO();
  771. }
  772. ON_EOF
  773. {
  774. TODO();
  775. }
  776. ANYTHING_ELSE
  777. {
  778. m_current_token.m_tag.attributes.last().value_builder.append(current_input_character.value());
  779. continue;
  780. }
  781. }
  782. END_STATE
  783. BEGIN_STATE(AfterAttributeValueQuoted)
  784. {
  785. ON_WHITESPACE
  786. {
  787. SWITCH_TO(BeforeAttributeName);
  788. }
  789. ON('/')
  790. {
  791. SWITCH_TO(SelfClosingStartTag);
  792. }
  793. ON('>')
  794. {
  795. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  796. }
  797. ON_EOF
  798. {
  799. TODO();
  800. }
  801. ANYTHING_ELSE
  802. {
  803. TODO();
  804. }
  805. }
  806. END_STATE
  807. BEGIN_STATE(CommentStart)
  808. {
  809. ON('-')
  810. {
  811. SWITCH_TO(CommentStartDash);
  812. }
  813. ON('>')
  814. {
  815. TODO();
  816. }
  817. ANYTHING_ELSE
  818. {
  819. RECONSUME_IN(Comment);
  820. }
  821. }
  822. END_STATE
  823. BEGIN_STATE(CommentStartDash)
  824. {
  825. ON('-')
  826. {
  827. SWITCH_TO(CommentEnd);
  828. }
  829. ON('>')
  830. {
  831. TODO();
  832. }
  833. ON_EOF
  834. {
  835. TODO();
  836. }
  837. ANYTHING_ELSE
  838. {
  839. m_current_token.m_comment_or_character.data.append('-');
  840. RECONSUME_IN(Comment);
  841. }
  842. }
  843. END_STATE
  844. BEGIN_STATE(Comment)
  845. {
  846. ON('<')
  847. {
  848. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  849. SWITCH_TO(CommentLessThanSign);
  850. }
  851. ON('-')
  852. {
  853. SWITCH_TO(CommentEndDash);
  854. }
  855. ON(0)
  856. {
  857. TODO();
  858. }
  859. ON_EOF
  860. {
  861. TODO();
  862. }
  863. ANYTHING_ELSE
  864. {
  865. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  866. continue;
  867. }
  868. }
  869. END_STATE
  870. BEGIN_STATE(CommentEnd)
  871. {
  872. ON('>')
  873. {
  874. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  875. }
  876. ON('!')
  877. {
  878. SWITCH_TO(CommentEndBang);
  879. }
  880. ON('-')
  881. {
  882. m_current_token.m_comment_or_character.data.append('-');
  883. continue;
  884. }
  885. ON_EOF
  886. {
  887. TODO();
  888. }
  889. ANYTHING_ELSE
  890. {
  891. m_current_token.m_comment_or_character.data.append('-');
  892. RECONSUME_IN(Comment);
  893. }
  894. }
  895. END_STATE
  896. BEGIN_STATE(CommentEndBang)
  897. {
  898. ON('-')
  899. {
  900. m_current_token.m_comment_or_character.data.append("--!");
  901. SWITCH_TO(CommentEndDash);
  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(CommentEndDash)
  919. {
  920. ON('-')
  921. {
  922. SWITCH_TO(CommentEnd);
  923. }
  924. ON_EOF
  925. {
  926. TODO();
  927. }
  928. ANYTHING_ELSE
  929. {
  930. m_current_token.m_comment_or_character.data.append('-');
  931. RECONSUME_IN(Comment);
  932. }
  933. }
  934. END_STATE
  935. BEGIN_STATE(CommentLessThanSign)
  936. {
  937. ON('!')
  938. {
  939. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  940. SWITCH_TO(CommentLessThanSignBang);
  941. }
  942. ON('<')
  943. {
  944. m_current_token.m_comment_or_character.data.append(current_input_character.value());
  945. continue;
  946. }
  947. ANYTHING_ELSE
  948. {
  949. RECONSUME_IN(Comment);
  950. }
  951. }
  952. END_STATE
  953. BEGIN_STATE(CommentLessThanSignBang)
  954. {
  955. ON('-')
  956. {
  957. SWITCH_TO(CommentLessThanSignBangDash);
  958. }
  959. ANYTHING_ELSE
  960. {
  961. RECONSUME_IN(Comment);
  962. }
  963. }
  964. END_STATE
  965. BEGIN_STATE(CommentLessThanSignBangDash)
  966. {
  967. ON('-')
  968. {
  969. SWITCH_TO(CommentLessThanSignBangDashDash);
  970. }
  971. ANYTHING_ELSE
  972. {
  973. RECONSUME_IN(Comment);
  974. }
  975. }
  976. END_STATE
  977. BEGIN_STATE(CommentLessThanSignBangDashDash)
  978. {
  979. ON('>')
  980. {
  981. SWITCH_TO(CommentEnd);
  982. }
  983. ANYTHING_ELSE
  984. {
  985. TODO();
  986. }
  987. }
  988. END_STATE
  989. BEGIN_STATE(CharacterReference)
  990. {
  991. }
  992. END_STATE
  993. BEGIN_STATE(RCDATA)
  994. {
  995. ON('&')
  996. {
  997. m_return_state = State::RCDATA;
  998. SWITCH_TO(CharacterReference);
  999. }
  1000. ON('<')
  1001. {
  1002. SWITCH_TO(RCDATALessThanSign);
  1003. }
  1004. ON(0)
  1005. {
  1006. TODO();
  1007. }
  1008. ON_EOF
  1009. {
  1010. EMIT_EOF;
  1011. }
  1012. ANYTHING_ELSE
  1013. {
  1014. EMIT_CURRENT_CHARACTER;
  1015. }
  1016. }
  1017. END_STATE
  1018. BEGIN_STATE(RCDATALessThanSign)
  1019. {
  1020. ON('/')
  1021. {
  1022. m_temporary_buffer.clear();
  1023. SWITCH_TO(RCDATAEndTagOpen);
  1024. }
  1025. ANYTHING_ELSE
  1026. {
  1027. EMIT_CHARACTER('<');
  1028. RECONSUME_IN(RCDATA);
  1029. }
  1030. }
  1031. END_STATE
  1032. BEGIN_STATE(RCDATAEndTagOpen)
  1033. {
  1034. ON_ASCII_ALPHA
  1035. {
  1036. create_new_token(HTMLToken::Type::EndTag);
  1037. RECONSUME_IN(RCDATAEndTagName);
  1038. }
  1039. ANYTHING_ELSE
  1040. {
  1041. // FIXME: Emit a U+003C LESS-THAN SIGN character token and a U+002F SOLIDUS character token. Reconsume in the RCDATA state.
  1042. TODO();
  1043. }
  1044. }
  1045. END_STATE
  1046. BEGIN_STATE(RCDATAEndTagName)
  1047. {
  1048. ON_WHITESPACE
  1049. {
  1050. TODO();
  1051. }
  1052. ON('/')
  1053. {
  1054. TODO();
  1055. }
  1056. ON('>')
  1057. {
  1058. if (!current_end_tag_token_is_appropriate()) {
  1059. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1060. TODO();
  1061. }
  1062. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1063. }
  1064. ON_ASCII_UPPER_ALPHA
  1065. {
  1066. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1067. m_temporary_buffer.append(current_input_character.value());
  1068. continue;
  1069. }
  1070. ON_ASCII_LOWER_ALPHA
  1071. {
  1072. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1073. m_temporary_buffer.append(current_input_character.value());
  1074. continue;
  1075. }
  1076. ANYTHING_ELSE
  1077. {
  1078. TODO();
  1079. }
  1080. }
  1081. END_STATE
  1082. BEGIN_STATE(RAWTEXT)
  1083. {
  1084. ON('<')
  1085. {
  1086. SWITCH_TO(RAWTEXTLessThanSign);
  1087. }
  1088. ON(0)
  1089. {
  1090. TODO();
  1091. }
  1092. ON_EOF
  1093. {
  1094. EMIT_EOF;
  1095. }
  1096. ANYTHING_ELSE
  1097. {
  1098. EMIT_CURRENT_CHARACTER;
  1099. }
  1100. }
  1101. END_STATE
  1102. BEGIN_STATE(RAWTEXTLessThanSign)
  1103. {
  1104. ON('/')
  1105. {
  1106. m_temporary_buffer.clear();
  1107. SWITCH_TO(RAWTEXTEndTagOpen);
  1108. }
  1109. ANYTHING_ELSE
  1110. {
  1111. EMIT_CHARACTER('<');
  1112. RECONSUME_IN(RAWTEXT);
  1113. }
  1114. }
  1115. END_STATE
  1116. BEGIN_STATE(RAWTEXTEndTagOpen)
  1117. {
  1118. ON_ASCII_ALPHA
  1119. {
  1120. create_new_token(HTMLToken::Type::EndTag);
  1121. RECONSUME_IN(RAWTEXTEndTagName);
  1122. }
  1123. ANYTHING_ELSE
  1124. {
  1125. // FIXME: Emit a U+003C LESS-THAN SIGN character token and a U+002F SOLIDUS character token. Reconsume in the RAWTEXT state.
  1126. TODO();
  1127. }
  1128. }
  1129. END_STATE
  1130. BEGIN_STATE(RAWTEXTEndTagName)
  1131. {
  1132. ON_WHITESPACE
  1133. {
  1134. TODO();
  1135. }
  1136. ON('/')
  1137. {
  1138. TODO();
  1139. }
  1140. ON('>')
  1141. {
  1142. if (!current_end_tag_token_is_appropriate()) {
  1143. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1144. TODO();
  1145. }
  1146. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1147. }
  1148. ON_ASCII_UPPER_ALPHA
  1149. {
  1150. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1151. m_temporary_buffer.append(current_input_character.value());
  1152. continue;
  1153. }
  1154. ON_ASCII_LOWER_ALPHA
  1155. {
  1156. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1157. m_temporary_buffer.append(current_input_character.value());
  1158. continue;
  1159. }
  1160. ANYTHING_ELSE
  1161. {
  1162. TODO();
  1163. }
  1164. }
  1165. END_STATE
  1166. BEGIN_STATE(ScriptData)
  1167. {
  1168. ON('<')
  1169. {
  1170. SWITCH_TO(ScriptDataLessThanSign);
  1171. }
  1172. ON(0)
  1173. {
  1174. TODO();
  1175. }
  1176. ON_EOF
  1177. {
  1178. EMIT_EOF;
  1179. }
  1180. ANYTHING_ELSE
  1181. {
  1182. EMIT_CURRENT_CHARACTER;
  1183. }
  1184. }
  1185. END_STATE
  1186. BEGIN_STATE(ScriptDataLessThanSign)
  1187. {
  1188. ON('/')
  1189. {
  1190. m_temporary_buffer.clear();
  1191. SWITCH_TO(ScriptDataEndTagOpen);
  1192. }
  1193. ON('!')
  1194. {
  1195. TODO();
  1196. }
  1197. ANYTHING_ELSE
  1198. {
  1199. EMIT_CHARACTER('<');
  1200. RECONSUME_IN(ScriptData);
  1201. }
  1202. }
  1203. END_STATE
  1204. BEGIN_STATE(ScriptDataEndTagOpen)
  1205. {
  1206. ON_ASCII_ALPHA
  1207. {
  1208. create_new_token(HTMLToken::Type::EndTag);
  1209. RECONSUME_IN(ScriptDataEndTagName);
  1210. }
  1211. ANYTHING_ELSE
  1212. {
  1213. TODO();
  1214. }
  1215. }
  1216. END_STATE
  1217. BEGIN_STATE(ScriptDataEndTagName)
  1218. {
  1219. ON_WHITESPACE
  1220. {
  1221. if (current_end_tag_token_is_appropriate())
  1222. SWITCH_TO(BeforeAttributeName);
  1223. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1224. TODO();
  1225. }
  1226. ON('/')
  1227. {
  1228. if (current_end_tag_token_is_appropriate())
  1229. SWITCH_TO(SelfClosingStartTag);
  1230. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1231. TODO();
  1232. }
  1233. ON('>')
  1234. {
  1235. if (current_end_tag_token_is_appropriate())
  1236. SWITCH_TO_AND_EMIT_CURRENT_TOKEN(Data);
  1237. // FIXME: Otherwise, treat it as per the "anything else" entry below.
  1238. TODO();
  1239. }
  1240. ON_ASCII_UPPER_ALPHA
  1241. {
  1242. m_current_token.m_tag.tag_name.append(tolower(current_input_character.value()));
  1243. m_temporary_buffer.append(current_input_character.value());
  1244. continue;
  1245. }
  1246. ON_ASCII_LOWER_ALPHA
  1247. {
  1248. m_current_token.m_tag.tag_name.append(current_input_character.value());
  1249. m_temporary_buffer.append(current_input_character.value());
  1250. continue;
  1251. }
  1252. ANYTHING_ELSE
  1253. {
  1254. TODO();
  1255. }
  1256. }
  1257. END_STATE
  1258. default:
  1259. TODO();
  1260. }
  1261. }
  1262. }
  1263. bool HTMLTokenizer::consume_next_if_match(const StringView& string, CaseSensitivity case_sensitivity)
  1264. {
  1265. for (size_t i = 0; i < string.length(); ++i) {
  1266. auto codepoint = peek_codepoint(i);
  1267. if (!codepoint.has_value())
  1268. return false;
  1269. // FIXME: This should be more Unicode-aware.
  1270. if (case_sensitivity == CaseSensitivity::CaseInsensitive) {
  1271. if (codepoint.value() < 0x80) {
  1272. if (tolower(codepoint.value()) != tolower(string[i]))
  1273. return false;
  1274. continue;
  1275. }
  1276. }
  1277. if (codepoint.value() != (u32)string[i])
  1278. return false;
  1279. }
  1280. m_cursor += string.length();
  1281. return true;
  1282. }
  1283. void HTMLTokenizer::create_new_token(HTMLToken::Type type)
  1284. {
  1285. m_current_token = {};
  1286. m_current_token.m_type = type;
  1287. }
  1288. HTMLTokenizer::HTMLTokenizer(const StringView& input)
  1289. : m_input(input)
  1290. {
  1291. }
  1292. void HTMLTokenizer::will_switch_to([[maybe_unused]] State new_state)
  1293. {
  1294. #ifdef TOKENIZER_TRACE
  1295. dbg() << "[" << state_name(m_state) << "] Switch to " << state_name(new_state);
  1296. #endif
  1297. }
  1298. void HTMLTokenizer::will_reconsume_in([[maybe_unused]] State new_state)
  1299. {
  1300. #ifdef TOKENIZER_TRACE
  1301. dbg() << "[" << state_name(m_state) << "] Reconsume in " << state_name(new_state);
  1302. #endif
  1303. }
  1304. void HTMLTokenizer::switch_to(Badge<HTMLDocumentParser>, State new_state)
  1305. {
  1306. #ifdef TOKENIZER_TRACE
  1307. dbg() << "[" << state_name(m_state) << "] Parser switches tokenizer state to " << state_name(new_state);
  1308. #endif
  1309. m_state = new_state;
  1310. }
  1311. void HTMLTokenizer::will_emit(HTMLToken& token)
  1312. {
  1313. if (token.is_start_tag())
  1314. m_last_emitted_start_tag = token;
  1315. }
  1316. bool HTMLTokenizer::current_end_tag_token_is_appropriate() const
  1317. {
  1318. ASSERT(m_current_token.is_end_tag());
  1319. if (!m_last_emitted_start_tag.is_start_tag())
  1320. return false;
  1321. return m_current_token.tag_name() == m_last_emitted_start_tag.tag_name();
  1322. }
  1323. }