HTMLDocumentParser.cpp 44 KB

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