HTMLDocumentParser.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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. auto scripts_to_execute_when_parsing_has_finished = m_document->take_scripts_to_execute_when_parsing_has_finished({});
  72. for (auto& script : scripts_to_execute_when_parsing_has_finished) {
  73. script.execute_script();
  74. }
  75. m_document->dispatch_event(Event::create("DOMContentLoaded"));
  76. auto scripts_to_execute_as_soon_as_possible = m_document->take_scripts_to_execute_as_soon_as_possible({});
  77. for (auto& script : scripts_to_execute_as_soon_as_possible) {
  78. script.execute_script();
  79. }
  80. }
  81. void HTMLDocumentParser::process_using_the_rules_for(InsertionMode mode, HTMLToken& token)
  82. {
  83. switch (mode) {
  84. case InsertionMode::Initial:
  85. handle_initial(token);
  86. break;
  87. case InsertionMode::BeforeHTML:
  88. handle_before_html(token);
  89. break;
  90. case InsertionMode::BeforeHead:
  91. handle_before_head(token);
  92. break;
  93. case InsertionMode::InHead:
  94. handle_in_head(token);
  95. break;
  96. case InsertionMode::InHeadNoscript:
  97. handle_in_head_noscript(token);
  98. break;
  99. case InsertionMode::AfterHead:
  100. handle_after_head(token);
  101. break;
  102. case InsertionMode::InBody:
  103. handle_in_body(token);
  104. break;
  105. case InsertionMode::AfterBody:
  106. handle_after_body(token);
  107. break;
  108. case InsertionMode::AfterAfterBody:
  109. handle_after_after_body(token);
  110. break;
  111. case InsertionMode::Text:
  112. handle_text(token);
  113. break;
  114. case InsertionMode::InTable:
  115. handle_in_table(token);
  116. break;
  117. case InsertionMode::InTableBody:
  118. handle_in_table_body(token);
  119. break;
  120. case InsertionMode::InRow:
  121. handle_in_row(token);
  122. break;
  123. case InsertionMode::InCell:
  124. handle_in_cell(token);
  125. break;
  126. case InsertionMode::InTableText:
  127. handle_in_table_text(token);
  128. break;
  129. default:
  130. ASSERT_NOT_REACHED();
  131. }
  132. }
  133. void HTMLDocumentParser::handle_initial(HTMLToken& token)
  134. {
  135. if (token.is_character() && token.is_parser_whitespace()) {
  136. return;
  137. }
  138. if (token.is_comment()) {
  139. auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
  140. document().append_child(move(comment));
  141. return;
  142. }
  143. if (token.is_doctype()) {
  144. auto doctype = adopt(*new DocumentType(document()));
  145. doctype->set_name(token.m_doctype.name.to_string());
  146. document().append_child(move(doctype));
  147. m_insertion_mode = InsertionMode::BeforeHTML;
  148. return;
  149. }
  150. PARSE_ERROR();
  151. document().set_quirks_mode(true);
  152. m_insertion_mode = InsertionMode::BeforeHTML;
  153. process_using_the_rules_for(InsertionMode::BeforeHTML, token);
  154. }
  155. void HTMLDocumentParser::handle_before_html(HTMLToken& token)
  156. {
  157. if (token.is_doctype()) {
  158. PARSE_ERROR();
  159. return;
  160. }
  161. if (token.is_comment()) {
  162. auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
  163. document().append_child(move(comment));
  164. return;
  165. }
  166. if (token.is_character() && token.is_parser_whitespace()) {
  167. return;
  168. }
  169. if (token.is_start_tag() && token.tag_name() == "html") {
  170. auto element = create_element_for(token);
  171. document().append_child(element);
  172. m_stack_of_open_elements.push(move(element));
  173. m_insertion_mode = InsertionMode::BeforeHead;
  174. return;
  175. }
  176. if (token.is_end_tag() && token.tag_name().is_one_of("head", "body", "html", "br")) {
  177. goto AnythingElse;
  178. }
  179. if (token.is_end_tag()) {
  180. PARSE_ERROR();
  181. return;
  182. }
  183. AnythingElse:
  184. auto element = create_element(document(), "html");
  185. m_stack_of_open_elements.push(element);
  186. // 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.
  187. m_insertion_mode = InsertionMode::BeforeHead;
  188. process_using_the_rules_for(InsertionMode::BeforeHead, token);
  189. return;
  190. }
  191. Element& HTMLDocumentParser::current_node()
  192. {
  193. return m_stack_of_open_elements.current_node();
  194. }
  195. RefPtr<Node> HTMLDocumentParser::find_appropriate_place_for_inserting_node()
  196. {
  197. auto& target = current_node();
  198. if (m_foster_parenting) {
  199. TODO();
  200. }
  201. return target;
  202. }
  203. NonnullRefPtr<Element> HTMLDocumentParser::create_element_for(HTMLToken& token)
  204. {
  205. auto element = create_element(document(), token.tag_name());
  206. for (auto& attribute : token.m_tag.attributes) {
  207. element->set_attribute(attribute.name_builder.to_string(), attribute.value_builder.to_string());
  208. }
  209. return element;
  210. }
  211. RefPtr<Element> HTMLDocumentParser::insert_html_element(HTMLToken& token)
  212. {
  213. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  214. auto element = create_element_for(token);
  215. // FIXME: Check if it's possible to insert `element` at `adjusted_insertion_location`
  216. adjusted_insertion_location->append_child(element);
  217. m_stack_of_open_elements.push(element);
  218. return element;
  219. }
  220. void HTMLDocumentParser::handle_before_head(HTMLToken& token)
  221. {
  222. if (token.is_character() && token.is_parser_whitespace()) {
  223. return;
  224. }
  225. if (token.is_comment()) {
  226. insert_comment(token);
  227. return;
  228. }
  229. if (token.is_doctype()) {
  230. PARSE_ERROR();
  231. return;
  232. }
  233. if (token.is_start_tag() && token.tag_name() == "html") {
  234. process_using_the_rules_for(InsertionMode::InBody, token);
  235. return;
  236. }
  237. if (token.is_start_tag() && token.tag_name() == "head") {
  238. auto element = insert_html_element(token);
  239. m_head_element = to<HTMLHeadElement>(element);
  240. m_insertion_mode = InsertionMode::InHead;
  241. return;
  242. }
  243. if (token.is_end_tag() && token.tag_name().is_one_of("head", "body", "html", "br")) {
  244. goto AnythingElse;
  245. }
  246. if (token.is_end_tag()) {
  247. PARSE_ERROR();
  248. return;
  249. }
  250. AnythingElse:
  251. HTMLToken fake_head_token;
  252. fake_head_token.m_type = HTMLToken::Type::StartTag;
  253. fake_head_token.m_tag.tag_name.append("head");
  254. m_head_element = to<HTMLHeadElement>(insert_html_element(fake_head_token));
  255. m_insertion_mode = InsertionMode::InHead;
  256. process_using_the_rules_for(InsertionMode::InHead, token);
  257. return;
  258. }
  259. void HTMLDocumentParser::insert_comment(HTMLToken& token)
  260. {
  261. auto data = token.m_comment_or_character.data.to_string();
  262. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  263. adjusted_insertion_location->append_child(adopt(*new Comment(document(), data)));
  264. }
  265. void HTMLDocumentParser::handle_in_head(HTMLToken& token)
  266. {
  267. if (token.is_parser_whitespace()) {
  268. insert_character(token.codepoint());
  269. return;
  270. }
  271. if (token.is_comment()) {
  272. insert_comment(token);
  273. return;
  274. }
  275. if (token.is_doctype()) {
  276. PARSE_ERROR();
  277. return;
  278. }
  279. if (token.is_start_tag() && token.tag_name() == "html") {
  280. process_using_the_rules_for(InsertionMode::InBody, token);
  281. return;
  282. }
  283. if (token.is_start_tag() && token.tag_name().is_one_of("base", "basefont", "bgsound", "link")) {
  284. insert_html_element(token);
  285. m_stack_of_open_elements.pop();
  286. token.acknowledge_self_closing_flag_if_set();
  287. return;
  288. }
  289. if (token.is_start_tag() && token.tag_name() == "meta") {
  290. auto element = insert_html_element(token);
  291. m_stack_of_open_elements.pop();
  292. token.acknowledge_self_closing_flag_if_set();
  293. return;
  294. }
  295. if (token.is_start_tag() && token.tag_name() == "title") {
  296. insert_html_element(token);
  297. m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
  298. m_original_insertion_mode = m_insertion_mode;
  299. m_insertion_mode = InsertionMode::Text;
  300. return;
  301. }
  302. if (token.is_start_tag() && ((token.tag_name() == "noscript" && m_scripting_enabled) || token.tag_name() == "noframes" || token.tag_name() == "style")) {
  303. parse_generic_raw_text_element(token);
  304. return;
  305. }
  306. if (token.is_start_tag() && token.tag_name() == "script") {
  307. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  308. auto element = create_element_for(token);
  309. auto& script_element = to<HTMLScriptElement>(*element);
  310. script_element.set_parser_document({}, document());
  311. script_element.set_non_blocking({}, false);
  312. if (m_parsing_fragment) {
  313. TODO();
  314. }
  315. if (m_invoked_via_document_write) {
  316. TODO();
  317. }
  318. adjusted_insertion_location->append_child(element, false);
  319. m_stack_of_open_elements.push(element);
  320. m_tokenizer.switch_to({}, HTMLTokenizer::State::ScriptData);
  321. m_original_insertion_mode = m_insertion_mode;
  322. m_insertion_mode = InsertionMode::Text;
  323. return;
  324. }
  325. if (token.is_end_tag() && token.tag_name() == "head") {
  326. m_stack_of_open_elements.pop();
  327. m_insertion_mode = InsertionMode::AfterHead;
  328. return;
  329. }
  330. if (token.is_end_tag() && token.tag_name().is_one_of("body", "html", "br")) {
  331. TODO();
  332. }
  333. if (token.is_start_tag() && token.tag_name() == "template") {
  334. TODO();
  335. }
  336. if (token.is_end_tag() && token.tag_name() == "template") {
  337. TODO();
  338. }
  339. if ((token.is_start_tag() && token.tag_name() == "head") || token.is_end_tag()) {
  340. PARSE_ERROR();
  341. return;
  342. }
  343. m_stack_of_open_elements.pop();
  344. m_insertion_mode = InsertionMode::AfterHead;
  345. process_using_the_rules_for(m_insertion_mode, token);
  346. }
  347. void HTMLDocumentParser::handle_in_head_noscript(HTMLToken&)
  348. {
  349. TODO();
  350. }
  351. void HTMLDocumentParser::parse_generic_raw_text_element(HTMLToken& token)
  352. {
  353. insert_html_element(token);
  354. m_tokenizer.switch_to({}, HTMLTokenizer::State::RAWTEXT);
  355. m_original_insertion_mode = m_insertion_mode;
  356. m_insertion_mode = InsertionMode::Text;
  357. }
  358. void HTMLDocumentParser::insert_character(u32 data)
  359. {
  360. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  361. if (adjusted_insertion_location->is_document())
  362. return;
  363. if (adjusted_insertion_location->last_child() && adjusted_insertion_location->last_child()->is_text()) {
  364. auto& existing_text_node = to<Text>(*adjusted_insertion_location->last_child());
  365. StringBuilder builder;
  366. builder.append(existing_text_node.data());
  367. builder.append(Utf32View { &data, 1 });
  368. existing_text_node.set_data(builder.to_string());
  369. return;
  370. }
  371. auto new_text_node = adopt(*new Text(document(), ""));
  372. adjusted_insertion_location->append_child(new_text_node);
  373. StringBuilder builder;
  374. builder.append(Utf32View { &data, 1 });
  375. new_text_node->set_data(builder.to_string());
  376. }
  377. void HTMLDocumentParser::handle_after_head(HTMLToken& token)
  378. {
  379. if (token.is_character()) {
  380. if (token.is_parser_whitespace()) {
  381. insert_character(token.codepoint());
  382. return;
  383. }
  384. TODO();
  385. }
  386. if (token.is_comment()) {
  387. TODO();
  388. }
  389. if (token.is_doctype()) {
  390. TODO();
  391. }
  392. if (token.is_start_tag() && token.tag_name() == "html") {
  393. TODO();
  394. }
  395. if (token.is_start_tag() && token.tag_name() == "body") {
  396. insert_html_element(token);
  397. m_frameset_ok = false;
  398. m_insertion_mode = InsertionMode::InBody;
  399. return;
  400. }
  401. if (token.is_start_tag() && token.tag_name() == "frameset") {
  402. TODO();
  403. }
  404. if (token.is_start_tag() && token.tag_name().is_one_of("base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title")) {
  405. TODO();
  406. }
  407. if (token.is_end_tag() && token.tag_name() == "template") {
  408. TODO();
  409. }
  410. if (token.is_end_tag() && token.tag_name().is_one_of("body", "html", "br")) {
  411. goto AnythingElse;
  412. }
  413. if ((token.is_start_tag() && token.tag_name() == "head") || token.is_end_tag()) {
  414. TODO();
  415. }
  416. AnythingElse:
  417. HTMLToken fake_body_token;
  418. fake_body_token.m_type = HTMLToken::Type::StartTag;
  419. fake_body_token.m_tag.tag_name.append("body");
  420. insert_html_element(fake_body_token);
  421. m_insertion_mode = InsertionMode::InBody;
  422. // FIXME: Reprocess the current token in InBody!
  423. }
  424. void HTMLDocumentParser::generate_implied_end_tags(const FlyString& exception)
  425. {
  426. while (current_node().tag_name() != exception && current_node().tag_name().is_one_of("dd", "dt", "li", "optgroup", "option", "p", "rb", "rp", "rt", "rtc"))
  427. m_stack_of_open_elements.pop();
  428. }
  429. void HTMLDocumentParser::close_a_p_element()
  430. {
  431. generate_implied_end_tags("p");
  432. if (current_node().tag_name() != "p") {
  433. PARSE_ERROR();
  434. }
  435. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("p");
  436. }
  437. void HTMLDocumentParser::handle_after_body(HTMLToken& token)
  438. {
  439. if (token.is_character() && token.is_parser_whitespace()) {
  440. process_using_the_rules_for(InsertionMode::InBody, token);
  441. return;
  442. }
  443. if (token.is_comment()) {
  444. TODO();
  445. }
  446. if (token.is_doctype()) {
  447. PARSE_ERROR();
  448. return;
  449. }
  450. if (token.is_start_tag() && token.tag_name() == "html") {
  451. process_using_the_rules_for(InsertionMode::InBody, token);
  452. return;
  453. }
  454. if (token.is_end_of_file()) {
  455. stop_parsing();
  456. return;
  457. }
  458. if (token.is_end_tag() && token.tag_name() == "html") {
  459. if (m_parsing_fragment) {
  460. TODO();
  461. }
  462. m_insertion_mode = InsertionMode::AfterAfterBody;
  463. return;
  464. }
  465. PARSE_ERROR();
  466. m_insertion_mode = InsertionMode::InBody;
  467. process_using_the_rules_for(InsertionMode::InBody, token);
  468. }
  469. void HTMLDocumentParser::handle_after_after_body(HTMLToken& token)
  470. {
  471. if (token.is_doctype() || token.is_parser_whitespace() || (token.is_start_tag() && token.tag_name() == "html")) {
  472. process_using_the_rules_for(InsertionMode::InBody, token);
  473. return;
  474. }
  475. if (token.is_end_of_file()) {
  476. stop_parsing();
  477. return;
  478. }
  479. TODO();
  480. }
  481. void HTMLDocumentParser::reconstruct_the_active_formatting_elements()
  482. {
  483. // FIXME: This needs to care about "markers"
  484. if (m_list_of_active_formatting_elements.is_empty())
  485. return;
  486. if (m_list_of_active_formatting_elements.entries().last().is_marker())
  487. return;
  488. if (m_stack_of_open_elements.contains(*m_list_of_active_formatting_elements.entries().last().element))
  489. return;
  490. ssize_t index = m_list_of_active_formatting_elements.entries().size() - 1;
  491. RefPtr<Element> entry = m_list_of_active_formatting_elements.entries().at(index).element;
  492. ASSERT(entry);
  493. Rewind:
  494. if (index == 0) {
  495. goto Create;
  496. }
  497. --index;
  498. entry = m_list_of_active_formatting_elements.entries().at(index).element;
  499. ASSERT(entry);
  500. if (!m_stack_of_open_elements.contains(*entry))
  501. goto Rewind;
  502. Advance:
  503. ++index;
  504. entry = m_list_of_active_formatting_elements.entries().at(index).element;
  505. ASSERT(entry);
  506. Create:
  507. // FIXME: Hold on to the real token!
  508. HTMLToken fake_token;
  509. fake_token.m_type = HTMLToken::Type::StartTag;
  510. fake_token.m_tag.tag_name.append(entry->tag_name());
  511. auto new_element = insert_html_element(fake_token);
  512. m_list_of_active_formatting_elements.entries().at(index).element = *new_element;
  513. if (index != (ssize_t)m_list_of_active_formatting_elements.entries().size() - 1)
  514. goto Advance;
  515. }
  516. HTMLDocumentParser::AdoptionAgencyAlgorithmOutcome HTMLDocumentParser::run_the_adoption_agency_algorithm(HTMLToken& token)
  517. {
  518. auto subject = token.tag_name();
  519. // If the current node is an HTML element whose tag name is subject,
  520. // and the current node is not in the list of active formatting elements,
  521. // then pop the current node off the stack of open elements, and return.
  522. if (current_node().tag_name() == subject && !m_list_of_active_formatting_elements.contains(current_node())) {
  523. m_stack_of_open_elements.pop();
  524. return AdoptionAgencyAlgorithmOutcome::DoNothing;
  525. }
  526. size_t outer_loop_counter = 0;
  527. //OuterLoop:
  528. if (outer_loop_counter >= 8)
  529. return AdoptionAgencyAlgorithmOutcome::DoNothing;
  530. ++outer_loop_counter;
  531. auto formatting_element = m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker(subject);
  532. if (!formatting_element)
  533. return AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps;
  534. if (!m_stack_of_open_elements.contains(*formatting_element)) {
  535. PARSE_ERROR();
  536. // FIXME: If formatting element is not in the stack of open elements,
  537. // then this is a parse error; remove the element from the list, and return.
  538. TODO();
  539. }
  540. if (!m_stack_of_open_elements.has_in_scope(*formatting_element)) {
  541. PARSE_ERROR();
  542. return AdoptionAgencyAlgorithmOutcome::DoNothing;
  543. }
  544. if (formatting_element != &current_node()) {
  545. PARSE_ERROR();
  546. }
  547. RefPtr<Element> furthest_block = m_stack_of_open_elements.topmost_special_node_below(*formatting_element);
  548. if (!furthest_block) {
  549. while (&current_node() != formatting_element)
  550. m_stack_of_open_elements.pop();
  551. m_stack_of_open_elements.pop();
  552. m_list_of_active_formatting_elements.remove(*formatting_element);
  553. return AdoptionAgencyAlgorithmOutcome::DoNothing;
  554. }
  555. // FIXME: Implement the rest of the AAA :^)
  556. TODO();
  557. }
  558. bool HTMLDocumentParser::is_special_tag(const FlyString& tag_name)
  559. {
  560. return tag_name.is_one_of(
  561. "address",
  562. "applet",
  563. "area",
  564. "article",
  565. "aside",
  566. "base",
  567. "basefont",
  568. "bgsound",
  569. "blockquote",
  570. "body",
  571. "br",
  572. "button",
  573. "caption",
  574. "center",
  575. "col",
  576. "colgroup",
  577. "dd",
  578. "details",
  579. "dir",
  580. "div",
  581. "dl",
  582. "dt",
  583. "embed",
  584. "fieldset",
  585. "figcaption",
  586. "figure",
  587. "footer",
  588. "form",
  589. "frame",
  590. "frameset",
  591. "h1",
  592. "h2",
  593. "h3",
  594. "h4",
  595. "h5",
  596. "h6",
  597. "head",
  598. "header",
  599. "hgroup",
  600. "hr",
  601. "html",
  602. "iframe",
  603. "img",
  604. "input",
  605. "keygen",
  606. "li",
  607. "link",
  608. "listing",
  609. "main",
  610. "marquee",
  611. "menu",
  612. "meta",
  613. "nav",
  614. "noembed",
  615. "noframes",
  616. "noscript",
  617. "object",
  618. "ol",
  619. "p",
  620. "param",
  621. "plaintext",
  622. "pre",
  623. "script",
  624. "section",
  625. "select",
  626. "source",
  627. "style",
  628. "summary",
  629. "table",
  630. "tbody",
  631. "td",
  632. "template",
  633. "textarea",
  634. "tfoot",
  635. "th",
  636. "thead",
  637. "title",
  638. "tr",
  639. "track",
  640. "ul",
  641. "wbr",
  642. "xmp");
  643. }
  644. void HTMLDocumentParser::handle_in_body(HTMLToken& token)
  645. {
  646. if (token.is_character()) {
  647. if (token.codepoint() == 0) {
  648. TODO();
  649. }
  650. if (token.is_parser_whitespace()) {
  651. reconstruct_the_active_formatting_elements();
  652. insert_character(token.codepoint());
  653. return;
  654. }
  655. reconstruct_the_active_formatting_elements();
  656. insert_character(token.codepoint());
  657. m_frameset_ok = false;
  658. return;
  659. }
  660. if (token.is_comment()) {
  661. insert_comment(token);
  662. return;
  663. }
  664. if (token.is_doctype()) {
  665. PARSE_ERROR();
  666. return;
  667. }
  668. if (token.is_start_tag() && token.tag_name() == "html") {
  669. TODO();
  670. }
  671. if (token.is_start_tag() && token.tag_name().is_one_of("base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title")) {
  672. process_using_the_rules_for(InsertionMode::InHead, token);
  673. return;
  674. }
  675. if (token.is_end_tag() && token.tag_name() == "template") {
  676. process_using_the_rules_for(InsertionMode::InHead, token);
  677. return;
  678. }
  679. if (token.is_start_tag() && token.tag_name() == "body") {
  680. TODO();
  681. }
  682. if (token.is_start_tag() && token.tag_name() == "frameset") {
  683. TODO();
  684. }
  685. if (token.is_end_of_file()) {
  686. // FIXME: If the stack of template insertion modes is not empty,
  687. // then process the token using the rules for the "in template" insertion mode.
  688. // FIXME: If there is a node in the stack of open elements that is not either
  689. // a dd element, a dt element, an li element, an optgroup element, an option element,
  690. // a p element, an rb element, an rp element, an rt element, an rtc element,
  691. // a tbody element, a td element, a tfoot element, a th element, a thead element,
  692. // a tr element, the body element, or the html element, then this is a parse error.
  693. stop_parsing();
  694. return;
  695. }
  696. if (token.is_end_tag() && token.tag_name() == "body") {
  697. if (!m_stack_of_open_elements.has_in_scope("body")) {
  698. TODO();
  699. }
  700. // FIXME: Otherwise, if there is a node in the stack of open elements that is
  701. // not either a dd element, a dt element, an li element, an optgroup element,
  702. // an option element, a p element, an rb element, an rp element, an rt element,
  703. // an rtc element, a tbody element, a td element, a tfoot element, a th element,
  704. // a thead element, a tr element, the body element, or the html element,
  705. // then this is a parse error.
  706. m_insertion_mode = InsertionMode::AfterBody;
  707. return;
  708. }
  709. if (token.is_start_tag() && token.tag_name() == "html") {
  710. TODO();
  711. }
  712. 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")) {
  713. if (m_stack_of_open_elements.has_in_button_scope("p"))
  714. close_a_p_element();
  715. insert_html_element(token);
  716. return;
  717. }
  718. if (token.is_start_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  719. if (m_stack_of_open_elements.has_in_button_scope("p"))
  720. close_a_p_element();
  721. if (current_node().tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  722. PARSE_ERROR();
  723. m_stack_of_open_elements.pop();
  724. }
  725. insert_html_element(token);
  726. return;
  727. }
  728. if (token.is_start_tag() && token.tag_name().is_one_of("pre", "listing")) {
  729. if (m_stack_of_open_elements.has_in_button_scope("p"))
  730. close_a_p_element();
  731. insert_html_element(token);
  732. m_frameset_ok = false;
  733. // If the next token is a U+000A LINE FEED (LF) character token,
  734. // then ignore that token and move on to the next one.
  735. // (Newlines at the start of pre blocks are ignored as an authoring convenience.)
  736. auto next_token = m_tokenizer.next_token();
  737. if (next_token.has_value() && next_token.value().is_character() && next_token.value().codepoint() == '\n') {
  738. // Ignore it.
  739. } else {
  740. process_using_the_rules_for(m_insertion_mode, next_token.value());
  741. }
  742. return;
  743. }
  744. if (token.is_start_tag() && token.tag_name() == "form") {
  745. if (m_form_element && m_stack_of_open_elements.contains("template")) {
  746. PARSE_ERROR();
  747. return;
  748. }
  749. if (m_stack_of_open_elements.has_in_button_scope("p"))
  750. close_a_p_element();
  751. auto element = insert_html_element(token);
  752. if (!m_stack_of_open_elements.contains("template"))
  753. m_form_element = to<HTMLFormElement>(*element);
  754. return;
  755. }
  756. if (token.is_start_tag() && token.tag_name() == "li") {
  757. m_frameset_ok = false;
  758. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  759. RefPtr<Element> node = m_stack_of_open_elements.elements()[i];
  760. if (node->tag_name() == "li") {
  761. generate_implied_end_tags("li");
  762. if (current_node().tag_name() != "li") {
  763. PARSE_ERROR();
  764. }
  765. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("li");
  766. break;
  767. }
  768. if (is_special_tag(node->tag_name()) && !node->tag_name().is_one_of("address", "div", "p"))
  769. break;
  770. }
  771. if (m_stack_of_open_elements.has_in_button_scope("p"))
  772. close_a_p_element();
  773. insert_html_element(token);
  774. return;
  775. }
  776. if (token.is_start_tag() && token.tag_name().is_one_of("dd", "dt")) {
  777. m_frameset_ok = false;
  778. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  779. RefPtr<Element> node = m_stack_of_open_elements.elements()[i];
  780. if (node->tag_name() == "dd") {
  781. generate_implied_end_tags("dd");
  782. if (current_node().tag_name() != "dd") {
  783. PARSE_ERROR();
  784. }
  785. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("dd");
  786. break;
  787. }
  788. if (node->tag_name() == "dt") {
  789. generate_implied_end_tags("dt");
  790. if (current_node().tag_name() != "dt") {
  791. PARSE_ERROR();
  792. }
  793. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("dt");
  794. break;
  795. }
  796. if (is_special_tag(node->tag_name()) && !node->tag_name().is_one_of("address", "div", "p"))
  797. break;
  798. }
  799. if (m_stack_of_open_elements.has_in_button_scope("p"))
  800. close_a_p_element();
  801. insert_html_element(token);
  802. return;
  803. }
  804. if (token.is_start_tag() && token.tag_name() == "plaintext") {
  805. if (m_stack_of_open_elements.has_in_button_scope("p"))
  806. close_a_p_element();
  807. insert_html_element(token);
  808. m_tokenizer.switch_to({}, HTMLTokenizer::State::PLAINTEXT);
  809. return;
  810. }
  811. if (token.is_start_tag() && token.tag_name() == "button") {
  812. if (m_stack_of_open_elements.has_in_button_scope("button")) {
  813. PARSE_ERROR();
  814. generate_implied_end_tags();
  815. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("button");
  816. }
  817. reconstruct_the_active_formatting_elements();
  818. insert_html_element(token);
  819. m_frameset_ok = false;
  820. return;
  821. }
  822. 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")) {
  823. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  824. PARSE_ERROR();
  825. return;
  826. }
  827. generate_implied_end_tags();
  828. if (current_node().tag_name() != token.tag_name()) {
  829. PARSE_ERROR();
  830. }
  831. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  832. return;
  833. }
  834. if (token.is_end_tag() && token.tag_name() == "form") {
  835. if (!m_stack_of_open_elements.contains("template")) {
  836. auto node = m_form_element;
  837. m_form_element = nullptr;
  838. if (!node || m_stack_of_open_elements.has_in_scope(*node)) {
  839. PARSE_ERROR();
  840. return;
  841. }
  842. generate_implied_end_tags();
  843. if (&current_node() != node) {
  844. PARSE_ERROR();
  845. }
  846. m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) { return entry.ptr() == node.ptr(); });
  847. } else {
  848. TODO();
  849. }
  850. return;
  851. }
  852. if (token.is_end_tag() && token.tag_name() == "p") {
  853. if (!m_stack_of_open_elements.has_in_button_scope("p")) {
  854. PARSE_ERROR();
  855. HTMLToken fake_p_token;
  856. fake_p_token.m_type = HTMLToken::Type::StartTag;
  857. fake_p_token.m_tag.tag_name.append("p");
  858. insert_html_element(fake_p_token);
  859. }
  860. close_a_p_element();
  861. return;
  862. }
  863. if (token.is_end_tag() && token.tag_name() == "li") {
  864. if (!m_stack_of_open_elements.has_in_list_item_scope("li")) {
  865. PARSE_ERROR();
  866. return;
  867. }
  868. generate_implied_end_tags("li");
  869. if (current_node().tag_name() != "li") {
  870. PARSE_ERROR();
  871. dbg() << "Expected <li> current node, but had <" << current_node().tag_name() << ">";
  872. }
  873. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("li");
  874. return;
  875. }
  876. if (token.is_end_tag() && token.tag_name().is_one_of("dd", "dt")) {
  877. TODO();
  878. }
  879. if (token.is_end_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  880. if (!m_stack_of_open_elements.has_in_scope("h1")
  881. && !m_stack_of_open_elements.has_in_scope("h2")
  882. && !m_stack_of_open_elements.has_in_scope("h3")
  883. && !m_stack_of_open_elements.has_in_scope("h4")
  884. && !m_stack_of_open_elements.has_in_scope("h5")
  885. && !m_stack_of_open_elements.has_in_scope("h6")) {
  886. PARSE_ERROR();
  887. return;
  888. }
  889. generate_implied_end_tags();
  890. if (current_node().tag_name() != token.tag_name()) {
  891. PARSE_ERROR();
  892. }
  893. for (;;) {
  894. auto popped_element = m_stack_of_open_elements.pop();
  895. if (popped_element->tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6"))
  896. break;
  897. }
  898. return;
  899. }
  900. if (token.is_start_tag() && token.tag_name() == "a") {
  901. if (auto* element = m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker("a")) {
  902. PARSE_ERROR();
  903. if (run_the_adoption_agency_algorithm(token) == AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps)
  904. goto AnyOtherEndTag;
  905. m_list_of_active_formatting_elements.remove(*element);
  906. m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) {
  907. return entry.ptr() == element;
  908. });
  909. }
  910. reconstruct_the_active_formatting_elements();
  911. auto element = insert_html_element(token);
  912. m_list_of_active_formatting_elements.add(*element);
  913. return;
  914. }
  915. if (token.is_start_tag() && token.tag_name().is_one_of("b", "big", "code", "em", "font", "i", "s", "small", "strike", "strong", "tt", "u")) {
  916. reconstruct_the_active_formatting_elements();
  917. auto element = insert_html_element(token);
  918. m_list_of_active_formatting_elements.add(*element);
  919. return;
  920. }
  921. if (token.is_start_tag() && token.tag_name() == "nobr") {
  922. TODO();
  923. }
  924. 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")) {
  925. if (run_the_adoption_agency_algorithm(token) == AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps)
  926. goto AnyOtherEndTag;
  927. return;
  928. }
  929. if (token.is_start_tag() && token.tag_name().is_one_of("applet", "marquee", "object")) {
  930. reconstruct_the_active_formatting_elements();
  931. insert_html_element(token);
  932. m_list_of_active_formatting_elements.add_marker();
  933. m_frameset_ok = false;
  934. return;
  935. }
  936. if (token.is_end_tag() && token.tag_name().is_one_of("applet", "marquee", "object")) {
  937. TODO();
  938. }
  939. if (token.is_start_tag() && token.tag_name() == "table") {
  940. if (!document().in_quirks_mode()) {
  941. if (m_stack_of_open_elements.has_in_button_scope("p"))
  942. close_a_p_element();
  943. }
  944. insert_html_element(token);
  945. m_frameset_ok = false;
  946. m_insertion_mode = InsertionMode::InTable;
  947. return;
  948. }
  949. if (token.is_end_tag() && token.tag_name() == "br") {
  950. TODO();
  951. }
  952. if (token.is_start_tag() && token.tag_name().is_one_of("area", "br", "embed", "img", "keygen", "wbr")) {
  953. reconstruct_the_active_formatting_elements();
  954. insert_html_element(token);
  955. m_stack_of_open_elements.pop();
  956. token.acknowledge_self_closing_flag_if_set();
  957. m_frameset_ok = false;
  958. return;
  959. }
  960. if (token.is_start_tag() && token.tag_name() == "input") {
  961. reconstruct_the_active_formatting_elements();
  962. insert_html_element(token);
  963. m_stack_of_open_elements.pop();
  964. token.acknowledge_self_closing_flag_if_set();
  965. auto type_attribute = token.attribute(HTML::AttributeNames::type);
  966. if (type_attribute.is_null() || type_attribute != "hidden") {
  967. m_frameset_ok = false;
  968. }
  969. return;
  970. }
  971. if (token.is_start_tag() && token.tag_name().is_one_of("param", "source", "track")) {
  972. TODO();
  973. }
  974. if (token.is_start_tag() && token.tag_name() == "hr") {
  975. if (m_stack_of_open_elements.has_in_button_scope("p"))
  976. close_a_p_element();
  977. insert_html_element(token);
  978. m_stack_of_open_elements.pop();
  979. token.acknowledge_self_closing_flag_if_set();
  980. m_frameset_ok = false;
  981. return;
  982. }
  983. if (token.is_start_tag() && token.tag_name() == "image") {
  984. // Parse error. Change the token's tag name to "img" and reprocess it. (Don't ask.)
  985. PARSE_ERROR();
  986. token.m_tag.tag_name.clear();
  987. token.m_tag.tag_name.append("img");
  988. process_using_the_rules_for(m_insertion_mode, token);
  989. return;
  990. }
  991. if (token.is_start_tag() && token.tag_name() == "textarea") {
  992. TODO();
  993. }
  994. if (token.is_start_tag() && token.tag_name() == "xmp") {
  995. TODO();
  996. }
  997. if (token.is_start_tag() && token.tag_name() == "iframe") {
  998. m_frameset_ok = false;
  999. parse_generic_raw_text_element(token);
  1000. return;
  1001. }
  1002. if (token.is_start_tag() && ((token.tag_name() == "noembed") || (token.tag_name() == "noscript" && m_scripting_enabled))) {
  1003. parse_generic_raw_text_element(token);
  1004. return;
  1005. }
  1006. if (token.is_start_tag() && token.tag_name() == "select") {
  1007. TODO();
  1008. }
  1009. if (token.is_start_tag() && token.tag_name().is_one_of("optgroup", "option")) {
  1010. if (current_node().tag_name() == "option")
  1011. m_stack_of_open_elements.pop();
  1012. reconstruct_the_active_formatting_elements();
  1013. insert_html_element(token);
  1014. return;
  1015. }
  1016. if (token.is_start_tag() && token.tag_name().is_one_of("rb", "rtc")) {
  1017. TODO();
  1018. }
  1019. if (token.is_start_tag() && token.tag_name().is_one_of("rp", "rt")) {
  1020. TODO();
  1021. }
  1022. if (token.is_start_tag() && token.tag_name() == "math") {
  1023. TODO();
  1024. }
  1025. if (token.is_start_tag() && token.tag_name() == "svg") {
  1026. TODO();
  1027. }
  1028. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr"))) {
  1029. PARSE_ERROR();
  1030. return;
  1031. }
  1032. // Any other start tag
  1033. if (token.is_start_tag()) {
  1034. reconstruct_the_active_formatting_elements();
  1035. insert_html_element(token);
  1036. return;
  1037. }
  1038. if (token.is_end_tag()) {
  1039. AnyOtherEndTag:
  1040. RefPtr<Element> node;
  1041. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1042. node = m_stack_of_open_elements.elements()[i];
  1043. if (node->tag_name() == token.tag_name()) {
  1044. generate_implied_end_tags(token.tag_name());
  1045. if (node != current_node()) {
  1046. PARSE_ERROR();
  1047. }
  1048. while (&current_node() != node) {
  1049. m_stack_of_open_elements.pop();
  1050. }
  1051. m_stack_of_open_elements.pop();
  1052. break;
  1053. }
  1054. // FIXME: Handle special elements!
  1055. }
  1056. return;
  1057. }
  1058. TODO();
  1059. }
  1060. void HTMLDocumentParser::increment_script_nesting_level()
  1061. {
  1062. ++m_script_nesting_level;
  1063. }
  1064. void HTMLDocumentParser::decrement_script_nesting_level()
  1065. {
  1066. ASSERT(m_script_nesting_level);
  1067. --m_script_nesting_level;
  1068. }
  1069. void HTMLDocumentParser::handle_text(HTMLToken& token)
  1070. {
  1071. if (token.is_character()) {
  1072. insert_character(token.codepoint());
  1073. return;
  1074. }
  1075. if (token.is_end_tag() && token.tag_name() == "script") {
  1076. NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
  1077. m_stack_of_open_elements.pop();
  1078. m_insertion_mode = m_original_insertion_mode;
  1079. // FIXME: Handle tokenizer insertion point stuff here.
  1080. increment_script_nesting_level();
  1081. script->prepare_script({});
  1082. decrement_script_nesting_level();
  1083. if (script_nesting_level() == 0)
  1084. m_parser_pause_flag = false;
  1085. // FIXME: Handle tokenizer insertion point stuff here too.
  1086. while (document().pending_parsing_blocking_script()) {
  1087. if (script_nesting_level() != 0) {
  1088. m_parser_pause_flag = true;
  1089. // FIXME: Abort the processing of any nested invocations of the tokenizer,
  1090. // yielding control back to the caller. (Tokenization will resume when
  1091. // the caller returns to the "outer" tree construction stage.)
  1092. TODO();
  1093. } else {
  1094. auto the_script = document().take_pending_parsing_blocking_script({});
  1095. m_tokenizer.set_blocked(true);
  1096. // FIXME: If the parser's Document has a style sheet that is blocking scripts
  1097. // or the script's "ready to be parser-executed" flag is not set:
  1098. // spin the event loop until the parser's Document has no style sheet
  1099. // that is blocking scripts and the script's "ready to be parser-executed"
  1100. // flag is set.
  1101. ASSERT(the_script->is_ready_to_be_parser_executed());
  1102. if (m_aborted)
  1103. return;
  1104. m_tokenizer.set_blocked(false);
  1105. // FIXME: Handle tokenizer insertion point stuff here too.
  1106. ASSERT(script_nesting_level() == 0);
  1107. increment_script_nesting_level();
  1108. the_script->execute_script();
  1109. decrement_script_nesting_level();
  1110. ASSERT(script_nesting_level() == 0);
  1111. m_parser_pause_flag = false;
  1112. // FIXME: Handle tokenizer insertion point stuff here too.
  1113. }
  1114. }
  1115. return;
  1116. }
  1117. // FIXME: This is a bit hackish, we can simplify this once we don't need to support
  1118. // the old parser anymore, since then we don't need to maintain its children_changed() semantics.
  1119. if (token.is_end_tag() && token.tag_name() == "style") {
  1120. current_node().children_changed();
  1121. // NOTE: We don't return here, keep going.
  1122. }
  1123. if (token.is_end_tag()) {
  1124. m_stack_of_open_elements.pop();
  1125. m_insertion_mode = m_original_insertion_mode;
  1126. return;
  1127. }
  1128. TODO();
  1129. }
  1130. void HTMLDocumentParser::clear_the_stack_back_to_a_table_context()
  1131. {
  1132. while (!current_node().tag_name().is_one_of("table", "template", "html"))
  1133. m_stack_of_open_elements.pop();
  1134. }
  1135. void HTMLDocumentParser::clear_the_stack_back_to_a_table_row_context()
  1136. {
  1137. while (!current_node().tag_name().is_one_of("tr", "template", "html"))
  1138. m_stack_of_open_elements.pop();
  1139. }
  1140. void HTMLDocumentParser::clear_the_stack_back_to_a_table_body_context()
  1141. {
  1142. while (!current_node().tag_name().is_one_of("tbody", "tfoot", "thead", "template", "html"))
  1143. m_stack_of_open_elements.pop();
  1144. }
  1145. void HTMLDocumentParser::handle_in_row(HTMLToken& token)
  1146. {
  1147. if (token.is_start_tag() && token.tag_name().is_one_of("th", "td")) {
  1148. clear_the_stack_back_to_a_table_row_context();
  1149. insert_html_element(token);
  1150. m_insertion_mode = InsertionMode::InCell;
  1151. m_list_of_active_formatting_elements.add_marker();
  1152. return;
  1153. }
  1154. if (token.is_end_tag() && token.tag_name() == "tr") {
  1155. if (!m_stack_of_open_elements.has_in_table_scope("tr")) {
  1156. PARSE_ERROR();
  1157. return;
  1158. }
  1159. clear_the_stack_back_to_a_table_row_context();
  1160. m_stack_of_open_elements.pop();
  1161. m_insertion_mode = InsertionMode::InTableBody;
  1162. return;
  1163. }
  1164. if (token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr")) {
  1165. if (m_stack_of_open_elements.has_in_table_scope("tr")) {
  1166. PARSE_ERROR();
  1167. return;
  1168. }
  1169. clear_the_stack_back_to_a_table_row_context();
  1170. m_stack_of_open_elements.pop();
  1171. m_insertion_mode = InsertionMode::InTableBody;
  1172. process_using_the_rules_for(m_insertion_mode, token);
  1173. return;
  1174. }
  1175. if (token.is_end_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  1176. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1177. PARSE_ERROR();
  1178. return;
  1179. }
  1180. if (!m_stack_of_open_elements.has_in_table_scope("tr")) {
  1181. return;
  1182. }
  1183. clear_the_stack_back_to_a_table_row_context();
  1184. m_stack_of_open_elements.pop();
  1185. m_insertion_mode = InsertionMode::InTableBody;
  1186. process_using_the_rules_for(m_insertion_mode, token);
  1187. return;
  1188. }
  1189. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html", "td", "th")) {
  1190. PARSE_ERROR();
  1191. return;
  1192. }
  1193. process_using_the_rules_for(InsertionMode::InTable, token);
  1194. }
  1195. void HTMLDocumentParser::close_the_cell()
  1196. {
  1197. generate_implied_end_tags();
  1198. if (!current_node().tag_name().is_one_of("td", "th")) {
  1199. PARSE_ERROR();
  1200. }
  1201. while (!current_node().tag_name().is_one_of("td", "th"))
  1202. m_stack_of_open_elements.pop();
  1203. m_stack_of_open_elements.pop();
  1204. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1205. m_insertion_mode = InsertionMode::InRow;
  1206. }
  1207. void HTMLDocumentParser::handle_in_cell(HTMLToken& token)
  1208. {
  1209. if (token.is_end_tag() && token.tag_name().is_one_of("td", "th")) {
  1210. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1211. PARSE_ERROR();
  1212. return;
  1213. }
  1214. generate_implied_end_tags();
  1215. if (current_node().tag_name() != token.tag_name()) {
  1216. PARSE_ERROR();
  1217. }
  1218. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1219. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1220. m_insertion_mode = InsertionMode::InRow;
  1221. return;
  1222. }
  1223. if (token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr")) {
  1224. if (!m_stack_of_open_elements.has_in_table_scope("td") && m_stack_of_open_elements.has_in_table_scope("th")) {
  1225. PARSE_ERROR();
  1226. return;
  1227. }
  1228. close_the_cell();
  1229. process_using_the_rules_for(m_insertion_mode, token);
  1230. return;
  1231. }
  1232. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html")) {
  1233. PARSE_ERROR();
  1234. return;
  1235. }
  1236. if (token.is_end_tag() && token.tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  1237. if (m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1238. PARSE_ERROR();
  1239. return;
  1240. }
  1241. close_the_cell();
  1242. // Reprocess the token.
  1243. process_using_the_rules_for(m_insertion_mode, token);
  1244. return;
  1245. }
  1246. process_using_the_rules_for(InsertionMode::InBody, token);
  1247. }
  1248. void HTMLDocumentParser::handle_in_table_text(HTMLToken& token)
  1249. {
  1250. if (token.is_character()) {
  1251. if (token.codepoint() == 0) {
  1252. PARSE_ERROR();
  1253. return;
  1254. }
  1255. m_pending_table_character_tokens.append(token);
  1256. return;
  1257. }
  1258. for (auto& pending_token : m_pending_table_character_tokens) {
  1259. ASSERT(pending_token.is_character());
  1260. if (!pending_token.is_parser_whitespace()) {
  1261. // FIXME: If any of the tokens in the pending table character tokens list
  1262. // are character tokens that are not ASCII whitespace, then this is a parse error:
  1263. // reprocess the character tokens in the pending table character tokens list using
  1264. // the rules given in the "anything else" entry in the "in table" insertion mode.
  1265. TODO();
  1266. }
  1267. }
  1268. for (auto& pending_token : m_pending_table_character_tokens) {
  1269. insert_character(pending_token.codepoint());
  1270. }
  1271. m_insertion_mode = m_original_insertion_mode;
  1272. process_using_the_rules_for(m_insertion_mode, token);
  1273. }
  1274. void HTMLDocumentParser::handle_in_table_body(HTMLToken& token)
  1275. {
  1276. if (token.is_start_tag() && token.tag_name() == "tr") {
  1277. clear_the_stack_back_to_a_table_body_context();
  1278. insert_html_element(token);
  1279. m_insertion_mode = InsertionMode::InRow;
  1280. return;
  1281. }
  1282. if (token.is_start_tag() && token.tag_name().is_one_of("th", "td")) {
  1283. TODO();
  1284. }
  1285. if (token.is_end_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  1286. TODO();
  1287. }
  1288. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "tfoot", "thead"))
  1289. || (token.is_end_tag() && token.tag_name() == "table")) {
  1290. // 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.
  1291. clear_the_stack_back_to_a_table_body_context();
  1292. m_stack_of_open_elements.pop();
  1293. m_insertion_mode = InsertionMode::InTable;
  1294. process_using_the_rules_for(InsertionMode::InTable, token);
  1295. return;
  1296. }
  1297. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html", "td", "th", "tr")) {
  1298. PARSE_ERROR();
  1299. return;
  1300. }
  1301. process_using_the_rules_for(InsertionMode::InTable, token);
  1302. }
  1303. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  1304. {
  1305. if (token.is_character() && current_node().tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  1306. m_pending_table_character_tokens.clear();
  1307. m_original_insertion_mode = m_insertion_mode;
  1308. m_insertion_mode = InsertionMode::InTableText;
  1309. process_using_the_rules_for(InsertionMode::InTableText, token);
  1310. return;
  1311. }
  1312. if (token.is_comment()) {
  1313. insert_comment(token);
  1314. return;
  1315. }
  1316. if (token.is_doctype()) {
  1317. PARSE_ERROR();
  1318. return;
  1319. }
  1320. if (token.is_start_tag() && token.tag_name() == "caption") {
  1321. TODO();
  1322. }
  1323. if (token.is_start_tag() && token.tag_name() == "colgroup") {
  1324. TODO();
  1325. }
  1326. if (token.is_start_tag() && token.tag_name() == "col") {
  1327. TODO();
  1328. }
  1329. if (token.is_start_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  1330. clear_the_stack_back_to_a_table_context();
  1331. insert_html_element(token);
  1332. m_insertion_mode = InsertionMode::InTableBody;
  1333. return;
  1334. }
  1335. if (token.is_start_tag() && token.tag_name().is_one_of("td", "th", "tr")) {
  1336. clear_the_stack_back_to_a_table_context();
  1337. HTMLToken fake_tbody_token;
  1338. fake_tbody_token.m_type = HTMLToken::Type::StartTag;
  1339. fake_tbody_token.m_tag.tag_name.append("tbody");
  1340. insert_html_element(fake_tbody_token);
  1341. m_insertion_mode = InsertionMode::InTableBody;
  1342. process_using_the_rules_for(InsertionMode::InTableBody, token);
  1343. return;
  1344. }
  1345. if (token.is_start_tag() && token.tag_name() == "table") {
  1346. PARSE_ERROR();
  1347. TODO();
  1348. }
  1349. if (token.is_end_tag()) {
  1350. if (!m_stack_of_open_elements.has_in_table_scope("table")) {
  1351. PARSE_ERROR();
  1352. return;
  1353. }
  1354. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("table");
  1355. reset_the_insertion_mode_appropriately();
  1356. return;
  1357. }
  1358. TODO();
  1359. }
  1360. void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
  1361. {
  1362. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1363. RefPtr<Element> node = m_stack_of_open_elements.elements().at(i);
  1364. if (node->tag_name() == "select") {
  1365. TODO();
  1366. }
  1367. if (node->tag_name().is_one_of("td", "th")) {
  1368. m_insertion_mode = InsertionMode::InCell;
  1369. return;
  1370. }
  1371. if (node->tag_name() == "tr") {
  1372. m_insertion_mode = InsertionMode::InRow;
  1373. return;
  1374. }
  1375. if (node->tag_name().is_one_of("tbody", "thead", "tfoot")) {
  1376. m_insertion_mode = InsertionMode::InTableBody;
  1377. return;
  1378. }
  1379. if (node->tag_name() == "caption") {
  1380. m_insertion_mode = InsertionMode::InCaption;
  1381. return;
  1382. }
  1383. if (node->tag_name() == "colgroup") {
  1384. m_insertion_mode = InsertionMode::InColumnGroup;
  1385. return;
  1386. }
  1387. if (node->tag_name() == "table") {
  1388. m_insertion_mode = InsertionMode::InTable;
  1389. return;
  1390. }
  1391. if (node->tag_name() == "template") {
  1392. TODO();
  1393. }
  1394. if (node->tag_name() == "body") {
  1395. m_insertion_mode = InsertionMode::InBody;
  1396. return;
  1397. }
  1398. if (node->tag_name() == "frameset") {
  1399. m_insertion_mode = InsertionMode::InFrameset;
  1400. if (m_parsing_fragment) {
  1401. TODO();
  1402. }
  1403. return;
  1404. }
  1405. if (node->tag_name() == "html") {
  1406. TODO();
  1407. }
  1408. }
  1409. m_insertion_mode = InsertionMode::InBody;
  1410. if (m_parsing_fragment) {
  1411. TODO();
  1412. }
  1413. }
  1414. const char* HTMLDocumentParser::insertion_mode_name() const
  1415. {
  1416. switch (m_insertion_mode) {
  1417. #define __ENUMERATE_INSERTION_MODE(mode) \
  1418. case InsertionMode::mode: \
  1419. return #mode;
  1420. ENUMERATE_INSERTION_MODES
  1421. #undef __ENUMERATE_INSERTION_MODE
  1422. }
  1423. ASSERT_NOT_REACHED();
  1424. }
  1425. Document& HTMLDocumentParser::document()
  1426. {
  1427. return *m_document;
  1428. }
  1429. }