HTMLDocumentParser.cpp 36 KB

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