HTMLDocumentParser.cpp 115 KB

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