HTMLDocumentParser.cpp 115 KB

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