HTMLDocumentParser.cpp 36 KB

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