HTMLDocumentParser.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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. if (m_form_element && m_stack_of_open_elements.contains("template")) {
  702. PARSE_ERROR();
  703. return;
  704. }
  705. if (m_stack_of_open_elements.has_in_button_scope("p"))
  706. close_a_p_element();
  707. auto element = insert_html_element(token);
  708. if (!m_stack_of_open_elements.contains("template"))
  709. m_form_element = to<HTMLFormElement>(*element);
  710. return;
  711. }
  712. if (token.is_start_tag() && token.tag_name() == "li") {
  713. m_frameset_ok = false;
  714. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  715. RefPtr<Element> node = m_stack_of_open_elements.elements()[i];
  716. if (node->tag_name() == "li") {
  717. generate_implied_end_tags("li");
  718. if (current_node().tag_name() != "li") {
  719. PARSE_ERROR();
  720. }
  721. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("li");
  722. break;
  723. }
  724. if (is_special_tag(node->tag_name()) && !node->tag_name().is_one_of("address", "div", "p"))
  725. break;
  726. }
  727. if (m_stack_of_open_elements.has_in_button_scope("p"))
  728. close_a_p_element();
  729. insert_html_element(token);
  730. return;
  731. }
  732. if (token.is_start_tag() && token.tag_name().is_one_of("dd", "dt")) {
  733. TODO();
  734. }
  735. if (token.is_start_tag() && token.tag_name() == "plaintext") {
  736. if (m_stack_of_open_elements.has_in_button_scope("p"))
  737. close_a_p_element();
  738. insert_html_element(token);
  739. m_tokenizer.switch_to({}, HTMLTokenizer::State::PLAINTEXT);
  740. return;
  741. }
  742. if (token.is_start_tag() && token.tag_name() == "button") {
  743. if (m_stack_of_open_elements.has_in_button_scope("button")) {
  744. PARSE_ERROR();
  745. generate_implied_end_tags();
  746. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("button");
  747. }
  748. reconstruct_the_active_formatting_elements();
  749. insert_html_element(token);
  750. m_frameset_ok = false;
  751. return;
  752. }
  753. 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")) {
  754. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  755. PARSE_ERROR();
  756. return;
  757. }
  758. generate_implied_end_tags();
  759. if (current_node().tag_name() != token.tag_name()) {
  760. PARSE_ERROR();
  761. }
  762. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  763. return;
  764. }
  765. if (token.is_end_tag() && token.tag_name() == "form") {
  766. if (!m_stack_of_open_elements.contains("template")) {
  767. auto node = m_form_element;
  768. m_form_element = nullptr;
  769. if (!node || m_stack_of_open_elements.has_in_scope(*node)) {
  770. PARSE_ERROR();
  771. return;
  772. }
  773. generate_implied_end_tags();
  774. if (&current_node() != node) {
  775. PARSE_ERROR();
  776. }
  777. m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) { return entry.ptr() == node.ptr(); });
  778. } else {
  779. TODO();
  780. }
  781. return;
  782. }
  783. if (token.is_end_tag() && token.tag_name() == "p") {
  784. if (!m_stack_of_open_elements.has_in_button_scope("p")) {
  785. PARSE_ERROR();
  786. HTMLToken fake_p_token;
  787. fake_p_token.m_type = HTMLToken::Type::StartTag;
  788. fake_p_token.m_tag.tag_name.append("p");
  789. insert_html_element(fake_p_token);
  790. }
  791. close_a_p_element();
  792. return;
  793. }
  794. if (token.is_end_tag() && token.tag_name() == "li") {
  795. if (!m_stack_of_open_elements.has_in_list_item_scope("li")) {
  796. PARSE_ERROR();
  797. return;
  798. }
  799. generate_implied_end_tags("li");
  800. if (current_node().tag_name() != "li") {
  801. PARSE_ERROR();
  802. dbg() << "Expected <li> current node, but had <" << current_node().tag_name() << ">";
  803. }
  804. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("li");
  805. return;
  806. }
  807. if (token.is_end_tag() && token.tag_name().is_one_of("dd", "dt")) {
  808. TODO();
  809. }
  810. if (token.is_end_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  811. if (!m_stack_of_open_elements.has_in_scope("h1")
  812. && !m_stack_of_open_elements.has_in_scope("h2")
  813. && !m_stack_of_open_elements.has_in_scope("h3")
  814. && !m_stack_of_open_elements.has_in_scope("h4")
  815. && !m_stack_of_open_elements.has_in_scope("h5")
  816. && !m_stack_of_open_elements.has_in_scope("h6")) {
  817. PARSE_ERROR();
  818. return;
  819. }
  820. generate_implied_end_tags();
  821. if (current_node().tag_name() != token.tag_name()) {
  822. PARSE_ERROR();
  823. }
  824. for (;;) {
  825. auto popped_element = m_stack_of_open_elements.pop();
  826. if (popped_element->tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6"))
  827. break;
  828. }
  829. return;
  830. }
  831. if (token.is_start_tag() && token.tag_name() == "a") {
  832. if (auto* element = m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker("a")) {
  833. PARSE_ERROR();
  834. run_the_adoption_agency_algorithm(token);
  835. m_list_of_active_formatting_elements.remove(*element);
  836. m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) {
  837. return entry.ptr() == element;
  838. });
  839. }
  840. reconstruct_the_active_formatting_elements();
  841. auto element = insert_html_element(token);
  842. m_list_of_active_formatting_elements.add(*element);
  843. return;
  844. }
  845. if (token.is_start_tag() && token.tag_name().is_one_of("b", "big", "code", "em", "font", "i", "s", "small", "strike", "strong", "tt", "u")) {
  846. reconstruct_the_active_formatting_elements();
  847. auto element = insert_html_element(token);
  848. m_list_of_active_formatting_elements.add(*element);
  849. return;
  850. }
  851. if (token.is_start_tag() && token.tag_name() == "nobr") {
  852. TODO();
  853. }
  854. 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")) {
  855. run_the_adoption_agency_algorithm(token);
  856. return;
  857. }
  858. if (token.is_start_tag() && token.tag_name().is_one_of("applet", "marquee", "object")) {
  859. reconstruct_the_active_formatting_elements();
  860. insert_html_element(token);
  861. m_list_of_active_formatting_elements.add_marker();
  862. m_frameset_ok = false;
  863. return;
  864. }
  865. if (token.is_end_tag() && token.tag_name().is_one_of("applet", "marquee", "object")) {
  866. TODO();
  867. }
  868. if (token.is_start_tag() && token.tag_name() == "table") {
  869. if (!document().in_quirks_mode()) {
  870. if (m_stack_of_open_elements.has_in_button_scope("p"))
  871. close_a_p_element();
  872. }
  873. insert_html_element(token);
  874. m_frameset_ok = false;
  875. m_insertion_mode = InsertionMode::InTable;
  876. return;
  877. }
  878. if (token.is_end_tag() && token.tag_name() == "br") {
  879. TODO();
  880. }
  881. if (token.is_start_tag() && token.tag_name().is_one_of("area", "br", "embed", "img", "keygen", "wbr")) {
  882. reconstruct_the_active_formatting_elements();
  883. insert_html_element(token);
  884. m_stack_of_open_elements.pop();
  885. token.acknowledge_self_closing_flag_if_set();
  886. m_frameset_ok = false;
  887. return;
  888. }
  889. if (token.is_start_tag() && token.tag_name() == "input") {
  890. reconstruct_the_active_formatting_elements();
  891. insert_html_element(token);
  892. m_stack_of_open_elements.pop();
  893. token.acknowledge_self_closing_flag_if_set();
  894. auto type_attribute = token.attribute(HTML::AttributeNames::type);
  895. if (type_attribute.is_null() || type_attribute != "hidden") {
  896. m_frameset_ok = false;
  897. }
  898. return;
  899. }
  900. if (token.is_start_tag() && token.tag_name().is_one_of("param", "source", "track")) {
  901. TODO();
  902. }
  903. if (token.is_start_tag() && token.tag_name() == "hr") {
  904. if (m_stack_of_open_elements.has_in_button_scope("p"))
  905. close_a_p_element();
  906. insert_html_element(token);
  907. m_stack_of_open_elements.pop();
  908. token.acknowledge_self_closing_flag_if_set();
  909. m_frameset_ok = false;
  910. return;
  911. }
  912. if (token.is_start_tag() && token.tag_name() == "image") {
  913. // Parse error. Change the token's tag name to "img" and reprocess it. (Don't ask.)
  914. PARSE_ERROR();
  915. token.m_tag.tag_name.clear();
  916. token.m_tag.tag_name.append("img");
  917. process_using_the_rules_for(m_insertion_mode, token);
  918. return;
  919. }
  920. if (token.is_start_tag() && token.tag_name() == "textarea") {
  921. TODO();
  922. }
  923. if (token.is_start_tag() && token.tag_name() == "xmp") {
  924. TODO();
  925. }
  926. if (token.is_start_tag() && token.tag_name() == "iframe") {
  927. m_frameset_ok = false;
  928. parse_generic_raw_text_element(token);
  929. return;
  930. }
  931. if (token.is_start_tag() && ((token.tag_name() == "noembed") || (token.tag_name() == "noscript" && m_scripting_enabled))) {
  932. parse_generic_raw_text_element(token);
  933. return;
  934. }
  935. if (token.is_start_tag() && token.tag_name() == "select") {
  936. TODO();
  937. }
  938. if (token.is_start_tag() && token.tag_name().is_one_of("optgroup", "option")) {
  939. if (current_node().tag_name() == "option")
  940. m_stack_of_open_elements.pop();
  941. reconstruct_the_active_formatting_elements();
  942. insert_html_element(token);
  943. return;
  944. }
  945. if (token.is_start_tag() && token.tag_name().is_one_of("rb", "rtc")) {
  946. TODO();
  947. }
  948. if (token.is_start_tag() && token.tag_name().is_one_of("rp", "rt")) {
  949. TODO();
  950. }
  951. if (token.is_start_tag() && token.tag_name() == "math") {
  952. TODO();
  953. }
  954. if (token.is_start_tag() && token.tag_name() == "svg") {
  955. TODO();
  956. }
  957. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr"))) {
  958. PARSE_ERROR();
  959. return;
  960. }
  961. // Any other start tag
  962. if (token.is_start_tag()) {
  963. reconstruct_the_active_formatting_elements();
  964. insert_html_element(token);
  965. return;
  966. }
  967. // Any other end tag
  968. if (token.is_end_tag()) {
  969. RefPtr<Element> node;
  970. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  971. node = m_stack_of_open_elements.elements()[i];
  972. if (node->tag_name() == token.tag_name()) {
  973. generate_implied_end_tags(token.tag_name());
  974. if (node != current_node()) {
  975. PARSE_ERROR();
  976. }
  977. while (&current_node() != node) {
  978. m_stack_of_open_elements.pop();
  979. }
  980. m_stack_of_open_elements.pop();
  981. break;
  982. }
  983. // FIXME: Handle special elements!
  984. }
  985. return;
  986. }
  987. TODO();
  988. }
  989. void HTMLDocumentParser::increment_script_nesting_level()
  990. {
  991. ++m_script_nesting_level;
  992. }
  993. void HTMLDocumentParser::decrement_script_nesting_level()
  994. {
  995. ASSERT(m_script_nesting_level);
  996. --m_script_nesting_level;
  997. }
  998. void HTMLDocumentParser::handle_text(HTMLToken& token)
  999. {
  1000. if (token.is_character()) {
  1001. insert_character(token.codepoint());
  1002. return;
  1003. }
  1004. if (token.is_end_tag() && token.tag_name() == "script") {
  1005. NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
  1006. m_stack_of_open_elements.pop();
  1007. m_insertion_mode = m_original_insertion_mode;
  1008. // FIXME: Handle tokenizer insertion point stuff here.
  1009. increment_script_nesting_level();
  1010. script->prepare_script({});
  1011. decrement_script_nesting_level();
  1012. if (script_nesting_level() == 0)
  1013. m_parser_pause_flag = false;
  1014. // FIXME: Handle tokenizer insertion point stuff here too.
  1015. while (document().pending_parsing_blocking_script()) {
  1016. if (script_nesting_level() != 0) {
  1017. m_parser_pause_flag = true;
  1018. // FIXME: Abort the processing of any nested invocations of the tokenizer,
  1019. // yielding control back to the caller. (Tokenization will resume when
  1020. // the caller returns to the "outer" tree construction stage.)
  1021. TODO();
  1022. } else {
  1023. auto the_script = document().take_pending_parsing_blocking_script({});
  1024. m_tokenizer.set_blocked(true);
  1025. // FIXME: If the parser's Document has a style sheet that is blocking scripts
  1026. // or the script's "ready to be parser-executed" flag is not set:
  1027. // spin the event loop until the parser's Document has no style sheet
  1028. // that is blocking scripts and the script's "ready to be parser-executed"
  1029. // flag is set.
  1030. ASSERT(the_script->is_ready_to_be_parser_executed());
  1031. if (m_aborted)
  1032. return;
  1033. m_tokenizer.set_blocked(false);
  1034. // FIXME: Handle tokenizer insertion point stuff here too.
  1035. ASSERT(script_nesting_level() == 0);
  1036. increment_script_nesting_level();
  1037. the_script->execute_script();
  1038. decrement_script_nesting_level();
  1039. ASSERT(script_nesting_level() == 0);
  1040. m_parser_pause_flag = false;
  1041. // FIXME: Handle tokenizer insertion point stuff here too.
  1042. }
  1043. }
  1044. return;
  1045. }
  1046. // FIXME: This is a bit hackish, we can simplify this once we don't need to support
  1047. // the old parser anymore, since then we don't need to maintain its children_changed() semantics.
  1048. if (token.is_end_tag() && token.tag_name() == "style") {
  1049. current_node().children_changed();
  1050. // NOTE: We don't return here, keep going.
  1051. }
  1052. if (token.is_end_tag()) {
  1053. m_stack_of_open_elements.pop();
  1054. m_insertion_mode = m_original_insertion_mode;
  1055. return;
  1056. }
  1057. TODO();
  1058. }
  1059. void HTMLDocumentParser::clear_the_stack_back_to_a_table_context()
  1060. {
  1061. while (!current_node().tag_name().is_one_of("table", "template", "html"))
  1062. m_stack_of_open_elements.pop();
  1063. }
  1064. void HTMLDocumentParser::clear_the_stack_back_to_a_table_row_context()
  1065. {
  1066. while (!current_node().tag_name().is_one_of("tr", "template", "html"))
  1067. m_stack_of_open_elements.pop();
  1068. }
  1069. void HTMLDocumentParser::clear_the_stack_back_to_a_table_body_context()
  1070. {
  1071. while (!current_node().tag_name().is_one_of("tbody", "tfoot", "thead", "template", "html"))
  1072. m_stack_of_open_elements.pop();
  1073. }
  1074. void HTMLDocumentParser::handle_in_row(HTMLToken& token)
  1075. {
  1076. if (token.is_start_tag() && token.tag_name().is_one_of("th", "td")) {
  1077. clear_the_stack_back_to_a_table_row_context();
  1078. insert_html_element(token);
  1079. m_insertion_mode = InsertionMode::InCell;
  1080. m_list_of_active_formatting_elements.add_marker();
  1081. return;
  1082. }
  1083. if (token.is_end_tag() && token.tag_name() == "tr") {
  1084. if (!m_stack_of_open_elements.has_in_table_scope("tr")) {
  1085. PARSE_ERROR();
  1086. return;
  1087. }
  1088. clear_the_stack_back_to_a_table_row_context();
  1089. m_stack_of_open_elements.pop();
  1090. m_insertion_mode = InsertionMode::InTableBody;
  1091. return;
  1092. }
  1093. TODO();
  1094. }
  1095. void HTMLDocumentParser::close_the_cell()
  1096. {
  1097. generate_implied_end_tags();
  1098. if (!current_node().tag_name().is_one_of("td", "th")) {
  1099. PARSE_ERROR();
  1100. }
  1101. while (!current_node().tag_name().is_one_of("td", "th"))
  1102. m_stack_of_open_elements.pop();
  1103. m_stack_of_open_elements.pop();
  1104. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1105. m_insertion_mode = InsertionMode::InRow;
  1106. }
  1107. void HTMLDocumentParser::handle_in_cell(HTMLToken& token)
  1108. {
  1109. if (token.is_end_tag() && token.tag_name().is_one_of("td", "th")) {
  1110. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1111. PARSE_ERROR();
  1112. return;
  1113. }
  1114. generate_implied_end_tags();
  1115. if (current_node().tag_name() != token.tag_name()) {
  1116. PARSE_ERROR();
  1117. }
  1118. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1119. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1120. m_insertion_mode = InsertionMode::InRow;
  1121. return;
  1122. }
  1123. if (token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr")) {
  1124. if (!m_stack_of_open_elements.has_in_table_scope("td") && m_stack_of_open_elements.has_in_table_scope("th")) {
  1125. PARSE_ERROR();
  1126. return;
  1127. }
  1128. close_the_cell();
  1129. process_using_the_rules_for(m_insertion_mode, token);
  1130. return;
  1131. }
  1132. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html")) {
  1133. PARSE_ERROR();
  1134. return;
  1135. }
  1136. if (token.is_end_tag() && token.tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  1137. TODO();
  1138. }
  1139. process_using_the_rules_for(InsertionMode::InBody, token);
  1140. }
  1141. void HTMLDocumentParser::handle_in_table_body(HTMLToken& token)
  1142. {
  1143. if (token.is_start_tag() && token.tag_name() == "tr") {
  1144. clear_the_stack_back_to_a_table_body_context();
  1145. insert_html_element(token);
  1146. m_insertion_mode = InsertionMode::InRow;
  1147. return;
  1148. }
  1149. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "tfoot", "thead"))
  1150. || (token.is_end_tag() && token.tag_name() == "table")) {
  1151. // 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.
  1152. clear_the_stack_back_to_a_table_body_context();
  1153. m_stack_of_open_elements.pop();
  1154. m_insertion_mode = InsertionMode::InTable;
  1155. process_using_the_rules_for(InsertionMode::InTable, token);
  1156. return;
  1157. }
  1158. TODO();
  1159. }
  1160. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  1161. {
  1162. if (token.is_character() && current_node().tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  1163. TODO();
  1164. }
  1165. if (token.is_comment()) {
  1166. insert_comment(token);
  1167. return;
  1168. }
  1169. if (token.is_doctype()) {
  1170. PARSE_ERROR();
  1171. return;
  1172. }
  1173. if (token.is_start_tag() && token.tag_name() == "caption") {
  1174. TODO();
  1175. }
  1176. if (token.is_start_tag() && token.tag_name() == "colgroup") {
  1177. TODO();
  1178. }
  1179. if (token.is_start_tag() && token.tag_name() == "col") {
  1180. TODO();
  1181. }
  1182. if (token.is_start_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  1183. TODO();
  1184. }
  1185. if (token.is_start_tag() && token.tag_name().is_one_of("td", "th", "tr")) {
  1186. clear_the_stack_back_to_a_table_context();
  1187. HTMLToken fake_tbody_token;
  1188. fake_tbody_token.m_type = HTMLToken::Type::StartTag;
  1189. fake_tbody_token.m_tag.tag_name.append("tbody");
  1190. insert_html_element(fake_tbody_token);
  1191. m_insertion_mode = InsertionMode::InTableBody;
  1192. process_using_the_rules_for(InsertionMode::InTableBody, token);
  1193. return;
  1194. }
  1195. if (token.is_start_tag() && token.tag_name() == "table") {
  1196. PARSE_ERROR();
  1197. TODO();
  1198. }
  1199. if (token.is_end_tag()) {
  1200. if (!m_stack_of_open_elements.has_in_table_scope("table")) {
  1201. PARSE_ERROR();
  1202. return;
  1203. }
  1204. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("table");
  1205. reset_the_insertion_mode_appropriately();
  1206. return;
  1207. }
  1208. TODO();
  1209. }
  1210. void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
  1211. {
  1212. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1213. RefPtr<Element> node = m_stack_of_open_elements.elements().at(i);
  1214. if (node->tag_name() == "select") {
  1215. TODO();
  1216. }
  1217. if (node->tag_name().is_one_of("td", "th")) {
  1218. m_insertion_mode = InsertionMode::InCell;
  1219. return;
  1220. }
  1221. if (node->tag_name() == "tr") {
  1222. m_insertion_mode = InsertionMode::InRow;
  1223. return;
  1224. }
  1225. if (node->tag_name().is_one_of("tbody", "thead", "tfoot")) {
  1226. m_insertion_mode = InsertionMode::InTableBody;
  1227. return;
  1228. }
  1229. if (node->tag_name() == "caption") {
  1230. m_insertion_mode = InsertionMode::InCaption;
  1231. return;
  1232. }
  1233. if (node->tag_name() == "colgroup") {
  1234. m_insertion_mode = InsertionMode::InColumnGroup;
  1235. return;
  1236. }
  1237. if (node->tag_name() == "table") {
  1238. m_insertion_mode = InsertionMode::InTable;
  1239. return;
  1240. }
  1241. if (node->tag_name() == "template") {
  1242. TODO();
  1243. }
  1244. if (node->tag_name() == "body") {
  1245. m_insertion_mode = InsertionMode::InBody;
  1246. return;
  1247. }
  1248. if (node->tag_name() == "frameset") {
  1249. m_insertion_mode = InsertionMode::InFrameset;
  1250. if (m_parsing_fragment) {
  1251. TODO();
  1252. }
  1253. return;
  1254. }
  1255. if (node->tag_name() == "html") {
  1256. TODO();
  1257. }
  1258. }
  1259. m_insertion_mode = InsertionMode::InBody;
  1260. if (m_parsing_fragment) {
  1261. TODO();
  1262. }
  1263. }
  1264. const char* HTMLDocumentParser::insertion_mode_name() const
  1265. {
  1266. switch (m_insertion_mode) {
  1267. #define __ENUMERATE_INSERTION_MODE(mode) \
  1268. case InsertionMode::mode: \
  1269. return #mode;
  1270. ENUMERATE_INSERTION_MODES
  1271. #undef __ENUMERATE_INSERTION_MODE
  1272. }
  1273. ASSERT_NOT_REACHED();
  1274. }
  1275. Document& HTMLDocumentParser::document()
  1276. {
  1277. return *m_document;
  1278. }
  1279. }