HTMLDocumentParser.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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_comment()) {
  418. TODO();
  419. }
  420. if (token.is_doctype()) {
  421. PARSE_ERROR();
  422. return;
  423. }
  424. if (token.is_start_tag() && token.tag_name() == "html") {
  425. process_using_the_rules_for(InsertionMode::InBody, token);
  426. return;
  427. }
  428. if (token.is_end_of_file()) {
  429. // FIXME: Stop parsing!
  430. TODO();
  431. }
  432. if (token.is_end_tag() && token.tag_name() == "html") {
  433. if (m_parsing_fragment) {
  434. ASSERT_NOT_REACHED();
  435. }
  436. m_insertion_mode = InsertionMode::AfterAfterBody;
  437. return;
  438. }
  439. PARSE_ERROR();
  440. m_insertion_mode = InsertionMode::InBody;
  441. process_using_the_rules_for(InsertionMode::InBody, token);
  442. }
  443. void HTMLDocumentParser::handle_after_after_body(HTMLToken& token)
  444. {
  445. if (token.is_doctype() || token.is_parser_whitespace() || (token.is_start_tag() && token.tag_name() == "html")) {
  446. process_using_the_rules_for(InsertionMode::InBody, token);
  447. return;
  448. }
  449. if (token.is_end_of_file()) {
  450. dbg() << "Stop parsing! :^)";
  451. return;
  452. }
  453. ASSERT_NOT_REACHED();
  454. }
  455. void HTMLDocumentParser::reconstruct_the_active_formatting_elements()
  456. {
  457. // FIXME: This needs to care about "markers"
  458. if (m_list_of_active_formatting_elements.is_empty())
  459. return;
  460. if (m_list_of_active_formatting_elements.entries().last().is_marker())
  461. return;
  462. if (m_stack_of_open_elements.contains(*m_list_of_active_formatting_elements.entries().last().element))
  463. return;
  464. ssize_t index = m_list_of_active_formatting_elements.entries().size() - 1;
  465. RefPtr<Element> entry = m_list_of_active_formatting_elements.entries().at(index).element;
  466. ASSERT(entry);
  467. Rewind:
  468. if (index == 0) {
  469. goto Create;
  470. }
  471. --index;
  472. entry = m_list_of_active_formatting_elements.entries().at(index).element;
  473. ASSERT(entry);
  474. if (!m_stack_of_open_elements.contains(*entry))
  475. goto Rewind;
  476. Advance:
  477. ++index;
  478. entry = m_list_of_active_formatting_elements.entries().at(index).element;
  479. ASSERT(entry);
  480. Create:
  481. // FIXME: Hold on to the real token!
  482. HTMLToken fake_token;
  483. fake_token.m_type = HTMLToken::Type::StartTag;
  484. fake_token.m_tag.tag_name.append(entry->tag_name());
  485. auto new_element = insert_html_element(fake_token);
  486. m_list_of_active_formatting_elements.entries().at(index).element = *new_element;
  487. if (index != (ssize_t)m_list_of_active_formatting_elements.entries().size() - 1)
  488. goto Advance;
  489. }
  490. void HTMLDocumentParser::run_the_adoption_agency_algorithm(HTMLToken& token)
  491. {
  492. auto subject = token.tag_name();
  493. // If the current node is an HTML element whose tag name is subject,
  494. // and the current node is not in the list of active formatting elements,
  495. // then pop the current node off the stack of open elements, and return.
  496. if (current_node().tag_name() == subject && !m_list_of_active_formatting_elements.contains(current_node())) {
  497. m_stack_of_open_elements.pop();
  498. return;
  499. }
  500. size_t outer_loop_counter = 0;
  501. //OuterLoop:
  502. if (outer_loop_counter >= 8)
  503. return;
  504. ++outer_loop_counter;
  505. auto formatting_element = m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker(subject);
  506. if (!formatting_element) {
  507. // FIXME: If there is no such element, then return and instead act as
  508. // described in the "any other end tag" entry above.
  509. TODO();
  510. }
  511. if (!m_stack_of_open_elements.contains(*formatting_element)) {
  512. PARSE_ERROR();
  513. // FIXME: If formatting element is not in the stack of open elements,
  514. // then this is a parse error; remove the element from the list, and return.
  515. TODO();
  516. }
  517. if (!m_stack_of_open_elements.has_in_scope(*formatting_element)) {
  518. PARSE_ERROR();
  519. return;
  520. }
  521. if (formatting_element != &current_node()) {
  522. PARSE_ERROR();
  523. }
  524. // FIXME: Let furthest block be the topmost node in the stack of open elements
  525. // that is lower in the stack than formatting element, and is an element
  526. // in the special category. There might not be one.
  527. RefPtr<Element> furthest_block = nullptr;
  528. if (!furthest_block) {
  529. while (&current_node() != formatting_element)
  530. m_stack_of_open_elements.pop();
  531. m_stack_of_open_elements.pop();
  532. m_list_of_active_formatting_elements.remove(*formatting_element);
  533. return;
  534. }
  535. // FIXME: Implement the rest of the AAA :^)
  536. TODO();
  537. }
  538. void HTMLDocumentParser::handle_in_body(HTMLToken& token)
  539. {
  540. if (token.is_character()) {
  541. if (token.codepoint() == 0) {
  542. ASSERT_NOT_REACHED();
  543. }
  544. if (token.is_parser_whitespace()) {
  545. reconstruct_the_active_formatting_elements();
  546. insert_character(token.codepoint());
  547. return;
  548. }
  549. reconstruct_the_active_formatting_elements();
  550. insert_character(token.codepoint());
  551. m_frameset_ok = false;
  552. return;
  553. }
  554. if (token.is_comment()) {
  555. insert_comment(token);
  556. return;
  557. }
  558. if (token.is_end_tag() && token.tag_name() == "body") {
  559. if (!m_stack_of_open_elements.has_in_scope("body")) {
  560. ASSERT_NOT_REACHED();
  561. }
  562. // FIXME: Otherwise, if there is a node in the stack of open elements that is
  563. // not either a dd element, a dt element, an li element, an optgroup element,
  564. // an option element, a p element, an rb element, an rp element, an rt element,
  565. // an rtc element, a tbody element, a td element, a tfoot element, a th element,
  566. // a thead element, a tr element, the body element, or the html element,
  567. // then this is a parse error.
  568. m_insertion_mode = InsertionMode::AfterBody;
  569. return;
  570. }
  571. if (token.is_start_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  572. if (m_stack_of_open_elements.has_in_button_scope("p"))
  573. close_a_p_element();
  574. if (current_node().tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  575. PARSE_ERROR();
  576. m_stack_of_open_elements.pop();
  577. }
  578. insert_html_element(token);
  579. return;
  580. }
  581. if (token.is_end_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  582. if (!m_stack_of_open_elements.has_in_scope("h1")
  583. && !m_stack_of_open_elements.has_in_scope("h2")
  584. && !m_stack_of_open_elements.has_in_scope("h3")
  585. && !m_stack_of_open_elements.has_in_scope("h4")
  586. && !m_stack_of_open_elements.has_in_scope("h5")
  587. && !m_stack_of_open_elements.has_in_scope("h6")) {
  588. PARSE_ERROR();
  589. return;
  590. }
  591. generate_implied_end_tags();
  592. if (current_node().tag_name() != token.tag_name()) {
  593. PARSE_ERROR();
  594. }
  595. for (;;) {
  596. auto popped_element = m_stack_of_open_elements.pop();
  597. if (popped_element->tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6"))
  598. break;
  599. }
  600. return;
  601. }
  602. if (token.is_end_tag() && token.tag_name() == "p") {
  603. if (!m_stack_of_open_elements.has_in_button_scope("p")) {
  604. TODO();
  605. }
  606. close_a_p_element();
  607. return;
  608. }
  609. if (token.is_start_tag() && token.tag_name().is_one_of("b", "big", "code", "em", "font", "i", "s", "small", "strike", "strong", "tt", "u")) {
  610. reconstruct_the_active_formatting_elements();
  611. auto element = insert_html_element(token);
  612. m_list_of_active_formatting_elements.add(*element);
  613. return;
  614. }
  615. 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")) {
  616. run_the_adoption_agency_algorithm(token);
  617. return;
  618. }
  619. 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")) {
  620. if (m_stack_of_open_elements.has_in_button_scope("p"))
  621. close_a_p_element();
  622. insert_html_element(token);
  623. return;
  624. }
  625. 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")) {
  626. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  627. PARSE_ERROR();
  628. return;
  629. }
  630. generate_implied_end_tags();
  631. if (current_node().tag_name() != token.tag_name()) {
  632. PARSE_ERROR();
  633. }
  634. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  635. return;
  636. }
  637. if (token.is_start_tag() && token.tag_name() == "table") {
  638. // FIXME: If the Document is not set to quirks mode,
  639. // and the stack of open elements has a p element in button scope, then close a p element.
  640. insert_html_element(token);
  641. m_frameset_ok = false;
  642. m_insertion_mode = InsertionMode::InTable;
  643. return;
  644. }
  645. if (token.is_start_tag() && token.tag_name().is_one_of("area", "br", "embed", "img", "keygen", "wbr")) {
  646. reconstruct_the_active_formatting_elements();
  647. insert_html_element(token);
  648. m_stack_of_open_elements.pop();
  649. token.acknowledge_self_closing_flag_if_set();
  650. m_frameset_ok = false;
  651. return;
  652. }
  653. if (token.is_start_tag() && token.tag_name() == "input") {
  654. reconstruct_the_active_formatting_elements();
  655. insert_html_element(token);
  656. m_stack_of_open_elements.pop();
  657. token.acknowledge_self_closing_flag_if_set();
  658. auto type_attribute = token.attribute(HTML::AttributeNames::type);
  659. if (type_attribute.is_null() || type_attribute != "hidden") {
  660. m_frameset_ok = false;
  661. }
  662. return;
  663. }
  664. if (token.is_start_tag()) {
  665. reconstruct_the_active_formatting_elements();
  666. insert_html_element(token);
  667. return;
  668. }
  669. if (token.is_end_tag()) {
  670. RefPtr<Element> node;
  671. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  672. node = m_stack_of_open_elements.elements()[i];
  673. if (node->tag_name() == token.tag_name()) {
  674. generate_implied_end_tags(token.tag_name());
  675. if (node != current_node()) {
  676. PARSE_ERROR();
  677. }
  678. while (&current_node() != node) {
  679. m_stack_of_open_elements.pop();
  680. }
  681. m_stack_of_open_elements.pop();
  682. break;
  683. }
  684. // FIXME: Handle special elements!
  685. }
  686. return;
  687. }
  688. ASSERT_NOT_REACHED();
  689. }
  690. void HTMLDocumentParser::increment_script_nesting_level()
  691. {
  692. ++m_script_nesting_level;
  693. }
  694. void HTMLDocumentParser::decrement_script_nesting_level()
  695. {
  696. ASSERT(m_script_nesting_level);
  697. --m_script_nesting_level;
  698. }
  699. void HTMLDocumentParser::handle_text(HTMLToken& token)
  700. {
  701. if (token.is_character()) {
  702. insert_character(token.codepoint());
  703. return;
  704. }
  705. if (token.is_end_tag() && token.tag_name() == "script") {
  706. NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
  707. m_stack_of_open_elements.pop();
  708. m_insertion_mode = m_original_insertion_mode;
  709. // FIXME: Handle tokenizer insertion point stuff here.
  710. increment_script_nesting_level();
  711. script->prepare_script({});
  712. decrement_script_nesting_level();
  713. if (script_nesting_level() == 0)
  714. m_parser_pause_flag = false;
  715. // FIXME: Handle tokenizer insertion point stuff here too.
  716. while (document().pending_parsing_blocking_script()) {
  717. if (script_nesting_level() != 0) {
  718. m_parser_pause_flag = true;
  719. // FIXME: Abort the processing of any nested invocations of the tokenizer,
  720. // yielding control back to the caller. (Tokenization will resume when
  721. // the caller returns to the "outer" tree construction stage.)
  722. TODO();
  723. } else {
  724. auto the_script = document().take_pending_parsing_blocking_script({});
  725. m_tokenizer.set_blocked(true);
  726. // FIXME: If the parser's Document has a style sheet that is blocking scripts
  727. // or the script's "ready to be parser-executed" flag is not set:
  728. // spin the event loop until the parser's Document has no style sheet
  729. // that is blocking scripts and the script's "ready to be parser-executed"
  730. // flag is set.
  731. ASSERT(the_script->is_ready_to_be_parser_executed());
  732. if (m_aborted)
  733. return;
  734. m_tokenizer.set_blocked(false);
  735. // FIXME: Handle tokenizer insertion point stuff here too.
  736. ASSERT(script_nesting_level() == 0);
  737. increment_script_nesting_level();
  738. the_script->execute_script();
  739. decrement_script_nesting_level();
  740. ASSERT(script_nesting_level() == 0);
  741. m_parser_pause_flag = false;
  742. // FIXME: Handle tokenizer insertion point stuff here too.
  743. }
  744. }
  745. return;
  746. }
  747. // FIXME: This is a bit hackish, we can simplify this once we don't need to support
  748. // the old parser anymore, since then we don't need to maintain its children_changed() semantics.
  749. if (token.is_end_tag() && token.tag_name() == "style") {
  750. current_node().children_changed();
  751. // NOTE: We don't return here, keep going.
  752. }
  753. if (token.is_end_tag()) {
  754. m_stack_of_open_elements.pop();
  755. m_insertion_mode = m_original_insertion_mode;
  756. return;
  757. }
  758. ASSERT_NOT_REACHED();
  759. }
  760. void HTMLDocumentParser::clear_the_stack_back_to_a_table_context()
  761. {
  762. while (!current_node().tag_name().is_one_of("table", "template", "html"))
  763. m_stack_of_open_elements.pop();
  764. }
  765. void HTMLDocumentParser::clear_the_stack_back_to_a_table_row_context()
  766. {
  767. while (!current_node().tag_name().is_one_of("tr", "template", "html"))
  768. m_stack_of_open_elements.pop();
  769. }
  770. void HTMLDocumentParser::clear_the_stack_back_to_a_table_body_context()
  771. {
  772. while (!current_node().tag_name().is_one_of("tbody", "tfoot", "thead", "template", "html"))
  773. m_stack_of_open_elements.pop();
  774. }
  775. void HTMLDocumentParser::handle_in_row(HTMLToken& token)
  776. {
  777. if (token.is_start_tag() && token.tag_name().is_one_of("th", "td")) {
  778. clear_the_stack_back_to_a_table_row_context();
  779. insert_html_element(token);
  780. m_insertion_mode = InsertionMode::InCell;
  781. m_list_of_active_formatting_elements.add_marker();
  782. return;
  783. }
  784. if (token.is_end_tag() && token.tag_name() == "tr") {
  785. if (!m_stack_of_open_elements.has_in_table_scope("tr")) {
  786. PARSE_ERROR();
  787. return;
  788. }
  789. clear_the_stack_back_to_a_table_row_context();
  790. m_stack_of_open_elements.pop();
  791. m_insertion_mode = InsertionMode::InTableBody;
  792. return;
  793. }
  794. TODO();
  795. }
  796. void HTMLDocumentParser::close_the_cell()
  797. {
  798. generate_implied_end_tags();
  799. if (!current_node().tag_name().is_one_of("td", "th")) {
  800. PARSE_ERROR();
  801. }
  802. while (!current_node().tag_name().is_one_of("td", "th"))
  803. m_stack_of_open_elements.pop();
  804. m_stack_of_open_elements.pop();
  805. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  806. m_insertion_mode = InsertionMode::InRow;
  807. }
  808. void HTMLDocumentParser::handle_in_cell(HTMLToken& token)
  809. {
  810. if (token.is_end_tag() && token.tag_name().is_one_of("td", "th")) {
  811. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  812. PARSE_ERROR();
  813. return;
  814. }
  815. generate_implied_end_tags();
  816. if (current_node().tag_name() != token.tag_name()) {
  817. PARSE_ERROR();
  818. }
  819. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  820. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  821. m_insertion_mode = InsertionMode::InRow;
  822. return;
  823. }
  824. if (token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr")) {
  825. if (!m_stack_of_open_elements.has_in_table_scope("td") && m_stack_of_open_elements.has_in_table_scope("th")) {
  826. PARSE_ERROR();
  827. return;
  828. }
  829. close_the_cell();
  830. process_using_the_rules_for(m_insertion_mode, token);
  831. return;
  832. }
  833. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html")) {
  834. PARSE_ERROR();
  835. return;
  836. }
  837. if (token.is_end_tag() && token.tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  838. TODO();
  839. }
  840. process_using_the_rules_for(InsertionMode::InBody, token);
  841. }
  842. void HTMLDocumentParser::handle_in_table_body(HTMLToken& token)
  843. {
  844. if (token.is_start_tag() && token.tag_name() == "tr") {
  845. clear_the_stack_back_to_a_table_body_context();
  846. insert_html_element(token);
  847. m_insertion_mode = InsertionMode::InRow;
  848. return;
  849. }
  850. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "tfoot", "thead"))
  851. || (token.is_end_tag() && token.tag_name() == "table")) {
  852. // 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.
  853. clear_the_stack_back_to_a_table_body_context();
  854. m_stack_of_open_elements.pop();
  855. m_insertion_mode = InsertionMode::InTable;
  856. process_using_the_rules_for(InsertionMode::InTable, token);
  857. return;
  858. }
  859. TODO();
  860. }
  861. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  862. {
  863. if (token.is_character() && current_node().tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  864. TODO();
  865. }
  866. if (token.is_comment()) {
  867. insert_comment(token);
  868. return;
  869. }
  870. if (token.is_doctype()) {
  871. PARSE_ERROR();
  872. return;
  873. }
  874. if (token.is_start_tag() && token.tag_name() == "caption") {
  875. TODO();
  876. }
  877. if (token.is_start_tag() && token.tag_name() == "colgroup") {
  878. TODO();
  879. }
  880. if (token.is_start_tag() && token.tag_name() == "col") {
  881. TODO();
  882. }
  883. if (token.is_start_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  884. TODO();
  885. }
  886. if (token.is_start_tag() && token.tag_name().is_one_of("td", "th", "tr")) {
  887. clear_the_stack_back_to_a_table_context();
  888. HTMLToken fake_tbody_token;
  889. fake_tbody_token.m_type = HTMLToken::Type::StartTag;
  890. fake_tbody_token.m_tag.tag_name.append("tbody");
  891. insert_html_element(fake_tbody_token);
  892. m_insertion_mode = InsertionMode::InTableBody;
  893. process_using_the_rules_for(InsertionMode::InTableBody, token);
  894. return;
  895. }
  896. if (token.is_start_tag() && token.tag_name() == "table") {
  897. PARSE_ERROR();
  898. TODO();
  899. }
  900. if (token.is_end_tag()) {
  901. if (!m_stack_of_open_elements.has_in_table_scope("table")) {
  902. PARSE_ERROR();
  903. return;
  904. }
  905. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("table");
  906. reset_the_insertion_mode_appropriately();
  907. return;
  908. }
  909. TODO();
  910. }
  911. void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
  912. {
  913. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  914. RefPtr<Element> node = m_stack_of_open_elements.elements().at(i);
  915. if (node->tag_name() == "select") {
  916. TODO();
  917. }
  918. if (node->tag_name().is_one_of("td", "th")) {
  919. m_insertion_mode = InsertionMode::InCell;
  920. return;
  921. }
  922. if (node->tag_name() == "tr") {
  923. m_insertion_mode = InsertionMode::InRow;
  924. return;
  925. }
  926. if (node->tag_name().is_one_of("tbody", "thead", "tfoot")) {
  927. m_insertion_mode = InsertionMode::InTableBody;
  928. return;
  929. }
  930. if (node->tag_name() == "caption") {
  931. m_insertion_mode = InsertionMode::InCaption;
  932. return;
  933. }
  934. if (node->tag_name() == "colgroup") {
  935. m_insertion_mode = InsertionMode::InColumnGroup;
  936. return;
  937. }
  938. if (node->tag_name() == "table") {
  939. m_insertion_mode = InsertionMode::InTable;
  940. return;
  941. }
  942. if (node->tag_name() == "template") {
  943. TODO();
  944. }
  945. if (node->tag_name() == "body") {
  946. m_insertion_mode = InsertionMode::InBody;
  947. return;
  948. }
  949. if (node->tag_name() == "frameset") {
  950. m_insertion_mode = InsertionMode::InFrameset;
  951. if (m_parsing_fragment) {
  952. TODO();
  953. }
  954. return;
  955. }
  956. if (node->tag_name() == "html") {
  957. TODO();
  958. }
  959. }
  960. m_insertion_mode = InsertionMode::InBody;
  961. if (m_parsing_fragment) {
  962. TODO();
  963. }
  964. }
  965. const char* HTMLDocumentParser::insertion_mode_name() const
  966. {
  967. switch (m_insertion_mode) {
  968. #define __ENUMERATE_INSERTION_MODE(mode) \
  969. case InsertionMode::mode: \
  970. return #mode;
  971. ENUMERATE_INSERTION_MODES
  972. #undef __ENUMERATE_INSERTION_MODE
  973. }
  974. ASSERT_NOT_REACHED();
  975. }
  976. Document& HTMLDocumentParser::document()
  977. {
  978. return *m_document;
  979. }
  980. }