HTMLDocumentParser.cpp 28 KB

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