HTMLDocumentParser.cpp 59 KB

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