HTMLDocumentParser.cpp 61 KB

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