HTMLDocumentParser.cpp 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660
  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. RefPtr<Document> parse_html_document(const StringView& data, const URL& url, const String& encoding)
  45. {
  46. HTMLDocumentParser parser(data, encoding);
  47. parser.run(url);
  48. return parser.document();
  49. }
  50. HTMLDocumentParser::HTMLDocumentParser(const StringView& input, const String& encoding)
  51. : m_tokenizer(input, encoding)
  52. {
  53. m_document = adopt(*new Document);
  54. }
  55. HTMLDocumentParser::~HTMLDocumentParser()
  56. {
  57. }
  58. void HTMLDocumentParser::run(const URL& url)
  59. {
  60. m_document->set_url(url);
  61. m_document->set_source(m_tokenizer.source());
  62. for (;;) {
  63. auto optional_token = m_tokenizer.next_token();
  64. if (!optional_token.has_value())
  65. break;
  66. auto& token = optional_token.value();
  67. #ifdef PARSER_DEBUG
  68. dbg() << "[" << insertion_mode_name() << "] " << token.to_string();
  69. #endif
  70. process_using_the_rules_for(m_insertion_mode, token);
  71. if (m_stop_parsing) {
  72. #ifdef PARSER_DEBUG
  73. dbg() << "Stop parsing! :^)";
  74. #endif
  75. break;
  76. }
  77. }
  78. flush_character_insertions();
  79. // "The end"
  80. auto scripts_to_execute_when_parsing_has_finished = m_document->take_scripts_to_execute_when_parsing_has_finished({});
  81. for (auto& script : scripts_to_execute_when_parsing_has_finished) {
  82. script.execute_script();
  83. }
  84. m_document->dispatch_event(Event::create("DOMContentLoaded"));
  85. auto scripts_to_execute_as_soon_as_possible = m_document->take_scripts_to_execute_as_soon_as_possible({});
  86. for (auto& script : scripts_to_execute_as_soon_as_possible) {
  87. script.execute_script();
  88. }
  89. }
  90. void HTMLDocumentParser::process_using_the_rules_for(InsertionMode mode, HTMLToken& token)
  91. {
  92. switch (mode) {
  93. case InsertionMode::Initial:
  94. handle_initial(token);
  95. break;
  96. case InsertionMode::BeforeHTML:
  97. handle_before_html(token);
  98. break;
  99. case InsertionMode::BeforeHead:
  100. handle_before_head(token);
  101. break;
  102. case InsertionMode::InHead:
  103. handle_in_head(token);
  104. break;
  105. case InsertionMode::InHeadNoscript:
  106. handle_in_head_noscript(token);
  107. break;
  108. case InsertionMode::AfterHead:
  109. handle_after_head(token);
  110. break;
  111. case InsertionMode::InBody:
  112. handle_in_body(token);
  113. break;
  114. case InsertionMode::AfterBody:
  115. handle_after_body(token);
  116. break;
  117. case InsertionMode::AfterAfterBody:
  118. handle_after_after_body(token);
  119. break;
  120. case InsertionMode::Text:
  121. handle_text(token);
  122. break;
  123. case InsertionMode::InTable:
  124. handle_in_table(token);
  125. break;
  126. case InsertionMode::InTableBody:
  127. handle_in_table_body(token);
  128. break;
  129. case InsertionMode::InRow:
  130. handle_in_row(token);
  131. break;
  132. case InsertionMode::InCell:
  133. handle_in_cell(token);
  134. break;
  135. case InsertionMode::InTableText:
  136. handle_in_table_text(token);
  137. break;
  138. case InsertionMode::InSelectInTable:
  139. handle_in_select_in_table(token);
  140. break;
  141. case InsertionMode::InSelect:
  142. handle_in_select(token);
  143. break;
  144. case InsertionMode::InCaption:
  145. handle_in_caption(token);
  146. break;
  147. case InsertionMode::InColumnGroup:
  148. handle_in_column_group(token);
  149. break;
  150. case InsertionMode::InTemplate:
  151. handle_in_template(token);
  152. break;
  153. case InsertionMode::InFrameset:
  154. handle_in_frameset(token);
  155. break;
  156. case InsertionMode::AfterFrameset:
  157. handle_after_frameset(token);
  158. break;
  159. case InsertionMode::AfterAfterFrameset:
  160. handle_after_after_frameset(token);
  161. break;
  162. default:
  163. ASSERT_NOT_REACHED();
  164. }
  165. }
  166. void HTMLDocumentParser::handle_initial(HTMLToken& token)
  167. {
  168. if (token.is_character() && token.is_parser_whitespace()) {
  169. return;
  170. }
  171. if (token.is_comment()) {
  172. auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
  173. document().append_child(move(comment));
  174. return;
  175. }
  176. if (token.is_doctype()) {
  177. auto doctype = adopt(*new DocumentType(document()));
  178. doctype->set_name(token.m_doctype.name.to_string());
  179. document().append_child(move(doctype));
  180. document().set_quirks_mode(token.m_doctype.force_quirks);
  181. m_insertion_mode = InsertionMode::BeforeHTML;
  182. return;
  183. }
  184. PARSE_ERROR();
  185. document().set_quirks_mode(true);
  186. m_insertion_mode = InsertionMode::BeforeHTML;
  187. process_using_the_rules_for(InsertionMode::BeforeHTML, token);
  188. }
  189. void HTMLDocumentParser::handle_before_html(HTMLToken& token)
  190. {
  191. if (token.is_doctype()) {
  192. PARSE_ERROR();
  193. return;
  194. }
  195. if (token.is_comment()) {
  196. auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
  197. document().append_child(move(comment));
  198. return;
  199. }
  200. if (token.is_character() && token.is_parser_whitespace()) {
  201. return;
  202. }
  203. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  204. auto element = create_element_for(token);
  205. document().append_child(element);
  206. m_stack_of_open_elements.push(move(element));
  207. m_insertion_mode = InsertionMode::BeforeHead;
  208. return;
  209. }
  210. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::head, HTML::TagNames::body, HTML::TagNames::html, HTML::TagNames::br)) {
  211. goto AnythingElse;
  212. }
  213. if (token.is_end_tag()) {
  214. PARSE_ERROR();
  215. return;
  216. }
  217. AnythingElse:
  218. auto element = create_element(document(), HTML::TagNames::html);
  219. document().append_child(element);
  220. m_stack_of_open_elements.push(element);
  221. // 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.
  222. m_insertion_mode = InsertionMode::BeforeHead;
  223. process_using_the_rules_for(InsertionMode::BeforeHead, token);
  224. return;
  225. }
  226. Element& HTMLDocumentParser::current_node()
  227. {
  228. return m_stack_of_open_elements.current_node();
  229. }
  230. Element& HTMLDocumentParser::node_before_current_node()
  231. {
  232. return m_stack_of_open_elements.elements().at(m_stack_of_open_elements.elements().size() - 2);
  233. }
  234. HTMLDocumentParser::AdjustedInsertionLocation HTMLDocumentParser::find_appropriate_place_for_inserting_node()
  235. {
  236. auto& target = current_node();
  237. if (m_foster_parenting && target.tag_name().is_one_of(HTML::TagNames::table, HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead, HTML::TagNames::tr)) {
  238. // FIXME: There's a bunch of steps for <template> elements here.
  239. auto last_table = m_stack_of_open_elements.last_element_with_tag_name(HTML::TagNames::table);
  240. if (!last_table)
  241. return { m_stack_of_open_elements.elements().first(), nullptr };
  242. if (last_table->parent_node())
  243. return { last_table->parent_node(), last_table };
  244. return { m_stack_of_open_elements.element_before(*last_table), nullptr };
  245. }
  246. return { target, nullptr };
  247. }
  248. NonnullRefPtr<Element> HTMLDocumentParser::create_element_for(HTMLToken& token)
  249. {
  250. auto element = create_element(document(), token.tag_name());
  251. for (auto& attribute : token.m_tag.attributes) {
  252. element->set_attribute(attribute.local_name_builder.to_string(), attribute.value_builder.to_string());
  253. }
  254. return element;
  255. }
  256. RefPtr<Element> HTMLDocumentParser::insert_html_element(HTMLToken& token)
  257. {
  258. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  259. auto element = create_element_for(token);
  260. // FIXME: Check if it's possible to insert `element` at `adjusted_insertion_location`
  261. adjusted_insertion_location.parent->insert_before(element, adjusted_insertion_location.insert_before_sibling);
  262. m_stack_of_open_elements.push(element);
  263. return element;
  264. }
  265. void HTMLDocumentParser::handle_before_head(HTMLToken& token)
  266. {
  267. if (token.is_character() && token.is_parser_whitespace()) {
  268. return;
  269. }
  270. if (token.is_comment()) {
  271. insert_comment(token);
  272. return;
  273. }
  274. if (token.is_doctype()) {
  275. PARSE_ERROR();
  276. return;
  277. }
  278. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  279. process_using_the_rules_for(InsertionMode::InBody, token);
  280. return;
  281. }
  282. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::head) {
  283. auto element = insert_html_element(token);
  284. m_head_element = to<HTMLHeadElement>(element);
  285. m_insertion_mode = InsertionMode::InHead;
  286. return;
  287. }
  288. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::head, HTML::TagNames::body, HTML::TagNames::html, HTML::TagNames::br)) {
  289. goto AnythingElse;
  290. }
  291. if (token.is_end_tag()) {
  292. PARSE_ERROR();
  293. return;
  294. }
  295. AnythingElse:
  296. HTMLToken fake_head_token;
  297. fake_head_token.m_type = HTMLToken::Type::StartTag;
  298. fake_head_token.m_tag.tag_name.append(HTML::TagNames::head);
  299. m_head_element = to<HTMLHeadElement>(insert_html_element(fake_head_token));
  300. m_insertion_mode = InsertionMode::InHead;
  301. process_using_the_rules_for(InsertionMode::InHead, token);
  302. return;
  303. }
  304. void HTMLDocumentParser::insert_comment(HTMLToken& token)
  305. {
  306. auto data = token.m_comment_or_character.data.to_string();
  307. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  308. adjusted_insertion_location.parent->insert_before(adopt(*new Comment(document(), data)), adjusted_insertion_location.insert_before_sibling);
  309. }
  310. void HTMLDocumentParser::handle_in_head(HTMLToken& token)
  311. {
  312. if (token.is_parser_whitespace()) {
  313. insert_character(token.codepoint());
  314. return;
  315. }
  316. if (token.is_comment()) {
  317. insert_comment(token);
  318. return;
  319. }
  320. if (token.is_doctype()) {
  321. PARSE_ERROR();
  322. return;
  323. }
  324. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  325. process_using_the_rules_for(InsertionMode::InBody, token);
  326. return;
  327. }
  328. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::base, HTML::TagNames::basefont, HTML::TagNames::bgsound, HTML::TagNames::link)) {
  329. insert_html_element(token);
  330. m_stack_of_open_elements.pop();
  331. token.acknowledge_self_closing_flag_if_set();
  332. return;
  333. }
  334. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::meta) {
  335. auto element = insert_html_element(token);
  336. m_stack_of_open_elements.pop();
  337. token.acknowledge_self_closing_flag_if_set();
  338. return;
  339. }
  340. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::title) {
  341. insert_html_element(token);
  342. m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
  343. m_original_insertion_mode = m_insertion_mode;
  344. m_insertion_mode = InsertionMode::Text;
  345. return;
  346. }
  347. if (token.is_start_tag() && ((token.tag_name() == HTML::TagNames::noscript && m_scripting_enabled) || token.tag_name() == HTML::TagNames::noframes || token.tag_name() == HTML::TagNames::style)) {
  348. parse_generic_raw_text_element(token);
  349. return;
  350. }
  351. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::noscript && !m_scripting_enabled) {
  352. insert_html_element(token);
  353. m_insertion_mode = InsertionMode::InHeadNoscript;
  354. return;
  355. }
  356. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::script) {
  357. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  358. auto element = create_element_for(token);
  359. auto& script_element = to<HTMLScriptElement>(*element);
  360. script_element.set_parser_document({}, document());
  361. script_element.set_non_blocking({}, false);
  362. if (m_parsing_fragment) {
  363. TODO();
  364. }
  365. if (m_invoked_via_document_write) {
  366. TODO();
  367. }
  368. adjusted_insertion_location.parent->insert_before(element, adjusted_insertion_location.insert_before_sibling, false);
  369. m_stack_of_open_elements.push(element);
  370. m_tokenizer.switch_to({}, HTMLTokenizer::State::ScriptData);
  371. m_original_insertion_mode = m_insertion_mode;
  372. m_insertion_mode = InsertionMode::Text;
  373. return;
  374. }
  375. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::head) {
  376. m_stack_of_open_elements.pop();
  377. m_insertion_mode = InsertionMode::AfterHead;
  378. return;
  379. }
  380. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::body, HTML::TagNames::html, HTML::TagNames::br)) {
  381. goto AnythingElse;
  382. }
  383. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::template_) {
  384. // FIXME: Support this properly
  385. insert_html_element(token);
  386. return;
  387. }
  388. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
  389. // FIXME: Support this properly
  390. ASSERT(current_node().tag_name() == HTML::TagNames::template_);
  391. m_stack_of_open_elements.pop();
  392. return;
  393. }
  394. if ((token.is_start_tag() && token.tag_name() == HTML::TagNames::head) || token.is_end_tag()) {
  395. PARSE_ERROR();
  396. return;
  397. }
  398. AnythingElse:
  399. m_stack_of_open_elements.pop();
  400. m_insertion_mode = InsertionMode::AfterHead;
  401. process_using_the_rules_for(m_insertion_mode, token);
  402. }
  403. void HTMLDocumentParser::handle_in_head_noscript(HTMLToken& token)
  404. {
  405. if (token.is_doctype()) {
  406. PARSE_ERROR();
  407. return;
  408. }
  409. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  410. process_using_the_rules_for(InsertionMode::InBody, token);
  411. return;
  412. }
  413. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::noscript) {
  414. m_stack_of_open_elements.pop();
  415. m_insertion_mode = InsertionMode::InHead;
  416. return;
  417. }
  418. if (token.is_parser_whitespace() || token.is_comment() || (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::basefont, HTML::TagNames::bgsound, HTML::TagNames::link, HTML::TagNames::meta, HTML::TagNames::noframes, HTML::TagNames::style))) {
  419. process_using_the_rules_for(InsertionMode::InHead, token);
  420. return;
  421. }
  422. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::br) {
  423. goto AnythingElse;
  424. }
  425. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::head, HTML::TagNames::noscript)) {
  426. PARSE_ERROR();
  427. return;
  428. }
  429. AnythingElse:
  430. PARSE_ERROR();
  431. m_stack_of_open_elements.pop();
  432. m_insertion_mode = InsertionMode::InHead;
  433. process_using_the_rules_for(m_insertion_mode, token);
  434. }
  435. void HTMLDocumentParser::parse_generic_raw_text_element(HTMLToken& token)
  436. {
  437. insert_html_element(token);
  438. m_tokenizer.switch_to({}, HTMLTokenizer::State::RAWTEXT);
  439. m_original_insertion_mode = m_insertion_mode;
  440. m_insertion_mode = InsertionMode::Text;
  441. }
  442. Text* HTMLDocumentParser::find_character_insertion_node()
  443. {
  444. auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
  445. if (adjusted_insertion_location.insert_before_sibling) {
  446. TODO();
  447. }
  448. if (adjusted_insertion_location.parent->is_document())
  449. return nullptr;
  450. if (adjusted_insertion_location.parent->last_child() && adjusted_insertion_location.parent->last_child()->is_text())
  451. return to<Text>(adjusted_insertion_location.parent->last_child());
  452. auto new_text_node = adopt(*new Text(document(), ""));
  453. adjusted_insertion_location.parent->append_child(new_text_node);
  454. return new_text_node;
  455. }
  456. void HTMLDocumentParser::flush_character_insertions()
  457. {
  458. if (m_character_insertion_builder.is_empty())
  459. return;
  460. m_character_insertion_node->set_data(m_character_insertion_builder.to_string());
  461. m_character_insertion_node->parent()->children_changed();
  462. m_character_insertion_builder.clear();
  463. }
  464. void HTMLDocumentParser::insert_character(u32 data)
  465. {
  466. auto node = find_character_insertion_node();
  467. if (node == m_character_insertion_node) {
  468. m_character_insertion_builder.append(Utf32View { &data, 1 });
  469. return;
  470. }
  471. if (!m_character_insertion_node) {
  472. m_character_insertion_node = node;
  473. m_character_insertion_builder.append(Utf32View { &data, 1 });
  474. return;
  475. }
  476. flush_character_insertions();
  477. m_character_insertion_node = node;
  478. m_character_insertion_builder.append(Utf32View { &data, 1 });
  479. }
  480. void HTMLDocumentParser::handle_after_head(HTMLToken& token)
  481. {
  482. if (token.is_character() && token.is_parser_whitespace()) {
  483. insert_character(token.codepoint());
  484. return;
  485. }
  486. if (token.is_comment()) {
  487. insert_comment(token);
  488. return;
  489. }
  490. if (token.is_doctype()) {
  491. PARSE_ERROR();
  492. return;
  493. }
  494. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  495. process_using_the_rules_for(InsertionMode::InBody, token);
  496. return;
  497. }
  498. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::body) {
  499. insert_html_element(token);
  500. m_frameset_ok = false;
  501. m_insertion_mode = InsertionMode::InBody;
  502. return;
  503. }
  504. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::frameset) {
  505. insert_html_element(token);
  506. m_insertion_mode = InsertionMode::InFrameset;
  507. return;
  508. }
  509. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::base, HTML::TagNames::basefont, HTML::TagNames::bgsound, HTML::TagNames::link, HTML::TagNames::meta, HTML::TagNames::noframes, HTML::TagNames::script, HTML::TagNames::style, HTML::TagNames::template_, HTML::TagNames::title)) {
  510. PARSE_ERROR();
  511. m_stack_of_open_elements.push(*m_head_element);
  512. process_using_the_rules_for(InsertionMode::InHead, token);
  513. m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) {
  514. return entry.ptr() == m_head_element;
  515. });
  516. return;
  517. }
  518. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
  519. process_using_the_rules_for(InsertionMode::InHead, token);
  520. return;
  521. }
  522. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::body, HTML::TagNames::html, HTML::TagNames::br)) {
  523. goto AnythingElse;
  524. }
  525. if ((token.is_start_tag() && token.tag_name() == HTML::TagNames::head) || token.is_end_tag()) {
  526. PARSE_ERROR();
  527. return;
  528. }
  529. AnythingElse:
  530. HTMLToken fake_body_token;
  531. fake_body_token.m_type = HTMLToken::Type::StartTag;
  532. fake_body_token.m_tag.tag_name.append(HTML::TagNames::body);
  533. insert_html_element(fake_body_token);
  534. m_insertion_mode = InsertionMode::InBody;
  535. process_using_the_rules_for(m_insertion_mode, token);
  536. }
  537. void HTMLDocumentParser::generate_implied_end_tags(const FlyString& exception)
  538. {
  539. while (current_node().tag_name() != exception && current_node().tag_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt, HTML::TagNames::li, HTML::TagNames::optgroup, HTML::TagNames::option, HTML::TagNames::p, HTML::TagNames::rb, HTML::TagNames::rp, HTML::TagNames::rt, HTML::TagNames::rtc))
  540. m_stack_of_open_elements.pop();
  541. }
  542. void HTMLDocumentParser::close_a_p_element()
  543. {
  544. generate_implied_end_tags(HTML::TagNames::p);
  545. if (current_node().tag_name() != HTML::TagNames::p) {
  546. PARSE_ERROR();
  547. }
  548. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::p);
  549. }
  550. void HTMLDocumentParser::handle_after_body(HTMLToken& token)
  551. {
  552. if (token.is_character() && token.is_parser_whitespace()) {
  553. process_using_the_rules_for(InsertionMode::InBody, token);
  554. return;
  555. }
  556. if (token.is_comment()) {
  557. auto data = token.m_comment_or_character.data.to_string();
  558. auto& insertion_location = m_stack_of_open_elements.first();
  559. insertion_location.append_child(adopt(*new Comment(document(), data)));
  560. return;
  561. }
  562. if (token.is_doctype()) {
  563. PARSE_ERROR();
  564. return;
  565. }
  566. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  567. process_using_the_rules_for(InsertionMode::InBody, token);
  568. return;
  569. }
  570. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::html) {
  571. if (m_parsing_fragment) {
  572. TODO();
  573. }
  574. m_insertion_mode = InsertionMode::AfterAfterBody;
  575. return;
  576. }
  577. if (token.is_end_of_file()) {
  578. stop_parsing();
  579. return;
  580. }
  581. PARSE_ERROR();
  582. m_insertion_mode = InsertionMode::InBody;
  583. process_using_the_rules_for(InsertionMode::InBody, token);
  584. }
  585. void HTMLDocumentParser::handle_after_after_body(HTMLToken& token)
  586. {
  587. if (token.is_comment()) {
  588. auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
  589. document().append_child(move(comment));
  590. return;
  591. }
  592. if (token.is_doctype() || token.is_parser_whitespace() || (token.is_start_tag() && token.tag_name() == HTML::TagNames::html)) {
  593. process_using_the_rules_for(InsertionMode::InBody, token);
  594. return;
  595. }
  596. if (token.is_end_of_file()) {
  597. stop_parsing();
  598. return;
  599. }
  600. PARSE_ERROR();
  601. m_insertion_mode = InsertionMode::InBody;
  602. process_using_the_rules_for(m_insertion_mode, token);
  603. }
  604. void HTMLDocumentParser::reconstruct_the_active_formatting_elements()
  605. {
  606. // FIXME: This needs to care about "markers"
  607. if (m_list_of_active_formatting_elements.is_empty())
  608. return;
  609. if (m_list_of_active_formatting_elements.entries().last().is_marker())
  610. return;
  611. if (m_stack_of_open_elements.contains(*m_list_of_active_formatting_elements.entries().last().element))
  612. return;
  613. ssize_t index = m_list_of_active_formatting_elements.entries().size() - 1;
  614. RefPtr<Element> entry = m_list_of_active_formatting_elements.entries().at(index).element;
  615. ASSERT(entry);
  616. Rewind:
  617. if (index == 0) {
  618. goto Create;
  619. }
  620. --index;
  621. entry = m_list_of_active_formatting_elements.entries().at(index).element;
  622. ASSERT(entry);
  623. if (!m_stack_of_open_elements.contains(*entry))
  624. goto Rewind;
  625. Advance:
  626. ++index;
  627. entry = m_list_of_active_formatting_elements.entries().at(index).element;
  628. ASSERT(entry);
  629. Create:
  630. // FIXME: Hold on to the real token!
  631. HTMLToken fake_token;
  632. fake_token.m_type = HTMLToken::Type::StartTag;
  633. fake_token.m_tag.tag_name.append(entry->tag_name());
  634. auto new_element = insert_html_element(fake_token);
  635. m_list_of_active_formatting_elements.entries().at(index).element = *new_element;
  636. if (index != (ssize_t)m_list_of_active_formatting_elements.entries().size() - 1)
  637. goto Advance;
  638. }
  639. HTMLDocumentParser::AdoptionAgencyAlgorithmOutcome HTMLDocumentParser::run_the_adoption_agency_algorithm(HTMLToken& token)
  640. {
  641. auto subject = token.tag_name();
  642. // If the current node is an HTML element whose tag name is subject,
  643. // and the current node is not in the list of active formatting elements,
  644. // then pop the current node off the stack of open elements, and return.
  645. if (current_node().tag_name() == subject && !m_list_of_active_formatting_elements.contains(current_node())) {
  646. m_stack_of_open_elements.pop();
  647. return AdoptionAgencyAlgorithmOutcome::DoNothing;
  648. }
  649. size_t outer_loop_counter = 0;
  650. //OuterLoop:
  651. if (outer_loop_counter >= 8)
  652. return AdoptionAgencyAlgorithmOutcome::DoNothing;
  653. ++outer_loop_counter;
  654. auto formatting_element = m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker(subject);
  655. if (!formatting_element)
  656. return AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps;
  657. if (!m_stack_of_open_elements.contains(*formatting_element)) {
  658. PARSE_ERROR();
  659. // FIXME: If formatting element is not in the stack of open elements,
  660. // then this is a parse error; remove the element from the list, and return.
  661. TODO();
  662. }
  663. if (!m_stack_of_open_elements.has_in_scope(*formatting_element)) {
  664. PARSE_ERROR();
  665. return AdoptionAgencyAlgorithmOutcome::DoNothing;
  666. }
  667. if (formatting_element != &current_node()) {
  668. PARSE_ERROR();
  669. }
  670. RefPtr<Element> furthest_block = m_stack_of_open_elements.topmost_special_node_below(*formatting_element);
  671. if (!furthest_block) {
  672. while (&current_node() != formatting_element)
  673. m_stack_of_open_elements.pop();
  674. m_stack_of_open_elements.pop();
  675. m_list_of_active_formatting_elements.remove(*formatting_element);
  676. return AdoptionAgencyAlgorithmOutcome::DoNothing;
  677. }
  678. // FIXME: Implement the rest of the AAA :^)
  679. TODO();
  680. }
  681. bool HTMLDocumentParser::is_special_tag(const FlyString& tag_name)
  682. {
  683. return tag_name.is_one_of(
  684. HTML::TagNames::address,
  685. HTML::TagNames::applet,
  686. HTML::TagNames::area,
  687. HTML::TagNames::article,
  688. HTML::TagNames::aside,
  689. HTML::TagNames::base,
  690. HTML::TagNames::basefont,
  691. HTML::TagNames::bgsound,
  692. HTML::TagNames::blockquote,
  693. HTML::TagNames::body,
  694. HTML::TagNames::br,
  695. HTML::TagNames::button,
  696. HTML::TagNames::caption,
  697. HTML::TagNames::center,
  698. HTML::TagNames::col,
  699. HTML::TagNames::colgroup,
  700. HTML::TagNames::dd,
  701. HTML::TagNames::details,
  702. HTML::TagNames::dir,
  703. HTML::TagNames::div,
  704. HTML::TagNames::dl,
  705. HTML::TagNames::dt,
  706. HTML::TagNames::embed,
  707. HTML::TagNames::fieldset,
  708. HTML::TagNames::figcaption,
  709. HTML::TagNames::figure,
  710. HTML::TagNames::footer,
  711. HTML::TagNames::form,
  712. HTML::TagNames::frame,
  713. HTML::TagNames::frameset,
  714. HTML::TagNames::h1,
  715. HTML::TagNames::h2,
  716. HTML::TagNames::h3,
  717. HTML::TagNames::h4,
  718. HTML::TagNames::h5,
  719. HTML::TagNames::h6,
  720. HTML::TagNames::head,
  721. HTML::TagNames::header,
  722. HTML::TagNames::hgroup,
  723. HTML::TagNames::hr,
  724. HTML::TagNames::html,
  725. HTML::TagNames::iframe,
  726. HTML::TagNames::img,
  727. HTML::TagNames::input,
  728. HTML::TagNames::keygen,
  729. HTML::TagNames::li,
  730. HTML::TagNames::link,
  731. HTML::TagNames::listing,
  732. HTML::TagNames::main,
  733. HTML::TagNames::marquee,
  734. HTML::TagNames::menu,
  735. HTML::TagNames::meta,
  736. HTML::TagNames::nav,
  737. HTML::TagNames::noembed,
  738. HTML::TagNames::noframes,
  739. HTML::TagNames::noscript,
  740. HTML::TagNames::object,
  741. HTML::TagNames::ol,
  742. HTML::TagNames::p,
  743. HTML::TagNames::param,
  744. HTML::TagNames::plaintext,
  745. HTML::TagNames::pre,
  746. HTML::TagNames::script,
  747. HTML::TagNames::section,
  748. HTML::TagNames::select,
  749. HTML::TagNames::source,
  750. HTML::TagNames::style,
  751. HTML::TagNames::summary,
  752. HTML::TagNames::table,
  753. HTML::TagNames::tbody,
  754. HTML::TagNames::td,
  755. HTML::TagNames::template_,
  756. HTML::TagNames::textarea,
  757. HTML::TagNames::tfoot,
  758. HTML::TagNames::th,
  759. HTML::TagNames::thead,
  760. HTML::TagNames::title,
  761. HTML::TagNames::tr,
  762. HTML::TagNames::track,
  763. HTML::TagNames::ul,
  764. HTML::TagNames::wbr,
  765. HTML::TagNames::xmp);
  766. }
  767. void HTMLDocumentParser::handle_in_body(HTMLToken& token)
  768. {
  769. if (token.is_character()) {
  770. if (token.codepoint() == 0) {
  771. PARSE_ERROR();
  772. return;
  773. }
  774. if (token.is_parser_whitespace()) {
  775. reconstruct_the_active_formatting_elements();
  776. insert_character(token.codepoint());
  777. return;
  778. }
  779. reconstruct_the_active_formatting_elements();
  780. insert_character(token.codepoint());
  781. m_frameset_ok = false;
  782. return;
  783. }
  784. if (token.is_comment()) {
  785. insert_comment(token);
  786. return;
  787. }
  788. if (token.is_doctype()) {
  789. PARSE_ERROR();
  790. return;
  791. }
  792. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  793. PARSE_ERROR();
  794. if (m_stack_of_open_elements.contains(HTML::TagNames::template_))
  795. return;
  796. for (auto& attribute : token.m_tag.attributes) {
  797. if (current_node().has_attribute(attribute.local_name_builder.string_view()))
  798. continue;
  799. current_node().set_attribute(attribute.local_name_builder.to_string(), attribute.value_builder.to_string());
  800. }
  801. return;
  802. }
  803. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::base, HTML::TagNames::basefont, HTML::TagNames::bgsound, HTML::TagNames::link, HTML::TagNames::meta, HTML::TagNames::noframes, HTML::TagNames::script, HTML::TagNames::style, HTML::TagNames::template_, HTML::TagNames::title)) {
  804. process_using_the_rules_for(InsertionMode::InHead, token);
  805. return;
  806. }
  807. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
  808. process_using_the_rules_for(InsertionMode::InHead, token);
  809. return;
  810. }
  811. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::body) {
  812. PARSE_ERROR();
  813. if (m_stack_of_open_elements.elements().size() == 1
  814. || node_before_current_node().tag_name() != HTML::TagNames::body
  815. || m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
  816. return;
  817. }
  818. m_frameset_ok = false;
  819. for (auto& attribute : token.m_tag.attributes) {
  820. if (node_before_current_node().has_attribute(attribute.local_name_builder.string_view()))
  821. continue;
  822. node_before_current_node().set_attribute(attribute.local_name_builder.to_string(), attribute.value_builder.to_string());
  823. }
  824. return;
  825. }
  826. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::frameset) {
  827. TODO();
  828. }
  829. if (token.is_end_of_file()) {
  830. // FIXME: If the stack of template insertion modes is not empty,
  831. // then process the token using the rules for the "in template" insertion mode.
  832. // FIXME: If there is a node in the stack of open elements that is not either
  833. // a dd element, a dt element, an li element, an optgroup element, an option element,
  834. // a p element, an rb element, an rp element, an rt element, an rtc element,
  835. // a tbody element, a td element, a tfoot element, a th element, a thead element,
  836. // a tr element, the body element, or the html element, then this is a parse error.
  837. stop_parsing();
  838. return;
  839. }
  840. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::body) {
  841. if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::body)) {
  842. PARSE_ERROR();
  843. return;
  844. }
  845. for (auto& node : m_stack_of_open_elements.elements()) {
  846. if (!node.tag_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt, HTML::TagNames::li, HTML::TagNames::optgroup, HTML::TagNames::option, HTML::TagNames::p, HTML::TagNames::rb, HTML::TagNames::rp, HTML::TagNames::rt, HTML::TagNames::rtc, HTML::TagNames::tbody, HTML::TagNames::td, HTML::TagNames::tfoot, HTML::TagNames::th, HTML::TagNames::thead, HTML::TagNames::tr, HTML::TagNames::body, HTML::TagNames::html)) {
  847. PARSE_ERROR();
  848. break;
  849. }
  850. }
  851. m_insertion_mode = InsertionMode::AfterBody;
  852. return;
  853. }
  854. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::html) {
  855. if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::body)) {
  856. PARSE_ERROR();
  857. return;
  858. }
  859. for (auto& node : m_stack_of_open_elements.elements()) {
  860. if (!node.tag_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt, HTML::TagNames::li, HTML::TagNames::optgroup, HTML::TagNames::option, HTML::TagNames::p, HTML::TagNames::rb, HTML::TagNames::rp, HTML::TagNames::rt, HTML::TagNames::rtc, HTML::TagNames::tbody, HTML::TagNames::td, HTML::TagNames::tfoot, HTML::TagNames::th, HTML::TagNames::thead, HTML::TagNames::tr, HTML::TagNames::body, HTML::TagNames::html)) {
  861. PARSE_ERROR();
  862. break;
  863. }
  864. }
  865. m_insertion_mode = InsertionMode::AfterBody;
  866. process_using_the_rules_for(m_insertion_mode, token);
  867. return;
  868. }
  869. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::address, HTML::TagNames::article, HTML::TagNames::aside, HTML::TagNames::blockquote, HTML::TagNames::center, HTML::TagNames::details, HTML::TagNames::dialog, HTML::TagNames::dir, HTML::TagNames::div, HTML::TagNames::dl, HTML::TagNames::fieldset, HTML::TagNames::figcaption, HTML::TagNames::figure, HTML::TagNames::footer, HTML::TagNames::header, HTML::TagNames::hgroup, HTML::TagNames::main, HTML::TagNames::menu, HTML::TagNames::nav, HTML::TagNames::ol, HTML::TagNames::p, HTML::TagNames::section, HTML::TagNames::summary, HTML::TagNames::ul)) {
  870. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  871. close_a_p_element();
  872. insert_html_element(token);
  873. return;
  874. }
  875. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::h1, HTML::TagNames::h2, HTML::TagNames::h3, HTML::TagNames::h4, HTML::TagNames::h5, HTML::TagNames::h6)) {
  876. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  877. close_a_p_element();
  878. if (current_node().tag_name().is_one_of(HTML::TagNames::h1, HTML::TagNames::h2, HTML::TagNames::h3, HTML::TagNames::h4, HTML::TagNames::h5, HTML::TagNames::h6)) {
  879. PARSE_ERROR();
  880. m_stack_of_open_elements.pop();
  881. }
  882. insert_html_element(token);
  883. return;
  884. }
  885. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::pre, HTML::TagNames::listing)) {
  886. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  887. close_a_p_element();
  888. insert_html_element(token);
  889. m_frameset_ok = false;
  890. // If the next token is a U+000A LINE FEED (LF) character token,
  891. // then ignore that token and move on to the next one.
  892. // (Newlines at the start of pre blocks are ignored as an authoring convenience.)
  893. auto next_token = m_tokenizer.next_token();
  894. if (next_token.has_value() && next_token.value().is_character() && next_token.value().codepoint() == '\n') {
  895. // Ignore it.
  896. } else {
  897. process_using_the_rules_for(m_insertion_mode, next_token.value());
  898. }
  899. return;
  900. }
  901. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::form) {
  902. if (m_form_element && !m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
  903. PARSE_ERROR();
  904. return;
  905. }
  906. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  907. close_a_p_element();
  908. auto element = insert_html_element(token);
  909. if (!m_stack_of_open_elements.contains(HTML::TagNames::template_))
  910. m_form_element = to<HTMLFormElement>(*element);
  911. return;
  912. }
  913. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::li) {
  914. m_frameset_ok = false;
  915. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  916. RefPtr<Element> node = m_stack_of_open_elements.elements()[i];
  917. if (node->tag_name() == HTML::TagNames::li) {
  918. generate_implied_end_tags(HTML::TagNames::li);
  919. if (current_node().tag_name() != HTML::TagNames::li) {
  920. PARSE_ERROR();
  921. }
  922. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::li);
  923. break;
  924. }
  925. if (is_special_tag(node->tag_name()) && !node->tag_name().is_one_of(HTML::TagNames::address, HTML::TagNames::div, HTML::TagNames::p))
  926. break;
  927. }
  928. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  929. close_a_p_element();
  930. insert_html_element(token);
  931. return;
  932. }
  933. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt)) {
  934. m_frameset_ok = false;
  935. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  936. RefPtr<Element> node = m_stack_of_open_elements.elements()[i];
  937. if (node->tag_name() == HTML::TagNames::dd) {
  938. generate_implied_end_tags(HTML::TagNames::dd);
  939. if (current_node().tag_name() != HTML::TagNames::dd) {
  940. PARSE_ERROR();
  941. }
  942. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::dd);
  943. break;
  944. }
  945. if (node->tag_name() == HTML::TagNames::dt) {
  946. generate_implied_end_tags(HTML::TagNames::dt);
  947. if (current_node().tag_name() != HTML::TagNames::dt) {
  948. PARSE_ERROR();
  949. }
  950. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::dt);
  951. break;
  952. }
  953. if (is_special_tag(node->tag_name()) && !node->tag_name().is_one_of(HTML::TagNames::address, HTML::TagNames::div, HTML::TagNames::p))
  954. break;
  955. }
  956. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  957. close_a_p_element();
  958. insert_html_element(token);
  959. return;
  960. }
  961. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::plaintext) {
  962. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  963. close_a_p_element();
  964. insert_html_element(token);
  965. m_tokenizer.switch_to({}, HTMLTokenizer::State::PLAINTEXT);
  966. return;
  967. }
  968. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::button) {
  969. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::button)) {
  970. PARSE_ERROR();
  971. generate_implied_end_tags();
  972. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::button);
  973. }
  974. reconstruct_the_active_formatting_elements();
  975. insert_html_element(token);
  976. m_frameset_ok = false;
  977. return;
  978. }
  979. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::address, HTML::TagNames::article, HTML::TagNames::aside, HTML::TagNames::blockquote, HTML::TagNames::button, HTML::TagNames::center, HTML::TagNames::details, HTML::TagNames::dialog, HTML::TagNames::dir, HTML::TagNames::div, HTML::TagNames::dl, HTML::TagNames::fieldset, HTML::TagNames::figcaption, HTML::TagNames::figure, HTML::TagNames::footer, HTML::TagNames::header, HTML::TagNames::hgroup, HTML::TagNames::listing, HTML::TagNames::main, HTML::TagNames::menu, HTML::TagNames::nav, HTML::TagNames::ol, HTML::TagNames::pre, HTML::TagNames::section, HTML::TagNames::summary, HTML::TagNames::ul)) {
  980. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  981. PARSE_ERROR();
  982. return;
  983. }
  984. generate_implied_end_tags();
  985. if (current_node().tag_name() != token.tag_name()) {
  986. PARSE_ERROR();
  987. }
  988. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  989. return;
  990. }
  991. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::form) {
  992. if (!m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
  993. auto node = m_form_element;
  994. m_form_element = nullptr;
  995. if (!node || !m_stack_of_open_elements.has_in_scope(*node)) {
  996. PARSE_ERROR();
  997. return;
  998. }
  999. generate_implied_end_tags();
  1000. if (&current_node() != node) {
  1001. PARSE_ERROR();
  1002. }
  1003. m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) { return entry.ptr() == node.ptr(); });
  1004. } else {
  1005. if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::form)) {
  1006. PARSE_ERROR();
  1007. return;
  1008. }
  1009. generate_implied_end_tags();
  1010. if (current_node().tag_name() != HTML::TagNames::form) {
  1011. PARSE_ERROR();
  1012. }
  1013. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::form);
  1014. }
  1015. return;
  1016. }
  1017. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::p) {
  1018. if (!m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p)) {
  1019. PARSE_ERROR();
  1020. HTMLToken fake_p_token;
  1021. fake_p_token.m_type = HTMLToken::Type::StartTag;
  1022. fake_p_token.m_tag.tag_name.append(HTML::TagNames::p);
  1023. insert_html_element(fake_p_token);
  1024. }
  1025. close_a_p_element();
  1026. return;
  1027. }
  1028. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::li) {
  1029. if (!m_stack_of_open_elements.has_in_list_item_scope(HTML::TagNames::li)) {
  1030. PARSE_ERROR();
  1031. return;
  1032. }
  1033. generate_implied_end_tags(HTML::TagNames::li);
  1034. if (current_node().tag_name() != HTML::TagNames::li) {
  1035. PARSE_ERROR();
  1036. dbg() << "Expected <li> current node, but had <" << current_node().tag_name() << ">";
  1037. }
  1038. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::li);
  1039. return;
  1040. }
  1041. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt)) {
  1042. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  1043. PARSE_ERROR();
  1044. return;
  1045. }
  1046. generate_implied_end_tags(token.tag_name());
  1047. if (current_node().tag_name() != token.tag_name()) {
  1048. PARSE_ERROR();
  1049. }
  1050. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1051. return;
  1052. }
  1053. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::h1, HTML::TagNames::h2, HTML::TagNames::h3, HTML::TagNames::h4, HTML::TagNames::h5, HTML::TagNames::h6)) {
  1054. if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::h1)
  1055. && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h2)
  1056. && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h3)
  1057. && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h4)
  1058. && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h5)
  1059. && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h6)) {
  1060. PARSE_ERROR();
  1061. return;
  1062. }
  1063. generate_implied_end_tags();
  1064. if (current_node().tag_name() != token.tag_name()) {
  1065. PARSE_ERROR();
  1066. }
  1067. for (;;) {
  1068. auto popped_element = m_stack_of_open_elements.pop();
  1069. if (popped_element->tag_name().is_one_of(HTML::TagNames::h1, HTML::TagNames::h2, HTML::TagNames::h3, HTML::TagNames::h4, HTML::TagNames::h5, HTML::TagNames::h6))
  1070. break;
  1071. }
  1072. return;
  1073. }
  1074. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::a) {
  1075. if (auto* element = m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker(HTML::TagNames::a)) {
  1076. PARSE_ERROR();
  1077. if (run_the_adoption_agency_algorithm(token) == AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps)
  1078. goto AnyOtherEndTag;
  1079. m_list_of_active_formatting_elements.remove(*element);
  1080. m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) {
  1081. return entry.ptr() == element;
  1082. });
  1083. }
  1084. reconstruct_the_active_formatting_elements();
  1085. auto element = insert_html_element(token);
  1086. m_list_of_active_formatting_elements.add(*element);
  1087. return;
  1088. }
  1089. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::b, HTML::TagNames::big, HTML::TagNames::code, HTML::TagNames::em, HTML::TagNames::font, HTML::TagNames::i, HTML::TagNames::s, HTML::TagNames::small, HTML::TagNames::strike, HTML::TagNames::strong, HTML::TagNames::tt, HTML::TagNames::u)) {
  1090. reconstruct_the_active_formatting_elements();
  1091. auto element = insert_html_element(token);
  1092. m_list_of_active_formatting_elements.add(*element);
  1093. return;
  1094. }
  1095. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::nobr) {
  1096. reconstruct_the_active_formatting_elements();
  1097. if (m_stack_of_open_elements.has_in_scope(HTML::TagNames::nobr)) {
  1098. PARSE_ERROR();
  1099. run_the_adoption_agency_algorithm(token);
  1100. reconstruct_the_active_formatting_elements();
  1101. }
  1102. auto element = insert_html_element(token);
  1103. m_list_of_active_formatting_elements.add(*element);
  1104. return;
  1105. }
  1106. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::a, HTML::TagNames::b, HTML::TagNames::big, HTML::TagNames::code, HTML::TagNames::em, HTML::TagNames::font, HTML::TagNames::i, HTML::TagNames::nobr, HTML::TagNames::s, HTML::TagNames::small, HTML::TagNames::strike, HTML::TagNames::strong, HTML::TagNames::tt, HTML::TagNames::u)) {
  1107. if (run_the_adoption_agency_algorithm(token) == AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps)
  1108. goto AnyOtherEndTag;
  1109. return;
  1110. }
  1111. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::applet, HTML::TagNames::marquee, HTML::TagNames::object)) {
  1112. reconstruct_the_active_formatting_elements();
  1113. insert_html_element(token);
  1114. m_list_of_active_formatting_elements.add_marker();
  1115. m_frameset_ok = false;
  1116. return;
  1117. }
  1118. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::applet, HTML::TagNames::marquee, HTML::TagNames::object)) {
  1119. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  1120. PARSE_ERROR();
  1121. return;
  1122. }
  1123. generate_implied_end_tags();
  1124. if (current_node().tag_name() != token.tag_name()) {
  1125. PARSE_ERROR();
  1126. }
  1127. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1128. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1129. return;
  1130. }
  1131. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::table) {
  1132. if (!document().in_quirks_mode()) {
  1133. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  1134. close_a_p_element();
  1135. }
  1136. insert_html_element(token);
  1137. m_frameset_ok = false;
  1138. m_insertion_mode = InsertionMode::InTable;
  1139. return;
  1140. }
  1141. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::br) {
  1142. token.drop_attributes();
  1143. goto BRStartTag;
  1144. }
  1145. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::area, HTML::TagNames::br, HTML::TagNames::embed, HTML::TagNames::img, HTML::TagNames::keygen, HTML::TagNames::wbr)) {
  1146. BRStartTag:
  1147. reconstruct_the_active_formatting_elements();
  1148. insert_html_element(token);
  1149. m_stack_of_open_elements.pop();
  1150. token.acknowledge_self_closing_flag_if_set();
  1151. m_frameset_ok = false;
  1152. return;
  1153. }
  1154. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::input) {
  1155. reconstruct_the_active_formatting_elements();
  1156. insert_html_element(token);
  1157. m_stack_of_open_elements.pop();
  1158. token.acknowledge_self_closing_flag_if_set();
  1159. auto type_attribute = token.attribute(HTML::AttributeNames::type);
  1160. if (type_attribute.is_null() || !type_attribute.equals_ignoring_case("hidden")) {
  1161. m_frameset_ok = false;
  1162. }
  1163. return;
  1164. }
  1165. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::param, HTML::TagNames::source, HTML::TagNames::track)) {
  1166. insert_html_element(token);
  1167. m_stack_of_open_elements.pop();
  1168. token.acknowledge_self_closing_flag_if_set();
  1169. return;
  1170. }
  1171. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::hr) {
  1172. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  1173. close_a_p_element();
  1174. insert_html_element(token);
  1175. m_stack_of_open_elements.pop();
  1176. token.acknowledge_self_closing_flag_if_set();
  1177. m_frameset_ok = false;
  1178. return;
  1179. }
  1180. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::image) {
  1181. // Parse error. Change the token's tag name to HTML::TagNames::img and reprocess it. (Don't ask.)
  1182. PARSE_ERROR();
  1183. token.m_tag.tag_name.clear();
  1184. token.m_tag.tag_name.append(HTML::TagNames::img);
  1185. process_using_the_rules_for(m_insertion_mode, token);
  1186. return;
  1187. }
  1188. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::textarea) {
  1189. insert_html_element(token);
  1190. m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
  1191. // If the next token is a U+000A LINE FEED (LF) character token,
  1192. // then ignore that token and move on to the next one.
  1193. // (Newlines at the start of pre blocks are ignored as an authoring convenience.)
  1194. auto next_token = m_tokenizer.next_token();
  1195. m_original_insertion_mode = m_insertion_mode;
  1196. m_frameset_ok = false;
  1197. m_insertion_mode = InsertionMode::Text;
  1198. if (next_token.has_value() && next_token.value().is_character() && next_token.value().codepoint() == '\n') {
  1199. // Ignore it.
  1200. } else {
  1201. process_using_the_rules_for(m_insertion_mode, next_token.value());
  1202. }
  1203. return;
  1204. }
  1205. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::xmp) {
  1206. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p)) {
  1207. close_a_p_element();
  1208. }
  1209. reconstruct_the_active_formatting_elements();
  1210. m_frameset_ok = false;
  1211. parse_generic_raw_text_element(token);
  1212. return;
  1213. }
  1214. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::iframe) {
  1215. m_frameset_ok = false;
  1216. parse_generic_raw_text_element(token);
  1217. return;
  1218. }
  1219. if (token.is_start_tag() && ((token.tag_name() == HTML::TagNames::noembed) || (token.tag_name() == HTML::TagNames::noscript && m_scripting_enabled))) {
  1220. parse_generic_raw_text_element(token);
  1221. return;
  1222. }
  1223. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::select) {
  1224. reconstruct_the_active_formatting_elements();
  1225. insert_html_element(token);
  1226. m_frameset_ok = false;
  1227. switch (m_insertion_mode) {
  1228. case InsertionMode::InTable:
  1229. case InsertionMode::InCaption:
  1230. case InsertionMode::InTableBody:
  1231. case InsertionMode::InRow:
  1232. case InsertionMode::InCell:
  1233. m_insertion_mode = InsertionMode::InSelectInTable;
  1234. break;
  1235. default:
  1236. m_insertion_mode = InsertionMode::InSelect;
  1237. break;
  1238. }
  1239. return;
  1240. }
  1241. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::optgroup, HTML::TagNames::option)) {
  1242. if (current_node().tag_name() == HTML::TagNames::option)
  1243. m_stack_of_open_elements.pop();
  1244. reconstruct_the_active_formatting_elements();
  1245. insert_html_element(token);
  1246. return;
  1247. }
  1248. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::rb, HTML::TagNames::rtc)) {
  1249. if (m_stack_of_open_elements.has_in_scope(HTML::TagNames::ruby))
  1250. generate_implied_end_tags();
  1251. if (current_node().tag_name() != HTML::TagNames::ruby)
  1252. PARSE_ERROR();
  1253. insert_html_element(token);
  1254. return;
  1255. }
  1256. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::rp, HTML::TagNames::rt)) {
  1257. if (m_stack_of_open_elements.has_in_scope(HTML::TagNames::ruby))
  1258. generate_implied_end_tags(HTML::TagNames::rtc);
  1259. if (current_node().tag_name() != HTML::TagNames::rtc || current_node().tag_name() != HTML::TagNames::ruby)
  1260. PARSE_ERROR();
  1261. insert_html_element(token);
  1262. return;
  1263. }
  1264. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::math) {
  1265. dbg() << "<math> element encountered.";
  1266. reconstruct_the_active_formatting_elements();
  1267. adjust_mathml_attributes(token);
  1268. adjust_foreign_attributes(token);
  1269. // FIXME: this should insert a foreign element, but lets just insert it normally for now :^)
  1270. insert_html_element(token);
  1271. if (token.is_self_closing()) {
  1272. m_stack_of_open_elements.pop();
  1273. token.acknowledge_self_closing_flag_if_set();
  1274. }
  1275. return;
  1276. }
  1277. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::svg) {
  1278. dbg() << "<svg> element encountered.";
  1279. reconstruct_the_active_formatting_elements();
  1280. adjust_svg_attributes(token);
  1281. adjust_foreign_attributes(token);
  1282. // FIXME: this should insert a foreign element, but lets just insert it normally for now :^)
  1283. insert_html_element(token);
  1284. if (token.is_self_closing()) {
  1285. m_stack_of_open_elements.pop();
  1286. token.acknowledge_self_closing_flag_if_set();
  1287. }
  1288. return;
  1289. }
  1290. if ((token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::caption, HTML::TagNames::col, HTML::TagNames::colgroup, HTML::TagNames::frame, HTML::TagNames::head, HTML::TagNames::tbody, HTML::TagNames::td, HTML::TagNames::tfoot, HTML::TagNames::th, HTML::TagNames::thead, HTML::TagNames::tr))) {
  1291. PARSE_ERROR();
  1292. return;
  1293. }
  1294. // Any other start tag
  1295. if (token.is_start_tag()) {
  1296. reconstruct_the_active_formatting_elements();
  1297. insert_html_element(token);
  1298. return;
  1299. }
  1300. if (token.is_end_tag()) {
  1301. AnyOtherEndTag:
  1302. RefPtr<Element> node;
  1303. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1304. node = m_stack_of_open_elements.elements()[i];
  1305. if (node->tag_name() == token.tag_name()) {
  1306. generate_implied_end_tags(token.tag_name());
  1307. if (node != current_node()) {
  1308. PARSE_ERROR();
  1309. }
  1310. while (&current_node() != node) {
  1311. m_stack_of_open_elements.pop();
  1312. }
  1313. m_stack_of_open_elements.pop();
  1314. break;
  1315. }
  1316. if (is_special_tag(node->tag_name())) {
  1317. PARSE_ERROR();
  1318. return;
  1319. }
  1320. }
  1321. return;
  1322. }
  1323. }
  1324. void HTMLDocumentParser::adjust_mathml_attributes(HTMLToken& token)
  1325. {
  1326. token.adjust_attribute_name("definitionurl", "definitionURL");
  1327. }
  1328. void HTMLDocumentParser::adjust_svg_attributes(HTMLToken& token)
  1329. {
  1330. token.adjust_attribute_name("attributename", "attributeName");
  1331. token.adjust_attribute_name("attributetype", "attributeType");
  1332. token.adjust_attribute_name("basefrequency", "baseFrequency");
  1333. token.adjust_attribute_name("baseprofile", "baseProfile");
  1334. token.adjust_attribute_name("calcmode", "calcMode");
  1335. token.adjust_attribute_name("clippathunits", "clipPathUnits");
  1336. token.adjust_attribute_name("diffuseconstant", "diffuseConstant");
  1337. token.adjust_attribute_name("edgemode", "edgeMode");
  1338. token.adjust_attribute_name("filterunits", "filterUnits");
  1339. token.adjust_attribute_name("glyphref", "glyphRef");
  1340. token.adjust_attribute_name("gradienttransform", "gradientTransform");
  1341. token.adjust_attribute_name("gradientunits", "gradientUnits");
  1342. token.adjust_attribute_name("kernelmatrix", "kernelMatrix");
  1343. token.adjust_attribute_name("kernelunitlength", "kernelUnitLength");
  1344. token.adjust_attribute_name("keypoints", "keyPoints");
  1345. token.adjust_attribute_name("keysplines", "keySplines");
  1346. token.adjust_attribute_name("keytimes", "keyTimes");
  1347. token.adjust_attribute_name("lengthadjust", "lengthAdjust");
  1348. token.adjust_attribute_name("limitingconeangle", "limitingConeAngle");
  1349. token.adjust_attribute_name("markerheight", "markerHeight");
  1350. token.adjust_attribute_name("markerunits", "markerUnits");
  1351. token.adjust_attribute_name("markerwidth", "markerWidth");
  1352. token.adjust_attribute_name("maskcontentunits", "maskContentUnits");
  1353. token.adjust_attribute_name("maskunits", "maskUnits");
  1354. token.adjust_attribute_name("numoctaves", "numOctaves");
  1355. token.adjust_attribute_name("pathlength", "pathLength");
  1356. token.adjust_attribute_name("patterncontentunits", "patternContentUnits");
  1357. token.adjust_attribute_name("patterntransform", "patternTransform");
  1358. token.adjust_attribute_name("patternunits", "patternUnits");
  1359. token.adjust_attribute_name("pointsatx", "pointsAtX");
  1360. token.adjust_attribute_name("pointsaty", "pointsAtY");
  1361. token.adjust_attribute_name("pointsatz", "pointsAtZ");
  1362. token.adjust_attribute_name("preservealpha", "preserveAlpha");
  1363. token.adjust_attribute_name("preserveaspectratio", "preserveAspectRatio");
  1364. token.adjust_attribute_name("primitiveunits", "primitiveUnits");
  1365. token.adjust_attribute_name("refx", "refX");
  1366. token.adjust_attribute_name("refy", "refY");
  1367. token.adjust_attribute_name("repeatcount", "repeatCount");
  1368. token.adjust_attribute_name("repeatdur", "repeatDur");
  1369. token.adjust_attribute_name("requiredextensions", "requiredExtensions");
  1370. token.adjust_attribute_name("requiredfeatures", "requiredFeatures");
  1371. token.adjust_attribute_name("specularconstant", "specularConstant");
  1372. token.adjust_attribute_name("specularexponent", "specularExponent");
  1373. token.adjust_attribute_name("spreadmethod", "spreadMethod");
  1374. token.adjust_attribute_name("startoffset", "startOffset");
  1375. token.adjust_attribute_name("stddeviation", "stdDeviation");
  1376. token.adjust_attribute_name("stitchtiles", "stitchTiles");
  1377. token.adjust_attribute_name("surfacescale", "surfaceScale");
  1378. token.adjust_attribute_name("systemlanguage", "systemLanguage");
  1379. token.adjust_attribute_name("tablevalues", "tableValues");
  1380. token.adjust_attribute_name("targetx", "targetX");
  1381. token.adjust_attribute_name("targety", "targetY");
  1382. token.adjust_attribute_name("textlength", "textLength");
  1383. token.adjust_attribute_name("viewbox", "viewBox");
  1384. token.adjust_attribute_name("viewtarget", "viewTarget");
  1385. token.adjust_attribute_name("xchannelselector", "xChannelSelector");
  1386. token.adjust_attribute_name("ychannelselector", "yChannelSelector");
  1387. token.adjust_attribute_name("zoomandpan", "zoomAndPan");
  1388. }
  1389. void HTMLDocumentParser::adjust_foreign_attributes(HTMLToken& token)
  1390. {
  1391. auto xlink_namespace = "http://www.w3.org/1999/xlink";
  1392. token.adjust_foreign_attribute("xlink:actuate", "xlink", "actuate", xlink_namespace);
  1393. token.adjust_foreign_attribute("xlink:arcrole", "xlink", "arcrole", xlink_namespace);
  1394. token.adjust_foreign_attribute("xlink:href", "xlink", "href", xlink_namespace);
  1395. token.adjust_foreign_attribute("xlink:role", "xlink", "role", xlink_namespace);
  1396. token.adjust_foreign_attribute("xlink:show", "xlink", "show", xlink_namespace);
  1397. token.adjust_foreign_attribute("xlink:title", "xlink", "title", xlink_namespace);
  1398. token.adjust_foreign_attribute("xlink:type", "xlink", "type", xlink_namespace);
  1399. auto xml_namespace = "http://www.w3.org/XML/1998/namespace";
  1400. token.adjust_foreign_attribute("xml:lang", "xml", "lang", xml_namespace);
  1401. token.adjust_foreign_attribute("xml:space", "xml", "space", xml_namespace);
  1402. auto xmlns_namespace = "http://www.w3.org/2000/xmlns/";
  1403. token.adjust_foreign_attribute("xmlns", "", "xmlns", xmlns_namespace);
  1404. token.adjust_foreign_attribute("xmlns:xlink", "xmlns", "xlink", xmlns_namespace);
  1405. }
  1406. void HTMLDocumentParser::increment_script_nesting_level()
  1407. {
  1408. ++m_script_nesting_level;
  1409. }
  1410. void HTMLDocumentParser::decrement_script_nesting_level()
  1411. {
  1412. ASSERT(m_script_nesting_level);
  1413. --m_script_nesting_level;
  1414. }
  1415. void HTMLDocumentParser::handle_text(HTMLToken& token)
  1416. {
  1417. if (token.is_character()) {
  1418. insert_character(token.codepoint());
  1419. return;
  1420. }
  1421. if (token.is_end_of_file()) {
  1422. PARSE_ERROR();
  1423. if (current_node().tag_name() == HTML::TagNames::script)
  1424. to<HTMLScriptElement>(current_node()).set_already_started({}, true);
  1425. m_stack_of_open_elements.pop();
  1426. m_insertion_mode = m_original_insertion_mode;
  1427. process_using_the_rules_for(m_insertion_mode, token);
  1428. return;
  1429. }
  1430. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::script) {
  1431. // Make sure the <script> element has up-to-date text content before preparing the script.
  1432. flush_character_insertions();
  1433. NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
  1434. m_stack_of_open_elements.pop();
  1435. m_insertion_mode = m_original_insertion_mode;
  1436. // FIXME: Handle tokenizer insertion point stuff here.
  1437. increment_script_nesting_level();
  1438. script->prepare_script({});
  1439. decrement_script_nesting_level();
  1440. if (script_nesting_level() == 0)
  1441. m_parser_pause_flag = false;
  1442. // FIXME: Handle tokenizer insertion point stuff here too.
  1443. while (document().pending_parsing_blocking_script()) {
  1444. if (script_nesting_level() != 0) {
  1445. m_parser_pause_flag = true;
  1446. // FIXME: Abort the processing of any nested invocations of the tokenizer,
  1447. // yielding control back to the caller. (Tokenization will resume when
  1448. // the caller returns to the "outer" tree construction stage.)
  1449. TODO();
  1450. } else {
  1451. auto the_script = document().take_pending_parsing_blocking_script({});
  1452. m_tokenizer.set_blocked(true);
  1453. // FIXME: If the parser's Document has a style sheet that is blocking scripts
  1454. // or the script's "ready to be parser-executed" flag is not set:
  1455. // spin the event loop until the parser's Document has no style sheet
  1456. // that is blocking scripts and the script's "ready to be parser-executed"
  1457. // flag is set.
  1458. if (the_script->failed_to_load())
  1459. return;
  1460. ASSERT(the_script->is_ready_to_be_parser_executed());
  1461. if (m_aborted)
  1462. return;
  1463. m_tokenizer.set_blocked(false);
  1464. // FIXME: Handle tokenizer insertion point stuff here too.
  1465. ASSERT(script_nesting_level() == 0);
  1466. increment_script_nesting_level();
  1467. the_script->execute_script();
  1468. decrement_script_nesting_level();
  1469. ASSERT(script_nesting_level() == 0);
  1470. m_parser_pause_flag = false;
  1471. // FIXME: Handle tokenizer insertion point stuff here too.
  1472. }
  1473. }
  1474. return;
  1475. }
  1476. if (token.is_end_tag()) {
  1477. m_stack_of_open_elements.pop();
  1478. m_insertion_mode = m_original_insertion_mode;
  1479. return;
  1480. }
  1481. TODO();
  1482. }
  1483. void HTMLDocumentParser::clear_the_stack_back_to_a_table_context()
  1484. {
  1485. while (!current_node().tag_name().is_one_of(HTML::TagNames::table, HTML::TagNames::template_, HTML::TagNames::html))
  1486. m_stack_of_open_elements.pop();
  1487. }
  1488. void HTMLDocumentParser::clear_the_stack_back_to_a_table_row_context()
  1489. {
  1490. while (!current_node().tag_name().is_one_of(HTML::TagNames::tr, HTML::TagNames::template_, HTML::TagNames::html))
  1491. m_stack_of_open_elements.pop();
  1492. }
  1493. void HTMLDocumentParser::clear_the_stack_back_to_a_table_body_context()
  1494. {
  1495. while (!current_node().tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead, HTML::TagNames::template_, HTML::TagNames::html))
  1496. m_stack_of_open_elements.pop();
  1497. }
  1498. void HTMLDocumentParser::handle_in_row(HTMLToken& token)
  1499. {
  1500. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::th, HTML::TagNames::td)) {
  1501. clear_the_stack_back_to_a_table_row_context();
  1502. insert_html_element(token);
  1503. m_insertion_mode = InsertionMode::InCell;
  1504. m_list_of_active_formatting_elements.add_marker();
  1505. return;
  1506. }
  1507. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::tr) {
  1508. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tr)) {
  1509. PARSE_ERROR();
  1510. return;
  1511. }
  1512. clear_the_stack_back_to_a_table_row_context();
  1513. m_stack_of_open_elements.pop();
  1514. m_insertion_mode = InsertionMode::InTableBody;
  1515. return;
  1516. }
  1517. if ((token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::caption, HTML::TagNames::col, HTML::TagNames::colgroup, HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead, HTML::TagNames::tr))
  1518. || (token.is_end_tag() && token.tag_name() == HTML::TagNames::table)) {
  1519. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tr)) {
  1520. PARSE_ERROR();
  1521. return;
  1522. }
  1523. clear_the_stack_back_to_a_table_row_context();
  1524. m_stack_of_open_elements.pop();
  1525. m_insertion_mode = InsertionMode::InTableBody;
  1526. process_using_the_rules_for(m_insertion_mode, token);
  1527. return;
  1528. }
  1529. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead)) {
  1530. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1531. PARSE_ERROR();
  1532. return;
  1533. }
  1534. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tr)) {
  1535. return;
  1536. }
  1537. clear_the_stack_back_to_a_table_row_context();
  1538. m_stack_of_open_elements.pop();
  1539. m_insertion_mode = InsertionMode::InTableBody;
  1540. process_using_the_rules_for(m_insertion_mode, token);
  1541. return;
  1542. }
  1543. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::body, HTML::TagNames::caption, HTML::TagNames::col, HTML::TagNames::colgroup, HTML::TagNames::html, HTML::TagNames::td, HTML::TagNames::th)) {
  1544. PARSE_ERROR();
  1545. return;
  1546. }
  1547. process_using_the_rules_for(InsertionMode::InTable, token);
  1548. }
  1549. void HTMLDocumentParser::close_the_cell()
  1550. {
  1551. generate_implied_end_tags();
  1552. if (!current_node().tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
  1553. PARSE_ERROR();
  1554. }
  1555. while (!current_node().tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th))
  1556. m_stack_of_open_elements.pop();
  1557. m_stack_of_open_elements.pop();
  1558. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1559. m_insertion_mode = InsertionMode::InRow;
  1560. }
  1561. void HTMLDocumentParser::handle_in_cell(HTMLToken& token)
  1562. {
  1563. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
  1564. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1565. PARSE_ERROR();
  1566. return;
  1567. }
  1568. generate_implied_end_tags();
  1569. if (current_node().tag_name() != token.tag_name()) {
  1570. PARSE_ERROR();
  1571. }
  1572. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1573. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1574. m_insertion_mode = InsertionMode::InRow;
  1575. return;
  1576. }
  1577. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::caption, HTML::TagNames::col, HTML::TagNames::colgroup, HTML::TagNames::tbody, HTML::TagNames::td, HTML::TagNames::tfoot, HTML::TagNames::th, HTML::TagNames::thead, HTML::TagNames::tr)) {
  1578. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::td) && m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::th)) {
  1579. PARSE_ERROR();
  1580. return;
  1581. }
  1582. close_the_cell();
  1583. process_using_the_rules_for(m_insertion_mode, token);
  1584. return;
  1585. }
  1586. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::body, HTML::TagNames::caption, HTML::TagNames::col, HTML::TagNames::colgroup, HTML::TagNames::html)) {
  1587. PARSE_ERROR();
  1588. return;
  1589. }
  1590. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::table, HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead, HTML::TagNames::tr)) {
  1591. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1592. PARSE_ERROR();
  1593. return;
  1594. }
  1595. close_the_cell();
  1596. // Reprocess the token.
  1597. process_using_the_rules_for(m_insertion_mode, token);
  1598. return;
  1599. }
  1600. process_using_the_rules_for(InsertionMode::InBody, token);
  1601. }
  1602. void HTMLDocumentParser::handle_in_table_text(HTMLToken& token)
  1603. {
  1604. if (token.is_character()) {
  1605. if (token.codepoint() == 0) {
  1606. PARSE_ERROR();
  1607. return;
  1608. }
  1609. m_pending_table_character_tokens.append(token);
  1610. return;
  1611. }
  1612. for (auto& pending_token : m_pending_table_character_tokens) {
  1613. ASSERT(pending_token.is_character());
  1614. if (!pending_token.is_parser_whitespace()) {
  1615. // FIXME: If any of the tokens in the pending table character tokens list
  1616. // are character tokens that are not ASCII whitespace, then this is a parse error:
  1617. // reprocess the character tokens in the pending table character tokens list using
  1618. // the rules given in the "anything else" entry in the "in table" insertion mode.
  1619. TODO();
  1620. }
  1621. }
  1622. for (auto& pending_token : m_pending_table_character_tokens) {
  1623. insert_character(pending_token.codepoint());
  1624. }
  1625. m_insertion_mode = m_original_insertion_mode;
  1626. process_using_the_rules_for(m_insertion_mode, token);
  1627. }
  1628. void HTMLDocumentParser::handle_in_table_body(HTMLToken& token)
  1629. {
  1630. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::tr) {
  1631. clear_the_stack_back_to_a_table_body_context();
  1632. insert_html_element(token);
  1633. m_insertion_mode = InsertionMode::InRow;
  1634. return;
  1635. }
  1636. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::th, HTML::TagNames::td)) {
  1637. PARSE_ERROR();
  1638. clear_the_stack_back_to_a_table_body_context();
  1639. HTMLToken fake_tr_token;
  1640. fake_tr_token.m_type = HTMLToken::Type::StartTag;
  1641. fake_tr_token.m_tag.tag_name.append(HTML::TagNames::tr);
  1642. insert_html_element(fake_tr_token);
  1643. m_insertion_mode = InsertionMode::InRow;
  1644. process_using_the_rules_for(m_insertion_mode, token);
  1645. return;
  1646. }
  1647. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead)) {
  1648. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1649. PARSE_ERROR();
  1650. return;
  1651. }
  1652. clear_the_stack_back_to_a_table_body_context();
  1653. m_stack_of_open_elements.pop();
  1654. m_insertion_mode = InsertionMode::InTable;
  1655. return;
  1656. }
  1657. if ((token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::caption, HTML::TagNames::col, HTML::TagNames::colgroup, HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead))
  1658. || (token.is_end_tag() && token.tag_name() == HTML::TagNames::table)) {
  1659. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tbody)
  1660. && !m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::thead)
  1661. && !m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tfoot)) {
  1662. PARSE_ERROR();
  1663. return;
  1664. }
  1665. clear_the_stack_back_to_a_table_body_context();
  1666. m_stack_of_open_elements.pop();
  1667. m_insertion_mode = InsertionMode::InTable;
  1668. process_using_the_rules_for(InsertionMode::InTable, token);
  1669. return;
  1670. }
  1671. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::body, HTML::TagNames::caption, HTML::TagNames::col, HTML::TagNames::colgroup, HTML::TagNames::html, HTML::TagNames::td, HTML::TagNames::th, HTML::TagNames::tr)) {
  1672. PARSE_ERROR();
  1673. return;
  1674. }
  1675. process_using_the_rules_for(InsertionMode::InTable, token);
  1676. }
  1677. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  1678. {
  1679. if (token.is_character() && current_node().tag_name().is_one_of(HTML::TagNames::table, HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead, HTML::TagNames::tr)) {
  1680. m_pending_table_character_tokens.clear();
  1681. m_original_insertion_mode = m_insertion_mode;
  1682. m_insertion_mode = InsertionMode::InTableText;
  1683. process_using_the_rules_for(InsertionMode::InTableText, token);
  1684. return;
  1685. }
  1686. if (token.is_comment()) {
  1687. insert_comment(token);
  1688. return;
  1689. }
  1690. if (token.is_doctype()) {
  1691. PARSE_ERROR();
  1692. return;
  1693. }
  1694. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::caption) {
  1695. clear_the_stack_back_to_a_table_context();
  1696. m_list_of_active_formatting_elements.add_marker();
  1697. insert_html_element(token);
  1698. m_insertion_mode = InsertionMode::InCaption;
  1699. return;
  1700. }
  1701. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::colgroup) {
  1702. clear_the_stack_back_to_a_table_context();
  1703. insert_html_element(token);
  1704. m_insertion_mode = InsertionMode::InColumnGroup;
  1705. return;
  1706. }
  1707. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::col) {
  1708. clear_the_stack_back_to_a_table_context();
  1709. HTMLToken fake_colgroup_token;
  1710. fake_colgroup_token.m_type = HTMLToken::Type::StartTag;
  1711. fake_colgroup_token.m_tag.tag_name.append(HTML::TagNames::colgroup);
  1712. insert_html_element(fake_colgroup_token);
  1713. m_insertion_mode = InsertionMode::InColumnGroup;
  1714. process_using_the_rules_for(m_insertion_mode, token);
  1715. return;
  1716. }
  1717. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead)) {
  1718. clear_the_stack_back_to_a_table_context();
  1719. insert_html_element(token);
  1720. m_insertion_mode = InsertionMode::InTableBody;
  1721. return;
  1722. }
  1723. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th, HTML::TagNames::tr)) {
  1724. clear_the_stack_back_to_a_table_context();
  1725. HTMLToken fake_tbody_token;
  1726. fake_tbody_token.m_type = HTMLToken::Type::StartTag;
  1727. fake_tbody_token.m_tag.tag_name.append(HTML::TagNames::tbody);
  1728. insert_html_element(fake_tbody_token);
  1729. m_insertion_mode = InsertionMode::InTableBody;
  1730. process_using_the_rules_for(m_insertion_mode, token);
  1731. return;
  1732. }
  1733. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::table) {
  1734. PARSE_ERROR();
  1735. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::table))
  1736. return;
  1737. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::table);
  1738. reset_the_insertion_mode_appropriately();
  1739. process_using_the_rules_for(m_insertion_mode, token);
  1740. return;
  1741. }
  1742. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::table) {
  1743. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::table)) {
  1744. PARSE_ERROR();
  1745. return;
  1746. }
  1747. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::table);
  1748. reset_the_insertion_mode_appropriately();
  1749. return;
  1750. }
  1751. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::body, HTML::TagNames::caption, HTML::TagNames::col, HTML::TagNames::colgroup, HTML::TagNames::html, HTML::TagNames::tbody, HTML::TagNames::td, HTML::TagNames::tfoot, HTML::TagNames::th, HTML::TagNames::thead, HTML::TagNames::tr)) {
  1752. PARSE_ERROR();
  1753. return;
  1754. }
  1755. if ((token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::style, HTML::TagNames::script, HTML::TagNames::template_))
  1756. || (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_)) {
  1757. process_using_the_rules_for(InsertionMode::InHead, token);
  1758. return;
  1759. }
  1760. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::input) {
  1761. auto type_attribute = token.attribute(HTML::AttributeNames::type);
  1762. if (type_attribute.is_null() || !type_attribute.equals_ignoring_case("hidden")) {
  1763. goto AnythingElse;
  1764. }
  1765. PARSE_ERROR();
  1766. insert_html_element(token);
  1767. // FIXME: Is this the correct interpretation of "Pop that input element off the stack of open elements."?
  1768. // Because this wording is the first time it's seen in the spec.
  1769. // Other times it's worded as: "Immediately pop the current node off the stack of open elements."
  1770. m_stack_of_open_elements.pop();
  1771. token.acknowledge_self_closing_flag_if_set();
  1772. return;
  1773. }
  1774. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::form) {
  1775. PARSE_ERROR();
  1776. if (m_form_element || m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
  1777. return;
  1778. }
  1779. m_form_element = to<HTMLFormElement>(insert_html_element(token));
  1780. // FIXME: See previous FIXME, as this is the same situation but for form.
  1781. m_stack_of_open_elements.pop();
  1782. return;
  1783. }
  1784. if (token.is_end_of_file()) {
  1785. process_using_the_rules_for(InsertionMode::InBody, token);
  1786. return;
  1787. }
  1788. AnythingElse:
  1789. PARSE_ERROR();
  1790. m_foster_parenting = true;
  1791. process_using_the_rules_for(InsertionMode::InBody, token);
  1792. m_foster_parenting = false;
  1793. }
  1794. void HTMLDocumentParser::handle_in_select_in_table(HTMLToken& token)
  1795. {
  1796. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::caption, HTML::TagNames::table, HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead, HTML::TagNames::tr, HTML::TagNames::td, HTML::TagNames::th)) {
  1797. PARSE_ERROR();
  1798. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
  1799. reset_the_insertion_mode_appropriately();
  1800. process_using_the_rules_for(m_insertion_mode, token);
  1801. return;
  1802. }
  1803. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::caption, HTML::TagNames::table, HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead, HTML::TagNames::tr, HTML::TagNames::td, HTML::TagNames::th)) {
  1804. PARSE_ERROR();
  1805. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name()))
  1806. return;
  1807. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
  1808. reset_the_insertion_mode_appropriately();
  1809. process_using_the_rules_for(m_insertion_mode, token);
  1810. return;
  1811. }
  1812. process_using_the_rules_for(InsertionMode::InSelect, token);
  1813. }
  1814. void HTMLDocumentParser::handle_in_select(HTMLToken& token)
  1815. {
  1816. if (token.is_character()) {
  1817. if (token.codepoint() == 0) {
  1818. PARSE_ERROR();
  1819. return;
  1820. }
  1821. insert_character(token.codepoint());
  1822. return;
  1823. }
  1824. if (token.is_comment()) {
  1825. insert_comment(token);
  1826. return;
  1827. }
  1828. if (token.is_doctype()) {
  1829. PARSE_ERROR();
  1830. return;
  1831. }
  1832. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  1833. process_using_the_rules_for(InsertionMode::InBody, token);
  1834. return;
  1835. }
  1836. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::option) {
  1837. if (current_node().tag_name() == HTML::TagNames::option) {
  1838. m_stack_of_open_elements.pop();
  1839. }
  1840. insert_html_element(token);
  1841. return;
  1842. }
  1843. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::optgroup) {
  1844. if (current_node().tag_name() == HTML::TagNames::option) {
  1845. m_stack_of_open_elements.pop();
  1846. }
  1847. if (current_node().tag_name() == HTML::TagNames::optgroup) {
  1848. m_stack_of_open_elements.pop();
  1849. }
  1850. insert_html_element(token);
  1851. return;
  1852. }
  1853. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::optgroup) {
  1854. if (current_node().tag_name() == HTML::TagNames::option && node_before_current_node().tag_name() == HTML::TagNames::optgroup)
  1855. m_stack_of_open_elements.pop();
  1856. if (current_node().tag_name() == HTML::TagNames::optgroup) {
  1857. m_stack_of_open_elements.pop();
  1858. } else {
  1859. PARSE_ERROR();
  1860. return;
  1861. }
  1862. return;
  1863. }
  1864. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::option) {
  1865. if (current_node().tag_name() == HTML::TagNames::option) {
  1866. m_stack_of_open_elements.pop();
  1867. } else {
  1868. PARSE_ERROR();
  1869. return;
  1870. }
  1871. return;
  1872. }
  1873. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::select) {
  1874. if (!m_stack_of_open_elements.has_in_select_scope(HTML::TagNames::select)) {
  1875. PARSE_ERROR();
  1876. return;
  1877. }
  1878. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
  1879. reset_the_insertion_mode_appropriately();
  1880. return;
  1881. }
  1882. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::select) {
  1883. PARSE_ERROR();
  1884. if (!m_stack_of_open_elements.has_in_select_scope(HTML::TagNames::select))
  1885. return;
  1886. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
  1887. reset_the_insertion_mode_appropriately();
  1888. return;
  1889. }
  1890. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::input, HTML::TagNames::keygen, HTML::TagNames::textarea)) {
  1891. PARSE_ERROR();
  1892. if (!m_stack_of_open_elements.has_in_select_scope(HTML::TagNames::select)) {
  1893. return;
  1894. }
  1895. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
  1896. reset_the_insertion_mode_appropriately();
  1897. process_using_the_rules_for(m_insertion_mode, token);
  1898. return;
  1899. }
  1900. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::script, HTML::TagNames::template_)) {
  1901. process_using_the_rules_for(InsertionMode::InHead, token);
  1902. return;
  1903. }
  1904. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
  1905. process_using_the_rules_for(InsertionMode::InHead, token);
  1906. return;
  1907. }
  1908. if (token.is_end_of_file()) {
  1909. process_using_the_rules_for(InsertionMode::InBody, token);
  1910. return;
  1911. }
  1912. PARSE_ERROR();
  1913. }
  1914. void HTMLDocumentParser::handle_in_caption(HTMLToken& token)
  1915. {
  1916. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::caption) {
  1917. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::caption)) {
  1918. PARSE_ERROR();
  1919. return;
  1920. }
  1921. generate_implied_end_tags();
  1922. if (current_node().tag_name() != HTML::TagNames::caption)
  1923. PARSE_ERROR();
  1924. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::caption);
  1925. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1926. m_insertion_mode = InsertionMode::InTable;
  1927. return;
  1928. }
  1929. if ((token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::caption, HTML::TagNames::col, HTML::TagNames::colgroup, HTML::TagNames::tbody, HTML::TagNames::td, HTML::TagNames::tfoot, HTML::TagNames::th, HTML::TagNames::thead, HTML::TagNames::tr))
  1930. || (token.is_end_tag() && token.tag_name() == HTML::TagNames::table)) {
  1931. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::caption)) {
  1932. PARSE_ERROR();
  1933. return;
  1934. }
  1935. generate_implied_end_tags();
  1936. if (current_node().tag_name() != HTML::TagNames::caption)
  1937. PARSE_ERROR();
  1938. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::caption);
  1939. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1940. m_insertion_mode = InsertionMode::InTable;
  1941. process_using_the_rules_for(m_insertion_mode, token);
  1942. return;
  1943. }
  1944. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::body, HTML::TagNames::col, HTML::TagNames::colgroup, HTML::TagNames::html, HTML::TagNames::tbody, HTML::TagNames::td, HTML::TagNames::tfoot, HTML::TagNames::th, HTML::TagNames::thead, HTML::TagNames::tr)) {
  1945. PARSE_ERROR();
  1946. return;
  1947. }
  1948. process_using_the_rules_for(InsertionMode::InBody, token);
  1949. }
  1950. void HTMLDocumentParser::handle_in_column_group(HTMLToken& token)
  1951. {
  1952. if (token.is_character() && token.is_parser_whitespace()) {
  1953. insert_character(token.codepoint());
  1954. return;
  1955. }
  1956. if (token.is_comment()) {
  1957. insert_comment(token);
  1958. return;
  1959. }
  1960. if (token.is_doctype()) {
  1961. PARSE_ERROR();
  1962. return;
  1963. }
  1964. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  1965. process_using_the_rules_for(InsertionMode::InBody, token);
  1966. return;
  1967. }
  1968. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::col) {
  1969. insert_html_element(token);
  1970. m_stack_of_open_elements.pop();
  1971. token.acknowledge_self_closing_flag_if_set();
  1972. return;
  1973. }
  1974. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::colgroup) {
  1975. if (current_node().tag_name() != HTML::TagNames::colgroup) {
  1976. PARSE_ERROR();
  1977. return;
  1978. }
  1979. m_stack_of_open_elements.pop();
  1980. m_insertion_mode = InsertionMode::InTable;
  1981. return;
  1982. }
  1983. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::col) {
  1984. PARSE_ERROR();
  1985. return;
  1986. }
  1987. if ((token.is_start_tag() || token.is_end_tag()) && token.tag_name() == HTML::TagNames::template_) {
  1988. process_using_the_rules_for(InsertionMode::InHead, token);
  1989. return;
  1990. }
  1991. if (token.is_end_of_file()) {
  1992. process_using_the_rules_for(InsertionMode::InBody, token);
  1993. return;
  1994. }
  1995. if (current_node().tag_name() != HTML::TagNames::colgroup) {
  1996. PARSE_ERROR();
  1997. return;
  1998. }
  1999. m_stack_of_open_elements.pop();
  2000. m_insertion_mode = InsertionMode::InTable;
  2001. process_using_the_rules_for(m_insertion_mode, token);
  2002. }
  2003. void HTMLDocumentParser::handle_in_template(HTMLToken& token)
  2004. {
  2005. if (token.is_character() || token.is_comment() || token.is_doctype()) {
  2006. process_using_the_rules_for(InsertionMode::InBody, token);
  2007. return;
  2008. }
  2009. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::base, HTML::TagNames::basefont, HTML::TagNames::bgsound, HTML::TagNames::link, HTML::TagNames::meta, HTML::TagNames::noframes, HTML::TagNames::script, HTML::TagNames::style, HTML::TagNames::template_, HTML::TagNames::title)) {
  2010. process_using_the_rules_for(InsertionMode::InHead, token);
  2011. return;
  2012. }
  2013. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
  2014. process_using_the_rules_for(InsertionMode::InHead, token);
  2015. return;
  2016. }
  2017. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::caption, HTML::TagNames::colgroup, HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead)) {
  2018. m_stack_of_template_insertion_modes.take_last();
  2019. m_stack_of_template_insertion_modes.append(InsertionMode::InTable);
  2020. m_insertion_mode = InsertionMode::InTable;
  2021. process_using_the_rules_for(m_insertion_mode, token);
  2022. return;
  2023. }
  2024. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::col) {
  2025. m_stack_of_template_insertion_modes.take_last();
  2026. m_stack_of_template_insertion_modes.append(InsertionMode::InColumnGroup);
  2027. m_insertion_mode = InsertionMode::InColumnGroup;
  2028. process_using_the_rules_for(m_insertion_mode, token);
  2029. return;
  2030. }
  2031. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::tr) {
  2032. m_stack_of_template_insertion_modes.take_last();
  2033. m_stack_of_template_insertion_modes.append(InsertionMode::InTableBody);
  2034. m_insertion_mode = InsertionMode::InTableBody;
  2035. process_using_the_rules_for(m_insertion_mode, token);
  2036. return;
  2037. }
  2038. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
  2039. m_stack_of_template_insertion_modes.take_last();
  2040. m_stack_of_template_insertion_modes.append(InsertionMode::InRow);
  2041. m_insertion_mode = InsertionMode::InRow;
  2042. process_using_the_rules_for(m_insertion_mode, token);
  2043. return;
  2044. }
  2045. if (token.is_start_tag()) {
  2046. m_stack_of_template_insertion_modes.take_last();
  2047. m_stack_of_template_insertion_modes.append(InsertionMode::InBody);
  2048. m_insertion_mode = InsertionMode::InBody;
  2049. process_using_the_rules_for(m_insertion_mode, token);
  2050. return;
  2051. }
  2052. if (token.is_end_tag()) {
  2053. PARSE_ERROR();
  2054. return;
  2055. }
  2056. if (token.is_end_of_file()) {
  2057. if (!m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
  2058. stop_parsing();
  2059. } else {
  2060. PARSE_ERROR();
  2061. }
  2062. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::template_);
  2063. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  2064. m_stack_of_template_insertion_modes.take_last();
  2065. reset_the_insertion_mode_appropriately();
  2066. process_using_the_rules_for(m_insertion_mode, token);
  2067. }
  2068. }
  2069. void HTMLDocumentParser::handle_in_frameset(HTMLToken& token)
  2070. {
  2071. if (token.is_character() && token.is_parser_whitespace()) {
  2072. insert_character(token.codepoint());
  2073. return;
  2074. }
  2075. if (token.is_comment()) {
  2076. insert_comment(token);
  2077. return;
  2078. }
  2079. if (token.is_doctype()) {
  2080. PARSE_ERROR();
  2081. return;
  2082. }
  2083. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  2084. process_using_the_rules_for(InsertionMode::InBody, token);
  2085. return;
  2086. }
  2087. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::frameset) {
  2088. insert_html_element(token);
  2089. return;
  2090. }
  2091. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::frameset) {
  2092. // FIXME: If the current node is the root html element, then this is a parse error; ignore the token. (fragment case)
  2093. m_stack_of_open_elements.pop();
  2094. if (m_parsing_fragment && current_node().tag_name() != HTML::TagNames::frameset) {
  2095. m_insertion_mode = InsertionMode::AfterFrameset;
  2096. }
  2097. return;
  2098. }
  2099. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::frame) {
  2100. insert_html_element(token);
  2101. m_stack_of_open_elements.pop();
  2102. token.acknowledge_self_closing_flag_if_set();
  2103. return;
  2104. }
  2105. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::noframes) {
  2106. process_using_the_rules_for(InsertionMode::InHead, token);
  2107. return;
  2108. }
  2109. if (token.is_end_of_file()) {
  2110. //FIXME: If the current node is not the root html element, then this is a parse error.
  2111. stop_parsing();
  2112. return;
  2113. }
  2114. PARSE_ERROR();
  2115. }
  2116. void HTMLDocumentParser::handle_after_frameset(HTMLToken& token)
  2117. {
  2118. if (token.is_character() && token.is_parser_whitespace()) {
  2119. insert_character(token.codepoint());
  2120. return;
  2121. }
  2122. if (token.is_comment()) {
  2123. insert_comment(token);
  2124. return;
  2125. }
  2126. if (token.is_doctype()) {
  2127. PARSE_ERROR();
  2128. return;
  2129. }
  2130. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  2131. process_using_the_rules_for(InsertionMode::InBody, token);
  2132. return;
  2133. }
  2134. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::html) {
  2135. m_insertion_mode = InsertionMode::AfterAfterFrameset;
  2136. return;
  2137. }
  2138. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::noframes) {
  2139. process_using_the_rules_for(InsertionMode::InHead, token);
  2140. return;
  2141. }
  2142. if (token.is_end_of_file()) {
  2143. stop_parsing();
  2144. return;
  2145. }
  2146. PARSE_ERROR();
  2147. }
  2148. void HTMLDocumentParser::handle_after_after_frameset(HTMLToken& token)
  2149. {
  2150. if (token.is_comment()) {
  2151. auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
  2152. document().append_child(move(comment));
  2153. return;
  2154. }
  2155. if (token.is_doctype() || token.is_parser_whitespace() || (token.is_start_tag() && token.tag_name() == HTML::TagNames::html)) {
  2156. process_using_the_rules_for(InsertionMode::InBody, token);
  2157. return;
  2158. }
  2159. if (token.is_end_of_file()) {
  2160. stop_parsing();
  2161. return;
  2162. }
  2163. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::noframes) {
  2164. process_using_the_rules_for(InsertionMode::InHead, token);
  2165. return;
  2166. }
  2167. PARSE_ERROR();
  2168. }
  2169. void HTMLDocumentParser::reset_the_insertion_mode_appropriately(const Element* context_element)
  2170. {
  2171. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  2172. // NOTE: When parsing fragments, we substitute the context element for the root of the stack of open elements.
  2173. RefPtr<Element> node;
  2174. if (i == 0 && context_element) {
  2175. node = context_element;
  2176. } else {
  2177. node = m_stack_of_open_elements.elements().at(i);
  2178. }
  2179. if (node->tag_name() == HTML::TagNames::select) {
  2180. TODO();
  2181. }
  2182. if (node->tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
  2183. m_insertion_mode = InsertionMode::InCell;
  2184. return;
  2185. }
  2186. if (node->tag_name() == HTML::TagNames::tr) {
  2187. m_insertion_mode = InsertionMode::InRow;
  2188. return;
  2189. }
  2190. if (node->tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::thead, HTML::TagNames::tfoot)) {
  2191. m_insertion_mode = InsertionMode::InTableBody;
  2192. return;
  2193. }
  2194. if (node->tag_name() == HTML::TagNames::caption) {
  2195. m_insertion_mode = InsertionMode::InCaption;
  2196. return;
  2197. }
  2198. if (node->tag_name() == HTML::TagNames::colgroup) {
  2199. m_insertion_mode = InsertionMode::InColumnGroup;
  2200. return;
  2201. }
  2202. if (node->tag_name() == HTML::TagNames::table) {
  2203. m_insertion_mode = InsertionMode::InTable;
  2204. return;
  2205. }
  2206. if (node->tag_name() == HTML::TagNames::template_) {
  2207. TODO();
  2208. }
  2209. if (node->tag_name() == HTML::TagNames::body) {
  2210. m_insertion_mode = InsertionMode::InBody;
  2211. return;
  2212. }
  2213. if (node->tag_name() == HTML::TagNames::frameset) {
  2214. m_insertion_mode = InsertionMode::InFrameset;
  2215. if (m_parsing_fragment) {
  2216. TODO();
  2217. }
  2218. return;
  2219. }
  2220. if (node->tag_name() == HTML::TagNames::html) {
  2221. TODO();
  2222. }
  2223. }
  2224. m_insertion_mode = InsertionMode::InBody;
  2225. }
  2226. const char* HTMLDocumentParser::insertion_mode_name() const
  2227. {
  2228. switch (m_insertion_mode) {
  2229. #define __ENUMERATE_INSERTION_MODE(mode) \
  2230. case InsertionMode::mode: \
  2231. return #mode;
  2232. ENUMERATE_INSERTION_MODES
  2233. #undef __ENUMERATE_INSERTION_MODE
  2234. }
  2235. ASSERT_NOT_REACHED();
  2236. }
  2237. Document& HTMLDocumentParser::document()
  2238. {
  2239. return *m_document;
  2240. }
  2241. NonnullRefPtrVector<Node> HTMLDocumentParser::parse_html_fragment(Element& context_element, const StringView& markup)
  2242. {
  2243. HTMLDocumentParser parser(markup, "utf-8");
  2244. parser.m_parsing_fragment = true;
  2245. parser.document().set_quirks_mode(context_element.document().in_quirks_mode());
  2246. if (context_element.tag_name().is_one_of(HTML::TagNames::title, HTML::TagNames::textarea)) {
  2247. parser.m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
  2248. } else if (context_element.tag_name().is_one_of(HTML::TagNames::style, HTML::TagNames::xmp, HTML::TagNames::iframe, HTML::TagNames::noembed, HTML::TagNames::noframes)) {
  2249. parser.m_tokenizer.switch_to({}, HTMLTokenizer::State::RAWTEXT);
  2250. } else if (context_element.tag_name().is_one_of(HTML::TagNames::script)) {
  2251. parser.m_tokenizer.switch_to({}, HTMLTokenizer::State::ScriptData);
  2252. } else if (context_element.tag_name().is_one_of(HTML::TagNames::noscript)) {
  2253. if (context_element.document().is_scripting_enabled())
  2254. parser.m_tokenizer.switch_to({}, HTMLTokenizer::State::RAWTEXT);
  2255. } else if (context_element.tag_name().is_one_of(HTML::TagNames::plaintext)) {
  2256. parser.m_tokenizer.switch_to({}, HTMLTokenizer::State::PLAINTEXT);
  2257. }
  2258. auto root = create_element(context_element.document(), HTML::TagNames::html);
  2259. parser.document().append_child(root);
  2260. parser.m_stack_of_open_elements.push(root);
  2261. if (context_element.tag_name() == HTML::TagNames::template_) {
  2262. TODO();
  2263. }
  2264. // FIXME: Create a start tag token whose name is the local name of context and whose attributes are the attributes of context.
  2265. parser.reset_the_insertion_mode_appropriately(&context_element);
  2266. for (auto* form_candidate = &context_element; form_candidate; form_candidate = form_candidate->parent_element()) {
  2267. if (is<HTMLFormElement>(*form_candidate)) {
  2268. parser.m_form_element = to<HTMLFormElement>(*form_candidate);
  2269. break;
  2270. }
  2271. }
  2272. parser.run(context_element.document().url());
  2273. NonnullRefPtrVector<Node> children;
  2274. while (RefPtr<Node> child = root->first_child()) {
  2275. root->remove_child(*child);
  2276. context_element.document().adopt_node(*child);
  2277. children.append(*child);
  2278. }
  2279. return children;
  2280. }
  2281. }