HTMLDocumentParser.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. #define PARSER_DEBUG
  27. #include <AK/Utf32View.h>
  28. #include <LibWeb/DOM/Comment.h>
  29. #include <LibWeb/DOM/Document.h>
  30. #include <LibWeb/DOM/DocumentType.h>
  31. #include <LibWeb/DOM/ElementFactory.h>
  32. #include <LibWeb/DOM/Event.h>
  33. #include <LibWeb/DOM/HTMLFormElement.h>
  34. #include <LibWeb/DOM/HTMLHeadElement.h>
  35. #include <LibWeb/DOM/HTMLScriptElement.h>
  36. #include <LibWeb/DOM/Text.h>
  37. #include <LibWeb/Parser/HTMLDocumentParser.h>
  38. #include <LibWeb/Parser/HTMLToken.h>
  39. #define TODO() \
  40. do { \
  41. ASSERT_NOT_REACHED(); \
  42. } while (0)
  43. #define PARSE_ERROR() \
  44. do { \
  45. dbg() << "Parse error!"; \
  46. } while (0)
  47. namespace Web {
  48. HTMLDocumentParser::HTMLDocumentParser(const StringView& input)
  49. : m_tokenizer(input)
  50. {
  51. }
  52. HTMLDocumentParser::~HTMLDocumentParser()
  53. {
  54. }
  55. void HTMLDocumentParser::run(const URL& url)
  56. {
  57. m_document = adopt(*new Document);
  58. m_document->set_url(url);
  59. for (;;) {
  60. auto optional_token = m_tokenizer.next_token();
  61. if (!optional_token.has_value())
  62. break;
  63. auto& token = optional_token.value();
  64. #ifdef PARSER_DEBUG
  65. dbg() << "[" << insertion_mode_name() << "] " << token.to_string();
  66. #endif
  67. process_using_the_rules_for(m_insertion_mode, token);
  68. }
  69. // "The end"
  70. m_document->dispatch_event(Event::create("DOMContentLoaded"));
  71. }
  72. void HTMLDocumentParser::process_using_the_rules_for(InsertionMode mode, HTMLToken& token)
  73. {
  74. switch (mode) {
  75. case InsertionMode::Initial:
  76. handle_initial(token);
  77. break;
  78. case InsertionMode::BeforeHTML:
  79. handle_before_html(token);
  80. break;
  81. case InsertionMode::BeforeHead:
  82. handle_before_head(token);
  83. break;
  84. case InsertionMode::InHead:
  85. handle_in_head(token);
  86. break;
  87. case InsertionMode::InHeadNoscript:
  88. handle_in_head_noscript(token);
  89. break;
  90. case InsertionMode::AfterHead:
  91. handle_after_head(token);
  92. break;
  93. case InsertionMode::InBody:
  94. handle_in_body(token);
  95. break;
  96. case InsertionMode::AfterBody:
  97. handle_after_body(token);
  98. break;
  99. case InsertionMode::AfterAfterBody:
  100. handle_after_after_body(token);
  101. break;
  102. case InsertionMode::Text:
  103. handle_text(token);
  104. break;
  105. case InsertionMode::InTable:
  106. handle_in_table(token);
  107. break;
  108. default:
  109. ASSERT_NOT_REACHED();
  110. }
  111. }
  112. void HTMLDocumentParser::handle_initial(HTMLToken& token)
  113. {
  114. if (token.is_character() && token.is_parser_whitespace()) {
  115. return;
  116. }
  117. if (token.is_comment()) {
  118. auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
  119. document().append_child(move(comment));
  120. return;
  121. }
  122. if (token.is_doctype()) {
  123. auto doctype = adopt(*new DocumentType(document()));
  124. doctype->set_name(token.m_doctype.name.to_string());
  125. document().append_child(move(doctype));
  126. m_insertion_mode = InsertionMode::BeforeHTML;
  127. return;
  128. }
  129. PARSE_ERROR();
  130. document().set_quirks_mode(true);
  131. m_insertion_mode = InsertionMode::BeforeHTML;
  132. process_using_the_rules_for(InsertionMode::BeforeHTML, token);
  133. }
  134. void HTMLDocumentParser::handle_before_html(HTMLToken& token)
  135. {
  136. if (token.is_doctype()) {
  137. PARSE_ERROR();
  138. return;
  139. }
  140. if (token.is_comment()) {
  141. auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
  142. document().append_child(move(comment));
  143. return;
  144. }
  145. if (token.is_character() && token.is_parser_whitespace()) {
  146. return;
  147. }
  148. if (token.is_start_tag() && token.tag_name() == "html") {
  149. auto element = create_element_for(token);
  150. document().append_child(element);
  151. m_stack_of_open_elements.push(move(element));
  152. m_insertion_mode = InsertionMode::BeforeHead;
  153. return;
  154. }
  155. if (token.is_end_tag() && token.tag_name().is_one_of("head", "body", "html", "br")) {
  156. goto AnythingElse;
  157. }
  158. if (token.is_end_tag()) {
  159. PARSE_ERROR();
  160. return;
  161. }
  162. AnythingElse:
  163. auto element = create_element(document(), "html");
  164. m_stack_of_open_elements.push(element);
  165. // FIXME: If the Document is being loaded as part of navigation of a browsing context, then: run the application cache selection algorithm with no manifest, passing it the Document object.
  166. m_insertion_mode = InsertionMode::BeforeHead;
  167. process_using_the_rules_for(InsertionMode::BeforeHead, token);
  168. return;
  169. }
  170. Element& HTMLDocumentParser::current_node()
  171. {
  172. return m_stack_of_open_elements.current_node();
  173. }
  174. RefPtr<Node> HTMLDocumentParser::find_appropriate_place_for_inserting_node()
  175. {
  176. auto& target = current_node();
  177. if (m_foster_parenting) {
  178. ASSERT_NOT_REACHED();
  179. }
  180. return target;
  181. }
  182. NonnullRefPtr<Element> HTMLDocumentParser::create_element_for(HTMLToken& token)
  183. {
  184. auto element = create_element(document(), token.tag_name());
  185. for (auto& attribute : token.m_tag.attributes) {
  186. element->set_attribute(attribute.name_builder.to_string(), attribute.value_builder.to_string());
  187. }
  188. return element;
  189. }
  190. RefPtr<Element> HTMLDocumentParser::insert_html_element(HTMLToken& token)
  191. {
  192. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  193. auto element = create_element_for(token);
  194. // FIXME: Check if it's possible to insert `element` at `adjusted_insertion_location`
  195. adjusted_insertion_location->append_child(element);
  196. m_stack_of_open_elements.push(element);
  197. return element;
  198. }
  199. void HTMLDocumentParser::handle_before_head(HTMLToken& token)
  200. {
  201. if (token.is_character() && token.is_parser_whitespace()) {
  202. return;
  203. }
  204. if (token.is_comment()) {
  205. insert_comment(token);
  206. return;
  207. }
  208. if (token.is_doctype()) {
  209. PARSE_ERROR();
  210. return;
  211. }
  212. if (token.is_start_tag() && token.tag_name() == "html") {
  213. process_using_the_rules_for(InsertionMode::InBody, token);
  214. return;
  215. }
  216. if (token.is_start_tag() && token.tag_name() == "head") {
  217. auto element = insert_html_element(token);
  218. m_head_element = to<HTMLHeadElement>(element);
  219. m_insertion_mode = InsertionMode::InHead;
  220. return;
  221. }
  222. if (token.is_end_tag() && token.tag_name().is_one_of("head", "body", "html", "br")) {
  223. goto AnythingElse;
  224. }
  225. if (token.is_end_tag()) {
  226. PARSE_ERROR();
  227. return;
  228. }
  229. AnythingElse:
  230. HTMLToken fake_head_token;
  231. fake_head_token.m_type = HTMLToken::Type::StartTag;
  232. fake_head_token.m_tag.tag_name.append("head");
  233. m_head_element = to<HTMLHeadElement>(insert_html_element(fake_head_token));
  234. m_insertion_mode = InsertionMode::InHead;
  235. process_using_the_rules_for(InsertionMode::InHead, token);
  236. return;
  237. }
  238. void HTMLDocumentParser::insert_comment(HTMLToken& token)
  239. {
  240. auto data = token.m_comment_or_character.data.to_string();
  241. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  242. adjusted_insertion_location->append_child(adopt(*new Comment(document(), data)));
  243. }
  244. void HTMLDocumentParser::handle_in_head(HTMLToken& token)
  245. {
  246. if (token.is_parser_whitespace()) {
  247. insert_character(token.codepoint());
  248. return;
  249. }
  250. if (token.is_comment()) {
  251. insert_comment(token);
  252. return;
  253. }
  254. if (token.is_doctype()) {
  255. PARSE_ERROR();
  256. return;
  257. }
  258. if (token.is_start_tag() && token.tag_name() == "html") {
  259. process_using_the_rules_for(InsertionMode::InBody, token);
  260. return;
  261. }
  262. if (token.is_start_tag() && token.tag_name().is_one_of("base", "basefont", "bgsound", "link")) {
  263. insert_html_element(token);
  264. m_stack_of_open_elements.pop();
  265. token.acknowledge_self_closing_flag_if_set();
  266. return;
  267. }
  268. if (token.is_start_tag() && token.tag_name() == "title") {
  269. insert_html_element(token);
  270. m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
  271. m_original_insertion_mode = m_insertion_mode;
  272. m_insertion_mode = InsertionMode::Text;
  273. return;
  274. }
  275. if (token.is_start_tag() && ((token.tag_name() == "noscript" && m_scripting_enabled) || token.tag_name() == "noframes" || token.tag_name() == "style")) {
  276. parse_generic_raw_text_element(token);
  277. return;
  278. }
  279. if (token.is_start_tag() && token.tag_name() == "script") {
  280. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  281. auto element = create_element_for(token);
  282. auto& script_element = to<HTMLScriptElement>(*element);
  283. script_element.set_parser_document({}, document());
  284. script_element.set_non_blocking({}, false);
  285. if (m_parsing_fragment) {
  286. TODO();
  287. }
  288. if (m_invoked_via_document_write) {
  289. TODO();
  290. }
  291. adjusted_insertion_location->append_child(element, false);
  292. m_stack_of_open_elements.push(element);
  293. m_tokenizer.switch_to({}, HTMLTokenizer::State::ScriptData);
  294. m_original_insertion_mode = m_insertion_mode;
  295. m_insertion_mode = InsertionMode::Text;
  296. return;
  297. }
  298. if (token.is_start_tag() && token.tag_name() == "meta") {
  299. auto element = insert_html_element(token);
  300. m_stack_of_open_elements.pop();
  301. token.acknowledge_self_closing_flag_if_set();
  302. return;
  303. }
  304. if (token.is_end_tag() && token.tag_name() == "head") {
  305. m_stack_of_open_elements.pop();
  306. m_insertion_mode = InsertionMode::AfterHead;
  307. return;
  308. }
  309. ASSERT_NOT_REACHED();
  310. }
  311. void HTMLDocumentParser::handle_in_head_noscript(HTMLToken&)
  312. {
  313. ASSERT_NOT_REACHED();
  314. }
  315. void HTMLDocumentParser::parse_generic_raw_text_element(HTMLToken& token)
  316. {
  317. insert_html_element(token);
  318. m_tokenizer.switch_to({}, HTMLTokenizer::State::RAWTEXT);
  319. m_original_insertion_mode = m_insertion_mode;
  320. m_insertion_mode = InsertionMode::Text;
  321. }
  322. void HTMLDocumentParser::insert_character(u32 data)
  323. {
  324. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  325. if (adjusted_insertion_location->is_document())
  326. return;
  327. if (adjusted_insertion_location->last_child() && adjusted_insertion_location->last_child()->is_text()) {
  328. auto& existing_text_node = to<Text>(*adjusted_insertion_location->last_child());
  329. StringBuilder builder;
  330. builder.append(existing_text_node.data());
  331. builder.append(Utf32View { &data, 1 });
  332. existing_text_node.set_data(builder.to_string());
  333. return;
  334. }
  335. auto new_text_node = adopt(*new Text(document(), ""));
  336. adjusted_insertion_location->append_child(new_text_node);
  337. StringBuilder builder;
  338. builder.append(Utf32View { &data, 1 });
  339. new_text_node->set_data(builder.to_string());
  340. }
  341. void HTMLDocumentParser::handle_after_head(HTMLToken& token)
  342. {
  343. if (token.is_character()) {
  344. if (token.is_parser_whitespace()) {
  345. insert_character(token.codepoint());
  346. return;
  347. }
  348. ASSERT_NOT_REACHED();
  349. }
  350. if (token.is_comment()) {
  351. ASSERT_NOT_REACHED();
  352. }
  353. if (token.is_doctype()) {
  354. ASSERT_NOT_REACHED();
  355. }
  356. if (token.is_start_tag() && token.tag_name() == "html") {
  357. ASSERT_NOT_REACHED();
  358. }
  359. if (token.is_start_tag() && token.tag_name() == "body") {
  360. insert_html_element(token);
  361. m_frameset_ok = false;
  362. m_insertion_mode = InsertionMode::InBody;
  363. return;
  364. }
  365. if (token.is_start_tag() && token.tag_name() == "frameset") {
  366. ASSERT_NOT_REACHED();
  367. }
  368. if (token.is_start_tag() && token.tag_name().is_one_of("base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title")) {
  369. ASSERT_NOT_REACHED();
  370. }
  371. if (token.is_end_tag() && token.tag_name() == "template") {
  372. ASSERT_NOT_REACHED();
  373. }
  374. if (token.is_end_tag() && token.tag_name().is_one_of("body", "html", "br")) {
  375. goto AnythingElse;
  376. }
  377. if ((token.is_start_tag() && token.tag_name() == "head") || token.is_end_tag()) {
  378. ASSERT_NOT_REACHED();
  379. }
  380. AnythingElse:
  381. HTMLToken fake_body_token;
  382. fake_body_token.m_type = HTMLToken::Type::StartTag;
  383. fake_body_token.m_tag.tag_name.append("body");
  384. insert_html_element(fake_body_token);
  385. m_insertion_mode = InsertionMode::InBody;
  386. // FIXME: Reprocess the current token in InBody!
  387. }
  388. void HTMLDocumentParser::generate_implied_end_tags(const FlyString& exception)
  389. {
  390. while (current_node().tag_name() != exception && current_node().tag_name().is_one_of("dd", "dt", "li", "optgroup", "option", "p", "rb", "rp", "rt", "rtc"))
  391. m_stack_of_open_elements.pop();
  392. }
  393. void HTMLDocumentParser::close_a_p_element()
  394. {
  395. generate_implied_end_tags("p");
  396. if (current_node().tag_name() != "p") {
  397. PARSE_ERROR();
  398. }
  399. for (;;) {
  400. auto popped_element = m_stack_of_open_elements.pop();
  401. if (popped_element->tag_name() == "p")
  402. break;
  403. }
  404. }
  405. void HTMLDocumentParser::handle_after_body(HTMLToken& token)
  406. {
  407. if (token.is_character() && token.is_parser_whitespace()) {
  408. process_using_the_rules_for(InsertionMode::InBody, token);
  409. return;
  410. }
  411. if (token.is_end_tag() && token.tag_name() == "html") {
  412. if (m_parsing_fragment) {
  413. ASSERT_NOT_REACHED();
  414. }
  415. m_insertion_mode = InsertionMode::AfterAfterBody;
  416. return;
  417. }
  418. ASSERT_NOT_REACHED();
  419. }
  420. void HTMLDocumentParser::handle_after_after_body(HTMLToken& token)
  421. {
  422. if (token.is_doctype() || token.is_parser_whitespace() || (token.is_start_tag() && token.tag_name() == "html")) {
  423. process_using_the_rules_for(InsertionMode::InBody, token);
  424. return;
  425. }
  426. if (token.is_end_of_file()) {
  427. dbg() << "Stop parsing! :^)";
  428. return;
  429. }
  430. ASSERT_NOT_REACHED();
  431. }
  432. void HTMLDocumentParser::reconstruct_the_active_formatting_elements()
  433. {
  434. // FIXME: This needs to care about "markers"
  435. if (m_list_of_active_formatting_elements.is_empty())
  436. return;
  437. if (m_stack_of_open_elements.contains(*m_list_of_active_formatting_elements.entries().last().element))
  438. return;
  439. ssize_t index = m_list_of_active_formatting_elements.entries().size() - 1;
  440. RefPtr<Element> entry = m_list_of_active_formatting_elements.entries().at(index).element;
  441. ASSERT(entry);
  442. Rewind:
  443. if (index == 0) {
  444. goto Create;
  445. }
  446. --index;
  447. entry = m_list_of_active_formatting_elements.entries().at(index).element;
  448. ASSERT(entry);
  449. if (!m_stack_of_open_elements.contains(*entry))
  450. goto Rewind;
  451. Advance:
  452. ++index;
  453. entry = m_list_of_active_formatting_elements.entries().at(index).element;
  454. ASSERT(entry);
  455. Create:
  456. // FIXME: Hold on to the real token!
  457. HTMLToken fake_token;
  458. fake_token.m_type = HTMLToken::Type::StartTag;
  459. fake_token.m_tag.tag_name.append(entry->tag_name());
  460. auto new_element = insert_html_element(fake_token);
  461. m_list_of_active_formatting_elements.entries().at(index).element = *new_element;
  462. if (index != (ssize_t)m_list_of_active_formatting_elements.entries().size() - 1)
  463. goto Advance;
  464. }
  465. void HTMLDocumentParser::run_the_adoption_agency_algorithm(HTMLToken& token)
  466. {
  467. auto subject = token.tag_name();
  468. // If the current node is an HTML element whose tag name is subject,
  469. // and the current node is not in the list of active formatting elements,
  470. // then pop the current node off the stack of open elements, and return.
  471. if (current_node().tag_name() == subject && !m_list_of_active_formatting_elements.contains(current_node())) {
  472. m_stack_of_open_elements.pop();
  473. return;
  474. }
  475. size_t outer_loop_counter = 0;
  476. //OuterLoop:
  477. if (outer_loop_counter >= 8)
  478. return;
  479. ++outer_loop_counter;
  480. auto formatting_element = m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker(subject);
  481. if (!formatting_element) {
  482. // FIXME: If there is no such element, then return and instead act as
  483. // described in the "any other end tag" entry above.
  484. TODO();
  485. }
  486. if (!m_stack_of_open_elements.contains(*formatting_element)) {
  487. PARSE_ERROR();
  488. // FIXME: If formatting element is not in the stack of open elements,
  489. // then this is a parse error; remove the element from the list, and return.
  490. TODO();
  491. }
  492. if (!m_stack_of_open_elements.has_in_scope(*formatting_element)) {
  493. PARSE_ERROR();
  494. return;
  495. }
  496. if (formatting_element != &current_node()) {
  497. PARSE_ERROR();
  498. }
  499. // FIXME: Let furthest block be the topmost node in the stack of open elements
  500. // that is lower in the stack than formatting element, and is an element
  501. // in the special category. There might not be one.
  502. RefPtr<Element> furthest_block = nullptr;
  503. if (!furthest_block) {
  504. while (&current_node() != formatting_element)
  505. m_stack_of_open_elements.pop();
  506. m_stack_of_open_elements.pop();
  507. m_list_of_active_formatting_elements.remove(*formatting_element);
  508. return;
  509. }
  510. // FIXME: Implement the rest of the AAA :^)
  511. TODO();
  512. }
  513. void HTMLDocumentParser::handle_in_body(HTMLToken& token)
  514. {
  515. if (token.is_character()) {
  516. if (token.codepoint() == 0) {
  517. ASSERT_NOT_REACHED();
  518. }
  519. if (token.is_parser_whitespace()) {
  520. reconstruct_the_active_formatting_elements();
  521. insert_character(token.codepoint());
  522. return;
  523. }
  524. reconstruct_the_active_formatting_elements();
  525. insert_character(token.codepoint());
  526. m_frameset_ok = false;
  527. return;
  528. }
  529. if (token.is_end_tag() && token.tag_name() == "body") {
  530. if (!m_stack_of_open_elements.has_in_scope("body")) {
  531. ASSERT_NOT_REACHED();
  532. }
  533. // FIXME: Otherwise, if there is a node in the stack of open elements that is
  534. // not either a dd element, a dt element, an li element, an optgroup element,
  535. // an option element, a p element, an rb element, an rp element, an rt element,
  536. // an rtc element, a tbody element, a td element, a tfoot element, a th element,
  537. // a thead element, a tr element, the body element, or the html element,
  538. // then this is a parse error.
  539. m_insertion_mode = InsertionMode::AfterBody;
  540. return;
  541. }
  542. if (token.is_start_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  543. if (m_stack_of_open_elements.has_in_button_scope("p"))
  544. close_a_p_element();
  545. if (current_node().tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  546. PARSE_ERROR();
  547. m_stack_of_open_elements.pop();
  548. }
  549. insert_html_element(token);
  550. return;
  551. }
  552. if (token.is_end_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  553. if (!m_stack_of_open_elements.has_in_scope("h1")
  554. && !m_stack_of_open_elements.has_in_scope("h2")
  555. && !m_stack_of_open_elements.has_in_scope("h3")
  556. && !m_stack_of_open_elements.has_in_scope("h4")
  557. && !m_stack_of_open_elements.has_in_scope("h5")
  558. && !m_stack_of_open_elements.has_in_scope("h6")) {
  559. PARSE_ERROR();
  560. return;
  561. }
  562. generate_implied_end_tags();
  563. if (current_node().tag_name() != token.tag_name()) {
  564. PARSE_ERROR();
  565. }
  566. for (;;) {
  567. auto popped_element = m_stack_of_open_elements.pop();
  568. if (popped_element->tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6"))
  569. break;
  570. }
  571. return;
  572. }
  573. if (token.is_end_tag() && token.tag_name() == "p") {
  574. if (!m_stack_of_open_elements.has_in_button_scope("p")) {
  575. TODO();
  576. }
  577. close_a_p_element();
  578. return;
  579. }
  580. if (token.is_start_tag() && token.tag_name().is_one_of("b", "big", "code", "em", "font", "i", "s", "small", "strike", "strong", "tt", "u")) {
  581. reconstruct_the_active_formatting_elements();
  582. auto element = insert_html_element(token);
  583. m_list_of_active_formatting_elements.add(*element);
  584. return;
  585. }
  586. if (token.is_end_tag() && token.tag_name().is_one_of("a", "b", "big", "code", "em", "font", "i", "nobr", "s", "small", "strike", "strong", "tt", "u")) {
  587. run_the_adoption_agency_algorithm(token);
  588. return;
  589. }
  590. 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")) {
  591. if (m_stack_of_open_elements.has_in_button_scope("p"))
  592. close_a_p_element();
  593. insert_html_element(token);
  594. return;
  595. }
  596. 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")) {
  597. if (m_stack_of_open_elements.has_in_button_scope("p"))
  598. close_a_p_element();
  599. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  600. PARSE_ERROR();
  601. return;
  602. }
  603. generate_implied_end_tags();
  604. if (current_node().tag_name() != token.tag_name()) {
  605. PARSE_ERROR();
  606. }
  607. m_stack_of_open_elements.pop();
  608. return;
  609. }
  610. if (token.is_start_tag() && token.tag_name() == "table") {
  611. // FIXME: If the Document is not set to quirks mode,
  612. // and the stack of open elements has a p element in button scope, then close a p element.
  613. insert_html_element(token);
  614. m_frameset_ok = false;
  615. m_insertion_mode = InsertionMode::InTable;
  616. return;
  617. }
  618. if (token.is_start_tag() && token.tag_name().is_one_of("area", "br", "embed", "img", "keygen", "wbr")) {
  619. reconstruct_the_active_formatting_elements();
  620. insert_html_element(token);
  621. m_stack_of_open_elements.pop();
  622. token.acknowledge_self_closing_flag_if_set();
  623. m_frameset_ok = false;
  624. return;
  625. }
  626. if (token.is_start_tag()) {
  627. reconstruct_the_active_formatting_elements();
  628. insert_html_element(token);
  629. return;
  630. }
  631. if (token.is_end_tag()) {
  632. RefPtr<Element> node;
  633. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  634. node = m_stack_of_open_elements.elements()[i];
  635. if (node->tag_name() == token.tag_name()) {
  636. generate_implied_end_tags(token.tag_name());
  637. if (node != current_node()) {
  638. PARSE_ERROR();
  639. }
  640. while (&current_node() != node) {
  641. m_stack_of_open_elements.pop();
  642. }
  643. m_stack_of_open_elements.pop();
  644. break;
  645. }
  646. // FIXME: Handle special elements!
  647. }
  648. return;
  649. }
  650. ASSERT_NOT_REACHED();
  651. }
  652. void HTMLDocumentParser::increment_script_nesting_level()
  653. {
  654. ++m_script_nesting_level;
  655. }
  656. void HTMLDocumentParser::decrement_script_nesting_level()
  657. {
  658. ASSERT(m_script_nesting_level);
  659. --m_script_nesting_level;
  660. }
  661. void HTMLDocumentParser::handle_text(HTMLToken& token)
  662. {
  663. if (token.is_character()) {
  664. insert_character(token.codepoint());
  665. return;
  666. }
  667. if (token.is_end_tag() && token.tag_name() == "script") {
  668. NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
  669. m_stack_of_open_elements.pop();
  670. m_insertion_mode = m_original_insertion_mode;
  671. // FIXME: Handle tokenizer insertion point stuff here.
  672. increment_script_nesting_level();
  673. script->prepare_script({});
  674. decrement_script_nesting_level();
  675. if (script_nesting_level() == 0)
  676. m_parser_pause_flag = false;
  677. // FIXME: Handle tokenizer insertion point stuff here too.
  678. while (document().pending_parsing_blocking_script()) {
  679. if (script_nesting_level() != 0) {
  680. m_parser_pause_flag = true;
  681. // FIXME: Abort the processing of any nested invocations of the tokenizer,
  682. // yielding control back to the caller. (Tokenization will resume when
  683. // the caller returns to the "outer" tree construction stage.)
  684. TODO();
  685. } else {
  686. auto the_script = document().take_pending_parsing_blocking_script({});
  687. m_tokenizer.set_blocked(true);
  688. // FIXME: If the parser's Document has a style sheet that is blocking scripts
  689. // or the script's "ready to be parser-executed" flag is not set:
  690. // spin the event loop until the parser's Document has no style sheet
  691. // that is blocking scripts and the script's "ready to be parser-executed"
  692. // flag is set.
  693. ASSERT(the_script->is_ready_to_be_parser_executed());
  694. if (m_aborted)
  695. return;
  696. m_tokenizer.set_blocked(false);
  697. // FIXME: Handle tokenizer insertion point stuff here too.
  698. ASSERT(script_nesting_level() == 0);
  699. increment_script_nesting_level();
  700. the_script->execute_script();
  701. decrement_script_nesting_level();
  702. ASSERT(script_nesting_level() == 0);
  703. m_parser_pause_flag = false;
  704. // FIXME: Handle tokenizer insertion point stuff here too.
  705. }
  706. }
  707. return;
  708. }
  709. // FIXME: This is a bit hackish, we can simplify this once we don't need to support
  710. // the old parser anymore, since then we don't need to maintain its children_changed() semantics.
  711. if (token.is_end_tag() && token.tag_name() == "style") {
  712. current_node().children_changed();
  713. // NOTE: We don't return here, keep going.
  714. }
  715. if (token.is_end_tag()) {
  716. m_stack_of_open_elements.pop();
  717. m_insertion_mode = m_original_insertion_mode;
  718. return;
  719. }
  720. ASSERT_NOT_REACHED();
  721. }
  722. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  723. {
  724. if (token.is_character() && current_node().tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  725. TODO();
  726. }
  727. if (token.is_comment()) {
  728. insert_comment(token);
  729. return;
  730. }
  731. if (token.is_doctype()) {
  732. PARSE_ERROR();
  733. return;
  734. }
  735. if (token.is_start_tag() && token.tag_name() == "caption") {
  736. TODO();
  737. }
  738. if (token.is_start_tag() && token.tag_name() == "colgroup") {
  739. TODO();
  740. }
  741. if (token.is_start_tag() && token.tag_name() == "col") {
  742. TODO();
  743. }
  744. if (token.is_start_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  745. TODO();
  746. }
  747. if (token.is_start_tag() && token.tag_name().is_one_of("td", "th", "tr")) {
  748. TODO();
  749. }
  750. if (token.is_start_tag() && token.tag_name() == "table") {
  751. PARSE_ERROR();
  752. TODO();
  753. }
  754. if (token.is_end_tag()) {
  755. if (!m_stack_of_open_elements.has_in_table_scope("table")) {
  756. PARSE_ERROR();
  757. return;
  758. }
  759. while (current_node().tag_name() != "table")
  760. m_stack_of_open_elements.pop();
  761. m_stack_of_open_elements.pop();
  762. reset_the_insertion_mode_appropriately();
  763. return;
  764. }
  765. TODO();
  766. }
  767. void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
  768. {
  769. TODO();
  770. }
  771. const char* HTMLDocumentParser::insertion_mode_name() const
  772. {
  773. switch (m_insertion_mode) {
  774. #define __ENUMERATE_INSERTION_MODE(mode) \
  775. case InsertionMode::mode: \
  776. return #mode;
  777. ENUMERATE_INSERTION_MODES
  778. #undef __ENUMERATE_INSERTION_MODE
  779. }
  780. ASSERT_NOT_REACHED();
  781. }
  782. Document& HTMLDocumentParser::document()
  783. {
  784. return *m_document;
  785. }
  786. }