HTMLDocumentParser.cpp 63 KB

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