HTMLDocumentParser.cpp 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060
  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. // FIXME: If formatting element is not in the stack of open elements,
  830. // then this is a parse error; remove the element from the list, and return.
  831. TODO();
  832. }
  833. if (!m_stack_of_open_elements.has_in_scope(*formatting_element)) {
  834. PARSE_ERROR();
  835. return AdoptionAgencyAlgorithmOutcome::DoNothing;
  836. }
  837. if (formatting_element != &current_node()) {
  838. PARSE_ERROR();
  839. }
  840. RefPtr<DOM::Element> furthest_block = m_stack_of_open_elements.topmost_special_node_below(*formatting_element);
  841. if (!furthest_block) {
  842. while (&current_node() != formatting_element)
  843. m_stack_of_open_elements.pop();
  844. m_stack_of_open_elements.pop();
  845. m_list_of_active_formatting_elements.remove(*formatting_element);
  846. return AdoptionAgencyAlgorithmOutcome::DoNothing;
  847. }
  848. // FIXME: Implement the rest of the AAA :^)
  849. TODO();
  850. }
  851. bool HTMLDocumentParser::is_special_tag(const FlyString& tag_name, const FlyString& namespace_)
  852. {
  853. if (namespace_ == Namespace::HTML) {
  854. return tag_name.is_one_of(
  855. HTML::TagNames::address,
  856. HTML::TagNames::applet,
  857. HTML::TagNames::area,
  858. HTML::TagNames::article,
  859. HTML::TagNames::aside,
  860. HTML::TagNames::base,
  861. HTML::TagNames::basefont,
  862. HTML::TagNames::bgsound,
  863. HTML::TagNames::blockquote,
  864. HTML::TagNames::body,
  865. HTML::TagNames::br,
  866. HTML::TagNames::button,
  867. HTML::TagNames::caption,
  868. HTML::TagNames::center,
  869. HTML::TagNames::col,
  870. HTML::TagNames::colgroup,
  871. HTML::TagNames::dd,
  872. HTML::TagNames::details,
  873. HTML::TagNames::dir,
  874. HTML::TagNames::div,
  875. HTML::TagNames::dl,
  876. HTML::TagNames::dt,
  877. HTML::TagNames::embed,
  878. HTML::TagNames::fieldset,
  879. HTML::TagNames::figcaption,
  880. HTML::TagNames::figure,
  881. HTML::TagNames::footer,
  882. HTML::TagNames::form,
  883. HTML::TagNames::frame,
  884. HTML::TagNames::frameset,
  885. HTML::TagNames::h1,
  886. HTML::TagNames::h2,
  887. HTML::TagNames::h3,
  888. HTML::TagNames::h4,
  889. HTML::TagNames::h5,
  890. HTML::TagNames::h6,
  891. HTML::TagNames::head,
  892. HTML::TagNames::header,
  893. HTML::TagNames::hgroup,
  894. HTML::TagNames::hr,
  895. HTML::TagNames::html,
  896. HTML::TagNames::iframe,
  897. HTML::TagNames::img,
  898. HTML::TagNames::input,
  899. HTML::TagNames::keygen,
  900. HTML::TagNames::li,
  901. HTML::TagNames::link,
  902. HTML::TagNames::listing,
  903. HTML::TagNames::main,
  904. HTML::TagNames::marquee,
  905. HTML::TagNames::menu,
  906. HTML::TagNames::meta,
  907. HTML::TagNames::nav,
  908. HTML::TagNames::noembed,
  909. HTML::TagNames::noframes,
  910. HTML::TagNames::noscript,
  911. HTML::TagNames::object,
  912. HTML::TagNames::ol,
  913. HTML::TagNames::p,
  914. HTML::TagNames::param,
  915. HTML::TagNames::plaintext,
  916. HTML::TagNames::pre,
  917. HTML::TagNames::script,
  918. HTML::TagNames::section,
  919. HTML::TagNames::select,
  920. HTML::TagNames::source,
  921. HTML::TagNames::style,
  922. HTML::TagNames::summary,
  923. HTML::TagNames::table,
  924. HTML::TagNames::tbody,
  925. HTML::TagNames::td,
  926. HTML::TagNames::template_,
  927. HTML::TagNames::textarea,
  928. HTML::TagNames::tfoot,
  929. HTML::TagNames::th,
  930. HTML::TagNames::thead,
  931. HTML::TagNames::title,
  932. HTML::TagNames::tr,
  933. HTML::TagNames::track,
  934. HTML::TagNames::ul,
  935. HTML::TagNames::wbr,
  936. HTML::TagNames::xmp);
  937. } else if (namespace_ == Namespace::SVG) {
  938. return tag_name.is_one_of(
  939. SVG::TagNames::desc,
  940. SVG::TagNames::foreignObject,
  941. SVG::TagNames::title);
  942. } else if (namespace_ == Namespace::MathML) {
  943. TODO();
  944. }
  945. return false;
  946. }
  947. void HTMLDocumentParser::handle_in_body(HTMLToken& token)
  948. {
  949. if (token.is_character()) {
  950. if (token.code_point() == 0) {
  951. PARSE_ERROR();
  952. return;
  953. }
  954. if (token.is_parser_whitespace()) {
  955. reconstruct_the_active_formatting_elements();
  956. insert_character(token.code_point());
  957. return;
  958. }
  959. reconstruct_the_active_formatting_elements();
  960. insert_character(token.code_point());
  961. m_frameset_ok = false;
  962. return;
  963. }
  964. if (token.is_comment()) {
  965. insert_comment(token);
  966. return;
  967. }
  968. if (token.is_doctype()) {
  969. PARSE_ERROR();
  970. return;
  971. }
  972. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  973. PARSE_ERROR();
  974. if (m_stack_of_open_elements.contains(HTML::TagNames::template_))
  975. return;
  976. for (auto& attribute : token.m_tag.attributes) {
  977. if (current_node().has_attribute(attribute.local_name_builder.string_view()))
  978. continue;
  979. current_node().set_attribute(attribute.local_name_builder.to_string(), attribute.value_builder.to_string());
  980. }
  981. return;
  982. }
  983. 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)) {
  984. process_using_the_rules_for(InsertionMode::InHead, token);
  985. return;
  986. }
  987. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
  988. process_using_the_rules_for(InsertionMode::InHead, token);
  989. return;
  990. }
  991. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::body) {
  992. PARSE_ERROR();
  993. if (m_stack_of_open_elements.elements().size() == 1
  994. || m_stack_of_open_elements.elements().at(1).local_name() != HTML::TagNames::body
  995. || m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
  996. VERIFY(m_parsing_fragment);
  997. return;
  998. }
  999. m_frameset_ok = false;
  1000. auto& body_element = m_stack_of_open_elements.elements().at(1);
  1001. for (auto& attribute : token.m_tag.attributes) {
  1002. if (body_element.has_attribute(attribute.local_name_builder.string_view()))
  1003. continue;
  1004. body_element.set_attribute(attribute.local_name_builder.to_string(), attribute.value_builder.to_string());
  1005. }
  1006. return;
  1007. }
  1008. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::frameset) {
  1009. PARSE_ERROR();
  1010. if (m_stack_of_open_elements.elements().size() == 1
  1011. || m_stack_of_open_elements.elements().at(1).local_name() != HTML::TagNames::body) {
  1012. VERIFY(m_parsing_fragment);
  1013. return;
  1014. }
  1015. if (!m_frameset_ok)
  1016. return;
  1017. TODO();
  1018. }
  1019. if (token.is_end_of_file()) {
  1020. if (!m_stack_of_template_insertion_modes.is_empty()) {
  1021. process_using_the_rules_for(InsertionMode::InTemplate, token);
  1022. return;
  1023. }
  1024. for (auto& node : m_stack_of_open_elements.elements()) {
  1025. 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)) {
  1026. PARSE_ERROR();
  1027. break;
  1028. }
  1029. }
  1030. stop_parsing();
  1031. return;
  1032. }
  1033. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::body) {
  1034. if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::body)) {
  1035. PARSE_ERROR();
  1036. return;
  1037. }
  1038. for (auto& node : m_stack_of_open_elements.elements()) {
  1039. 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)) {
  1040. PARSE_ERROR();
  1041. break;
  1042. }
  1043. }
  1044. m_insertion_mode = InsertionMode::AfterBody;
  1045. return;
  1046. }
  1047. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::html) {
  1048. if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::body)) {
  1049. PARSE_ERROR();
  1050. return;
  1051. }
  1052. for (auto& node : m_stack_of_open_elements.elements()) {
  1053. 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)) {
  1054. PARSE_ERROR();
  1055. break;
  1056. }
  1057. }
  1058. m_insertion_mode = InsertionMode::AfterBody;
  1059. process_using_the_rules_for(m_insertion_mode, token);
  1060. return;
  1061. }
  1062. 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)) {
  1063. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  1064. close_a_p_element();
  1065. insert_html_element(token);
  1066. return;
  1067. }
  1068. 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)) {
  1069. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  1070. close_a_p_element();
  1071. 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)) {
  1072. PARSE_ERROR();
  1073. m_stack_of_open_elements.pop();
  1074. }
  1075. insert_html_element(token);
  1076. return;
  1077. }
  1078. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::pre, HTML::TagNames::listing)) {
  1079. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  1080. close_a_p_element();
  1081. insert_html_element(token);
  1082. m_frameset_ok = false;
  1083. // If the next token is a U+000A LINE FEED (LF) character token,
  1084. // then ignore that token and move on to the next one.
  1085. // (Newlines at the start of pre blocks are ignored as an authoring convenience.)
  1086. auto next_token = m_tokenizer.next_token();
  1087. if (next_token.has_value() && next_token.value().is_character() && next_token.value().code_point() == '\n') {
  1088. // Ignore it.
  1089. } else {
  1090. process_using_the_rules_for(m_insertion_mode, next_token.value());
  1091. }
  1092. return;
  1093. }
  1094. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::form) {
  1095. if (m_form_element && !m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
  1096. PARSE_ERROR();
  1097. return;
  1098. }
  1099. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  1100. close_a_p_element();
  1101. auto element = insert_html_element(token);
  1102. if (!m_stack_of_open_elements.contains(HTML::TagNames::template_))
  1103. m_form_element = downcast<HTMLFormElement>(*element);
  1104. return;
  1105. }
  1106. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::li) {
  1107. m_frameset_ok = false;
  1108. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1109. RefPtr<DOM::Element> node = m_stack_of_open_elements.elements()[i];
  1110. if (node->local_name() == HTML::TagNames::li) {
  1111. generate_implied_end_tags(HTML::TagNames::li);
  1112. if (current_node().local_name() != HTML::TagNames::li) {
  1113. PARSE_ERROR();
  1114. }
  1115. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::li);
  1116. break;
  1117. }
  1118. if (is_special_tag(node->local_name(), node->namespace_()) && !node->local_name().is_one_of(HTML::TagNames::address, HTML::TagNames::div, HTML::TagNames::p))
  1119. break;
  1120. }
  1121. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  1122. close_a_p_element();
  1123. insert_html_element(token);
  1124. return;
  1125. }
  1126. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt)) {
  1127. m_frameset_ok = false;
  1128. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1129. RefPtr<DOM::Element> node = m_stack_of_open_elements.elements()[i];
  1130. if (node->local_name() == HTML::TagNames::dd) {
  1131. generate_implied_end_tags(HTML::TagNames::dd);
  1132. if (current_node().local_name() != HTML::TagNames::dd) {
  1133. PARSE_ERROR();
  1134. }
  1135. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::dd);
  1136. break;
  1137. }
  1138. if (node->local_name() == HTML::TagNames::dt) {
  1139. generate_implied_end_tags(HTML::TagNames::dt);
  1140. if (current_node().local_name() != HTML::TagNames::dt) {
  1141. PARSE_ERROR();
  1142. }
  1143. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::dt);
  1144. break;
  1145. }
  1146. if (is_special_tag(node->local_name(), node->namespace_()) && !node->local_name().is_one_of(HTML::TagNames::address, HTML::TagNames::div, HTML::TagNames::p))
  1147. break;
  1148. }
  1149. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  1150. close_a_p_element();
  1151. insert_html_element(token);
  1152. return;
  1153. }
  1154. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::plaintext) {
  1155. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  1156. close_a_p_element();
  1157. insert_html_element(token);
  1158. m_tokenizer.switch_to({}, HTMLTokenizer::State::PLAINTEXT);
  1159. return;
  1160. }
  1161. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::button) {
  1162. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::button)) {
  1163. PARSE_ERROR();
  1164. generate_implied_end_tags();
  1165. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::button);
  1166. }
  1167. reconstruct_the_active_formatting_elements();
  1168. insert_html_element(token);
  1169. m_frameset_ok = false;
  1170. return;
  1171. }
  1172. 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)) {
  1173. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  1174. PARSE_ERROR();
  1175. return;
  1176. }
  1177. generate_implied_end_tags();
  1178. if (current_node().local_name() != token.tag_name()) {
  1179. PARSE_ERROR();
  1180. }
  1181. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1182. return;
  1183. }
  1184. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::form) {
  1185. if (!m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
  1186. auto node = m_form_element;
  1187. m_form_element = nullptr;
  1188. if (!node || !m_stack_of_open_elements.has_in_scope(*node)) {
  1189. PARSE_ERROR();
  1190. return;
  1191. }
  1192. generate_implied_end_tags();
  1193. if (&current_node() != node) {
  1194. PARSE_ERROR();
  1195. }
  1196. m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) { return entry.ptr() == node.ptr(); });
  1197. } else {
  1198. if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::form)) {
  1199. PARSE_ERROR();
  1200. return;
  1201. }
  1202. generate_implied_end_tags();
  1203. if (current_node().local_name() != HTML::TagNames::form) {
  1204. PARSE_ERROR();
  1205. }
  1206. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::form);
  1207. }
  1208. return;
  1209. }
  1210. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::p) {
  1211. if (!m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p)) {
  1212. PARSE_ERROR();
  1213. insert_html_element(HTMLToken::make_start_tag(HTML::TagNames::p));
  1214. }
  1215. close_a_p_element();
  1216. return;
  1217. }
  1218. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::li) {
  1219. if (!m_stack_of_open_elements.has_in_list_item_scope(HTML::TagNames::li)) {
  1220. PARSE_ERROR();
  1221. return;
  1222. }
  1223. generate_implied_end_tags(HTML::TagNames::li);
  1224. if (current_node().local_name() != HTML::TagNames::li) {
  1225. PARSE_ERROR();
  1226. dbgln("Expected <li> current node, but had <{}>", current_node().local_name());
  1227. }
  1228. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::li);
  1229. return;
  1230. }
  1231. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt)) {
  1232. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  1233. PARSE_ERROR();
  1234. return;
  1235. }
  1236. generate_implied_end_tags(token.tag_name());
  1237. if (current_node().local_name() != token.tag_name()) {
  1238. PARSE_ERROR();
  1239. }
  1240. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1241. return;
  1242. }
  1243. 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)) {
  1244. if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::h1)
  1245. && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h2)
  1246. && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h3)
  1247. && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h4)
  1248. && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h5)
  1249. && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h6)) {
  1250. PARSE_ERROR();
  1251. return;
  1252. }
  1253. generate_implied_end_tags();
  1254. if (current_node().local_name() != token.tag_name()) {
  1255. PARSE_ERROR();
  1256. }
  1257. for (;;) {
  1258. auto popped_element = m_stack_of_open_elements.pop();
  1259. 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))
  1260. break;
  1261. }
  1262. return;
  1263. }
  1264. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::a) {
  1265. if (auto* element = m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker(HTML::TagNames::a)) {
  1266. PARSE_ERROR();
  1267. if (run_the_adoption_agency_algorithm(token) == AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps)
  1268. goto AnyOtherEndTag;
  1269. m_list_of_active_formatting_elements.remove(*element);
  1270. m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) {
  1271. return entry.ptr() == element;
  1272. });
  1273. }
  1274. reconstruct_the_active_formatting_elements();
  1275. auto element = insert_html_element(token);
  1276. m_list_of_active_formatting_elements.add(*element);
  1277. return;
  1278. }
  1279. 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)) {
  1280. reconstruct_the_active_formatting_elements();
  1281. auto element = insert_html_element(token);
  1282. m_list_of_active_formatting_elements.add(*element);
  1283. return;
  1284. }
  1285. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::nobr) {
  1286. reconstruct_the_active_formatting_elements();
  1287. if (m_stack_of_open_elements.has_in_scope(HTML::TagNames::nobr)) {
  1288. PARSE_ERROR();
  1289. run_the_adoption_agency_algorithm(token);
  1290. reconstruct_the_active_formatting_elements();
  1291. }
  1292. auto element = insert_html_element(token);
  1293. m_list_of_active_formatting_elements.add(*element);
  1294. return;
  1295. }
  1296. 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)) {
  1297. if (run_the_adoption_agency_algorithm(token) == AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps)
  1298. goto AnyOtherEndTag;
  1299. return;
  1300. }
  1301. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::applet, HTML::TagNames::marquee, HTML::TagNames::object)) {
  1302. reconstruct_the_active_formatting_elements();
  1303. insert_html_element(token);
  1304. m_list_of_active_formatting_elements.add_marker();
  1305. m_frameset_ok = false;
  1306. return;
  1307. }
  1308. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::applet, HTML::TagNames::marquee, HTML::TagNames::object)) {
  1309. if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
  1310. PARSE_ERROR();
  1311. return;
  1312. }
  1313. generate_implied_end_tags();
  1314. if (current_node().local_name() != token.tag_name()) {
  1315. PARSE_ERROR();
  1316. }
  1317. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1318. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1319. return;
  1320. }
  1321. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::table) {
  1322. if (!document().in_quirks_mode()) {
  1323. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  1324. close_a_p_element();
  1325. }
  1326. insert_html_element(token);
  1327. m_frameset_ok = false;
  1328. m_insertion_mode = InsertionMode::InTable;
  1329. return;
  1330. }
  1331. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::br) {
  1332. token.drop_attributes();
  1333. goto BRStartTag;
  1334. }
  1335. 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)) {
  1336. BRStartTag:
  1337. reconstruct_the_active_formatting_elements();
  1338. insert_html_element(token);
  1339. m_stack_of_open_elements.pop();
  1340. token.acknowledge_self_closing_flag_if_set();
  1341. m_frameset_ok = false;
  1342. return;
  1343. }
  1344. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::input) {
  1345. reconstruct_the_active_formatting_elements();
  1346. insert_html_element(token);
  1347. m_stack_of_open_elements.pop();
  1348. token.acknowledge_self_closing_flag_if_set();
  1349. auto type_attribute = token.attribute(HTML::AttributeNames::type);
  1350. if (type_attribute.is_null() || !type_attribute.equals_ignoring_case("hidden")) {
  1351. m_frameset_ok = false;
  1352. }
  1353. return;
  1354. }
  1355. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::param, HTML::TagNames::source, HTML::TagNames::track)) {
  1356. insert_html_element(token);
  1357. m_stack_of_open_elements.pop();
  1358. token.acknowledge_self_closing_flag_if_set();
  1359. return;
  1360. }
  1361. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::hr) {
  1362. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
  1363. close_a_p_element();
  1364. insert_html_element(token);
  1365. m_stack_of_open_elements.pop();
  1366. token.acknowledge_self_closing_flag_if_set();
  1367. m_frameset_ok = false;
  1368. return;
  1369. }
  1370. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::image) {
  1371. // Parse error. Change the token's tag name to HTML::TagNames::img and reprocess it. (Don't ask.)
  1372. PARSE_ERROR();
  1373. token.m_tag.tag_name.clear();
  1374. token.m_tag.tag_name.append(HTML::TagNames::img);
  1375. process_using_the_rules_for(m_insertion_mode, token);
  1376. return;
  1377. }
  1378. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::textarea) {
  1379. insert_html_element(token);
  1380. m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
  1381. // If the next token is a U+000A LINE FEED (LF) character token,
  1382. // then ignore that token and move on to the next one.
  1383. // (Newlines at the start of pre blocks are ignored as an authoring convenience.)
  1384. auto next_token = m_tokenizer.next_token();
  1385. m_original_insertion_mode = m_insertion_mode;
  1386. m_frameset_ok = false;
  1387. m_insertion_mode = InsertionMode::Text;
  1388. if (next_token.has_value() && next_token.value().is_character() && next_token.value().code_point() == '\n') {
  1389. // Ignore it.
  1390. } else {
  1391. process_using_the_rules_for(m_insertion_mode, next_token.value());
  1392. }
  1393. return;
  1394. }
  1395. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::xmp) {
  1396. if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p)) {
  1397. close_a_p_element();
  1398. }
  1399. reconstruct_the_active_formatting_elements();
  1400. m_frameset_ok = false;
  1401. parse_generic_raw_text_element(token);
  1402. return;
  1403. }
  1404. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::iframe) {
  1405. m_frameset_ok = false;
  1406. parse_generic_raw_text_element(token);
  1407. return;
  1408. }
  1409. if (token.is_start_tag() && ((token.tag_name() == HTML::TagNames::noembed) || (token.tag_name() == HTML::TagNames::noscript && m_scripting_enabled))) {
  1410. parse_generic_raw_text_element(token);
  1411. return;
  1412. }
  1413. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::select) {
  1414. reconstruct_the_active_formatting_elements();
  1415. insert_html_element(token);
  1416. m_frameset_ok = false;
  1417. switch (m_insertion_mode) {
  1418. case InsertionMode::InTable:
  1419. case InsertionMode::InCaption:
  1420. case InsertionMode::InTableBody:
  1421. case InsertionMode::InRow:
  1422. case InsertionMode::InCell:
  1423. m_insertion_mode = InsertionMode::InSelectInTable;
  1424. break;
  1425. default:
  1426. m_insertion_mode = InsertionMode::InSelect;
  1427. break;
  1428. }
  1429. return;
  1430. }
  1431. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::optgroup, HTML::TagNames::option)) {
  1432. if (current_node().local_name() == HTML::TagNames::option)
  1433. m_stack_of_open_elements.pop();
  1434. reconstruct_the_active_formatting_elements();
  1435. insert_html_element(token);
  1436. return;
  1437. }
  1438. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::rb, HTML::TagNames::rtc)) {
  1439. if (m_stack_of_open_elements.has_in_scope(HTML::TagNames::ruby))
  1440. generate_implied_end_tags();
  1441. if (current_node().local_name() != HTML::TagNames::ruby)
  1442. PARSE_ERROR();
  1443. insert_html_element(token);
  1444. return;
  1445. }
  1446. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::rp, HTML::TagNames::rt)) {
  1447. if (m_stack_of_open_elements.has_in_scope(HTML::TagNames::ruby))
  1448. generate_implied_end_tags(HTML::TagNames::rtc);
  1449. if (current_node().local_name() != HTML::TagNames::rtc || current_node().local_name() != HTML::TagNames::ruby)
  1450. PARSE_ERROR();
  1451. insert_html_element(token);
  1452. return;
  1453. }
  1454. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::math) {
  1455. reconstruct_the_active_formatting_elements();
  1456. adjust_mathml_attributes(token);
  1457. adjust_foreign_attributes(token);
  1458. insert_foreign_element(token, Namespace::MathML);
  1459. if (token.is_self_closing()) {
  1460. m_stack_of_open_elements.pop();
  1461. token.acknowledge_self_closing_flag_if_set();
  1462. }
  1463. return;
  1464. }
  1465. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::svg) {
  1466. reconstruct_the_active_formatting_elements();
  1467. adjust_svg_attributes(token);
  1468. adjust_foreign_attributes(token);
  1469. insert_foreign_element(token, Namespace::SVG);
  1470. if (token.is_self_closing()) {
  1471. m_stack_of_open_elements.pop();
  1472. token.acknowledge_self_closing_flag_if_set();
  1473. }
  1474. return;
  1475. }
  1476. 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))) {
  1477. PARSE_ERROR();
  1478. return;
  1479. }
  1480. // Any other start tag
  1481. if (token.is_start_tag()) {
  1482. reconstruct_the_active_formatting_elements();
  1483. insert_html_element(token);
  1484. return;
  1485. }
  1486. if (token.is_end_tag()) {
  1487. AnyOtherEndTag:
  1488. RefPtr<DOM::Element> node;
  1489. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  1490. node = m_stack_of_open_elements.elements()[i];
  1491. if (node->local_name() == token.tag_name()) {
  1492. generate_implied_end_tags(token.tag_name());
  1493. if (node != current_node()) {
  1494. PARSE_ERROR();
  1495. }
  1496. while (&current_node() != node) {
  1497. m_stack_of_open_elements.pop();
  1498. }
  1499. m_stack_of_open_elements.pop();
  1500. break;
  1501. }
  1502. if (is_special_tag(node->local_name(), node->namespace_())) {
  1503. PARSE_ERROR();
  1504. return;
  1505. }
  1506. }
  1507. return;
  1508. }
  1509. }
  1510. void HTMLDocumentParser::adjust_mathml_attributes(HTMLToken& token)
  1511. {
  1512. token.adjust_attribute_name("definitionurl", "definitionURL");
  1513. }
  1514. void HTMLDocumentParser::adjust_svg_tag_names(HTMLToken& token)
  1515. {
  1516. token.adjust_tag_name("altglyph", "altGlyph");
  1517. token.adjust_tag_name("altglyphdef", "altGlyphDef");
  1518. token.adjust_tag_name("altglyphitem", "altGlyphItem");
  1519. token.adjust_tag_name("animatecolor", "animateColor");
  1520. token.adjust_tag_name("animatemotion", "animateMotion");
  1521. token.adjust_tag_name("animatetransform", "animateTransform");
  1522. token.adjust_tag_name("clippath", "clipPath");
  1523. token.adjust_tag_name("feblend", "feBlend");
  1524. token.adjust_tag_name("fecolormatrix", "feColorMatrix");
  1525. token.adjust_tag_name("fecomponenttransfer", "feComponentTransfer");
  1526. token.adjust_tag_name("fecomposite", "feComposite");
  1527. token.adjust_tag_name("feconvolvematrix", "feConvolveMatrix");
  1528. token.adjust_tag_name("fediffuselighting", "feDiffuseLighting");
  1529. token.adjust_tag_name("fedisplacementmap", "feDisplacementMap");
  1530. token.adjust_tag_name("fedistantlight", "feDistantLight");
  1531. token.adjust_tag_name("fedropshadow", "feDropShadow");
  1532. token.adjust_tag_name("feflood", "feFlood");
  1533. token.adjust_tag_name("fefunca", "feFuncA");
  1534. token.adjust_tag_name("fefuncb", "feFuncB");
  1535. token.adjust_tag_name("fefuncg", "feFuncG");
  1536. token.adjust_tag_name("fefuncr", "feFuncR");
  1537. token.adjust_tag_name("fegaussianblur", "feGaussianBlur");
  1538. token.adjust_tag_name("feimage", "feImage");
  1539. token.adjust_tag_name("femerge", "feMerge");
  1540. token.adjust_tag_name("femergenode", "feMergeNode");
  1541. token.adjust_tag_name("femorphology", "feMorphology");
  1542. token.adjust_tag_name("feoffset", "feOffset");
  1543. token.adjust_tag_name("fepointlight", "fePointLight");
  1544. token.adjust_tag_name("fespecularlighting", "feSpecularLighting");
  1545. token.adjust_tag_name("fespotlight", "feSpotlight");
  1546. token.adjust_tag_name("glyphref", "glyphRef");
  1547. token.adjust_tag_name("lineargradient", "linearGradient");
  1548. token.adjust_tag_name("radialgradient", "radialGradient");
  1549. token.adjust_tag_name("textpath", "textPath");
  1550. }
  1551. void HTMLDocumentParser::adjust_svg_attributes(HTMLToken& token)
  1552. {
  1553. token.adjust_attribute_name("attributename", "attributeName");
  1554. token.adjust_attribute_name("attributetype", "attributeType");
  1555. token.adjust_attribute_name("basefrequency", "baseFrequency");
  1556. token.adjust_attribute_name("baseprofile", "baseProfile");
  1557. token.adjust_attribute_name("calcmode", "calcMode");
  1558. token.adjust_attribute_name("clippathunits", "clipPathUnits");
  1559. token.adjust_attribute_name("diffuseconstant", "diffuseConstant");
  1560. token.adjust_attribute_name("edgemode", "edgeMode");
  1561. token.adjust_attribute_name("filterunits", "filterUnits");
  1562. token.adjust_attribute_name("glyphref", "glyphRef");
  1563. token.adjust_attribute_name("gradienttransform", "gradientTransform");
  1564. token.adjust_attribute_name("gradientunits", "gradientUnits");
  1565. token.adjust_attribute_name("kernelmatrix", "kernelMatrix");
  1566. token.adjust_attribute_name("kernelunitlength", "kernelUnitLength");
  1567. token.adjust_attribute_name("keypoints", "keyPoints");
  1568. token.adjust_attribute_name("keysplines", "keySplines");
  1569. token.adjust_attribute_name("keytimes", "keyTimes");
  1570. token.adjust_attribute_name("lengthadjust", "lengthAdjust");
  1571. token.adjust_attribute_name("limitingconeangle", "limitingConeAngle");
  1572. token.adjust_attribute_name("markerheight", "markerHeight");
  1573. token.adjust_attribute_name("markerunits", "markerUnits");
  1574. token.adjust_attribute_name("markerwidth", "markerWidth");
  1575. token.adjust_attribute_name("maskcontentunits", "maskContentUnits");
  1576. token.adjust_attribute_name("maskunits", "maskUnits");
  1577. token.adjust_attribute_name("numoctaves", "numOctaves");
  1578. token.adjust_attribute_name("pathlength", "pathLength");
  1579. token.adjust_attribute_name("patterncontentunits", "patternContentUnits");
  1580. token.adjust_attribute_name("patterntransform", "patternTransform");
  1581. token.adjust_attribute_name("patternunits", "patternUnits");
  1582. token.adjust_attribute_name("pointsatx", "pointsAtX");
  1583. token.adjust_attribute_name("pointsaty", "pointsAtY");
  1584. token.adjust_attribute_name("pointsatz", "pointsAtZ");
  1585. token.adjust_attribute_name("preservealpha", "preserveAlpha");
  1586. token.adjust_attribute_name("preserveaspectratio", "preserveAspectRatio");
  1587. token.adjust_attribute_name("primitiveunits", "primitiveUnits");
  1588. token.adjust_attribute_name("refx", "refX");
  1589. token.adjust_attribute_name("refy", "refY");
  1590. token.adjust_attribute_name("repeatcount", "repeatCount");
  1591. token.adjust_attribute_name("repeatdur", "repeatDur");
  1592. token.adjust_attribute_name("requiredextensions", "requiredExtensions");
  1593. token.adjust_attribute_name("requiredfeatures", "requiredFeatures");
  1594. token.adjust_attribute_name("specularconstant", "specularConstant");
  1595. token.adjust_attribute_name("specularexponent", "specularExponent");
  1596. token.adjust_attribute_name("spreadmethod", "spreadMethod");
  1597. token.adjust_attribute_name("startoffset", "startOffset");
  1598. token.adjust_attribute_name("stddeviation", "stdDeviation");
  1599. token.adjust_attribute_name("stitchtiles", "stitchTiles");
  1600. token.adjust_attribute_name("surfacescale", "surfaceScale");
  1601. token.adjust_attribute_name("systemlanguage", "systemLanguage");
  1602. token.adjust_attribute_name("tablevalues", "tableValues");
  1603. token.adjust_attribute_name("targetx", "targetX");
  1604. token.adjust_attribute_name("targety", "targetY");
  1605. token.adjust_attribute_name("textlength", "textLength");
  1606. token.adjust_attribute_name("viewbox", "viewBox");
  1607. token.adjust_attribute_name("viewtarget", "viewTarget");
  1608. token.adjust_attribute_name("xchannelselector", "xChannelSelector");
  1609. token.adjust_attribute_name("ychannelselector", "yChannelSelector");
  1610. token.adjust_attribute_name("zoomandpan", "zoomAndPan");
  1611. }
  1612. void HTMLDocumentParser::adjust_foreign_attributes(HTMLToken& token)
  1613. {
  1614. token.adjust_foreign_attribute("xlink:actuate", "xlink", "actuate", Namespace::XLink);
  1615. token.adjust_foreign_attribute("xlink:arcrole", "xlink", "arcrole", Namespace::XLink);
  1616. token.adjust_foreign_attribute("xlink:href", "xlink", "href", Namespace::XLink);
  1617. token.adjust_foreign_attribute("xlink:role", "xlink", "role", Namespace::XLink);
  1618. token.adjust_foreign_attribute("xlink:show", "xlink", "show", Namespace::XLink);
  1619. token.adjust_foreign_attribute("xlink:title", "xlink", "title", Namespace::XLink);
  1620. token.adjust_foreign_attribute("xlink:type", "xlink", "type", Namespace::XLink);
  1621. token.adjust_foreign_attribute("xml:lang", "xml", "lang", Namespace::XML);
  1622. token.adjust_foreign_attribute("xml:space", "xml", "space", Namespace::XML);
  1623. token.adjust_foreign_attribute("xmlns", "", "xmlns", Namespace::XMLNS);
  1624. token.adjust_foreign_attribute("xmlns:xlink", "xmlns", "xlink", Namespace::XMLNS);
  1625. }
  1626. void HTMLDocumentParser::increment_script_nesting_level()
  1627. {
  1628. ++m_script_nesting_level;
  1629. }
  1630. void HTMLDocumentParser::decrement_script_nesting_level()
  1631. {
  1632. VERIFY(m_script_nesting_level);
  1633. --m_script_nesting_level;
  1634. }
  1635. void HTMLDocumentParser::handle_text(HTMLToken& token)
  1636. {
  1637. if (token.is_character()) {
  1638. insert_character(token.code_point());
  1639. return;
  1640. }
  1641. if (token.is_end_of_file()) {
  1642. PARSE_ERROR();
  1643. if (current_node().local_name() == HTML::TagNames::script)
  1644. downcast<HTMLScriptElement>(current_node()).set_already_started({}, true);
  1645. m_stack_of_open_elements.pop();
  1646. m_insertion_mode = m_original_insertion_mode;
  1647. process_using_the_rules_for(m_insertion_mode, token);
  1648. return;
  1649. }
  1650. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::script) {
  1651. // Make sure the <script> element has up-to-date text content before preparing the script.
  1652. flush_character_insertions();
  1653. NonnullRefPtr<HTMLScriptElement> script = downcast<HTMLScriptElement>(current_node());
  1654. m_stack_of_open_elements.pop();
  1655. m_insertion_mode = m_original_insertion_mode;
  1656. // FIXME: Handle tokenizer insertion point stuff here.
  1657. increment_script_nesting_level();
  1658. script->prepare_script({});
  1659. decrement_script_nesting_level();
  1660. if (script_nesting_level() == 0)
  1661. m_parser_pause_flag = false;
  1662. // FIXME: Handle tokenizer insertion point stuff here too.
  1663. while (document().pending_parsing_blocking_script()) {
  1664. if (script_nesting_level() != 0) {
  1665. m_parser_pause_flag = true;
  1666. // FIXME: Abort the processing of any nested invocations of the tokenizer,
  1667. // yielding control back to the caller. (Tokenization will resume when
  1668. // the caller returns to the "outer" tree construction stage.)
  1669. TODO();
  1670. } else {
  1671. auto the_script = document().take_pending_parsing_blocking_script({});
  1672. m_tokenizer.set_blocked(true);
  1673. // FIXME: If the parser's Document has a style sheet that is blocking scripts
  1674. // or the script's "ready to be parser-executed" flag is not set:
  1675. // spin the event loop until the parser's Document has no style sheet
  1676. // that is blocking scripts and the script's "ready to be parser-executed"
  1677. // flag is set.
  1678. if (the_script->failed_to_load())
  1679. return;
  1680. VERIFY(the_script->is_ready_to_be_parser_executed());
  1681. if (m_aborted)
  1682. return;
  1683. m_tokenizer.set_blocked(false);
  1684. // FIXME: Handle tokenizer insertion point stuff here too.
  1685. VERIFY(script_nesting_level() == 0);
  1686. increment_script_nesting_level();
  1687. the_script->execute_script();
  1688. decrement_script_nesting_level();
  1689. VERIFY(script_nesting_level() == 0);
  1690. m_parser_pause_flag = false;
  1691. // FIXME: Handle tokenizer insertion point stuff here too.
  1692. }
  1693. }
  1694. return;
  1695. }
  1696. if (token.is_end_tag()) {
  1697. m_stack_of_open_elements.pop();
  1698. m_insertion_mode = m_original_insertion_mode;
  1699. return;
  1700. }
  1701. TODO();
  1702. }
  1703. void HTMLDocumentParser::clear_the_stack_back_to_a_table_context()
  1704. {
  1705. while (!current_node().local_name().is_one_of(HTML::TagNames::table, HTML::TagNames::template_, HTML::TagNames::html))
  1706. m_stack_of_open_elements.pop();
  1707. if (current_node().local_name() == HTML::TagNames::html)
  1708. VERIFY(m_parsing_fragment);
  1709. }
  1710. void HTMLDocumentParser::clear_the_stack_back_to_a_table_row_context()
  1711. {
  1712. while (!current_node().local_name().is_one_of(HTML::TagNames::tr, HTML::TagNames::template_, HTML::TagNames::html))
  1713. m_stack_of_open_elements.pop();
  1714. if (current_node().local_name() == HTML::TagNames::html)
  1715. VERIFY(m_parsing_fragment);
  1716. }
  1717. void HTMLDocumentParser::clear_the_stack_back_to_a_table_body_context()
  1718. {
  1719. while (!current_node().local_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead, HTML::TagNames::template_, HTML::TagNames::html))
  1720. m_stack_of_open_elements.pop();
  1721. if (current_node().local_name() == HTML::TagNames::html)
  1722. VERIFY(m_parsing_fragment);
  1723. }
  1724. void HTMLDocumentParser::handle_in_row(HTMLToken& token)
  1725. {
  1726. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::th, HTML::TagNames::td)) {
  1727. clear_the_stack_back_to_a_table_row_context();
  1728. insert_html_element(token);
  1729. m_insertion_mode = InsertionMode::InCell;
  1730. m_list_of_active_formatting_elements.add_marker();
  1731. return;
  1732. }
  1733. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::tr) {
  1734. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tr)) {
  1735. PARSE_ERROR();
  1736. return;
  1737. }
  1738. clear_the_stack_back_to_a_table_row_context();
  1739. m_stack_of_open_elements.pop();
  1740. m_insertion_mode = InsertionMode::InTableBody;
  1741. return;
  1742. }
  1743. 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))
  1744. || (token.is_end_tag() && token.tag_name() == HTML::TagNames::table)) {
  1745. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tr)) {
  1746. PARSE_ERROR();
  1747. return;
  1748. }
  1749. clear_the_stack_back_to_a_table_row_context();
  1750. m_stack_of_open_elements.pop();
  1751. m_insertion_mode = InsertionMode::InTableBody;
  1752. process_using_the_rules_for(m_insertion_mode, token);
  1753. return;
  1754. }
  1755. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead)) {
  1756. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1757. PARSE_ERROR();
  1758. return;
  1759. }
  1760. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tr)) {
  1761. return;
  1762. }
  1763. clear_the_stack_back_to_a_table_row_context();
  1764. m_stack_of_open_elements.pop();
  1765. m_insertion_mode = InsertionMode::InTableBody;
  1766. process_using_the_rules_for(m_insertion_mode, token);
  1767. return;
  1768. }
  1769. 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)) {
  1770. PARSE_ERROR();
  1771. return;
  1772. }
  1773. process_using_the_rules_for(InsertionMode::InTable, token);
  1774. }
  1775. void HTMLDocumentParser::close_the_cell()
  1776. {
  1777. generate_implied_end_tags();
  1778. if (!current_node().local_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
  1779. PARSE_ERROR();
  1780. }
  1781. while (!current_node().local_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th))
  1782. m_stack_of_open_elements.pop();
  1783. m_stack_of_open_elements.pop();
  1784. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1785. m_insertion_mode = InsertionMode::InRow;
  1786. }
  1787. void HTMLDocumentParser::handle_in_cell(HTMLToken& token)
  1788. {
  1789. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
  1790. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1791. PARSE_ERROR();
  1792. return;
  1793. }
  1794. generate_implied_end_tags();
  1795. if (current_node().local_name() != token.tag_name()) {
  1796. PARSE_ERROR();
  1797. }
  1798. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
  1799. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  1800. m_insertion_mode = InsertionMode::InRow;
  1801. return;
  1802. }
  1803. 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)) {
  1804. 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)) {
  1805. VERIFY(m_parsing_fragment);
  1806. PARSE_ERROR();
  1807. return;
  1808. }
  1809. close_the_cell();
  1810. process_using_the_rules_for(m_insertion_mode, token);
  1811. return;
  1812. }
  1813. 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)) {
  1814. PARSE_ERROR();
  1815. return;
  1816. }
  1817. 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)) {
  1818. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1819. PARSE_ERROR();
  1820. return;
  1821. }
  1822. close_the_cell();
  1823. // Reprocess the token.
  1824. process_using_the_rules_for(m_insertion_mode, token);
  1825. return;
  1826. }
  1827. process_using_the_rules_for(InsertionMode::InBody, token);
  1828. }
  1829. void HTMLDocumentParser::handle_in_table_text(HTMLToken& token)
  1830. {
  1831. if (token.is_character()) {
  1832. if (token.code_point() == 0) {
  1833. PARSE_ERROR();
  1834. return;
  1835. }
  1836. m_pending_table_character_tokens.append(token);
  1837. return;
  1838. }
  1839. for (auto& pending_token : m_pending_table_character_tokens) {
  1840. VERIFY(pending_token.is_character());
  1841. if (!pending_token.is_parser_whitespace()) {
  1842. // If any of the tokens in the pending table character tokens list
  1843. // are character tokens that are not ASCII whitespace, then this is a parse error:
  1844. // reprocess the character tokens in the pending table character tokens list using
  1845. // the rules given in the "anything else" entry in the "in table" insertion mode.
  1846. PARSE_ERROR();
  1847. m_foster_parenting = true;
  1848. process_using_the_rules_for(InsertionMode::InBody, token);
  1849. m_foster_parenting = false;
  1850. return;
  1851. }
  1852. }
  1853. for (auto& pending_token : m_pending_table_character_tokens) {
  1854. insert_character(pending_token.code_point());
  1855. }
  1856. m_insertion_mode = m_original_insertion_mode;
  1857. process_using_the_rules_for(m_insertion_mode, token);
  1858. }
  1859. void HTMLDocumentParser::handle_in_table_body(HTMLToken& token)
  1860. {
  1861. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::tr) {
  1862. clear_the_stack_back_to_a_table_body_context();
  1863. insert_html_element(token);
  1864. m_insertion_mode = InsertionMode::InRow;
  1865. return;
  1866. }
  1867. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::th, HTML::TagNames::td)) {
  1868. PARSE_ERROR();
  1869. clear_the_stack_back_to_a_table_body_context();
  1870. insert_html_element(HTMLToken::make_start_tag(HTML::TagNames::tr));
  1871. m_insertion_mode = InsertionMode::InRow;
  1872. process_using_the_rules_for(m_insertion_mode, token);
  1873. return;
  1874. }
  1875. if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead)) {
  1876. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
  1877. PARSE_ERROR();
  1878. return;
  1879. }
  1880. clear_the_stack_back_to_a_table_body_context();
  1881. m_stack_of_open_elements.pop();
  1882. m_insertion_mode = InsertionMode::InTable;
  1883. return;
  1884. }
  1885. 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))
  1886. || (token.is_end_tag() && token.tag_name() == HTML::TagNames::table)) {
  1887. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tbody)
  1888. && !m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::thead)
  1889. && !m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tfoot)) {
  1890. PARSE_ERROR();
  1891. return;
  1892. }
  1893. clear_the_stack_back_to_a_table_body_context();
  1894. m_stack_of_open_elements.pop();
  1895. m_insertion_mode = InsertionMode::InTable;
  1896. process_using_the_rules_for(InsertionMode::InTable, token);
  1897. return;
  1898. }
  1899. 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)) {
  1900. PARSE_ERROR();
  1901. return;
  1902. }
  1903. process_using_the_rules_for(InsertionMode::InTable, token);
  1904. }
  1905. void HTMLDocumentParser::handle_in_table(HTMLToken& token)
  1906. {
  1907. 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)) {
  1908. m_pending_table_character_tokens.clear();
  1909. m_original_insertion_mode = m_insertion_mode;
  1910. m_insertion_mode = InsertionMode::InTableText;
  1911. process_using_the_rules_for(InsertionMode::InTableText, token);
  1912. return;
  1913. }
  1914. if (token.is_comment()) {
  1915. insert_comment(token);
  1916. return;
  1917. }
  1918. if (token.is_doctype()) {
  1919. PARSE_ERROR();
  1920. return;
  1921. }
  1922. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::caption) {
  1923. clear_the_stack_back_to_a_table_context();
  1924. m_list_of_active_formatting_elements.add_marker();
  1925. insert_html_element(token);
  1926. m_insertion_mode = InsertionMode::InCaption;
  1927. return;
  1928. }
  1929. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::colgroup) {
  1930. clear_the_stack_back_to_a_table_context();
  1931. insert_html_element(token);
  1932. m_insertion_mode = InsertionMode::InColumnGroup;
  1933. return;
  1934. }
  1935. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::col) {
  1936. clear_the_stack_back_to_a_table_context();
  1937. insert_html_element(HTMLToken::make_start_tag(HTML::TagNames::colgroup));
  1938. m_insertion_mode = InsertionMode::InColumnGroup;
  1939. process_using_the_rules_for(m_insertion_mode, token);
  1940. return;
  1941. }
  1942. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead)) {
  1943. clear_the_stack_back_to_a_table_context();
  1944. insert_html_element(token);
  1945. m_insertion_mode = InsertionMode::InTableBody;
  1946. return;
  1947. }
  1948. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th, HTML::TagNames::tr)) {
  1949. clear_the_stack_back_to_a_table_context();
  1950. insert_html_element(HTMLToken::make_start_tag(HTML::TagNames::tbody));
  1951. m_insertion_mode = InsertionMode::InTableBody;
  1952. process_using_the_rules_for(m_insertion_mode, token);
  1953. return;
  1954. }
  1955. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::table) {
  1956. PARSE_ERROR();
  1957. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::table))
  1958. return;
  1959. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::table);
  1960. reset_the_insertion_mode_appropriately();
  1961. process_using_the_rules_for(m_insertion_mode, token);
  1962. return;
  1963. }
  1964. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::table) {
  1965. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::table)) {
  1966. PARSE_ERROR();
  1967. return;
  1968. }
  1969. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::table);
  1970. reset_the_insertion_mode_appropriately();
  1971. return;
  1972. }
  1973. 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)) {
  1974. PARSE_ERROR();
  1975. return;
  1976. }
  1977. if ((token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::style, HTML::TagNames::script, HTML::TagNames::template_))
  1978. || (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_)) {
  1979. process_using_the_rules_for(InsertionMode::InHead, token);
  1980. return;
  1981. }
  1982. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::input) {
  1983. auto type_attribute = token.attribute(HTML::AttributeNames::type);
  1984. if (type_attribute.is_null() || !type_attribute.equals_ignoring_case("hidden")) {
  1985. goto AnythingElse;
  1986. }
  1987. PARSE_ERROR();
  1988. insert_html_element(token);
  1989. // FIXME: Is this the correct interpretation of "Pop that input element off the stack of open elements."?
  1990. // Because this wording is the first time it's seen in the spec.
  1991. // Other times it's worded as: "Immediately pop the current node off the stack of open elements."
  1992. m_stack_of_open_elements.pop();
  1993. token.acknowledge_self_closing_flag_if_set();
  1994. return;
  1995. }
  1996. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::form) {
  1997. PARSE_ERROR();
  1998. if (m_form_element || m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
  1999. return;
  2000. }
  2001. m_form_element = downcast<HTMLFormElement>(*insert_html_element(token));
  2002. // FIXME: See previous FIXME, as this is the same situation but for form.
  2003. m_stack_of_open_elements.pop();
  2004. return;
  2005. }
  2006. if (token.is_end_of_file()) {
  2007. process_using_the_rules_for(InsertionMode::InBody, token);
  2008. return;
  2009. }
  2010. AnythingElse:
  2011. PARSE_ERROR();
  2012. m_foster_parenting = true;
  2013. process_using_the_rules_for(InsertionMode::InBody, token);
  2014. m_foster_parenting = false;
  2015. }
  2016. void HTMLDocumentParser::handle_in_select_in_table(HTMLToken& token)
  2017. {
  2018. 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)) {
  2019. PARSE_ERROR();
  2020. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
  2021. reset_the_insertion_mode_appropriately();
  2022. process_using_the_rules_for(m_insertion_mode, token);
  2023. return;
  2024. }
  2025. 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)) {
  2026. PARSE_ERROR();
  2027. if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name()))
  2028. return;
  2029. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
  2030. reset_the_insertion_mode_appropriately();
  2031. process_using_the_rules_for(m_insertion_mode, token);
  2032. return;
  2033. }
  2034. process_using_the_rules_for(InsertionMode::InSelect, token);
  2035. }
  2036. void HTMLDocumentParser::handle_in_select(HTMLToken& token)
  2037. {
  2038. if (token.is_character()) {
  2039. if (token.code_point() == 0) {
  2040. PARSE_ERROR();
  2041. return;
  2042. }
  2043. insert_character(token.code_point());
  2044. return;
  2045. }
  2046. if (token.is_comment()) {
  2047. insert_comment(token);
  2048. return;
  2049. }
  2050. if (token.is_doctype()) {
  2051. PARSE_ERROR();
  2052. return;
  2053. }
  2054. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  2055. process_using_the_rules_for(InsertionMode::InBody, token);
  2056. return;
  2057. }
  2058. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::option) {
  2059. if (current_node().local_name() == HTML::TagNames::option) {
  2060. m_stack_of_open_elements.pop();
  2061. }
  2062. insert_html_element(token);
  2063. return;
  2064. }
  2065. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::optgroup) {
  2066. if (current_node().local_name() == HTML::TagNames::option) {
  2067. m_stack_of_open_elements.pop();
  2068. }
  2069. if (current_node().local_name() == HTML::TagNames::optgroup) {
  2070. m_stack_of_open_elements.pop();
  2071. }
  2072. insert_html_element(token);
  2073. return;
  2074. }
  2075. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::optgroup) {
  2076. if (current_node().local_name() == HTML::TagNames::option && node_before_current_node().local_name() == HTML::TagNames::optgroup)
  2077. m_stack_of_open_elements.pop();
  2078. if (current_node().local_name() == HTML::TagNames::optgroup) {
  2079. m_stack_of_open_elements.pop();
  2080. } else {
  2081. PARSE_ERROR();
  2082. return;
  2083. }
  2084. return;
  2085. }
  2086. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::option) {
  2087. if (current_node().local_name() == HTML::TagNames::option) {
  2088. m_stack_of_open_elements.pop();
  2089. } else {
  2090. PARSE_ERROR();
  2091. return;
  2092. }
  2093. return;
  2094. }
  2095. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::select) {
  2096. if (!m_stack_of_open_elements.has_in_select_scope(HTML::TagNames::select)) {
  2097. VERIFY(m_parsing_fragment);
  2098. PARSE_ERROR();
  2099. return;
  2100. }
  2101. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
  2102. reset_the_insertion_mode_appropriately();
  2103. return;
  2104. }
  2105. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::select) {
  2106. PARSE_ERROR();
  2107. if (!m_stack_of_open_elements.has_in_select_scope(HTML::TagNames::select)) {
  2108. VERIFY(m_parsing_fragment);
  2109. return;
  2110. }
  2111. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
  2112. reset_the_insertion_mode_appropriately();
  2113. return;
  2114. }
  2115. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::input, HTML::TagNames::keygen, HTML::TagNames::textarea)) {
  2116. PARSE_ERROR();
  2117. if (!m_stack_of_open_elements.has_in_select_scope(HTML::TagNames::select)) {
  2118. VERIFY(m_parsing_fragment);
  2119. return;
  2120. }
  2121. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
  2122. reset_the_insertion_mode_appropriately();
  2123. process_using_the_rules_for(m_insertion_mode, token);
  2124. return;
  2125. }
  2126. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::script, HTML::TagNames::template_)) {
  2127. process_using_the_rules_for(InsertionMode::InHead, token);
  2128. return;
  2129. }
  2130. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
  2131. process_using_the_rules_for(InsertionMode::InHead, token);
  2132. return;
  2133. }
  2134. if (token.is_end_of_file()) {
  2135. process_using_the_rules_for(InsertionMode::InBody, token);
  2136. return;
  2137. }
  2138. PARSE_ERROR();
  2139. }
  2140. void HTMLDocumentParser::handle_in_caption(HTMLToken& token)
  2141. {
  2142. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::caption) {
  2143. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::caption)) {
  2144. VERIFY(m_parsing_fragment);
  2145. PARSE_ERROR();
  2146. return;
  2147. }
  2148. generate_implied_end_tags();
  2149. if (current_node().local_name() != HTML::TagNames::caption)
  2150. PARSE_ERROR();
  2151. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::caption);
  2152. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  2153. m_insertion_mode = InsertionMode::InTable;
  2154. return;
  2155. }
  2156. 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))
  2157. || (token.is_end_tag() && token.tag_name() == HTML::TagNames::table)) {
  2158. if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::caption)) {
  2159. VERIFY(m_parsing_fragment);
  2160. PARSE_ERROR();
  2161. return;
  2162. }
  2163. generate_implied_end_tags();
  2164. if (current_node().local_name() != HTML::TagNames::caption)
  2165. PARSE_ERROR();
  2166. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::caption);
  2167. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  2168. m_insertion_mode = InsertionMode::InTable;
  2169. process_using_the_rules_for(m_insertion_mode, token);
  2170. return;
  2171. }
  2172. 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)) {
  2173. PARSE_ERROR();
  2174. return;
  2175. }
  2176. process_using_the_rules_for(InsertionMode::InBody, token);
  2177. }
  2178. void HTMLDocumentParser::handle_in_column_group(HTMLToken& token)
  2179. {
  2180. if (token.is_character() && token.is_parser_whitespace()) {
  2181. insert_character(token.code_point());
  2182. return;
  2183. }
  2184. if (token.is_comment()) {
  2185. insert_comment(token);
  2186. return;
  2187. }
  2188. if (token.is_doctype()) {
  2189. PARSE_ERROR();
  2190. return;
  2191. }
  2192. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  2193. process_using_the_rules_for(InsertionMode::InBody, token);
  2194. return;
  2195. }
  2196. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::col) {
  2197. insert_html_element(token);
  2198. m_stack_of_open_elements.pop();
  2199. token.acknowledge_self_closing_flag_if_set();
  2200. return;
  2201. }
  2202. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::colgroup) {
  2203. if (current_node().local_name() != HTML::TagNames::colgroup) {
  2204. PARSE_ERROR();
  2205. return;
  2206. }
  2207. m_stack_of_open_elements.pop();
  2208. m_insertion_mode = InsertionMode::InTable;
  2209. return;
  2210. }
  2211. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::col) {
  2212. PARSE_ERROR();
  2213. return;
  2214. }
  2215. if ((token.is_start_tag() || token.is_end_tag()) && token.tag_name() == HTML::TagNames::template_) {
  2216. process_using_the_rules_for(InsertionMode::InHead, token);
  2217. return;
  2218. }
  2219. if (token.is_end_of_file()) {
  2220. process_using_the_rules_for(InsertionMode::InBody, token);
  2221. return;
  2222. }
  2223. if (current_node().local_name() != HTML::TagNames::colgroup) {
  2224. PARSE_ERROR();
  2225. return;
  2226. }
  2227. m_stack_of_open_elements.pop();
  2228. m_insertion_mode = InsertionMode::InTable;
  2229. process_using_the_rules_for(m_insertion_mode, token);
  2230. }
  2231. void HTMLDocumentParser::handle_in_template(HTMLToken& token)
  2232. {
  2233. if (token.is_character() || token.is_comment() || token.is_doctype()) {
  2234. process_using_the_rules_for(InsertionMode::InBody, token);
  2235. return;
  2236. }
  2237. 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)) {
  2238. process_using_the_rules_for(InsertionMode::InHead, token);
  2239. return;
  2240. }
  2241. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
  2242. process_using_the_rules_for(InsertionMode::InHead, token);
  2243. return;
  2244. }
  2245. 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)) {
  2246. m_stack_of_template_insertion_modes.take_last();
  2247. m_stack_of_template_insertion_modes.append(InsertionMode::InTable);
  2248. m_insertion_mode = InsertionMode::InTable;
  2249. process_using_the_rules_for(m_insertion_mode, token);
  2250. return;
  2251. }
  2252. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::col) {
  2253. m_stack_of_template_insertion_modes.take_last();
  2254. m_stack_of_template_insertion_modes.append(InsertionMode::InColumnGroup);
  2255. m_insertion_mode = InsertionMode::InColumnGroup;
  2256. process_using_the_rules_for(m_insertion_mode, token);
  2257. return;
  2258. }
  2259. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::tr) {
  2260. m_stack_of_template_insertion_modes.take_last();
  2261. m_stack_of_template_insertion_modes.append(InsertionMode::InTableBody);
  2262. m_insertion_mode = InsertionMode::InTableBody;
  2263. process_using_the_rules_for(m_insertion_mode, token);
  2264. return;
  2265. }
  2266. if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
  2267. m_stack_of_template_insertion_modes.take_last();
  2268. m_stack_of_template_insertion_modes.append(InsertionMode::InRow);
  2269. m_insertion_mode = InsertionMode::InRow;
  2270. process_using_the_rules_for(m_insertion_mode, token);
  2271. return;
  2272. }
  2273. if (token.is_start_tag()) {
  2274. m_stack_of_template_insertion_modes.take_last();
  2275. m_stack_of_template_insertion_modes.append(InsertionMode::InBody);
  2276. m_insertion_mode = InsertionMode::InBody;
  2277. process_using_the_rules_for(m_insertion_mode, token);
  2278. return;
  2279. }
  2280. if (token.is_end_tag()) {
  2281. PARSE_ERROR();
  2282. return;
  2283. }
  2284. if (token.is_end_of_file()) {
  2285. if (!m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
  2286. VERIFY(m_parsing_fragment);
  2287. stop_parsing();
  2288. return;
  2289. }
  2290. PARSE_ERROR();
  2291. m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::template_);
  2292. m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
  2293. m_stack_of_template_insertion_modes.take_last();
  2294. reset_the_insertion_mode_appropriately();
  2295. process_using_the_rules_for(m_insertion_mode, token);
  2296. }
  2297. }
  2298. void HTMLDocumentParser::handle_in_frameset(HTMLToken& token)
  2299. {
  2300. if (token.is_character() && token.is_parser_whitespace()) {
  2301. insert_character(token.code_point());
  2302. return;
  2303. }
  2304. if (token.is_comment()) {
  2305. insert_comment(token);
  2306. return;
  2307. }
  2308. if (token.is_doctype()) {
  2309. PARSE_ERROR();
  2310. return;
  2311. }
  2312. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  2313. process_using_the_rules_for(InsertionMode::InBody, token);
  2314. return;
  2315. }
  2316. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::frameset) {
  2317. insert_html_element(token);
  2318. return;
  2319. }
  2320. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::frameset) {
  2321. // FIXME: If the current node is the root html element, then this is a parse error; ignore the token. (fragment case)
  2322. m_stack_of_open_elements.pop();
  2323. if (!m_parsing_fragment && current_node().local_name() != HTML::TagNames::frameset) {
  2324. m_insertion_mode = InsertionMode::AfterFrameset;
  2325. }
  2326. return;
  2327. }
  2328. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::frame) {
  2329. insert_html_element(token);
  2330. m_stack_of_open_elements.pop();
  2331. token.acknowledge_self_closing_flag_if_set();
  2332. return;
  2333. }
  2334. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::noframes) {
  2335. process_using_the_rules_for(InsertionMode::InHead, token);
  2336. return;
  2337. }
  2338. if (token.is_end_of_file()) {
  2339. //FIXME: If the current node is not the root html element, then this is a parse error.
  2340. stop_parsing();
  2341. return;
  2342. }
  2343. PARSE_ERROR();
  2344. }
  2345. void HTMLDocumentParser::handle_after_frameset(HTMLToken& token)
  2346. {
  2347. if (token.is_character() && token.is_parser_whitespace()) {
  2348. insert_character(token.code_point());
  2349. return;
  2350. }
  2351. if (token.is_comment()) {
  2352. insert_comment(token);
  2353. return;
  2354. }
  2355. if (token.is_doctype()) {
  2356. PARSE_ERROR();
  2357. return;
  2358. }
  2359. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
  2360. process_using_the_rules_for(InsertionMode::InBody, token);
  2361. return;
  2362. }
  2363. if (token.is_end_tag() && token.tag_name() == HTML::TagNames::html) {
  2364. m_insertion_mode = InsertionMode::AfterAfterFrameset;
  2365. return;
  2366. }
  2367. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::noframes) {
  2368. process_using_the_rules_for(InsertionMode::InHead, token);
  2369. return;
  2370. }
  2371. if (token.is_end_of_file()) {
  2372. stop_parsing();
  2373. return;
  2374. }
  2375. PARSE_ERROR();
  2376. }
  2377. void HTMLDocumentParser::handle_after_after_frameset(HTMLToken& token)
  2378. {
  2379. if (token.is_comment()) {
  2380. auto comment = adopt(*new DOM::Comment(document(), token.m_comment_or_character.data.to_string()));
  2381. document().append_child(move(comment));
  2382. return;
  2383. }
  2384. if (token.is_doctype() || token.is_parser_whitespace() || (token.is_start_tag() && token.tag_name() == HTML::TagNames::html)) {
  2385. process_using_the_rules_for(InsertionMode::InBody, token);
  2386. return;
  2387. }
  2388. if (token.is_end_of_file()) {
  2389. stop_parsing();
  2390. return;
  2391. }
  2392. if (token.is_start_tag() && token.tag_name() == HTML::TagNames::noframes) {
  2393. process_using_the_rules_for(InsertionMode::InHead, token);
  2394. return;
  2395. }
  2396. PARSE_ERROR();
  2397. }
  2398. void HTMLDocumentParser::process_using_the_rules_for_foreign_content(HTMLToken& token)
  2399. {
  2400. if (token.is_character()) {
  2401. if (token.code_point() == 0) {
  2402. PARSE_ERROR();
  2403. insert_character(0xFFFD);
  2404. return;
  2405. }
  2406. if (token.is_parser_whitespace()) {
  2407. insert_character(token.code_point());
  2408. return;
  2409. }
  2410. insert_character(token.code_point());
  2411. m_frameset_ok = false;
  2412. return;
  2413. }
  2414. if (token.is_comment()) {
  2415. insert_comment(token);
  2416. return;
  2417. }
  2418. if (token.is_doctype()) {
  2419. PARSE_ERROR();
  2420. return;
  2421. }
  2422. 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))
  2423. || (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)))) {
  2424. PARSE_ERROR();
  2425. if (m_parsing_fragment) {
  2426. goto AnyOtherStartTag;
  2427. }
  2428. TODO();
  2429. }
  2430. if (token.is_start_tag()) {
  2431. AnyOtherStartTag:
  2432. if (adjusted_current_node().namespace_() == Namespace::MathML) {
  2433. adjust_mathml_attributes(token);
  2434. } else if (adjusted_current_node().namespace_() == Namespace::SVG) {
  2435. adjust_svg_tag_names(token);
  2436. adjust_svg_attributes(token);
  2437. }
  2438. adjust_foreign_attributes(token);
  2439. insert_foreign_element(token, adjusted_current_node().namespace_());
  2440. if (token.is_self_closing()) {
  2441. if (token.tag_name() == SVG::TagNames::script && current_node().namespace_() == Namespace::SVG) {
  2442. token.acknowledge_self_closing_flag_if_set();
  2443. goto ScriptEndTag;
  2444. }
  2445. m_stack_of_open_elements.pop();
  2446. token.acknowledge_self_closing_flag_if_set();
  2447. }
  2448. return;
  2449. }
  2450. if (token.is_end_tag() && current_node().namespace_() == Namespace::SVG && current_node().tag_name() == SVG::TagNames::script) {
  2451. ScriptEndTag:
  2452. m_stack_of_open_elements.pop();
  2453. TODO();
  2454. }
  2455. if (token.is_end_tag()) {
  2456. RefPtr<DOM::Element> node = current_node();
  2457. // FIXME: Not sure if this is the correct to_lowercase, as the specification says "to ASCII lowercase"
  2458. if (node->tag_name().to_lowercase() != token.tag_name())
  2459. PARSE_ERROR();
  2460. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  2461. if (node == m_stack_of_open_elements.first()) {
  2462. VERIFY(m_parsing_fragment);
  2463. return;
  2464. }
  2465. // FIXME: See the above FIXME
  2466. if (node->tag_name().to_lowercase() == token.tag_name()) {
  2467. while (current_node() != node)
  2468. m_stack_of_open_elements.pop();
  2469. m_stack_of_open_elements.pop();
  2470. return;
  2471. }
  2472. node = m_stack_of_open_elements.elements().at(i - 1);
  2473. if (node->namespace_() != Namespace::HTML)
  2474. continue;
  2475. process_using_the_rules_for(m_insertion_mode, token);
  2476. return;
  2477. }
  2478. }
  2479. VERIFY_NOT_REACHED();
  2480. }
  2481. // https://html.spec.whatwg.org/multipage/parsing.html#reset-the-insertion-mode-appropriately
  2482. void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
  2483. {
  2484. for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
  2485. bool last = i == 0;
  2486. // NOTE: When parsing fragments, we substitute the context element for the root of the stack of open elements.
  2487. RefPtr<DOM::Element> node;
  2488. if (last && m_parsing_fragment) {
  2489. node = m_context_element;
  2490. } else {
  2491. node = m_stack_of_open_elements.elements().at(i);
  2492. }
  2493. if (node->local_name() == HTML::TagNames::select) {
  2494. if (!last) {
  2495. for (ssize_t j = i; j > 0; --j) {
  2496. auto& ancestor = m_stack_of_open_elements.elements().at(j - 1);
  2497. if (is<HTMLTemplateElement>(ancestor))
  2498. break;
  2499. if (is<HTMLTableElement>(ancestor)) {
  2500. m_insertion_mode = InsertionMode::InSelectInTable;
  2501. return;
  2502. }
  2503. }
  2504. }
  2505. m_insertion_mode = InsertionMode::InSelect;
  2506. return;
  2507. }
  2508. if (!last && node->local_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
  2509. m_insertion_mode = InsertionMode::InCell;
  2510. return;
  2511. }
  2512. if (node->local_name() == HTML::TagNames::tr) {
  2513. m_insertion_mode = InsertionMode::InRow;
  2514. return;
  2515. }
  2516. if (node->local_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::thead, HTML::TagNames::tfoot)) {
  2517. m_insertion_mode = InsertionMode::InTableBody;
  2518. return;
  2519. }
  2520. if (node->local_name() == HTML::TagNames::caption) {
  2521. m_insertion_mode = InsertionMode::InCaption;
  2522. return;
  2523. }
  2524. if (node->local_name() == HTML::TagNames::colgroup) {
  2525. m_insertion_mode = InsertionMode::InColumnGroup;
  2526. return;
  2527. }
  2528. if (node->local_name() == HTML::TagNames::table) {
  2529. m_insertion_mode = InsertionMode::InTable;
  2530. return;
  2531. }
  2532. if (node->local_name() == HTML::TagNames::template_) {
  2533. m_insertion_mode = m_stack_of_template_insertion_modes.last();
  2534. return;
  2535. }
  2536. if (!last && node->local_name() == HTML::TagNames::head) {
  2537. m_insertion_mode = InsertionMode::InHead;
  2538. return;
  2539. }
  2540. if (node->local_name() == HTML::TagNames::body) {
  2541. m_insertion_mode = InsertionMode::InBody;
  2542. return;
  2543. }
  2544. if (node->local_name() == HTML::TagNames::frameset) {
  2545. VERIFY(m_parsing_fragment);
  2546. m_insertion_mode = InsertionMode::InFrameset;
  2547. return;
  2548. }
  2549. if (node->local_name() == HTML::TagNames::html) {
  2550. if (!m_head_element) {
  2551. VERIFY(m_parsing_fragment);
  2552. m_insertion_mode = InsertionMode::BeforeHead;
  2553. return;
  2554. }
  2555. m_insertion_mode = InsertionMode::AfterHead;
  2556. return;
  2557. }
  2558. }
  2559. VERIFY(m_parsing_fragment);
  2560. m_insertion_mode = InsertionMode::InBody;
  2561. }
  2562. const char* HTMLDocumentParser::insertion_mode_name() const
  2563. {
  2564. switch (m_insertion_mode) {
  2565. #define __ENUMERATE_INSERTION_MODE(mode) \
  2566. case InsertionMode::mode: \
  2567. return #mode;
  2568. ENUMERATE_INSERTION_MODES
  2569. #undef __ENUMERATE_INSERTION_MODE
  2570. }
  2571. VERIFY_NOT_REACHED();
  2572. }
  2573. DOM::Document& HTMLDocumentParser::document()
  2574. {
  2575. return *m_document;
  2576. }
  2577. NonnullRefPtrVector<DOM::Node> HTMLDocumentParser::parse_html_fragment(DOM::Element& context_element, const StringView& markup)
  2578. {
  2579. auto temp_document = DOM::Document::create();
  2580. HTMLDocumentParser parser(*temp_document, markup, "utf-8");
  2581. parser.m_context_element = context_element;
  2582. parser.m_parsing_fragment = true;
  2583. parser.document().set_quirks_mode(context_element.document().mode());
  2584. if (context_element.local_name().is_one_of(HTML::TagNames::title, HTML::TagNames::textarea)) {
  2585. parser.m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
  2586. } else if (context_element.local_name().is_one_of(HTML::TagNames::style, HTML::TagNames::xmp, HTML::TagNames::iframe, HTML::TagNames::noembed, HTML::TagNames::noframes)) {
  2587. parser.m_tokenizer.switch_to({}, HTMLTokenizer::State::RAWTEXT);
  2588. } else if (context_element.local_name().is_one_of(HTML::TagNames::script)) {
  2589. parser.m_tokenizer.switch_to({}, HTMLTokenizer::State::ScriptData);
  2590. } else if (context_element.local_name().is_one_of(HTML::TagNames::noscript)) {
  2591. if (context_element.document().is_scripting_enabled())
  2592. parser.m_tokenizer.switch_to({}, HTMLTokenizer::State::RAWTEXT);
  2593. } else if (context_element.local_name().is_one_of(HTML::TagNames::plaintext)) {
  2594. parser.m_tokenizer.switch_to({}, HTMLTokenizer::State::PLAINTEXT);
  2595. }
  2596. auto root = create_element(context_element.document(), HTML::TagNames::html, Namespace::HTML);
  2597. parser.document().append_child(root);
  2598. parser.m_stack_of_open_elements.push(root);
  2599. if (context_element.local_name() == HTML::TagNames::template_) {
  2600. parser.m_stack_of_template_insertion_modes.append(InsertionMode::InTemplate);
  2601. }
  2602. // FIXME: Create a start tag token whose name is the local name of context and whose attributes are the attributes of context.
  2603. parser.reset_the_insertion_mode_appropriately();
  2604. for (auto* form_candidate = &context_element; form_candidate; form_candidate = form_candidate->parent_element()) {
  2605. if (is<HTMLFormElement>(*form_candidate)) {
  2606. parser.m_form_element = downcast<HTMLFormElement>(*form_candidate);
  2607. break;
  2608. }
  2609. }
  2610. parser.run(context_element.document().url());
  2611. NonnullRefPtrVector<DOM::Node> children;
  2612. while (RefPtr<DOM::Node> child = root->first_child()) {
  2613. root->remove_child(*child);
  2614. context_element.document().adopt_node(*child);
  2615. children.append(*child);
  2616. }
  2617. return children;
  2618. }
  2619. }