HTMLDocumentParser.cpp 35 KB

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