HTMLDocumentParser.cpp 115 KB

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