HTMLDocumentParser.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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.last()))
  427. return;
  428. ssize_t index = m_list_of_active_formatting_elements.size() - 1;
  429. RefPtr<Element> entry = m_list_of_active_formatting_elements.at(index);
  430. Rewind:
  431. if (index == 0) {
  432. goto Create;
  433. }
  434. --index;
  435. entry = m_list_of_active_formatting_elements.at(index);
  436. if (!m_stack_of_open_elements.contains(*entry))
  437. goto Rewind;
  438. Advance:
  439. ++index;
  440. entry = m_list_of_active_formatting_elements.at(index);
  441. Create:
  442. // FIXME: Hold on to the real token!
  443. HTMLToken fake_token;
  444. fake_token.m_type = HTMLToken::Type::StartTag;
  445. fake_token.m_tag.tag_name.append(entry->tag_name());
  446. auto new_element = insert_html_element(fake_token);
  447. m_list_of_active_formatting_elements.ptr_at(index) = *new_element;
  448. if (index != (ssize_t)m_list_of_active_formatting_elements.size() - 1)
  449. goto Advance;
  450. }
  451. void HTMLDocumentParser::handle_in_body(HTMLToken& token)
  452. {
  453. if (token.is_character()) {
  454. if (token.codepoint() == 0) {
  455. ASSERT_NOT_REACHED();
  456. }
  457. if (token.is_parser_whitespace()) {
  458. reconstruct_the_active_formatting_elements();
  459. insert_character(token.codepoint());
  460. return;
  461. }
  462. reconstruct_the_active_formatting_elements();
  463. insert_character(token.codepoint());
  464. m_frameset_ok = false;
  465. return;
  466. }
  467. if (token.is_end_tag() && token.tag_name() == "body") {
  468. if (!m_stack_of_open_elements.has_in_scope("body")) {
  469. ASSERT_NOT_REACHED();
  470. }
  471. // FIXME: Otherwise, if there is a node in the stack of open elements that is
  472. // not either a dd element, a dt element, an li element, an optgroup element,
  473. // an option element, a p element, an rb element, an rp element, an rt element,
  474. // an rtc element, a tbody element, a td element, a tfoot element, a th element,
  475. // a thead element, a tr element, the body element, or the html element,
  476. // then this is a parse error.
  477. m_insertion_mode = InsertionMode::AfterBody;
  478. return;
  479. }
  480. if (token.is_start_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  481. if (m_stack_of_open_elements.has_in_button_scope("p"))
  482. close_a_p_element();
  483. if (current_node().tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  484. PARSE_ERROR();
  485. m_stack_of_open_elements.pop();
  486. }
  487. insert_html_element(token);
  488. return;
  489. }
  490. if (token.is_end_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  491. if (!m_stack_of_open_elements.has_in_scope("h1")
  492. && !m_stack_of_open_elements.has_in_scope("h2")
  493. && !m_stack_of_open_elements.has_in_scope("h3")
  494. && !m_stack_of_open_elements.has_in_scope("h4")
  495. && !m_stack_of_open_elements.has_in_scope("h5")
  496. && !m_stack_of_open_elements.has_in_scope("h6")) {
  497. PARSE_ERROR();
  498. return;
  499. }
  500. generate_implied_end_tags();
  501. if (current_node().tag_name() != token.tag_name()) {
  502. PARSE_ERROR();
  503. }
  504. for (;;) {
  505. auto popped_element = m_stack_of_open_elements.pop();
  506. if (popped_element->tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6"))
  507. break;
  508. }
  509. return;
  510. }
  511. if (token.is_end_tag() && token.tag_name() == "p") {
  512. if (!m_stack_of_open_elements.has_in_button_scope("p")) {
  513. TODO();
  514. }
  515. close_a_p_element();
  516. return;
  517. }
  518. {
  519. if (token.is_start_tag() && token.tag_name().is_one_of("b", "big", "code", "em", "font", "i", "s", "small", "strike", "strong", "tt", "u")) {
  520. reconstruct_the_active_formatting_elements();
  521. auto element = insert_html_element(token);
  522. m_list_of_active_formatting_elements.append(*element);
  523. return;
  524. }
  525. }
  526. 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")) {
  527. if (m_stack_of_open_elements.has_in_button_scope("p"))
  528. close_a_p_element();
  529. insert_html_element(token);
  530. return;
  531. }
  532. 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")) {
  533. if (m_stack_of_open_elements.has_in_button_scope("p"))
  534. close_a_p_element();
  535. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  536. PARSE_ERROR();
  537. return;
  538. }
  539. generate_implied_end_tags();
  540. if (current_node().tag_name() != token.tag_name()) {
  541. PARSE_ERROR();
  542. }
  543. m_stack_of_open_elements.pop();
  544. return;
  545. }
  546. if (token.is_start_tag() && token.tag_name() == "table") {
  547. // FIXME: If the Document is not set to quirks mode,
  548. // and the stack of open elements has a p element in button scope, then close a p element.
  549. insert_html_element(token);
  550. m_frameset_ok = false;
  551. m_insertion_mode = InsertionMode::InTable;
  552. return;
  553. }
  554. if (token.is_start_tag()) {
  555. reconstruct_the_active_formatting_elements();
  556. insert_html_element(token);
  557. return;
  558. }
  559. if (token.is_end_tag()) {
  560. RefPtr<Element> node;
  561. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  562. node = m_stack_of_open_elements.elements()[i];
  563. if (node->tag_name() == token.tag_name()) {
  564. generate_implied_end_tags(token.tag_name());
  565. if (node != current_node()) {
  566. PARSE_ERROR();
  567. }
  568. while (&current_node() != node) {
  569. m_stack_of_open_elements.pop();
  570. }
  571. m_stack_of_open_elements.pop();
  572. break;
  573. }
  574. // FIXME: Handle special elements!
  575. }
  576. return;
  577. }
  578. ASSERT_NOT_REACHED();
  579. }
  580. void HTMLDocumentParser::increment_script_nesting_level()
  581. {
  582. ++m_script_nesting_level;
  583. }
  584. void HTMLDocumentParser::decrement_script_nesting_level()
  585. {
  586. ASSERT(m_script_nesting_level);
  587. --m_script_nesting_level;
  588. }
  589. void HTMLDocumentParser::handle_text(HTMLToken& token)
  590. {
  591. if (token.is_character()) {
  592. insert_character(token.codepoint());
  593. return;
  594. }
  595. if (token.is_end_tag() && token.tag_name() == "script") {
  596. NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
  597. m_stack_of_open_elements.pop();
  598. m_insertion_mode = m_original_insertion_mode;
  599. // FIXME: Handle tokenizer insertion point stuff here.
  600. increment_script_nesting_level();
  601. script->prepare_script({});
  602. decrement_script_nesting_level();
  603. if (script_nesting_level() == 0)
  604. m_parser_pause_flag = false;
  605. // FIXME: Handle tokenizer insertion point stuff here too.
  606. while (document().pending_parsing_blocking_script()) {
  607. if (script_nesting_level() != 0) {
  608. m_parser_pause_flag = true;
  609. // FIXME: Abort the processing of any nested invocations of the tokenizer,
  610. // yielding control back to the caller. (Tokenization will resume when
  611. // the caller returns to the "outer" tree construction stage.)
  612. TODO();
  613. } else {
  614. auto the_script = document().take_pending_parsing_blocking_script({});
  615. m_tokenizer.set_blocked(true);
  616. // FIXME: If the parser's Document has a style sheet that is blocking scripts
  617. // or the script's "ready to be parser-executed" flag is not set:
  618. // spin the event loop until the parser's Document has no style sheet
  619. // that is blocking scripts and the script's "ready to be parser-executed"
  620. // flag is set.
  621. ASSERT(the_script->is_ready_to_be_parser_executed());
  622. if (m_aborted)
  623. return;
  624. m_tokenizer.set_blocked(false);
  625. // FIXME: Handle tokenizer insertion point stuff here too.
  626. ASSERT(script_nesting_level() == 0);
  627. increment_script_nesting_level();
  628. the_script->execute_script();
  629. decrement_script_nesting_level();
  630. ASSERT(script_nesting_level() == 0);
  631. m_parser_pause_flag = false;
  632. // FIXME: Handle tokenizer insertion point stuff here too.
  633. }
  634. }
  635. return;
  636. }
  637. if (token.is_end_tag()) {
  638. m_stack_of_open_elements.pop();
  639. m_insertion_mode = m_original_insertion_mode;
  640. return;
  641. }
  642. ASSERT_NOT_REACHED();
  643. }
  644. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  645. {
  646. if (token.is_character() && current_node().tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  647. TODO();
  648. }
  649. if (token.is_comment()) {
  650. insert_comment(token);
  651. return;
  652. }
  653. if (token.is_doctype()) {
  654. PARSE_ERROR();
  655. return;
  656. }
  657. if (token.is_start_tag() && token.tag_name() == "caption") {
  658. TODO();
  659. }
  660. if (token.is_start_tag() && token.tag_name() == "colgroup") {
  661. TODO();
  662. }
  663. if (token.is_start_tag() && token.tag_name() == "col") {
  664. TODO();
  665. }
  666. if (token.is_start_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  667. TODO();
  668. }
  669. if (token.is_start_tag() && token.tag_name().is_one_of("td", "th", "tr")) {
  670. TODO();
  671. }
  672. if (token.is_start_tag() && token.tag_name() == "table") {
  673. PARSE_ERROR();
  674. TODO();
  675. }
  676. if (token.is_end_tag()) {
  677. if (!m_stack_of_open_elements.has_in_table_scope("table")) {
  678. PARSE_ERROR();
  679. return;
  680. }
  681. while (current_node().tag_name() != "table")
  682. m_stack_of_open_elements.pop();
  683. m_stack_of_open_elements.pop();
  684. reset_the_insertion_mode_appropriately();
  685. return;
  686. }
  687. TODO();
  688. }
  689. void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
  690. {
  691. TODO();
  692. }
  693. const char* HTMLDocumentParser::insertion_mode_name() const
  694. {
  695. switch (m_insertion_mode) {
  696. #define __ENUMERATE_INSERTION_MODE(mode) \
  697. case InsertionMode::mode: \
  698. return #mode;
  699. ENUMERATE_INSERTION_MODES
  700. #undef __ENUMERATE_INSERTION_MODE
  701. }
  702. ASSERT_NOT_REACHED();
  703. }
  704. Document& HTMLDocumentParser::document()
  705. {
  706. return *m_document;
  707. }
  708. }