HTMLDocumentParser.cpp 115 KB

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