HTMLDocumentParser.cpp 103 KB

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