HTMLDocumentParser.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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 PARSE_ERROR() \
  40. do { \
  41. dbg() << "Parse error! " << __PRETTY_FUNCTION__ << " @ " << __LINE__; \
  42. } while (0)
  43. namespace Web {
  44. HTMLDocumentParser::HTMLDocumentParser(const StringView& input, const String& encoding)
  45. : m_tokenizer(input, encoding)
  46. {
  47. }
  48. HTMLDocumentParser::~HTMLDocumentParser()
  49. {
  50. }
  51. void HTMLDocumentParser::run(const URL& url)
  52. {
  53. m_document = adopt(*new Document);
  54. m_document->set_url(url);
  55. m_document->set_source(m_tokenizer.source());
  56. for (;;) {
  57. auto optional_token = m_tokenizer.next_token();
  58. if (!optional_token.has_value())
  59. break;
  60. auto& token = optional_token.value();
  61. #ifdef PARSER_DEBUG
  62. dbg() << "[" << insertion_mode_name() << "] " << token.to_string();
  63. #endif
  64. process_using_the_rules_for(m_insertion_mode, token);
  65. if (m_stop_parsing) {
  66. dbg() << "Stop parsing! :^)";
  67. break;
  68. }
  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. TODO();
  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. TODO();
  320. }
  321. void HTMLDocumentParser::handle_in_head_noscript(HTMLToken&)
  322. {
  323. TODO();
  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. TODO();
  359. }
  360. if (token.is_comment()) {
  361. TODO();
  362. }
  363. if (token.is_doctype()) {
  364. TODO();
  365. }
  366. if (token.is_start_tag() && token.tag_name() == "html") {
  367. TODO();
  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. TODO();
  377. }
  378. if (token.is_start_tag() && token.tag_name().is_one_of("base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title")) {
  379. TODO();
  380. }
  381. if (token.is_end_tag() && token.tag_name() == "template") {
  382. TODO();
  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. TODO();
  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. stop_parsing();
  430. return;
  431. }
  432. if (token.is_end_tag() && token.tag_name() == "html") {
  433. if (m_parsing_fragment) {
  434. TODO();
  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. stop_parsing();
  451. return;
  452. }
  453. TODO();
  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. RefPtr<Element> furthest_block = m_stack_of_open_elements.topmost_special_node_below(*formatting_element);
  525. if (!furthest_block) {
  526. while (&current_node() != formatting_element)
  527. m_stack_of_open_elements.pop();
  528. m_stack_of_open_elements.pop();
  529. m_list_of_active_formatting_elements.remove(*formatting_element);
  530. return;
  531. }
  532. // FIXME: Implement the rest of the AAA :^)
  533. TODO();
  534. }
  535. bool HTMLDocumentParser::is_special_tag(const FlyString& tag_name)
  536. {
  537. return tag_name.is_one_of(
  538. "address",
  539. "applet",
  540. "area",
  541. "article",
  542. "aside",
  543. "base",
  544. "basefont",
  545. "bgsound",
  546. "blockquote",
  547. "body",
  548. "br",
  549. "button",
  550. "caption",
  551. "center",
  552. "col",
  553. "colgroup",
  554. "dd",
  555. "details",
  556. "dir",
  557. "div",
  558. "dl",
  559. "dt",
  560. "embed",
  561. "fieldset",
  562. "figcaption",
  563. "figure",
  564. "footer",
  565. "form",
  566. "frame",
  567. "frameset",
  568. "h1",
  569. "h2",
  570. "h3",
  571. "h4",
  572. "h5",
  573. "h6",
  574. "head",
  575. "header",
  576. "hgroup",
  577. "hr",
  578. "html",
  579. "iframe",
  580. "img",
  581. "input",
  582. "keygen",
  583. "li",
  584. "link",
  585. "listing",
  586. "main",
  587. "marquee",
  588. "menu",
  589. "meta",
  590. "nav",
  591. "noembed",
  592. "noframes",
  593. "noscript",
  594. "object",
  595. "ol",
  596. "p",
  597. "param",
  598. "plaintext",
  599. "pre",
  600. "script",
  601. "section",
  602. "select",
  603. "source",
  604. "style",
  605. "summary",
  606. "table",
  607. "tbody",
  608. "td",
  609. "template",
  610. "textarea",
  611. "tfoot",
  612. "th",
  613. "thead",
  614. "title",
  615. "tr",
  616. "track",
  617. "ul",
  618. "wbr",
  619. "xmp");
  620. }
  621. void HTMLDocumentParser::handle_in_body(HTMLToken& token)
  622. {
  623. if (token.is_character()) {
  624. if (token.codepoint() == 0) {
  625. TODO();
  626. }
  627. if (token.is_parser_whitespace()) {
  628. reconstruct_the_active_formatting_elements();
  629. insert_character(token.codepoint());
  630. return;
  631. }
  632. reconstruct_the_active_formatting_elements();
  633. insert_character(token.codepoint());
  634. m_frameset_ok = false;
  635. return;
  636. }
  637. if (token.is_comment()) {
  638. insert_comment(token);
  639. return;
  640. }
  641. if (token.is_doctype()) {
  642. PARSE_ERROR();
  643. return;
  644. }
  645. if (token.is_start_tag() && token.tag_name() == "html") {
  646. TODO();
  647. }
  648. if (token.is_start_tag() && token.tag_name().is_one_of("base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title")) {
  649. process_using_the_rules_for(InsertionMode::InHead, token);
  650. return;
  651. }
  652. if (token.is_end_tag() && token.tag_name() == "template") {
  653. process_using_the_rules_for(InsertionMode::InHead, token);
  654. return;
  655. }
  656. if (token.is_start_tag() && token.tag_name() == "body") {
  657. TODO();
  658. }
  659. if (token.is_start_tag() && token.tag_name() == "frameset") {
  660. TODO();
  661. }
  662. if (token.is_end_of_file()) {
  663. TODO();
  664. }
  665. if (token.is_end_tag() && token.tag_name() == "body") {
  666. if (!m_stack_of_open_elements.has_in_scope("body")) {
  667. TODO();
  668. }
  669. // FIXME: Otherwise, if there is a node in the stack of open elements that is
  670. // not either a dd element, a dt element, an li element, an optgroup element,
  671. // an option element, a p element, an rb element, an rp element, an rt element,
  672. // an rtc element, a tbody element, a td element, a tfoot element, a th element,
  673. // a thead element, a tr element, the body element, or the html element,
  674. // then this is a parse error.
  675. m_insertion_mode = InsertionMode::AfterBody;
  676. return;
  677. }
  678. if (token.is_start_tag() && token.tag_name() == "html") {
  679. TODO();
  680. }
  681. 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")) {
  682. if (m_stack_of_open_elements.has_in_button_scope("p"))
  683. close_a_p_element();
  684. insert_html_element(token);
  685. return;
  686. }
  687. if (token.is_start_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  688. if (m_stack_of_open_elements.has_in_button_scope("p"))
  689. close_a_p_element();
  690. if (current_node().tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  691. PARSE_ERROR();
  692. m_stack_of_open_elements.pop();
  693. }
  694. insert_html_element(token);
  695. return;
  696. }
  697. if (token.is_start_tag() && token.tag_name().is_one_of("pre", "listing")) {
  698. TODO();
  699. }
  700. if (token.is_start_tag() && token.tag_name() == "form") {
  701. TODO();
  702. }
  703. if (token.is_start_tag() && token.tag_name() == "li") {
  704. m_frameset_ok = false;
  705. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  706. RefPtr<Element> node = m_stack_of_open_elements.elements()[i];
  707. if (node->tag_name() == "li") {
  708. generate_implied_end_tags("li");
  709. if (current_node().tag_name() != "li") {
  710. PARSE_ERROR();
  711. }
  712. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("li");
  713. break;
  714. }
  715. if (is_special_tag(node->tag_name()) && !node->tag_name().is_one_of("address", "div", "p"))
  716. break;
  717. }
  718. if (m_stack_of_open_elements.has_in_button_scope("p"))
  719. close_a_p_element();
  720. insert_html_element(token);
  721. return;
  722. }
  723. if (token.is_start_tag() && token.tag_name().is_one_of("dd", "dt")) {
  724. TODO();
  725. }
  726. if (token.is_start_tag() && token.tag_name() == "plaintext") {
  727. if (m_stack_of_open_elements.has_in_button_scope("p"))
  728. close_a_p_element();
  729. insert_html_element(token);
  730. m_tokenizer.switch_to({}, HTMLTokenizer::State::PLAINTEXT);
  731. return;
  732. }
  733. if (token.is_start_tag() && token.tag_name() == "button") {
  734. if (m_stack_of_open_elements.has_in_button_scope("button")) {
  735. PARSE_ERROR();
  736. generate_implied_end_tags();
  737. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("button");
  738. }
  739. reconstruct_the_active_formatting_elements();
  740. insert_html_element(token);
  741. m_frameset_ok = false;
  742. return;
  743. }
  744. if (token.is_end_tag() && token.tag_name().is_one_of("address", "article", "aside", "blockquote", "button", "center", "details", "dialog", "dir", "div", "dl", "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "listing", "main", "menu", "nav", "ol", "pre", "section", "summary", "ul")) {
  745. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  746. PARSE_ERROR();
  747. return;
  748. }
  749. generate_implied_end_tags();
  750. if (current_node().tag_name() != token.tag_name()) {
  751. PARSE_ERROR();
  752. }
  753. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  754. return;
  755. }
  756. if (token.is_end_tag() && token.tag_name() == "form") {
  757. TODO();
  758. }
  759. if (token.is_end_tag() && token.tag_name() == "p") {
  760. if (!m_stack_of_open_elements.has_in_button_scope("p")) {
  761. PARSE_ERROR();
  762. HTMLToken fake_p_token;
  763. fake_p_token.m_type = HTMLToken::Type::StartTag;
  764. fake_p_token.m_tag.tag_name.append("p");
  765. insert_html_element(fake_p_token);
  766. }
  767. close_a_p_element();
  768. return;
  769. }
  770. if (token.is_end_tag() && token.tag_name() == "li") {
  771. if (!m_stack_of_open_elements.has_in_list_item_scope("li")) {
  772. PARSE_ERROR();
  773. return;
  774. }
  775. generate_implied_end_tags("li");
  776. if (current_node().tag_name() != "li") {
  777. PARSE_ERROR();
  778. dbg() << "Expected <li> current node, but had <" << current_node().tag_name() << ">";
  779. }
  780. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("li");
  781. return;
  782. }
  783. if (token.is_end_tag() && token.tag_name().is_one_of("dd", "dt")) {
  784. TODO();
  785. }
  786. if (token.is_end_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  787. if (!m_stack_of_open_elements.has_in_scope("h1")
  788. && !m_stack_of_open_elements.has_in_scope("h2")
  789. && !m_stack_of_open_elements.has_in_scope("h3")
  790. && !m_stack_of_open_elements.has_in_scope("h4")
  791. && !m_stack_of_open_elements.has_in_scope("h5")
  792. && !m_stack_of_open_elements.has_in_scope("h6")) {
  793. PARSE_ERROR();
  794. return;
  795. }
  796. generate_implied_end_tags();
  797. if (current_node().tag_name() != token.tag_name()) {
  798. PARSE_ERROR();
  799. }
  800. for (;;) {
  801. auto popped_element = m_stack_of_open_elements.pop();
  802. if (popped_element->tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6"))
  803. break;
  804. }
  805. return;
  806. }
  807. if (token.is_start_tag() && token.tag_name() == "a") {
  808. if (m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker("a")) {
  809. TODO();
  810. }
  811. reconstruct_the_active_formatting_elements();
  812. auto element = insert_html_element(token);
  813. m_list_of_active_formatting_elements.add(*element);
  814. return;
  815. }
  816. if (token.is_start_tag() && token.tag_name().is_one_of("b", "big", "code", "em", "font", "i", "s", "small", "strike", "strong", "tt", "u")) {
  817. reconstruct_the_active_formatting_elements();
  818. auto element = insert_html_element(token);
  819. m_list_of_active_formatting_elements.add(*element);
  820. return;
  821. }
  822. if (token.is_start_tag() && token.tag_name() == "nobr") {
  823. TODO();
  824. }
  825. 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")) {
  826. run_the_adoption_agency_algorithm(token);
  827. return;
  828. }
  829. if (token.is_start_tag() && token.tag_name().is_one_of("applet", "marquee", "object")) {
  830. reconstruct_the_active_formatting_elements();
  831. insert_html_element(token);
  832. m_list_of_active_formatting_elements.add_marker();
  833. m_frameset_ok = false;
  834. return;
  835. }
  836. if (token.is_end_tag() && token.tag_name().is_one_of("applet", "marquee", "object")) {
  837. TODO();
  838. }
  839. if (token.is_start_tag() && token.tag_name() == "table") {
  840. if (!document().in_quirks_mode()) {
  841. if (m_stack_of_open_elements.has_in_button_scope("p"))
  842. close_a_p_element();
  843. }
  844. insert_html_element(token);
  845. m_frameset_ok = false;
  846. m_insertion_mode = InsertionMode::InTable;
  847. return;
  848. }
  849. if (token.is_end_tag() && token.tag_name() == "br") {
  850. TODO();
  851. }
  852. if (token.is_start_tag() && token.tag_name().is_one_of("area", "br", "embed", "img", "keygen", "wbr")) {
  853. reconstruct_the_active_formatting_elements();
  854. insert_html_element(token);
  855. m_stack_of_open_elements.pop();
  856. token.acknowledge_self_closing_flag_if_set();
  857. m_frameset_ok = false;
  858. return;
  859. }
  860. if (token.is_start_tag() && token.tag_name() == "input") {
  861. reconstruct_the_active_formatting_elements();
  862. insert_html_element(token);
  863. m_stack_of_open_elements.pop();
  864. token.acknowledge_self_closing_flag_if_set();
  865. auto type_attribute = token.attribute(HTML::AttributeNames::type);
  866. if (type_attribute.is_null() || type_attribute != "hidden") {
  867. m_frameset_ok = false;
  868. }
  869. return;
  870. }
  871. if (token.is_start_tag() && token.tag_name().is_one_of("param", "source", "track")) {
  872. TODO();
  873. }
  874. if (token.is_start_tag() && token.tag_name() == "hr") {
  875. if (m_stack_of_open_elements.has_in_button_scope("p"))
  876. close_a_p_element();
  877. insert_html_element(token);
  878. m_stack_of_open_elements.pop();
  879. token.acknowledge_self_closing_flag_if_set();
  880. m_frameset_ok = false;
  881. return;
  882. }
  883. if (token.is_start_tag() && token.tag_name() == "image") {
  884. // Parse error. Change the token's tag name to "img" and reprocess it. (Don't ask.)
  885. PARSE_ERROR();
  886. token.m_tag.tag_name.clear();
  887. token.m_tag.tag_name.append("img");
  888. process_using_the_rules_for(m_insertion_mode, token);
  889. return;
  890. }
  891. if (token.is_start_tag() && token.tag_name() == "textarea") {
  892. TODO();
  893. }
  894. if (token.is_start_tag() && token.tag_name() == "xmp") {
  895. TODO();
  896. }
  897. if (token.is_start_tag() && token.tag_name() == "iframe") {
  898. m_frameset_ok = false;
  899. parse_generic_raw_text_element(token);
  900. return;
  901. }
  902. if (token.is_start_tag() && ((token.tag_name() == "noembed") || (token.tag_name() == "noscript" && m_scripting_enabled))) {
  903. parse_generic_raw_text_element(token);
  904. return;
  905. }
  906. if (token.is_start_tag() && token.tag_name() == "select") {
  907. TODO();
  908. }
  909. if (token.is_start_tag() && token.tag_name().is_one_of("optgroup", "option")) {
  910. if (current_node().tag_name() == "option")
  911. m_stack_of_open_elements.pop();
  912. reconstruct_the_active_formatting_elements();
  913. insert_html_element(token);
  914. return;
  915. }
  916. if (token.is_start_tag() && token.tag_name().is_one_of("rb", "rtc")) {
  917. TODO();
  918. }
  919. if (token.is_start_tag() && token.tag_name().is_one_of("rp", "rt")) {
  920. TODO();
  921. }
  922. if (token.is_start_tag() && token.tag_name() == "math") {
  923. TODO();
  924. }
  925. if (token.is_start_tag() && token.tag_name() == "svg") {
  926. TODO();
  927. }
  928. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr"))) {
  929. PARSE_ERROR();
  930. return;
  931. }
  932. // Any other start tag
  933. if (token.is_start_tag()) {
  934. reconstruct_the_active_formatting_elements();
  935. insert_html_element(token);
  936. return;
  937. }
  938. // Any other end tag
  939. if (token.is_end_tag()) {
  940. RefPtr<Element> node;
  941. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  942. node = m_stack_of_open_elements.elements()[i];
  943. if (node->tag_name() == token.tag_name()) {
  944. generate_implied_end_tags(token.tag_name());
  945. if (node != current_node()) {
  946. PARSE_ERROR();
  947. }
  948. while (&current_node() != node) {
  949. m_stack_of_open_elements.pop();
  950. }
  951. m_stack_of_open_elements.pop();
  952. break;
  953. }
  954. // FIXME: Handle special elements!
  955. }
  956. return;
  957. }
  958. TODO();
  959. }
  960. void HTMLDocumentParser::increment_script_nesting_level()
  961. {
  962. ++m_script_nesting_level;
  963. }
  964. void HTMLDocumentParser::decrement_script_nesting_level()
  965. {
  966. ASSERT(m_script_nesting_level);
  967. --m_script_nesting_level;
  968. }
  969. void HTMLDocumentParser::handle_text(HTMLToken& token)
  970. {
  971. if (token.is_character()) {
  972. insert_character(token.codepoint());
  973. return;
  974. }
  975. if (token.is_end_tag() && token.tag_name() == "script") {
  976. NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
  977. m_stack_of_open_elements.pop();
  978. m_insertion_mode = m_original_insertion_mode;
  979. // FIXME: Handle tokenizer insertion point stuff here.
  980. increment_script_nesting_level();
  981. script->prepare_script({});
  982. decrement_script_nesting_level();
  983. if (script_nesting_level() == 0)
  984. m_parser_pause_flag = false;
  985. // FIXME: Handle tokenizer insertion point stuff here too.
  986. while (document().pending_parsing_blocking_script()) {
  987. if (script_nesting_level() != 0) {
  988. m_parser_pause_flag = true;
  989. // FIXME: Abort the processing of any nested invocations of the tokenizer,
  990. // yielding control back to the caller. (Tokenization will resume when
  991. // the caller returns to the "outer" tree construction stage.)
  992. TODO();
  993. } else {
  994. auto the_script = document().take_pending_parsing_blocking_script({});
  995. m_tokenizer.set_blocked(true);
  996. // FIXME: If the parser's Document has a style sheet that is blocking scripts
  997. // or the script's "ready to be parser-executed" flag is not set:
  998. // spin the event loop until the parser's Document has no style sheet
  999. // that is blocking scripts and the script's "ready to be parser-executed"
  1000. // flag is set.
  1001. ASSERT(the_script->is_ready_to_be_parser_executed());
  1002. if (m_aborted)
  1003. return;
  1004. m_tokenizer.set_blocked(false);
  1005. // FIXME: Handle tokenizer insertion point stuff here too.
  1006. ASSERT(script_nesting_level() == 0);
  1007. increment_script_nesting_level();
  1008. the_script->execute_script();
  1009. decrement_script_nesting_level();
  1010. ASSERT(script_nesting_level() == 0);
  1011. m_parser_pause_flag = false;
  1012. // FIXME: Handle tokenizer insertion point stuff here too.
  1013. }
  1014. }
  1015. return;
  1016. }
  1017. // FIXME: This is a bit hackish, we can simplify this once we don't need to support
  1018. // the old parser anymore, since then we don't need to maintain its children_changed() semantics.
  1019. if (token.is_end_tag() && token.tag_name() == "style") {
  1020. current_node().children_changed();
  1021. // NOTE: We don't return here, keep going.
  1022. }
  1023. if (token.is_end_tag()) {
  1024. m_stack_of_open_elements.pop();
  1025. m_insertion_mode = m_original_insertion_mode;
  1026. return;
  1027. }
  1028. TODO();
  1029. }
  1030. void HTMLDocumentParser::clear_the_stack_back_to_a_table_context()
  1031. {
  1032. while (!current_node().tag_name().is_one_of("table", "template", "html"))
  1033. m_stack_of_open_elements.pop();
  1034. }
  1035. void HTMLDocumentParser::clear_the_stack_back_to_a_table_row_context()
  1036. {
  1037. while (!current_node().tag_name().is_one_of("tr", "template", "html"))
  1038. m_stack_of_open_elements.pop();
  1039. }
  1040. void HTMLDocumentParser::clear_the_stack_back_to_a_table_body_context()
  1041. {
  1042. while (!current_node().tag_name().is_one_of("tbody", "tfoot", "thead", "template", "html"))
  1043. m_stack_of_open_elements.pop();
  1044. }
  1045. void HTMLDocumentParser::handle_in_row(HTMLToken& token)
  1046. {
  1047. if (token.is_start_tag() && token.tag_name().is_one_of("th", "td")) {
  1048. clear_the_stack_back_to_a_table_row_context();
  1049. insert_html_element(token);
  1050. m_insertion_mode = InsertionMode::InCell;
  1051. m_list_of_active_formatting_elements.add_marker();
  1052. return;
  1053. }
  1054. if (token.is_end_tag() && token.tag_name() == "tr") {
  1055. if (!m_stack_of_open_elements.has_in_table_scope("tr")) {
  1056. PARSE_ERROR();
  1057. return;
  1058. }
  1059. clear_the_stack_back_to_a_table_row_context();
  1060. m_stack_of_open_elements.pop();
  1061. m_insertion_mode = InsertionMode::InTableBody;
  1062. return;
  1063. }
  1064. TODO();
  1065. }
  1066. void HTMLDocumentParser::close_the_cell()
  1067. {
  1068. generate_implied_end_tags();
  1069. if (!current_node().tag_name().is_one_of("td", "th")) {
  1070. PARSE_ERROR();
  1071. }
  1072. while (!current_node().tag_name().is_one_of("td", "th"))
  1073. m_stack_of_open_elements.pop();
  1074. m_stack_of_open_elements.pop();
  1075. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1076. m_insertion_mode = InsertionMode::InRow;
  1077. }
  1078. void HTMLDocumentParser::handle_in_cell(HTMLToken& token)
  1079. {
  1080. if (token.is_end_tag() && token.tag_name().is_one_of("td", "th")) {
  1081. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1082. PARSE_ERROR();
  1083. return;
  1084. }
  1085. generate_implied_end_tags();
  1086. if (current_node().tag_name() != token.tag_name()) {
  1087. PARSE_ERROR();
  1088. }
  1089. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1090. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1091. m_insertion_mode = InsertionMode::InRow;
  1092. return;
  1093. }
  1094. if (token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr")) {
  1095. if (!m_stack_of_open_elements.has_in_table_scope("td") && m_stack_of_open_elements.has_in_table_scope("th")) {
  1096. PARSE_ERROR();
  1097. return;
  1098. }
  1099. close_the_cell();
  1100. process_using_the_rules_for(m_insertion_mode, token);
  1101. return;
  1102. }
  1103. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html")) {
  1104. PARSE_ERROR();
  1105. return;
  1106. }
  1107. if (token.is_end_tag() && token.tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  1108. TODO();
  1109. }
  1110. process_using_the_rules_for(InsertionMode::InBody, token);
  1111. }
  1112. void HTMLDocumentParser::handle_in_table_body(HTMLToken& token)
  1113. {
  1114. if (token.is_start_tag() && token.tag_name() == "tr") {
  1115. clear_the_stack_back_to_a_table_body_context();
  1116. insert_html_element(token);
  1117. m_insertion_mode = InsertionMode::InRow;
  1118. return;
  1119. }
  1120. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "tfoot", "thead"))
  1121. || (token.is_end_tag() && token.tag_name() == "table")) {
  1122. // 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.
  1123. clear_the_stack_back_to_a_table_body_context();
  1124. m_stack_of_open_elements.pop();
  1125. m_insertion_mode = InsertionMode::InTable;
  1126. process_using_the_rules_for(InsertionMode::InTable, token);
  1127. return;
  1128. }
  1129. TODO();
  1130. }
  1131. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  1132. {
  1133. if (token.is_character() && current_node().tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  1134. TODO();
  1135. }
  1136. if (token.is_comment()) {
  1137. insert_comment(token);
  1138. return;
  1139. }
  1140. if (token.is_doctype()) {
  1141. PARSE_ERROR();
  1142. return;
  1143. }
  1144. if (token.is_start_tag() && token.tag_name() == "caption") {
  1145. TODO();
  1146. }
  1147. if (token.is_start_tag() && token.tag_name() == "colgroup") {
  1148. TODO();
  1149. }
  1150. if (token.is_start_tag() && token.tag_name() == "col") {
  1151. TODO();
  1152. }
  1153. if (token.is_start_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  1154. TODO();
  1155. }
  1156. if (token.is_start_tag() && token.tag_name().is_one_of("td", "th", "tr")) {
  1157. clear_the_stack_back_to_a_table_context();
  1158. HTMLToken fake_tbody_token;
  1159. fake_tbody_token.m_type = HTMLToken::Type::StartTag;
  1160. fake_tbody_token.m_tag.tag_name.append("tbody");
  1161. insert_html_element(fake_tbody_token);
  1162. m_insertion_mode = InsertionMode::InTableBody;
  1163. process_using_the_rules_for(InsertionMode::InTableBody, token);
  1164. return;
  1165. }
  1166. if (token.is_start_tag() && token.tag_name() == "table") {
  1167. PARSE_ERROR();
  1168. TODO();
  1169. }
  1170. if (token.is_end_tag()) {
  1171. if (!m_stack_of_open_elements.has_in_table_scope("table")) {
  1172. PARSE_ERROR();
  1173. return;
  1174. }
  1175. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("table");
  1176. reset_the_insertion_mode_appropriately();
  1177. return;
  1178. }
  1179. TODO();
  1180. }
  1181. void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
  1182. {
  1183. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1184. RefPtr<Element> node = m_stack_of_open_elements.elements().at(i);
  1185. if (node->tag_name() == "select") {
  1186. TODO();
  1187. }
  1188. if (node->tag_name().is_one_of("td", "th")) {
  1189. m_insertion_mode = InsertionMode::InCell;
  1190. return;
  1191. }
  1192. if (node->tag_name() == "tr") {
  1193. m_insertion_mode = InsertionMode::InRow;
  1194. return;
  1195. }
  1196. if (node->tag_name().is_one_of("tbody", "thead", "tfoot")) {
  1197. m_insertion_mode = InsertionMode::InTableBody;
  1198. return;
  1199. }
  1200. if (node->tag_name() == "caption") {
  1201. m_insertion_mode = InsertionMode::InCaption;
  1202. return;
  1203. }
  1204. if (node->tag_name() == "colgroup") {
  1205. m_insertion_mode = InsertionMode::InColumnGroup;
  1206. return;
  1207. }
  1208. if (node->tag_name() == "table") {
  1209. m_insertion_mode = InsertionMode::InTable;
  1210. return;
  1211. }
  1212. if (node->tag_name() == "template") {
  1213. TODO();
  1214. }
  1215. if (node->tag_name() == "body") {
  1216. m_insertion_mode = InsertionMode::InBody;
  1217. return;
  1218. }
  1219. if (node->tag_name() == "frameset") {
  1220. m_insertion_mode = InsertionMode::InFrameset;
  1221. if (m_parsing_fragment) {
  1222. TODO();
  1223. }
  1224. return;
  1225. }
  1226. if (node->tag_name() == "html") {
  1227. TODO();
  1228. }
  1229. }
  1230. m_insertion_mode = InsertionMode::InBody;
  1231. if (m_parsing_fragment) {
  1232. TODO();
  1233. }
  1234. }
  1235. const char* HTMLDocumentParser::insertion_mode_name() const
  1236. {
  1237. switch (m_insertion_mode) {
  1238. #define __ENUMERATE_INSERTION_MODE(mode) \
  1239. case InsertionMode::mode: \
  1240. return #mode;
  1241. ENUMERATE_INSERTION_MODES
  1242. #undef __ENUMERATE_INSERTION_MODE
  1243. }
  1244. ASSERT_NOT_REACHED();
  1245. }
  1246. Document& HTMLDocumentParser::document()
  1247. {
  1248. return *m_document;
  1249. }
  1250. }