HTMLDocumentParser.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 <AK/Utf32View.h>
  27. #include <LibWeb/DOM/Comment.h>
  28. #include <LibWeb/DOM/Document.h>
  29. #include <LibWeb/DOM/DocumentType.h>
  30. #include <LibWeb/DOM/ElementFactory.h>
  31. #include <LibWeb/DOM/HTMLFormElement.h>
  32. #include <LibWeb/DOM/HTMLHeadElement.h>
  33. #include <LibWeb/DOM/HTMLScriptElement.h>
  34. #include <LibWeb/DOM/Text.h>
  35. #include <LibWeb/Parser/HTMLDocumentParser.h>
  36. #include <LibWeb/Parser/HTMLToken.h>
  37. #define TODO() \
  38. do { \
  39. ASSERT_NOT_REACHED(); \
  40. } while (0)
  41. #define PARSE_ERROR() \
  42. do { \
  43. dbg() << "Parse error!"; \
  44. } while (0)
  45. namespace Web {
  46. HTMLDocumentParser::HTMLDocumentParser(const StringView& input)
  47. : m_tokenizer(input)
  48. {
  49. }
  50. HTMLDocumentParser::~HTMLDocumentParser()
  51. {
  52. }
  53. void HTMLDocumentParser::run(const URL& url)
  54. {
  55. m_document = adopt(*new Document);
  56. m_document->set_url(url);
  57. for (;;) {
  58. auto optional_token = m_tokenizer.next_token();
  59. if (!optional_token.has_value())
  60. return;
  61. auto& token = optional_token.value();
  62. dbg() << "[" << insertion_mode_name() << "] " << token.to_string();
  63. process_using_the_rules_for(m_insertion_mode, token);
  64. }
  65. }
  66. void HTMLDocumentParser::process_using_the_rules_for(InsertionMode mode, HTMLToken& token)
  67. {
  68. switch (mode) {
  69. case InsertionMode::Initial:
  70. handle_initial(token);
  71. break;
  72. case InsertionMode::BeforeHTML:
  73. handle_before_html(token);
  74. break;
  75. case InsertionMode::BeforeHead:
  76. handle_before_head(token);
  77. break;
  78. case InsertionMode::InHead:
  79. handle_in_head(token);
  80. break;
  81. case InsertionMode::InHeadNoscript:
  82. handle_in_head_noscript(token);
  83. break;
  84. case InsertionMode::AfterHead:
  85. handle_after_head(token);
  86. break;
  87. case InsertionMode::InBody:
  88. handle_in_body(token);
  89. break;
  90. case InsertionMode::AfterBody:
  91. handle_after_body(token);
  92. break;
  93. case InsertionMode::AfterAfterBody:
  94. handle_after_after_body(token);
  95. break;
  96. case InsertionMode::Text:
  97. handle_text(token);
  98. break;
  99. case InsertionMode::InTable:
  100. handle_in_table(token);
  101. break;
  102. default:
  103. ASSERT_NOT_REACHED();
  104. }
  105. }
  106. void HTMLDocumentParser::handle_initial(HTMLToken& token)
  107. {
  108. if (token.type() == HTMLToken::Type::DOCTYPE) {
  109. auto doctype = adopt(*new DocumentType(document()));
  110. doctype->set_name(token.m_doctype.name.to_string());
  111. document().append_child(move(doctype));
  112. m_insertion_mode = InsertionMode::BeforeHTML;
  113. return;
  114. }
  115. ASSERT_NOT_REACHED();
  116. }
  117. void HTMLDocumentParser::handle_before_html(HTMLToken& token)
  118. {
  119. if (token.is_character() && token.is_parser_whitespace()) {
  120. return;
  121. }
  122. if (token.is_start_tag() && token.tag_name() == "html") {
  123. auto element = create_element_for(token);
  124. document().append_child(element);
  125. m_stack_of_open_elements.push(move(element));
  126. m_insertion_mode = InsertionMode::BeforeHead;
  127. return;
  128. }
  129. ASSERT_NOT_REACHED();
  130. }
  131. Element& HTMLDocumentParser::current_node()
  132. {
  133. return m_stack_of_open_elements.current_node();
  134. }
  135. RefPtr<Node> HTMLDocumentParser::find_appropriate_place_for_inserting_node()
  136. {
  137. auto& target = current_node();
  138. if (m_foster_parenting) {
  139. ASSERT_NOT_REACHED();
  140. }
  141. return target;
  142. }
  143. NonnullRefPtr<Element> HTMLDocumentParser::create_element_for(HTMLToken& token)
  144. {
  145. auto element = create_element(document(), token.tag_name());
  146. for (auto& attribute : token.m_tag.attributes) {
  147. element->set_attribute(attribute.name_builder.to_string(), attribute.value_builder.to_string());
  148. }
  149. return element;
  150. }
  151. RefPtr<Element> HTMLDocumentParser::insert_html_element(HTMLToken& token)
  152. {
  153. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  154. auto element = create_element_for(token);
  155. // FIXME: Check if it's possible to insert `element` at `adjusted_insertion_location`
  156. adjusted_insertion_location->append_child(element);
  157. m_stack_of_open_elements.push(element);
  158. return element;
  159. }
  160. void HTMLDocumentParser::handle_before_head(HTMLToken& token)
  161. {
  162. if (token.is_character() && token.is_parser_whitespace()) {
  163. return;
  164. }
  165. if (token.is_start_tag() && token.tag_name() == "head") {
  166. auto element = insert_html_element(token);
  167. m_head_element = to<HTMLHeadElement>(element);
  168. m_insertion_mode = InsertionMode::InHead;
  169. return;
  170. }
  171. ASSERT_NOT_REACHED();
  172. }
  173. void HTMLDocumentParser::insert_comment(HTMLToken& token)
  174. {
  175. auto data = token.m_comment_or_character.data.to_string();
  176. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  177. adjusted_insertion_location->append_child(adopt(*new Comment(document(), data)));
  178. }
  179. void HTMLDocumentParser::handle_in_head(HTMLToken& token)
  180. {
  181. if (token.is_parser_whitespace()) {
  182. insert_character(token.codepoint());
  183. return;
  184. }
  185. if (token.is_comment()) {
  186. insert_comment(token);
  187. return;
  188. }
  189. if (token.is_doctype()) {
  190. PARSE_ERROR();
  191. return;
  192. }
  193. if (token.is_start_tag() && token.tag_name() == "html") {
  194. process_using_the_rules_for(InsertionMode::InBody, token);
  195. return;
  196. }
  197. if (token.is_start_tag() && token.tag_name().is_one_of("base", "basefont", "bgsound", "link")) {
  198. insert_html_element(token);
  199. m_stack_of_open_elements.pop();
  200. token.acknowledge_self_closing_flag_if_set();
  201. return;
  202. }
  203. if (token.is_start_tag() && token.tag_name() == "title") {
  204. insert_html_element(token);
  205. m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
  206. m_original_insertion_mode = m_insertion_mode;
  207. m_insertion_mode = InsertionMode::Text;
  208. return;
  209. }
  210. if (token.is_start_tag() && ((token.tag_name() == "noscript" && m_scripting_enabled) || token.tag_name() == "noframes" || token.tag_name() == "style")) {
  211. parse_generic_raw_text_element(token);
  212. return;
  213. }
  214. if (token.is_start_tag() && token.tag_name() == "script") {
  215. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  216. auto element = create_element_for(token);
  217. auto& script_element = to<HTMLScriptElement>(*element);
  218. script_element.set_parser_document({}, document());
  219. script_element.set_non_blocking({}, false);
  220. if (m_parsing_fragment) {
  221. TODO();
  222. }
  223. if (m_invoked_via_document_write) {
  224. TODO();
  225. }
  226. adjusted_insertion_location->append_child(element, false);
  227. m_stack_of_open_elements.push(element);
  228. m_tokenizer.switch_to({}, HTMLTokenizer::State::ScriptData);
  229. m_original_insertion_mode = m_insertion_mode;
  230. m_insertion_mode = InsertionMode::Text;
  231. return;
  232. }
  233. if (token.is_start_tag() && token.tag_name() == "meta") {
  234. auto element = insert_html_element(token);
  235. m_stack_of_open_elements.pop();
  236. token.acknowledge_self_closing_flag_if_set();
  237. return;
  238. }
  239. if (token.is_end_tag() && token.tag_name() == "head") {
  240. m_stack_of_open_elements.pop();
  241. m_insertion_mode = InsertionMode::AfterHead;
  242. return;
  243. }
  244. ASSERT_NOT_REACHED();
  245. }
  246. void HTMLDocumentParser::handle_in_head_noscript(HTMLToken&)
  247. {
  248. ASSERT_NOT_REACHED();
  249. }
  250. void HTMLDocumentParser::parse_generic_raw_text_element(HTMLToken& token)
  251. {
  252. insert_html_element(token);
  253. m_tokenizer.switch_to({}, HTMLTokenizer::State::RAWTEXT);
  254. m_original_insertion_mode = m_insertion_mode;
  255. m_insertion_mode = InsertionMode::Text;
  256. }
  257. void HTMLDocumentParser::insert_character(u32 data)
  258. {
  259. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  260. if (adjusted_insertion_location->is_document())
  261. return;
  262. if (adjusted_insertion_location->last_child() && adjusted_insertion_location->last_child()->is_text()) {
  263. auto& existing_text_node = to<Text>(*adjusted_insertion_location->last_child());
  264. StringBuilder builder;
  265. builder.append(existing_text_node.data());
  266. builder.append(Utf32View { &data, 1 });
  267. existing_text_node.set_data(builder.to_string());
  268. return;
  269. }
  270. StringBuilder builder;
  271. builder.append(Utf32View { &data, 1 });
  272. adjusted_insertion_location->append_child(adopt(*new Text(document(), builder.to_string())));
  273. }
  274. void HTMLDocumentParser::handle_after_head(HTMLToken& token)
  275. {
  276. if (token.is_character()) {
  277. if (token.is_parser_whitespace()) {
  278. insert_character(token.codepoint());
  279. return;
  280. }
  281. ASSERT_NOT_REACHED();
  282. }
  283. if (token.is_comment()) {
  284. ASSERT_NOT_REACHED();
  285. }
  286. if (token.is_doctype()) {
  287. ASSERT_NOT_REACHED();
  288. }
  289. if (token.is_start_tag() && token.tag_name() == "html") {
  290. ASSERT_NOT_REACHED();
  291. }
  292. if (token.is_start_tag() && token.tag_name() == "body") {
  293. insert_html_element(token);
  294. m_frameset_ok = false;
  295. m_insertion_mode = InsertionMode::InBody;
  296. return;
  297. }
  298. if (token.is_start_tag() && token.tag_name() == "frameset") {
  299. ASSERT_NOT_REACHED();
  300. }
  301. if (token.is_start_tag() && token.tag_name().is_one_of("base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title")) {
  302. ASSERT_NOT_REACHED();
  303. }
  304. if (token.is_end_tag() && token.tag_name() == "template") {
  305. ASSERT_NOT_REACHED();
  306. }
  307. if (token.is_end_tag() && token.tag_name().is_one_of("body", "html", "br")) {
  308. goto AnythingElse;
  309. }
  310. if ((token.is_start_tag() && token.tag_name() == "head") || token.is_end_tag()) {
  311. ASSERT_NOT_REACHED();
  312. }
  313. AnythingElse:
  314. HTMLToken fake_body_token;
  315. fake_body_token.m_type = HTMLToken::Type::StartTag;
  316. fake_body_token.m_tag.tag_name.append("body");
  317. insert_html_element(fake_body_token);
  318. m_insertion_mode = InsertionMode::InBody;
  319. // FIXME: Reprocess the current token in InBody!
  320. }
  321. void HTMLDocumentParser::generate_implied_end_tags(const FlyString& exception)
  322. {
  323. while (current_node().tag_name() != exception && current_node().tag_name().is_one_of("dd", "dt", "li", "optgroup", "option", "p", "rb", "rp", "rt", "rtc"))
  324. m_stack_of_open_elements.pop();
  325. }
  326. void HTMLDocumentParser::close_a_p_element()
  327. {
  328. generate_implied_end_tags("p");
  329. if (current_node().tag_name() != "p") {
  330. PARSE_ERROR();
  331. }
  332. for (;;) {
  333. auto popped_element = m_stack_of_open_elements.pop();
  334. if (popped_element->tag_name() == "p")
  335. break;
  336. }
  337. }
  338. void HTMLDocumentParser::handle_after_body(HTMLToken& token)
  339. {
  340. if (token.is_character() && token.is_parser_whitespace()) {
  341. process_using_the_rules_for(InsertionMode::InBody, token);
  342. return;
  343. }
  344. if (token.is_end_tag() && token.tag_name() == "html") {
  345. if (m_parsing_fragment) {
  346. ASSERT_NOT_REACHED();
  347. }
  348. m_insertion_mode = InsertionMode::AfterAfterBody;
  349. return;
  350. }
  351. ASSERT_NOT_REACHED();
  352. }
  353. void HTMLDocumentParser::handle_after_after_body(HTMLToken& token)
  354. {
  355. if (token.is_doctype() || token.is_parser_whitespace() || (token.is_start_tag() && token.tag_name() == "html")) {
  356. process_using_the_rules_for(InsertionMode::InBody, token);
  357. return;
  358. }
  359. if (token.is_end_of_file()) {
  360. dbg() << "Stop parsing! :^)";
  361. return;
  362. }
  363. ASSERT_NOT_REACHED();
  364. }
  365. void HTMLDocumentParser::reconstruct_the_active_formatting_elements()
  366. {
  367. // FIXME: This needs to care about "markers"
  368. if (m_list_of_active_formatting_elements.is_empty())
  369. return;
  370. if (m_stack_of_open_elements.contains(m_list_of_active_formatting_elements.last()))
  371. return;
  372. ssize_t index = m_list_of_active_formatting_elements.size() - 1;
  373. RefPtr<Element> entry = m_list_of_active_formatting_elements.at(index);
  374. Rewind:
  375. if (m_list_of_active_formatting_elements.size() == 1) {
  376. goto Create;
  377. }
  378. --index;
  379. entry = m_list_of_active_formatting_elements.at(index);
  380. if (!m_stack_of_open_elements.contains(*entry))
  381. goto Rewind;
  382. Advance:
  383. ++index;
  384. entry = m_list_of_active_formatting_elements.at(index);
  385. Create:
  386. // FIXME: Hold on to the real token!
  387. HTMLToken fake_token;
  388. fake_token.m_type = HTMLToken::Type::StartTag;
  389. fake_token.m_tag.tag_name.append(entry->tag_name());
  390. auto new_element = insert_html_element(fake_token);
  391. m_list_of_active_formatting_elements.ptr_at(index) = *new_element;
  392. if (index != (ssize_t)m_list_of_active_formatting_elements.size() - 1)
  393. goto Advance;
  394. }
  395. void HTMLDocumentParser::handle_in_body(HTMLToken& token)
  396. {
  397. if (token.is_character()) {
  398. if (token.codepoint() == 0) {
  399. ASSERT_NOT_REACHED();
  400. }
  401. if (token.is_parser_whitespace()) {
  402. reconstruct_the_active_formatting_elements();
  403. insert_character(token.codepoint());
  404. return;
  405. }
  406. reconstruct_the_active_formatting_elements();
  407. insert_character(token.codepoint());
  408. m_frameset_ok = false;
  409. return;
  410. }
  411. if (token.is_end_tag() && token.tag_name() == "body") {
  412. if (!m_stack_of_open_elements.has_in_scope("body")) {
  413. ASSERT_NOT_REACHED();
  414. }
  415. // FIXME: Otherwise, if there is a node in the stack of open elements that is
  416. // not either a dd element, a dt element, an li element, an optgroup element,
  417. // an option element, a p element, an rb element, an rp element, an rt element,
  418. // an rtc element, a tbody element, a td element, a tfoot element, a th element,
  419. // a thead element, a tr element, the body element, or the html element,
  420. // then this is a parse error.
  421. m_insertion_mode = InsertionMode::AfterBody;
  422. return;
  423. }
  424. if (token.is_start_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  425. if (m_stack_of_open_elements.has_in_button_scope("p"))
  426. close_a_p_element();
  427. if (current_node().tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  428. // FIXME: This is a parse error!
  429. TODO();
  430. }
  431. insert_html_element(token);
  432. return;
  433. }
  434. if (token.is_end_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  435. if (!m_stack_of_open_elements.has_in_scope("h1")
  436. && !m_stack_of_open_elements.has_in_scope("h2")
  437. && !m_stack_of_open_elements.has_in_scope("h3")
  438. && !m_stack_of_open_elements.has_in_scope("h4")
  439. && !m_stack_of_open_elements.has_in_scope("h5")
  440. && !m_stack_of_open_elements.has_in_scope("h6")) {
  441. TODO();
  442. }
  443. generate_implied_end_tags();
  444. if (current_node().tag_name() != token.tag_name()) {
  445. TODO();
  446. }
  447. for (;;) {
  448. auto popped_element = m_stack_of_open_elements.pop();
  449. if (popped_element->tag_name() == "h1"
  450. || popped_element->tag_name() == "h2"
  451. || popped_element->tag_name() == "h3"
  452. || popped_element->tag_name() == "h4"
  453. || popped_element->tag_name() == "h5"
  454. || popped_element->tag_name() == "h6") {
  455. break;
  456. }
  457. }
  458. return;
  459. }
  460. if (token.is_end_tag() && token.tag_name() == "p") {
  461. if (!m_stack_of_open_elements.has_in_button_scope("p")) {
  462. TODO();
  463. }
  464. close_a_p_element();
  465. return;
  466. }
  467. {
  468. if (token.is_start_tag() && token.tag_name().is_one_of("b", "big", "code", "em", "font", "i", "s", "small", "strike", "strong", "tt", "u")) {
  469. reconstruct_the_active_formatting_elements();
  470. auto element = insert_html_element(token);
  471. m_list_of_active_formatting_elements.append(*element);
  472. return;
  473. }
  474. }
  475. if (token.is_start_tag() && token.tag_name().is_one_of("address", "article", "aside", "blockquote", "center", "details", "dialog", "dir", "div", "dl", "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "main", "menu", "nav", "ol", "p", "section", "summary", "ul")) {
  476. // FIXME: If the stack of open elements has a p element in button scope, then close a p element.
  477. insert_html_element(token);
  478. return;
  479. }
  480. if (token.is_end_tag() && token.tag_name().is_one_of("address", "article", "aside", "blockquote", "center", "details", "dialog", "dir", "div", "dl", "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "main", "menu", "nav", "ol", "p", "section", "summary", "ul")) {
  481. // FIXME: If the stack of open elements has a p element in button scope, then close a p element.
  482. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  483. ASSERT_NOT_REACHED();
  484. }
  485. generate_implied_end_tags();
  486. if (current_node().tag_name() != token.tag_name()) {
  487. ASSERT_NOT_REACHED();
  488. }
  489. m_stack_of_open_elements.pop();
  490. return;
  491. }
  492. if (token.is_start_tag() && token.tag_name() == "table") {
  493. // FIXME: If the Document is not set to quirks mode,
  494. // and the stack of open elements has a p element in button scope, then close a p element.
  495. insert_html_element(token);
  496. m_frameset_ok = false;
  497. m_insertion_mode = InsertionMode::InTable;
  498. return;
  499. }
  500. if (token.is_start_tag()) {
  501. reconstruct_the_active_formatting_elements();
  502. insert_html_element(token);
  503. return;
  504. }
  505. if (token.is_end_tag()) {
  506. RefPtr<Element> node;
  507. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  508. node = m_stack_of_open_elements.elements()[i];
  509. if (node->tag_name() == token.tag_name()) {
  510. generate_implied_end_tags(token.tag_name());
  511. if (node != current_node()) {
  512. // It's a parse error
  513. TODO();
  514. }
  515. while (&current_node() != node) {
  516. m_stack_of_open_elements.pop();
  517. }
  518. m_stack_of_open_elements.pop();
  519. break;
  520. }
  521. // FIXME: Handle special elements!
  522. }
  523. return;
  524. }
  525. ASSERT_NOT_REACHED();
  526. }
  527. void HTMLDocumentParser::increment_script_nesting_level()
  528. {
  529. ++m_script_nesting_level;
  530. }
  531. void HTMLDocumentParser::decrement_script_nesting_level()
  532. {
  533. ASSERT(m_script_nesting_level);
  534. --m_script_nesting_level;
  535. }
  536. void HTMLDocumentParser::handle_text(HTMLToken& token)
  537. {
  538. if (token.is_character()) {
  539. insert_character(token.codepoint());
  540. return;
  541. }
  542. if (token.is_end_tag() && token.tag_name() == "script") {
  543. NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
  544. m_stack_of_open_elements.pop();
  545. m_insertion_mode = m_original_insertion_mode;
  546. // FIXME: Handle tokenizer insertion point stuff here.
  547. increment_script_nesting_level();
  548. script->prepare_script({});
  549. decrement_script_nesting_level();
  550. if (script_nesting_level() == 0)
  551. m_parser_pause_flag = false;
  552. // FIXME: Handle tokenizer insertion point stuff here too.
  553. return;
  554. }
  555. if (token.is_end_tag()) {
  556. m_stack_of_open_elements.pop();
  557. m_insertion_mode = m_original_insertion_mode;
  558. return;
  559. }
  560. ASSERT_NOT_REACHED();
  561. }
  562. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  563. {
  564. if (token.is_character() && current_node().tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  565. TODO();
  566. }
  567. if (token.is_comment()) {
  568. insert_comment(token);
  569. return;
  570. }
  571. if (token.is_doctype()) {
  572. PARSE_ERROR();
  573. return;
  574. }
  575. if (token.is_start_tag() && token.tag_name() == "caption") {
  576. TODO();
  577. }
  578. if (token.is_start_tag() && token.tag_name() == "colgroup") {
  579. TODO();
  580. }
  581. if (token.is_start_tag() && token.tag_name() == "col") {
  582. TODO();
  583. }
  584. if (token.is_start_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  585. TODO();
  586. }
  587. if (token.is_start_tag() && token.tag_name().is_one_of("td", "th", "tr")) {
  588. TODO();
  589. }
  590. if (token.is_start_tag() && token.tag_name() == "table") {
  591. PARSE_ERROR();
  592. TODO();
  593. }
  594. if (token.is_end_tag()) {
  595. if (!m_stack_of_open_elements.has_in_table_scope("table")) {
  596. PARSE_ERROR();
  597. return;
  598. }
  599. while (current_node().tag_name() != "table")
  600. m_stack_of_open_elements.pop();
  601. m_stack_of_open_elements.pop();
  602. reset_the_insertion_mode_appropriately();
  603. return;
  604. }
  605. TODO();
  606. }
  607. void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
  608. {
  609. TODO();
  610. }
  611. const char* HTMLDocumentParser::insertion_mode_name() const
  612. {
  613. switch (m_insertion_mode) {
  614. #define __ENUMERATE_INSERTION_MODE(mode) \
  615. case InsertionMode::mode: \
  616. return #mode;
  617. ENUMERATE_INSERTION_MODES
  618. #undef __ENUMERATE_INSERTION_MODE
  619. }
  620. ASSERT_NOT_REACHED();
  621. }
  622. Document& HTMLDocumentParser::document()
  623. {
  624. return *m_document;
  625. }
  626. }