HTMLDocumentParser.cpp 117 KB

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