HTMLDocumentParser.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  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_end_tag() && token.tag_name() == "html") {
  781. if (!m_stack_of_open_elements.has_in_scope("body")) {
  782. PARSE_ERROR();
  783. return;
  784. }
  785. for (auto& node : m_stack_of_open_elements.elements()) {
  786. 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")) {
  787. PARSE_ERROR();
  788. break;
  789. }
  790. }
  791. m_insertion_mode = InsertionMode::AfterBody;
  792. process_using_the_rules_for(m_insertion_mode, token);
  793. return;
  794. }
  795. 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")) {
  796. if (m_stack_of_open_elements.has_in_button_scope("p"))
  797. close_a_p_element();
  798. insert_html_element(token);
  799. return;
  800. }
  801. if (token.is_start_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  802. if (m_stack_of_open_elements.has_in_button_scope("p"))
  803. close_a_p_element();
  804. if (current_node().tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  805. PARSE_ERROR();
  806. m_stack_of_open_elements.pop();
  807. }
  808. insert_html_element(token);
  809. return;
  810. }
  811. if (token.is_start_tag() && token.tag_name().is_one_of("pre", "listing")) {
  812. if (m_stack_of_open_elements.has_in_button_scope("p"))
  813. close_a_p_element();
  814. insert_html_element(token);
  815. m_frameset_ok = false;
  816. // If the next token is a U+000A LINE FEED (LF) character token,
  817. // then ignore that token and move on to the next one.
  818. // (Newlines at the start of pre blocks are ignored as an authoring convenience.)
  819. auto next_token = m_tokenizer.next_token();
  820. if (next_token.has_value() && next_token.value().is_character() && next_token.value().codepoint() == '\n') {
  821. // Ignore it.
  822. } else {
  823. process_using_the_rules_for(m_insertion_mode, next_token.value());
  824. }
  825. return;
  826. }
  827. if (token.is_start_tag() && token.tag_name() == "form") {
  828. if (m_form_element && m_stack_of_open_elements.contains("template")) {
  829. PARSE_ERROR();
  830. return;
  831. }
  832. if (m_stack_of_open_elements.has_in_button_scope("p"))
  833. close_a_p_element();
  834. auto element = insert_html_element(token);
  835. if (!m_stack_of_open_elements.contains("template"))
  836. m_form_element = to<HTMLFormElement>(*element);
  837. return;
  838. }
  839. if (token.is_start_tag() && token.tag_name() == "li") {
  840. m_frameset_ok = false;
  841. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  842. RefPtr<Element> node = m_stack_of_open_elements.elements()[i];
  843. if (node->tag_name() == "li") {
  844. generate_implied_end_tags("li");
  845. if (current_node().tag_name() != "li") {
  846. PARSE_ERROR();
  847. }
  848. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("li");
  849. break;
  850. }
  851. if (is_special_tag(node->tag_name()) && !node->tag_name().is_one_of("address", "div", "p"))
  852. break;
  853. }
  854. if (m_stack_of_open_elements.has_in_button_scope("p"))
  855. close_a_p_element();
  856. insert_html_element(token);
  857. return;
  858. }
  859. if (token.is_start_tag() && token.tag_name().is_one_of("dd", "dt")) {
  860. m_frameset_ok = false;
  861. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  862. RefPtr<Element> node = m_stack_of_open_elements.elements()[i];
  863. if (node->tag_name() == "dd") {
  864. generate_implied_end_tags("dd");
  865. if (current_node().tag_name() != "dd") {
  866. PARSE_ERROR();
  867. }
  868. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("dd");
  869. break;
  870. }
  871. if (node->tag_name() == "dt") {
  872. generate_implied_end_tags("dt");
  873. if (current_node().tag_name() != "dt") {
  874. PARSE_ERROR();
  875. }
  876. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("dt");
  877. break;
  878. }
  879. if (is_special_tag(node->tag_name()) && !node->tag_name().is_one_of("address", "div", "p"))
  880. break;
  881. }
  882. if (m_stack_of_open_elements.has_in_button_scope("p"))
  883. close_a_p_element();
  884. insert_html_element(token);
  885. return;
  886. }
  887. if (token.is_start_tag() && token.tag_name() == "plaintext") {
  888. if (m_stack_of_open_elements.has_in_button_scope("p"))
  889. close_a_p_element();
  890. insert_html_element(token);
  891. m_tokenizer.switch_to({}, HTMLTokenizer::State::PLAINTEXT);
  892. return;
  893. }
  894. if (token.is_start_tag() && token.tag_name() == "button") {
  895. if (m_stack_of_open_elements.has_in_button_scope("button")) {
  896. PARSE_ERROR();
  897. generate_implied_end_tags();
  898. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("button");
  899. }
  900. reconstruct_the_active_formatting_elements();
  901. insert_html_element(token);
  902. m_frameset_ok = false;
  903. return;
  904. }
  905. 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")) {
  906. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  907. PARSE_ERROR();
  908. return;
  909. }
  910. generate_implied_end_tags();
  911. if (current_node().tag_name() != token.tag_name()) {
  912. PARSE_ERROR();
  913. }
  914. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  915. return;
  916. }
  917. if (token.is_end_tag() && token.tag_name() == "form") {
  918. if (!m_stack_of_open_elements.contains("template")) {
  919. auto node = m_form_element;
  920. m_form_element = nullptr;
  921. if (!node || m_stack_of_open_elements.has_in_scope(*node)) {
  922. PARSE_ERROR();
  923. return;
  924. }
  925. generate_implied_end_tags();
  926. if (&current_node() != node) {
  927. PARSE_ERROR();
  928. }
  929. m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) { return entry.ptr() == node.ptr(); });
  930. } else {
  931. if (!m_stack_of_open_elements.has_in_scope("form")) {
  932. PARSE_ERROR();
  933. return;
  934. }
  935. generate_implied_end_tags();
  936. if (current_node().tag_name() != "form") {
  937. PARSE_ERROR();
  938. }
  939. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("form");
  940. }
  941. return;
  942. }
  943. if (token.is_end_tag() && token.tag_name() == "p") {
  944. if (!m_stack_of_open_elements.has_in_button_scope("p")) {
  945. PARSE_ERROR();
  946. HTMLToken fake_p_token;
  947. fake_p_token.m_type = HTMLToken::Type::StartTag;
  948. fake_p_token.m_tag.tag_name.append("p");
  949. insert_html_element(fake_p_token);
  950. }
  951. close_a_p_element();
  952. return;
  953. }
  954. if (token.is_end_tag() && token.tag_name() == "li") {
  955. if (!m_stack_of_open_elements.has_in_list_item_scope("li")) {
  956. PARSE_ERROR();
  957. return;
  958. }
  959. generate_implied_end_tags("li");
  960. if (current_node().tag_name() != "li") {
  961. PARSE_ERROR();
  962. dbg() << "Expected <li> current node, but had <" << current_node().tag_name() << ">";
  963. }
  964. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("li");
  965. return;
  966. }
  967. if (token.is_end_tag() && token.tag_name().is_one_of("dd", "dt")) {
  968. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  969. PARSE_ERROR();
  970. return;
  971. }
  972. generate_implied_end_tags(token.tag_name());
  973. if (current_node().tag_name() != token.tag_name()) {
  974. PARSE_ERROR();
  975. }
  976. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  977. return;
  978. }
  979. if (token.is_end_tag() && token.tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6")) {
  980. if (!m_stack_of_open_elements.has_in_scope("h1")
  981. && !m_stack_of_open_elements.has_in_scope("h2")
  982. && !m_stack_of_open_elements.has_in_scope("h3")
  983. && !m_stack_of_open_elements.has_in_scope("h4")
  984. && !m_stack_of_open_elements.has_in_scope("h5")
  985. && !m_stack_of_open_elements.has_in_scope("h6")) {
  986. PARSE_ERROR();
  987. return;
  988. }
  989. generate_implied_end_tags();
  990. if (current_node().tag_name() != token.tag_name()) {
  991. PARSE_ERROR();
  992. }
  993. for (;;) {
  994. auto popped_element = m_stack_of_open_elements.pop();
  995. if (popped_element->tag_name().is_one_of("h1", "h2", "h3", "h4", "h5", "h6"))
  996. break;
  997. }
  998. return;
  999. }
  1000. if (token.is_start_tag() && token.tag_name() == "a") {
  1001. if (auto* element = m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker("a")) {
  1002. PARSE_ERROR();
  1003. if (run_the_adoption_agency_algorithm(token) == AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps)
  1004. goto AnyOtherEndTag;
  1005. m_list_of_active_formatting_elements.remove(*element);
  1006. m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) {
  1007. return entry.ptr() == element;
  1008. });
  1009. }
  1010. reconstruct_the_active_formatting_elements();
  1011. auto element = insert_html_element(token);
  1012. m_list_of_active_formatting_elements.add(*element);
  1013. return;
  1014. }
  1015. if (token.is_start_tag() && token.tag_name().is_one_of("b", "big", "code", "em", "font", "i", "s", "small", "strike", "strong", "tt", "u")) {
  1016. reconstruct_the_active_formatting_elements();
  1017. auto element = insert_html_element(token);
  1018. m_list_of_active_formatting_elements.add(*element);
  1019. return;
  1020. }
  1021. if (token.is_start_tag() && token.tag_name() == "nobr") {
  1022. reconstruct_the_active_formatting_elements();
  1023. if (m_stack_of_open_elements.has_in_scope("nobr")) {
  1024. PARSE_ERROR();
  1025. run_the_adoption_agency_algorithm(token);
  1026. reconstruct_the_active_formatting_elements();
  1027. }
  1028. auto element = insert_html_element(token);
  1029. m_list_of_active_formatting_elements.add(*element);
  1030. return;
  1031. }
  1032. 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")) {
  1033. if (run_the_adoption_agency_algorithm(token) == AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps)
  1034. goto AnyOtherEndTag;
  1035. return;
  1036. }
  1037. if (token.is_start_tag() && token.tag_name().is_one_of("applet", "marquee", "object")) {
  1038. reconstruct_the_active_formatting_elements();
  1039. insert_html_element(token);
  1040. m_list_of_active_formatting_elements.add_marker();
  1041. m_frameset_ok = false;
  1042. return;
  1043. }
  1044. if (token.is_end_tag() && token.tag_name().is_one_of("applet", "marquee", "object")) {
  1045. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  1046. PARSE_ERROR();
  1047. return;
  1048. }
  1049. generate_implied_end_tags();
  1050. if (current_node().tag_name() != token.tag_name()) {
  1051. PARSE_ERROR();
  1052. }
  1053. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1054. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1055. return;
  1056. }
  1057. if (token.is_start_tag() && token.tag_name() == "table") {
  1058. if (!document().in_quirks_mode()) {
  1059. if (m_stack_of_open_elements.has_in_button_scope("p"))
  1060. close_a_p_element();
  1061. }
  1062. insert_html_element(token);
  1063. m_frameset_ok = false;
  1064. m_insertion_mode = InsertionMode::InTable;
  1065. return;
  1066. }
  1067. if (token.is_end_tag() && token.tag_name() == "br") {
  1068. token.drop_attributes();
  1069. goto BRStartTag;
  1070. }
  1071. if (token.is_start_tag() && token.tag_name().is_one_of("area", "br", "embed", "img", "keygen", "wbr")) {
  1072. BRStartTag:
  1073. reconstruct_the_active_formatting_elements();
  1074. insert_html_element(token);
  1075. m_stack_of_open_elements.pop();
  1076. token.acknowledge_self_closing_flag_if_set();
  1077. m_frameset_ok = false;
  1078. return;
  1079. }
  1080. if (token.is_start_tag() && token.tag_name() == "input") {
  1081. reconstruct_the_active_formatting_elements();
  1082. insert_html_element(token);
  1083. m_stack_of_open_elements.pop();
  1084. token.acknowledge_self_closing_flag_if_set();
  1085. auto type_attribute = token.attribute(HTML::AttributeNames::type);
  1086. if (type_attribute.is_null() || type_attribute != "hidden") {
  1087. m_frameset_ok = false;
  1088. }
  1089. return;
  1090. }
  1091. if (token.is_start_tag() && token.tag_name().is_one_of("param", "source", "track")) {
  1092. TODO();
  1093. }
  1094. if (token.is_start_tag() && token.tag_name() == "hr") {
  1095. if (m_stack_of_open_elements.has_in_button_scope("p"))
  1096. close_a_p_element();
  1097. insert_html_element(token);
  1098. m_stack_of_open_elements.pop();
  1099. token.acknowledge_self_closing_flag_if_set();
  1100. m_frameset_ok = false;
  1101. return;
  1102. }
  1103. if (token.is_start_tag() && token.tag_name() == "image") {
  1104. // Parse error. Change the token's tag name to "img" and reprocess it. (Don't ask.)
  1105. PARSE_ERROR();
  1106. token.m_tag.tag_name.clear();
  1107. token.m_tag.tag_name.append("img");
  1108. process_using_the_rules_for(m_insertion_mode, token);
  1109. return;
  1110. }
  1111. if (token.is_start_tag() && token.tag_name() == "textarea") {
  1112. insert_html_element(token);
  1113. // If the next token is a U+000A LINE FEED (LF) character token,
  1114. // then ignore that token and move on to the next one.
  1115. // (Newlines at the start of pre blocks are ignored as an authoring convenience.)
  1116. auto next_token = m_tokenizer.next_token();
  1117. m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
  1118. m_original_insertion_mode = m_insertion_mode;
  1119. m_frameset_ok = false;
  1120. m_insertion_mode = InsertionMode::Text;
  1121. if (next_token.has_value() && next_token.value().is_character() && next_token.value().codepoint() == '\n') {
  1122. // Ignore it.
  1123. } else {
  1124. process_using_the_rules_for(m_insertion_mode, next_token.value());
  1125. }
  1126. return;
  1127. }
  1128. if (token.is_start_tag() && token.tag_name() == "xmp") {
  1129. if (m_stack_of_open_elements.has_in_button_scope("p")) {
  1130. close_a_p_element();
  1131. }
  1132. reconstruct_the_active_formatting_elements();
  1133. m_frameset_ok = false;
  1134. parse_generic_raw_text_element(token);
  1135. return;
  1136. }
  1137. if (token.is_start_tag() && token.tag_name() == "iframe") {
  1138. m_frameset_ok = false;
  1139. parse_generic_raw_text_element(token);
  1140. return;
  1141. }
  1142. if (token.is_start_tag() && ((token.tag_name() == "noembed") || (token.tag_name() == "noscript" && m_scripting_enabled))) {
  1143. parse_generic_raw_text_element(token);
  1144. return;
  1145. }
  1146. if (token.is_start_tag() && token.tag_name() == "select") {
  1147. reconstruct_the_active_formatting_elements();
  1148. insert_html_element(token);
  1149. m_frameset_ok = false;
  1150. switch (m_insertion_mode) {
  1151. case InsertionMode::InTable:
  1152. case InsertionMode::InCaption:
  1153. case InsertionMode::InTableBody:
  1154. case InsertionMode::InRow:
  1155. case InsertionMode::InCell:
  1156. m_insertion_mode = InsertionMode::InSelectInTable;
  1157. break;
  1158. default:
  1159. m_insertion_mode = InsertionMode::InSelect;
  1160. break;
  1161. }
  1162. return;
  1163. }
  1164. if (token.is_start_tag() && token.tag_name().is_one_of("optgroup", "option")) {
  1165. if (current_node().tag_name() == "option")
  1166. m_stack_of_open_elements.pop();
  1167. reconstruct_the_active_formatting_elements();
  1168. insert_html_element(token);
  1169. return;
  1170. }
  1171. if (token.is_start_tag() && token.tag_name().is_one_of("rb", "rtc")) {
  1172. TODO();
  1173. }
  1174. if (token.is_start_tag() && token.tag_name().is_one_of("rp", "rt")) {
  1175. TODO();
  1176. }
  1177. if (token.is_start_tag() && token.tag_name() == "math") {
  1178. dbg() << "<math> element encountered.";
  1179. reconstruct_the_active_formatting_elements();
  1180. insert_html_element(token);
  1181. return;
  1182. }
  1183. if (token.is_start_tag() && token.tag_name() == "svg") {
  1184. dbg() << "<svg> element encountered.";
  1185. reconstruct_the_active_formatting_elements();
  1186. insert_html_element(token);
  1187. return;
  1188. }
  1189. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr"))) {
  1190. PARSE_ERROR();
  1191. return;
  1192. }
  1193. // Any other start tag
  1194. if (token.is_start_tag()) {
  1195. reconstruct_the_active_formatting_elements();
  1196. insert_html_element(token);
  1197. return;
  1198. }
  1199. if (token.is_end_tag()) {
  1200. AnyOtherEndTag:
  1201. RefPtr<Element> node;
  1202. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1203. node = m_stack_of_open_elements.elements()[i];
  1204. if (node->tag_name() == token.tag_name()) {
  1205. generate_implied_end_tags(token.tag_name());
  1206. if (node != current_node()) {
  1207. PARSE_ERROR();
  1208. }
  1209. while (&current_node() != node) {
  1210. m_stack_of_open_elements.pop();
  1211. }
  1212. m_stack_of_open_elements.pop();
  1213. break;
  1214. }
  1215. if (is_special_tag(node->tag_name())) {
  1216. PARSE_ERROR();
  1217. return;
  1218. }
  1219. }
  1220. return;
  1221. }
  1222. TODO();
  1223. }
  1224. void HTMLDocumentParser::increment_script_nesting_level()
  1225. {
  1226. ++m_script_nesting_level;
  1227. }
  1228. void HTMLDocumentParser::decrement_script_nesting_level()
  1229. {
  1230. ASSERT(m_script_nesting_level);
  1231. --m_script_nesting_level;
  1232. }
  1233. void HTMLDocumentParser::handle_text(HTMLToken& token)
  1234. {
  1235. if (token.is_character()) {
  1236. insert_character(token.codepoint());
  1237. return;
  1238. }
  1239. if (token.is_end_tag() && token.tag_name() == "script") {
  1240. NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
  1241. m_stack_of_open_elements.pop();
  1242. m_insertion_mode = m_original_insertion_mode;
  1243. // FIXME: Handle tokenizer insertion point stuff here.
  1244. increment_script_nesting_level();
  1245. script->prepare_script({});
  1246. decrement_script_nesting_level();
  1247. if (script_nesting_level() == 0)
  1248. m_parser_pause_flag = false;
  1249. // FIXME: Handle tokenizer insertion point stuff here too.
  1250. while (document().pending_parsing_blocking_script()) {
  1251. if (script_nesting_level() != 0) {
  1252. m_parser_pause_flag = true;
  1253. // FIXME: Abort the processing of any nested invocations of the tokenizer,
  1254. // yielding control back to the caller. (Tokenization will resume when
  1255. // the caller returns to the "outer" tree construction stage.)
  1256. TODO();
  1257. } else {
  1258. auto the_script = document().take_pending_parsing_blocking_script({});
  1259. m_tokenizer.set_blocked(true);
  1260. // FIXME: If the parser's Document has a style sheet that is blocking scripts
  1261. // or the script's "ready to be parser-executed" flag is not set:
  1262. // spin the event loop until the parser's Document has no style sheet
  1263. // that is blocking scripts and the script's "ready to be parser-executed"
  1264. // flag is set.
  1265. ASSERT(the_script->is_ready_to_be_parser_executed());
  1266. if (m_aborted)
  1267. return;
  1268. m_tokenizer.set_blocked(false);
  1269. // FIXME: Handle tokenizer insertion point stuff here too.
  1270. ASSERT(script_nesting_level() == 0);
  1271. increment_script_nesting_level();
  1272. the_script->execute_script();
  1273. decrement_script_nesting_level();
  1274. ASSERT(script_nesting_level() == 0);
  1275. m_parser_pause_flag = false;
  1276. // FIXME: Handle tokenizer insertion point stuff here too.
  1277. }
  1278. }
  1279. return;
  1280. }
  1281. if (token.is_end_tag()) {
  1282. m_stack_of_open_elements.pop();
  1283. m_insertion_mode = m_original_insertion_mode;
  1284. return;
  1285. }
  1286. TODO();
  1287. }
  1288. void HTMLDocumentParser::clear_the_stack_back_to_a_table_context()
  1289. {
  1290. while (!current_node().tag_name().is_one_of("table", "template", "html"))
  1291. m_stack_of_open_elements.pop();
  1292. }
  1293. void HTMLDocumentParser::clear_the_stack_back_to_a_table_row_context()
  1294. {
  1295. while (!current_node().tag_name().is_one_of("tr", "template", "html"))
  1296. m_stack_of_open_elements.pop();
  1297. }
  1298. void HTMLDocumentParser::clear_the_stack_back_to_a_table_body_context()
  1299. {
  1300. while (!current_node().tag_name().is_one_of("tbody", "tfoot", "thead", "template", "html"))
  1301. m_stack_of_open_elements.pop();
  1302. }
  1303. void HTMLDocumentParser::handle_in_row(HTMLToken& token)
  1304. {
  1305. if (token.is_start_tag() && token.tag_name().is_one_of("th", "td")) {
  1306. clear_the_stack_back_to_a_table_row_context();
  1307. insert_html_element(token);
  1308. m_insertion_mode = InsertionMode::InCell;
  1309. m_list_of_active_formatting_elements.add_marker();
  1310. return;
  1311. }
  1312. if (token.is_end_tag() && token.tag_name() == "tr") {
  1313. if (!m_stack_of_open_elements.has_in_table_scope("tr")) {
  1314. PARSE_ERROR();
  1315. return;
  1316. }
  1317. clear_the_stack_back_to_a_table_row_context();
  1318. m_stack_of_open_elements.pop();
  1319. m_insertion_mode = InsertionMode::InTableBody;
  1320. return;
  1321. }
  1322. if (token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr")) {
  1323. if (m_stack_of_open_elements.has_in_table_scope("tr")) {
  1324. PARSE_ERROR();
  1325. return;
  1326. }
  1327. clear_the_stack_back_to_a_table_row_context();
  1328. m_stack_of_open_elements.pop();
  1329. m_insertion_mode = InsertionMode::InTableBody;
  1330. process_using_the_rules_for(m_insertion_mode, token);
  1331. return;
  1332. }
  1333. if (token.is_end_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  1334. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1335. PARSE_ERROR();
  1336. return;
  1337. }
  1338. if (!m_stack_of_open_elements.has_in_table_scope("tr")) {
  1339. return;
  1340. }
  1341. clear_the_stack_back_to_a_table_row_context();
  1342. m_stack_of_open_elements.pop();
  1343. m_insertion_mode = InsertionMode::InTableBody;
  1344. process_using_the_rules_for(m_insertion_mode, token);
  1345. return;
  1346. }
  1347. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html", "td", "th")) {
  1348. PARSE_ERROR();
  1349. return;
  1350. }
  1351. process_using_the_rules_for(InsertionMode::InTable, token);
  1352. }
  1353. void HTMLDocumentParser::close_the_cell()
  1354. {
  1355. generate_implied_end_tags();
  1356. if (!current_node().tag_name().is_one_of("td", "th")) {
  1357. PARSE_ERROR();
  1358. }
  1359. while (!current_node().tag_name().is_one_of("td", "th"))
  1360. m_stack_of_open_elements.pop();
  1361. m_stack_of_open_elements.pop();
  1362. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1363. m_insertion_mode = InsertionMode::InRow;
  1364. }
  1365. void HTMLDocumentParser::handle_in_cell(HTMLToken& token)
  1366. {
  1367. if (token.is_end_tag() && token.tag_name().is_one_of("td", "th")) {
  1368. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1369. PARSE_ERROR();
  1370. return;
  1371. }
  1372. generate_implied_end_tags();
  1373. if (current_node().tag_name() != token.tag_name()) {
  1374. PARSE_ERROR();
  1375. }
  1376. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1377. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1378. m_insertion_mode = InsertionMode::InRow;
  1379. return;
  1380. }
  1381. if (token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr")) {
  1382. if (!m_stack_of_open_elements.has_in_table_scope("td") && m_stack_of_open_elements.has_in_table_scope("th")) {
  1383. PARSE_ERROR();
  1384. return;
  1385. }
  1386. close_the_cell();
  1387. process_using_the_rules_for(m_insertion_mode, token);
  1388. return;
  1389. }
  1390. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html")) {
  1391. PARSE_ERROR();
  1392. return;
  1393. }
  1394. if (token.is_end_tag() && token.tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  1395. if (m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1396. PARSE_ERROR();
  1397. return;
  1398. }
  1399. close_the_cell();
  1400. // Reprocess the token.
  1401. process_using_the_rules_for(m_insertion_mode, token);
  1402. return;
  1403. }
  1404. process_using_the_rules_for(InsertionMode::InBody, token);
  1405. }
  1406. void HTMLDocumentParser::handle_in_table_text(HTMLToken& token)
  1407. {
  1408. if (token.is_character()) {
  1409. if (token.codepoint() == 0) {
  1410. PARSE_ERROR();
  1411. return;
  1412. }
  1413. m_pending_table_character_tokens.append(token);
  1414. return;
  1415. }
  1416. for (auto& pending_token : m_pending_table_character_tokens) {
  1417. ASSERT(pending_token.is_character());
  1418. if (!pending_token.is_parser_whitespace()) {
  1419. // FIXME: If any of the tokens in the pending table character tokens list
  1420. // are character tokens that are not ASCII whitespace, then this is a parse error:
  1421. // reprocess the character tokens in the pending table character tokens list using
  1422. // the rules given in the "anything else" entry in the "in table" insertion mode.
  1423. TODO();
  1424. }
  1425. }
  1426. for (auto& pending_token : m_pending_table_character_tokens) {
  1427. insert_character(pending_token.codepoint());
  1428. }
  1429. m_insertion_mode = m_original_insertion_mode;
  1430. process_using_the_rules_for(m_insertion_mode, token);
  1431. }
  1432. void HTMLDocumentParser::handle_in_table_body(HTMLToken& token)
  1433. {
  1434. if (token.is_start_tag() && token.tag_name() == "tr") {
  1435. clear_the_stack_back_to_a_table_body_context();
  1436. insert_html_element(token);
  1437. m_insertion_mode = InsertionMode::InRow;
  1438. return;
  1439. }
  1440. if (token.is_start_tag() && token.tag_name().is_one_of("th", "td")) {
  1441. PARSE_ERROR();
  1442. clear_the_stack_back_to_a_table_body_context();
  1443. HTMLToken fake_tr_token;
  1444. fake_tr_token.m_type = HTMLToken::Type::StartTag;
  1445. fake_tr_token.m_tag.tag_name.append("tr");
  1446. insert_html_element(fake_tr_token);
  1447. m_insertion_mode = InsertionMode::InRow;
  1448. process_using_the_rules_for(m_insertion_mode, token);
  1449. return;
  1450. }
  1451. if (token.is_end_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  1452. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1453. PARSE_ERROR();
  1454. return;
  1455. }
  1456. clear_the_stack_back_to_a_table_body_context();
  1457. m_stack_of_open_elements.pop();
  1458. m_insertion_mode = InsertionMode::InTable;
  1459. return;
  1460. }
  1461. if ((token.is_start_tag() && token.tag_name().is_one_of("caption", "col", "colgroup", "tbody", "tfoot", "thead"))
  1462. || (token.is_end_tag() && token.tag_name() == "table")) {
  1463. // 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.
  1464. clear_the_stack_back_to_a_table_body_context();
  1465. m_stack_of_open_elements.pop();
  1466. m_insertion_mode = InsertionMode::InTable;
  1467. process_using_the_rules_for(InsertionMode::InTable, token);
  1468. return;
  1469. }
  1470. if (token.is_end_tag() && token.tag_name().is_one_of("body", "caption", "col", "colgroup", "html", "td", "th", "tr")) {
  1471. PARSE_ERROR();
  1472. return;
  1473. }
  1474. process_using_the_rules_for(InsertionMode::InTable, token);
  1475. }
  1476. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  1477. {
  1478. if (token.is_character() && current_node().tag_name().is_one_of("table", "tbody", "tfoot", "thead", "tr")) {
  1479. m_pending_table_character_tokens.clear();
  1480. m_original_insertion_mode = m_insertion_mode;
  1481. m_insertion_mode = InsertionMode::InTableText;
  1482. process_using_the_rules_for(InsertionMode::InTableText, token);
  1483. return;
  1484. }
  1485. if (token.is_comment()) {
  1486. insert_comment(token);
  1487. return;
  1488. }
  1489. if (token.is_doctype()) {
  1490. PARSE_ERROR();
  1491. return;
  1492. }
  1493. if (token.is_start_tag() && token.tag_name() == "caption") {
  1494. TODO();
  1495. }
  1496. if (token.is_start_tag() && token.tag_name() == "colgroup") {
  1497. TODO();
  1498. }
  1499. if (token.is_start_tag() && token.tag_name() == "col") {
  1500. TODO();
  1501. }
  1502. if (token.is_start_tag() && token.tag_name().is_one_of("tbody", "tfoot", "thead")) {
  1503. clear_the_stack_back_to_a_table_context();
  1504. insert_html_element(token);
  1505. m_insertion_mode = InsertionMode::InTableBody;
  1506. return;
  1507. }
  1508. if (token.is_start_tag() && token.tag_name().is_one_of("td", "th", "tr")) {
  1509. clear_the_stack_back_to_a_table_context();
  1510. HTMLToken fake_tbody_token;
  1511. fake_tbody_token.m_type = HTMLToken::Type::StartTag;
  1512. fake_tbody_token.m_tag.tag_name.append("tbody");
  1513. insert_html_element(fake_tbody_token);
  1514. m_insertion_mode = InsertionMode::InTableBody;
  1515. process_using_the_rules_for(InsertionMode::InTableBody, token);
  1516. return;
  1517. }
  1518. if (token.is_start_tag() && token.tag_name() == "table") {
  1519. PARSE_ERROR();
  1520. TODO();
  1521. }
  1522. if (token.is_end_tag()) {
  1523. if (!m_stack_of_open_elements.has_in_table_scope("table")) {
  1524. PARSE_ERROR();
  1525. return;
  1526. }
  1527. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("table");
  1528. reset_the_insertion_mode_appropriately();
  1529. return;
  1530. }
  1531. TODO();
  1532. }
  1533. void HTMLDocumentParser::handle_in_select_in_table(HTMLToken& token)
  1534. {
  1535. (void)token;
  1536. TODO();
  1537. }
  1538. void HTMLDocumentParser::handle_in_select(HTMLToken& token)
  1539. {
  1540. if (token.is_character()) {
  1541. if (token.codepoint() == 0) {
  1542. PARSE_ERROR();
  1543. return;
  1544. }
  1545. insert_character(token.codepoint());
  1546. return;
  1547. }
  1548. if (token.is_comment()) {
  1549. insert_comment(token);
  1550. return;
  1551. }
  1552. if (token.is_doctype()) {
  1553. PARSE_ERROR();
  1554. return;
  1555. }
  1556. if (token.is_start_tag() && token.tag_name() == "html") {
  1557. process_using_the_rules_for(InsertionMode::InBody, token);
  1558. return;
  1559. }
  1560. if (token.is_start_tag() && token.tag_name() == "option") {
  1561. if (current_node().tag_name() == "option") {
  1562. m_stack_of_open_elements.pop();
  1563. }
  1564. insert_html_element(token);
  1565. return;
  1566. }
  1567. if (token.is_start_tag() && token.tag_name() == "optgroup") {
  1568. if (current_node().tag_name() == "option") {
  1569. m_stack_of_open_elements.pop();
  1570. }
  1571. if (current_node().tag_name() == "optgroup") {
  1572. m_stack_of_open_elements.pop();
  1573. }
  1574. insert_html_element(token);
  1575. return;
  1576. }
  1577. if (token.is_end_tag() && token.tag_name() == "optgroup") {
  1578. if (current_node().tag_name() == "option" && node_before_current_node().tag_name() == "optgroup")
  1579. m_stack_of_open_elements.pop();
  1580. if (current_node().tag_name() == "optgroup") {
  1581. m_stack_of_open_elements.pop();
  1582. } else {
  1583. PARSE_ERROR();
  1584. return;
  1585. }
  1586. return;
  1587. }
  1588. if (token.is_end_tag() && token.tag_name() == "option") {
  1589. if (current_node().tag_name() == "option") {
  1590. m_stack_of_open_elements.pop();
  1591. } else {
  1592. PARSE_ERROR();
  1593. return;
  1594. }
  1595. return;
  1596. }
  1597. if (token.is_end_tag() && token.tag_name() == "select") {
  1598. if (m_stack_of_open_elements.has_in_select_scope("select")) {
  1599. PARSE_ERROR();
  1600. return;
  1601. }
  1602. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("select");
  1603. reset_the_insertion_mode_appropriately();
  1604. return;
  1605. }
  1606. if (token.is_start_tag() && token.tag_name() == "select") {
  1607. PARSE_ERROR();
  1608. if (!m_stack_of_open_elements.has_in_select_scope("select"))
  1609. return;
  1610. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("select");
  1611. reset_the_insertion_mode_appropriately();
  1612. return;
  1613. }
  1614. if (token.is_start_tag() && token.tag_name().is_one_of("input", "keygen", "textarea")) {
  1615. PARSE_ERROR();
  1616. if (!m_stack_of_open_elements.has_in_select_scope("select")) {
  1617. return;
  1618. }
  1619. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped("select");
  1620. reset_the_insertion_mode_appropriately();
  1621. process_using_the_rules_for(m_insertion_mode, token);
  1622. return;
  1623. }
  1624. if (token.is_start_tag() && token.tag_name().is_one_of("script", "template")) {
  1625. process_using_the_rules_for(InsertionMode::InHead, token);
  1626. return;
  1627. }
  1628. if (token.is_end_tag() && token.tag_name() == "template") {
  1629. process_using_the_rules_for(InsertionMode::InHead, token);
  1630. return;
  1631. }
  1632. if (token.is_end_of_file()) {
  1633. process_using_the_rules_for(InsertionMode::InBody, token);
  1634. return;
  1635. }
  1636. PARSE_ERROR();
  1637. }
  1638. void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
  1639. {
  1640. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1641. RefPtr<Element> node = m_stack_of_open_elements.elements().at(i);
  1642. if (node->tag_name() == "select") {
  1643. TODO();
  1644. }
  1645. if (node->tag_name().is_one_of("td", "th")) {
  1646. m_insertion_mode = InsertionMode::InCell;
  1647. return;
  1648. }
  1649. if (node->tag_name() == "tr") {
  1650. m_insertion_mode = InsertionMode::InRow;
  1651. return;
  1652. }
  1653. if (node->tag_name().is_one_of("tbody", "thead", "tfoot")) {
  1654. m_insertion_mode = InsertionMode::InTableBody;
  1655. return;
  1656. }
  1657. if (node->tag_name() == "caption") {
  1658. m_insertion_mode = InsertionMode::InCaption;
  1659. return;
  1660. }
  1661. if (node->tag_name() == "colgroup") {
  1662. m_insertion_mode = InsertionMode::InColumnGroup;
  1663. return;
  1664. }
  1665. if (node->tag_name() == "table") {
  1666. m_insertion_mode = InsertionMode::InTable;
  1667. return;
  1668. }
  1669. if (node->tag_name() == "template") {
  1670. TODO();
  1671. }
  1672. if (node->tag_name() == "body") {
  1673. m_insertion_mode = InsertionMode::InBody;
  1674. return;
  1675. }
  1676. if (node->tag_name() == "frameset") {
  1677. m_insertion_mode = InsertionMode::InFrameset;
  1678. if (m_parsing_fragment) {
  1679. TODO();
  1680. }
  1681. return;
  1682. }
  1683. if (node->tag_name() == "html") {
  1684. TODO();
  1685. }
  1686. }
  1687. m_insertion_mode = InsertionMode::InBody;
  1688. if (m_parsing_fragment) {
  1689. TODO();
  1690. }
  1691. }
  1692. const char* HTMLDocumentParser::insertion_mode_name() const
  1693. {
  1694. switch (m_insertion_mode) {
  1695. #define __ENUMERATE_INSERTION_MODE(mode) \
  1696. case InsertionMode::mode: \
  1697. return #mode;
  1698. ENUMERATE_INSERTION_MODES
  1699. #undef __ENUMERATE_INSERTION_MODE
  1700. }
  1701. ASSERT_NOT_REACHED();
  1702. }
  1703. Document& HTMLDocumentParser::document()
  1704. {
  1705. return *m_document;
  1706. }
  1707. }