HTMLDocumentParser.cpp 117 KB

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