HTMLDocumentParser.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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_comment()) {
  538. insert_comment(token);
  539. return;
  540. }
  541. if (token.is_end_tag() && token.tag_name() == "body") {
  542. if (!m_stack_of_open_elements.has_in_scope("body")) {
  543. ASSERT_NOT_REACHED();
  544. }
  545. // FIXME: Otherwise, if there is a node in the stack of open elements that is
  546. // not either a dd element, a dt element, an li element, an optgroup element,
  547. // an option element, a p element, an rb element, an rp element, an rt element,
  548. // an rtc element, a tbody element, a td element, a tfoot element, a th element,
  549. // a thead element, a tr element, the body element, or the html element,
  550. // then this is a parse error.
  551. m_insertion_mode = InsertionMode::AfterBody;
  552. return;
  553. }
  554. if (token.is_start_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  555. if (m_stack_of_open_elements.has_in_button_scope("p"))
  556. close_a_p_element();
  557. if (current_node().tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  558. PARSE_ERROR();
  559. m_stack_of_open_elements.pop();
  560. }
  561. insert_html_element(token);
  562. return;
  563. }
  564. if (token.is_end_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  565. if (!m_stack_of_open_elements.has_in_scope("h1")
  566. && !m_stack_of_open_elements.has_in_scope("h2")
  567. && !m_stack_of_open_elements.has_in_scope("h3")
  568. && !m_stack_of_open_elements.has_in_scope("h4")
  569. && !m_stack_of_open_elements.has_in_scope("h5")
  570. && !m_stack_of_open_elements.has_in_scope("h6")) {
  571. PARSE_ERROR();
  572. return;
  573. }
  574. generate_implied_end_tags();
  575. if (current_node().tag_name() != token.tag_name()) {
  576. PARSE_ERROR();
  577. }
  578. for (;;) {
  579. auto popped_element = m_stack_of_open_elements.pop();
  580. if (popped_element->tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6"))
  581. break;
  582. }
  583. return;
  584. }
  585. if (token.is_end_tag() && token.tag_name() == "p") {
  586. if (!m_stack_of_open_elements.has_in_button_scope("p")) {
  587. TODO();
  588. }
  589. close_a_p_element();
  590. return;
  591. }
  592. if (token.is_start_tag() && token.tag_name().is_one_of("b", "big", "code", "em", "font", "i", "s", "small", "strike", "strong", "tt", "u")) {
  593. reconstruct_the_active_formatting_elements();
  594. auto element = insert_html_element(token);
  595. m_list_of_active_formatting_elements.add(*element);
  596. return;
  597. }
  598. 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")) {
  599. run_the_adoption_agency_algorithm(token);
  600. return;
  601. }
  602. 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")) {
  603. if (m_stack_of_open_elements.has_in_button_scope("p"))
  604. close_a_p_element();
  605. insert_html_element(token);
  606. return;
  607. }
  608. 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")) {
  609. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  610. PARSE_ERROR();
  611. return;
  612. }
  613. generate_implied_end_tags();
  614. if (current_node().tag_name() != token.tag_name()) {
  615. PARSE_ERROR();
  616. }
  617. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  618. return;
  619. }
  620. if (token.is_start_tag() && token.tag_name() == "table") {
  621. // FIXME: If the Document is not set to quirks mode,
  622. // and the stack of open elements has a p element in button scope, then close a p element.
  623. insert_html_element(token);
  624. m_frameset_ok = false;
  625. m_insertion_mode = InsertionMode::InTable;
  626. return;
  627. }
  628. if (token.is_start_tag() && token.tag_name().is_one_of("area", "br", "embed", "img", "keygen", "wbr")) {
  629. reconstruct_the_active_formatting_elements();
  630. insert_html_element(token);
  631. m_stack_of_open_elements.pop();
  632. token.acknowledge_self_closing_flag_if_set();
  633. m_frameset_ok = false;
  634. return;
  635. }
  636. if (token.is_start_tag() && token.tag_name() == "input") {
  637. reconstruct_the_active_formatting_elements();
  638. insert_html_element(token);
  639. m_stack_of_open_elements.pop();
  640. token.acknowledge_self_closing_flag_if_set();
  641. auto type_attribute = token.attribute(HTML::AttributeNames::type);
  642. if (type_attribute.is_null() || type_attribute != "hidden") {
  643. m_frameset_ok = false;
  644. }
  645. return;
  646. }
  647. if (token.is_start_tag()) {
  648. reconstruct_the_active_formatting_elements();
  649. insert_html_element(token);
  650. return;
  651. }
  652. if (token.is_end_tag()) {
  653. RefPtr<Element> node;
  654. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  655. node = m_stack_of_open_elements.elements()[i];
  656. if (node->tag_name() == token.tag_name()) {
  657. generate_implied_end_tags(token.tag_name());
  658. if (node != current_node()) {
  659. PARSE_ERROR();
  660. }
  661. while (&current_node() != node) {
  662. m_stack_of_open_elements.pop();
  663. }
  664. m_stack_of_open_elements.pop();
  665. break;
  666. }
  667. // FIXME: Handle special elements!
  668. }
  669. return;
  670. }
  671. ASSERT_NOT_REACHED();
  672. }
  673. void HTMLDocumentParser::increment_script_nesting_level()
  674. {
  675. ++m_script_nesting_level;
  676. }
  677. void HTMLDocumentParser::decrement_script_nesting_level()
  678. {
  679. ASSERT(m_script_nesting_level);
  680. --m_script_nesting_level;
  681. }
  682. void HTMLDocumentParser::handle_text(HTMLToken& token)
  683. {
  684. if (token.is_character()) {
  685. insert_character(token.codepoint());
  686. return;
  687. }
  688. if (token.is_end_tag() && token.tag_name() == "script") {
  689. NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
  690. m_stack_of_open_elements.pop();
  691. m_insertion_mode = m_original_insertion_mode;
  692. // FIXME: Handle tokenizer insertion point stuff here.
  693. increment_script_nesting_level();
  694. script->prepare_script({});
  695. decrement_script_nesting_level();
  696. if (script_nesting_level() == 0)
  697. m_parser_pause_flag = false;
  698. // FIXME: Handle tokenizer insertion point stuff here too.
  699. while (document().pending_parsing_blocking_script()) {
  700. if (script_nesting_level() != 0) {
  701. m_parser_pause_flag = true;
  702. // FIXME: Abort the processing of any nested invocations of the tokenizer,
  703. // yielding control back to the caller. (Tokenization will resume when
  704. // the caller returns to the "outer" tree construction stage.)
  705. TODO();
  706. } else {
  707. auto the_script = document().take_pending_parsing_blocking_script({});
  708. m_tokenizer.set_blocked(true);
  709. // FIXME: If the parser's Document has a style sheet that is blocking scripts
  710. // or the script's "ready to be parser-executed" flag is not set:
  711. // spin the event loop until the parser's Document has no style sheet
  712. // that is blocking scripts and the script's "ready to be parser-executed"
  713. // flag is set.
  714. ASSERT(the_script->is_ready_to_be_parser_executed());
  715. if (m_aborted)
  716. return;
  717. m_tokenizer.set_blocked(false);
  718. // FIXME: Handle tokenizer insertion point stuff here too.
  719. ASSERT(script_nesting_level() == 0);
  720. increment_script_nesting_level();
  721. the_script->execute_script();
  722. decrement_script_nesting_level();
  723. ASSERT(script_nesting_level() == 0);
  724. m_parser_pause_flag = false;
  725. // FIXME: Handle tokenizer insertion point stuff here too.
  726. }
  727. }
  728. return;
  729. }
  730. // FIXME: This is a bit hackish, we can simplify this once we don't need to support
  731. // the old parser anymore, since then we don't need to maintain its children_changed() semantics.
  732. if (token.is_end_tag() && token.tag_name() == "style") {
  733. current_node().children_changed();
  734. // NOTE: We don't return here, keep going.
  735. }
  736. if (token.is_end_tag()) {
  737. m_stack_of_open_elements.pop();
  738. m_insertion_mode = m_original_insertion_mode;
  739. return;
  740. }
  741. ASSERT_NOT_REACHED();
  742. }
  743. void HTMLDocumentParser::clear_the_stack_back_to_a_table_context()
  744. {
  745. while (!current_node().tag_name().is_one_of("table", "template", "html"))
  746. m_stack_of_open_elements.pop();
  747. }
  748. void HTMLDocumentParser::clear_the_stack_back_to_a_table_row_context()
  749. {
  750. while (!current_node().tag_name().is_one_of("tr", "template", "html"))
  751. m_stack_of_open_elements.pop();
  752. }
  753. void HTMLDocumentParser::clear_the_stack_back_to_a_table_body_context()
  754. {
  755. while (!current_node().tag_name().is_one_of("tbody", "tfoot", "thead", "template", "html"))
  756. m_stack_of_open_elements.pop();
  757. }
  758. void HTMLDocumentParser::handle_in_row(HTMLToken& token)
  759. {
  760. if (token.is_start_tag() && token.tag_name().is_one_of("th", "td")) {
  761. clear_the_stack_back_to_a_table_row_context();
  762. insert_html_element(token);
  763. m_insertion_mode = InsertionMode::InCell;
  764. m_list_of_active_formatting_elements.add_marker();
  765. return;
  766. }
  767. if (token.is_end_tag() && token.tag_name() == "tr") {
  768. if (!m_stack_of_open_elements.has_in_table_scope("tr")) {
  769. PARSE_ERROR();
  770. return;
  771. }
  772. clear_the_stack_back_to_a_table_row_context();
  773. m_stack_of_open_elements.pop();
  774. m_insertion_mode = InsertionMode::InTableBody;
  775. return;
  776. }
  777. TODO();
  778. }
  779. void HTMLDocumentParser::close_the_cell()
  780. {
  781. generate_implied_end_tags();
  782. if (!current_node().tag_name().is_one_of("td", "th")) {
  783. PARSE_ERROR();
  784. }
  785. while (!current_node().tag_name().is_one_of("td", "th"))
  786. m_stack_of_open_elements.pop();
  787. m_stack_of_open_elements.pop();
  788. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  789. m_insertion_mode = InsertionMode::InRow;
  790. }
  791. void HTMLDocumentParser::handle_in_cell(HTMLToken& token)
  792. {
  793. if (token.is_end_tag() && token.tag_name().is_one_of("td", "th")) {
  794. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  795. PARSE_ERROR();
  796. return;
  797. }
  798. generate_implied_end_tags();
  799. if (current_node().tag_name() != token.tag_name()) {
  800. PARSE_ERROR();
  801. }
  802. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  803. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  804. m_insertion_mode = InsertionMode::InRow;
  805. return;
  806. }
  807. if (token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr")) {
  808. if (!m_stack_of_open_elements.has_in_table_scope("td") && m_stack_of_open_elements.has_in_table_scope("th")) {
  809. PARSE_ERROR();
  810. return;
  811. }
  812. close_the_cell();
  813. process_using_the_rules_for(m_insertion_mode, token);
  814. return;
  815. }
  816. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html")) {
  817. PARSE_ERROR();
  818. return;
  819. }
  820. if (token.is_end_tag() && token.tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  821. TODO();
  822. }
  823. process_using_the_rules_for(InsertionMode::InBody, token);
  824. }
  825. void HTMLDocumentParser::handle_in_table_body(HTMLToken& token)
  826. {
  827. if (token.is_start_tag() && token.tag_name() == "tr") {
  828. clear_the_stack_back_to_a_table_body_context();
  829. insert_html_element(token);
  830. m_insertion_mode = InsertionMode::InRow;
  831. return;
  832. }
  833. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "tfoot", "thead"))
  834. || (token.is_end_tag() && token.tag_name() == "table")) {
  835. // 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.
  836. clear_the_stack_back_to_a_table_body_context();
  837. m_stack_of_open_elements.pop();
  838. m_insertion_mode = InsertionMode::InTable;
  839. process_using_the_rules_for(InsertionMode::InTable, token);
  840. return;
  841. }
  842. TODO();
  843. }
  844. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  845. {
  846. if (token.is_character() && current_node().tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  847. TODO();
  848. }
  849. if (token.is_comment()) {
  850. insert_comment(token);
  851. return;
  852. }
  853. if (token.is_doctype()) {
  854. PARSE_ERROR();
  855. return;
  856. }
  857. if (token.is_start_tag() && token.tag_name() == "caption") {
  858. TODO();
  859. }
  860. if (token.is_start_tag() && token.tag_name() == "colgroup") {
  861. TODO();
  862. }
  863. if (token.is_start_tag() && token.tag_name() == "col") {
  864. TODO();
  865. }
  866. if (token.is_start_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  867. TODO();
  868. }
  869. if (token.is_start_tag() && token.tag_name().is_one_of("td", "th", "tr")) {
  870. clear_the_stack_back_to_a_table_context();
  871. HTMLToken fake_tbody_token;
  872. fake_tbody_token.m_type = HTMLToken::Type::StartTag;
  873. fake_tbody_token.m_tag.tag_name.append("tbody");
  874. insert_html_element(fake_tbody_token);
  875. m_insertion_mode = InsertionMode::InTableBody;
  876. process_using_the_rules_for(InsertionMode::InTableBody, token);
  877. return;
  878. }
  879. if (token.is_start_tag() && token.tag_name() == "table") {
  880. PARSE_ERROR();
  881. TODO();
  882. }
  883. if (token.is_end_tag()) {
  884. if (!m_stack_of_open_elements.has_in_table_scope("table")) {
  885. PARSE_ERROR();
  886. return;
  887. }
  888. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("table");
  889. reset_the_insertion_mode_appropriately();
  890. return;
  891. }
  892. TODO();
  893. }
  894. void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
  895. {
  896. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  897. RefPtr<Element> node = m_stack_of_open_elements.elements().at(i);
  898. if (node->tag_name() == "select") {
  899. TODO();
  900. }
  901. if (node->tag_name().is_one_of("td", "th")) {
  902. m_insertion_mode = InsertionMode::InCell;
  903. return;
  904. }
  905. if (node->tag_name() == "tr") {
  906. m_insertion_mode = InsertionMode::InRow;
  907. return;
  908. }
  909. if (node->tag_name().is_one_of("tbody", "thead", "tfoot")) {
  910. m_insertion_mode = InsertionMode::InTableBody;
  911. return;
  912. }
  913. if (node->tag_name() == "caption") {
  914. m_insertion_mode = InsertionMode::InCaption;
  915. return;
  916. }
  917. if (node->tag_name() == "colgroup") {
  918. m_insertion_mode = InsertionMode::InColumnGroup;
  919. return;
  920. }
  921. if (node->tag_name() == "table") {
  922. m_insertion_mode = InsertionMode::InTable;
  923. return;
  924. }
  925. if (node->tag_name() == "template") {
  926. TODO();
  927. }
  928. if (node->tag_name() == "body") {
  929. m_insertion_mode = InsertionMode::InBody;
  930. return;
  931. }
  932. if (node->tag_name() == "frameset") {
  933. m_insertion_mode = InsertionMode::InFrameset;
  934. if (m_parsing_fragment) {
  935. TODO();
  936. }
  937. return;
  938. }
  939. if (node->tag_name() == "html") {
  940. TODO();
  941. }
  942. }
  943. m_insertion_mode = InsertionMode::InBody;
  944. if (m_parsing_fragment) {
  945. TODO();
  946. }
  947. }
  948. const char* HTMLDocumentParser::insertion_mode_name() const
  949. {
  950. switch (m_insertion_mode) {
  951. #define __ENUMERATE_INSERTION_MODE(mode) \
  952. case InsertionMode::mode: \
  953. return #mode;
  954. ENUMERATE_INSERTION_MODES
  955. #undef __ENUMERATE_INSERTION_MODE
  956. }
  957. ASSERT_NOT_REACHED();
  958. }
  959. Document& HTMLDocumentParser::document()
  960. {
  961. return *m_document;
  962. }
  963. }