HTMLDocumentParser.cpp 116 KB

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