HTMLDocumentParser.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  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. insert_html_element(token);
  993. // If the next token is a U+000A LINE FEED (LF) character token,
  994. // then ignore that token and move on to the next one.
  995. // (Newlines at the start of pre blocks are ignored as an authoring convenience.)
  996. auto next_token = m_tokenizer.next_token();
  997. m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
  998. m_original_insertion_mode = m_insertion_mode;
  999. m_frameset_ok = false;
  1000. m_insertion_mode = InsertionMode::Text;
  1001. if (next_token.has_value() && next_token.value().is_character() && next_token.value().codepoint() == '\n') {
  1002. // Ignore it.
  1003. } else {
  1004. process_using_the_rules_for(m_insertion_mode, next_token.value());
  1005. }
  1006. return;
  1007. }
  1008. if (token.is_start_tag() && token.tag_name() == "xmp") {
  1009. TODO();
  1010. }
  1011. if (token.is_start_tag() && token.tag_name() == "iframe") {
  1012. m_frameset_ok = false;
  1013. parse_generic_raw_text_element(token);
  1014. return;
  1015. }
  1016. if (token.is_start_tag() && ((token.tag_name() == "noembed") || (token.tag_name() == "noscript" && m_scripting_enabled))) {
  1017. parse_generic_raw_text_element(token);
  1018. return;
  1019. }
  1020. if (token.is_start_tag() && token.tag_name() == "select") {
  1021. TODO();
  1022. }
  1023. if (token.is_start_tag() && token.tag_name().is_one_of("optgroup", "option")) {
  1024. if (current_node().tag_name() == "option")
  1025. m_stack_of_open_elements.pop();
  1026. reconstruct_the_active_formatting_elements();
  1027. insert_html_element(token);
  1028. return;
  1029. }
  1030. if (token.is_start_tag() && token.tag_name().is_one_of("rb", "rtc")) {
  1031. TODO();
  1032. }
  1033. if (token.is_start_tag() && token.tag_name().is_one_of("rp", "rt")) {
  1034. TODO();
  1035. }
  1036. if (token.is_start_tag() && token.tag_name() == "math") {
  1037. TODO();
  1038. }
  1039. if (token.is_start_tag() && token.tag_name() == "svg") {
  1040. TODO();
  1041. }
  1042. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr"))) {
  1043. PARSE_ERROR();
  1044. return;
  1045. }
  1046. // Any other start tag
  1047. if (token.is_start_tag()) {
  1048. reconstruct_the_active_formatting_elements();
  1049. insert_html_element(token);
  1050. return;
  1051. }
  1052. if (token.is_end_tag()) {
  1053. AnyOtherEndTag:
  1054. RefPtr<Element> node;
  1055. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1056. node = m_stack_of_open_elements.elements()[i];
  1057. if (node->tag_name() == token.tag_name()) {
  1058. generate_implied_end_tags(token.tag_name());
  1059. if (node != current_node()) {
  1060. PARSE_ERROR();
  1061. }
  1062. while (&current_node() != node) {
  1063. m_stack_of_open_elements.pop();
  1064. }
  1065. m_stack_of_open_elements.pop();
  1066. break;
  1067. }
  1068. if (is_special_tag(node->tag_name())) {
  1069. PARSE_ERROR();
  1070. return;
  1071. }
  1072. }
  1073. return;
  1074. }
  1075. TODO();
  1076. }
  1077. void HTMLDocumentParser::increment_script_nesting_level()
  1078. {
  1079. ++m_script_nesting_level;
  1080. }
  1081. void HTMLDocumentParser::decrement_script_nesting_level()
  1082. {
  1083. ASSERT(m_script_nesting_level);
  1084. --m_script_nesting_level;
  1085. }
  1086. void HTMLDocumentParser::handle_text(HTMLToken& token)
  1087. {
  1088. if (token.is_character()) {
  1089. insert_character(token.codepoint());
  1090. return;
  1091. }
  1092. if (token.is_end_tag() && token.tag_name() == "script") {
  1093. NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
  1094. m_stack_of_open_elements.pop();
  1095. m_insertion_mode = m_original_insertion_mode;
  1096. // FIXME: Handle tokenizer insertion point stuff here.
  1097. increment_script_nesting_level();
  1098. script->prepare_script({});
  1099. decrement_script_nesting_level();
  1100. if (script_nesting_level() == 0)
  1101. m_parser_pause_flag = false;
  1102. // FIXME: Handle tokenizer insertion point stuff here too.
  1103. while (document().pending_parsing_blocking_script()) {
  1104. if (script_nesting_level() != 0) {
  1105. m_parser_pause_flag = true;
  1106. // FIXME: Abort the processing of any nested invocations of the tokenizer,
  1107. // yielding control back to the caller. (Tokenization will resume when
  1108. // the caller returns to the "outer" tree construction stage.)
  1109. TODO();
  1110. } else {
  1111. auto the_script = document().take_pending_parsing_blocking_script({});
  1112. m_tokenizer.set_blocked(true);
  1113. // FIXME: If the parser's Document has a style sheet that is blocking scripts
  1114. // or the script's "ready to be parser-executed" flag is not set:
  1115. // spin the event loop until the parser's Document has no style sheet
  1116. // that is blocking scripts and the script's "ready to be parser-executed"
  1117. // flag is set.
  1118. ASSERT(the_script->is_ready_to_be_parser_executed());
  1119. if (m_aborted)
  1120. return;
  1121. m_tokenizer.set_blocked(false);
  1122. // FIXME: Handle tokenizer insertion point stuff here too.
  1123. ASSERT(script_nesting_level() == 0);
  1124. increment_script_nesting_level();
  1125. the_script->execute_script();
  1126. decrement_script_nesting_level();
  1127. ASSERT(script_nesting_level() == 0);
  1128. m_parser_pause_flag = false;
  1129. // FIXME: Handle tokenizer insertion point stuff here too.
  1130. }
  1131. }
  1132. return;
  1133. }
  1134. // FIXME: This is a bit hackish, we can simplify this once we don't need to support
  1135. // the old parser anymore, since then we don't need to maintain its children_changed() semantics.
  1136. if (token.is_end_tag() && token.tag_name() == "style") {
  1137. current_node().children_changed();
  1138. // NOTE: We don't return here, keep going.
  1139. }
  1140. if (token.is_end_tag()) {
  1141. m_stack_of_open_elements.pop();
  1142. m_insertion_mode = m_original_insertion_mode;
  1143. return;
  1144. }
  1145. TODO();
  1146. }
  1147. void HTMLDocumentParser::clear_the_stack_back_to_a_table_context()
  1148. {
  1149. while (!current_node().tag_name().is_one_of("table", "template", "html"))
  1150. m_stack_of_open_elements.pop();
  1151. }
  1152. void HTMLDocumentParser::clear_the_stack_back_to_a_table_row_context()
  1153. {
  1154. while (!current_node().tag_name().is_one_of("tr", "template", "html"))
  1155. m_stack_of_open_elements.pop();
  1156. }
  1157. void HTMLDocumentParser::clear_the_stack_back_to_a_table_body_context()
  1158. {
  1159. while (!current_node().tag_name().is_one_of("tbody", "tfoot", "thead", "template", "html"))
  1160. m_stack_of_open_elements.pop();
  1161. }
  1162. void HTMLDocumentParser::handle_in_row(HTMLToken& token)
  1163. {
  1164. if (token.is_start_tag() && token.tag_name().is_one_of("th", "td")) {
  1165. clear_the_stack_back_to_a_table_row_context();
  1166. insert_html_element(token);
  1167. m_insertion_mode = InsertionMode::InCell;
  1168. m_list_of_active_formatting_elements.add_marker();
  1169. return;
  1170. }
  1171. if (token.is_end_tag() && token.tag_name() == "tr") {
  1172. if (!m_stack_of_open_elements.has_in_table_scope("tr")) {
  1173. PARSE_ERROR();
  1174. return;
  1175. }
  1176. clear_the_stack_back_to_a_table_row_context();
  1177. m_stack_of_open_elements.pop();
  1178. m_insertion_mode = InsertionMode::InTableBody;
  1179. return;
  1180. }
  1181. if (token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr")) {
  1182. if (m_stack_of_open_elements.has_in_table_scope("tr")) {
  1183. PARSE_ERROR();
  1184. return;
  1185. }
  1186. clear_the_stack_back_to_a_table_row_context();
  1187. m_stack_of_open_elements.pop();
  1188. m_insertion_mode = InsertionMode::InTableBody;
  1189. process_using_the_rules_for(m_insertion_mode, token);
  1190. return;
  1191. }
  1192. if (token.is_end_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  1193. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1194. PARSE_ERROR();
  1195. return;
  1196. }
  1197. if (!m_stack_of_open_elements.has_in_table_scope("tr")) {
  1198. return;
  1199. }
  1200. clear_the_stack_back_to_a_table_row_context();
  1201. m_stack_of_open_elements.pop();
  1202. m_insertion_mode = InsertionMode::InTableBody;
  1203. process_using_the_rules_for(m_insertion_mode, token);
  1204. return;
  1205. }
  1206. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html", "td", "th")) {
  1207. PARSE_ERROR();
  1208. return;
  1209. }
  1210. process_using_the_rules_for(InsertionMode::InTable, token);
  1211. }
  1212. void HTMLDocumentParser::close_the_cell()
  1213. {
  1214. generate_implied_end_tags();
  1215. if (!current_node().tag_name().is_one_of("td", "th")) {
  1216. PARSE_ERROR();
  1217. }
  1218. while (!current_node().tag_name().is_one_of("td", "th"))
  1219. m_stack_of_open_elements.pop();
  1220. m_stack_of_open_elements.pop();
  1221. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1222. m_insertion_mode = InsertionMode::InRow;
  1223. }
  1224. void HTMLDocumentParser::handle_in_cell(HTMLToken& token)
  1225. {
  1226. if (token.is_end_tag() && token.tag_name().is_one_of("td", "th")) {
  1227. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1228. PARSE_ERROR();
  1229. return;
  1230. }
  1231. generate_implied_end_tags();
  1232. if (current_node().tag_name() != token.tag_name()) {
  1233. PARSE_ERROR();
  1234. }
  1235. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1236. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1237. m_insertion_mode = InsertionMode::InRow;
  1238. return;
  1239. }
  1240. if (token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr")) {
  1241. if (!m_stack_of_open_elements.has_in_table_scope("td") && m_stack_of_open_elements.has_in_table_scope("th")) {
  1242. PARSE_ERROR();
  1243. return;
  1244. }
  1245. close_the_cell();
  1246. process_using_the_rules_for(m_insertion_mode, token);
  1247. return;
  1248. }
  1249. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html")) {
  1250. PARSE_ERROR();
  1251. return;
  1252. }
  1253. if (token.is_end_tag() && token.tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  1254. if (m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1255. PARSE_ERROR();
  1256. return;
  1257. }
  1258. close_the_cell();
  1259. // Reprocess the token.
  1260. process_using_the_rules_for(m_insertion_mode, token);
  1261. return;
  1262. }
  1263. process_using_the_rules_for(InsertionMode::InBody, token);
  1264. }
  1265. void HTMLDocumentParser::handle_in_table_text(HTMLToken& token)
  1266. {
  1267. if (token.is_character()) {
  1268. if (token.codepoint() == 0) {
  1269. PARSE_ERROR();
  1270. return;
  1271. }
  1272. m_pending_table_character_tokens.append(token);
  1273. return;
  1274. }
  1275. for (auto& pending_token : m_pending_table_character_tokens) {
  1276. ASSERT(pending_token.is_character());
  1277. if (!pending_token.is_parser_whitespace()) {
  1278. // FIXME: If any of the tokens in the pending table character tokens list
  1279. // are character tokens that are not ASCII whitespace, then this is a parse error:
  1280. // reprocess the character tokens in the pending table character tokens list using
  1281. // the rules given in the "anything else" entry in the "in table" insertion mode.
  1282. TODO();
  1283. }
  1284. }
  1285. for (auto& pending_token : m_pending_table_character_tokens) {
  1286. insert_character(pending_token.codepoint());
  1287. }
  1288. m_insertion_mode = m_original_insertion_mode;
  1289. process_using_the_rules_for(m_insertion_mode, token);
  1290. }
  1291. void HTMLDocumentParser::handle_in_table_body(HTMLToken& token)
  1292. {
  1293. if (token.is_start_tag() && token.tag_name() == "tr") {
  1294. clear_the_stack_back_to_a_table_body_context();
  1295. insert_html_element(token);
  1296. m_insertion_mode = InsertionMode::InRow;
  1297. return;
  1298. }
  1299. if (token.is_start_tag() && token.tag_name().is_one_of("th", "td")) {
  1300. TODO();
  1301. }
  1302. if (token.is_end_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  1303. TODO();
  1304. }
  1305. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "tfoot", "thead"))
  1306. || (token.is_end_tag() && token.tag_name() == "table")) {
  1307. // 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.
  1308. clear_the_stack_back_to_a_table_body_context();
  1309. m_stack_of_open_elements.pop();
  1310. m_insertion_mode = InsertionMode::InTable;
  1311. process_using_the_rules_for(InsertionMode::InTable, token);
  1312. return;
  1313. }
  1314. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html", "td", "th", "tr")) {
  1315. PARSE_ERROR();
  1316. return;
  1317. }
  1318. process_using_the_rules_for(InsertionMode::InTable, token);
  1319. }
  1320. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  1321. {
  1322. if (token.is_character() && current_node().tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  1323. m_pending_table_character_tokens.clear();
  1324. m_original_insertion_mode = m_insertion_mode;
  1325. m_insertion_mode = InsertionMode::InTableText;
  1326. process_using_the_rules_for(InsertionMode::InTableText, token);
  1327. return;
  1328. }
  1329. if (token.is_comment()) {
  1330. insert_comment(token);
  1331. return;
  1332. }
  1333. if (token.is_doctype()) {
  1334. PARSE_ERROR();
  1335. return;
  1336. }
  1337. if (token.is_start_tag() && token.tag_name() == "caption") {
  1338. TODO();
  1339. }
  1340. if (token.is_start_tag() && token.tag_name() == "colgroup") {
  1341. TODO();
  1342. }
  1343. if (token.is_start_tag() && token.tag_name() == "col") {
  1344. TODO();
  1345. }
  1346. if (token.is_start_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  1347. clear_the_stack_back_to_a_table_context();
  1348. insert_html_element(token);
  1349. m_insertion_mode = InsertionMode::InTableBody;
  1350. return;
  1351. }
  1352. if (token.is_start_tag() && token.tag_name().is_one_of("td", "th", "tr")) {
  1353. clear_the_stack_back_to_a_table_context();
  1354. HTMLToken fake_tbody_token;
  1355. fake_tbody_token.m_type = HTMLToken::Type::StartTag;
  1356. fake_tbody_token.m_tag.tag_name.append("tbody");
  1357. insert_html_element(fake_tbody_token);
  1358. m_insertion_mode = InsertionMode::InTableBody;
  1359. process_using_the_rules_for(InsertionMode::InTableBody, token);
  1360. return;
  1361. }
  1362. if (token.is_start_tag() && token.tag_name() == "table") {
  1363. PARSE_ERROR();
  1364. TODO();
  1365. }
  1366. if (token.is_end_tag()) {
  1367. if (!m_stack_of_open_elements.has_in_table_scope("table")) {
  1368. PARSE_ERROR();
  1369. return;
  1370. }
  1371. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("table");
  1372. reset_the_insertion_mode_appropriately();
  1373. return;
  1374. }
  1375. TODO();
  1376. }
  1377. void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
  1378. {
  1379. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1380. RefPtr<Element> node = m_stack_of_open_elements.elements().at(i);
  1381. if (node->tag_name() == "select") {
  1382. TODO();
  1383. }
  1384. if (node->tag_name().is_one_of("td", "th")) {
  1385. m_insertion_mode = InsertionMode::InCell;
  1386. return;
  1387. }
  1388. if (node->tag_name() == "tr") {
  1389. m_insertion_mode = InsertionMode::InRow;
  1390. return;
  1391. }
  1392. if (node->tag_name().is_one_of("tbody", "thead", "tfoot")) {
  1393. m_insertion_mode = InsertionMode::InTableBody;
  1394. return;
  1395. }
  1396. if (node->tag_name() == "caption") {
  1397. m_insertion_mode = InsertionMode::InCaption;
  1398. return;
  1399. }
  1400. if (node->tag_name() == "colgroup") {
  1401. m_insertion_mode = InsertionMode::InColumnGroup;
  1402. return;
  1403. }
  1404. if (node->tag_name() == "table") {
  1405. m_insertion_mode = InsertionMode::InTable;
  1406. return;
  1407. }
  1408. if (node->tag_name() == "template") {
  1409. TODO();
  1410. }
  1411. if (node->tag_name() == "body") {
  1412. m_insertion_mode = InsertionMode::InBody;
  1413. return;
  1414. }
  1415. if (node->tag_name() == "frameset") {
  1416. m_insertion_mode = InsertionMode::InFrameset;
  1417. if (m_parsing_fragment) {
  1418. TODO();
  1419. }
  1420. return;
  1421. }
  1422. if (node->tag_name() == "html") {
  1423. TODO();
  1424. }
  1425. }
  1426. m_insertion_mode = InsertionMode::InBody;
  1427. if (m_parsing_fragment) {
  1428. TODO();
  1429. }
  1430. }
  1431. const char* HTMLDocumentParser::insertion_mode_name() const
  1432. {
  1433. switch (m_insertion_mode) {
  1434. #define __ENUMERATE_INSERTION_MODE(mode) \
  1435. case InsertionMode::mode: \
  1436. return #mode;
  1437. ENUMERATE_INSERTION_MODES
  1438. #undef __ENUMERATE_INSERTION_MODE
  1439. }
  1440. ASSERT_NOT_REACHED();
  1441. }
  1442. Document& HTMLDocumentParser::document()
  1443. {
  1444. return *m_document;
  1445. }
  1446. }