HTMLDocumentParser.cpp 115 KB

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