HTMLDocumentParser.cpp 115 KB

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