HTMLDocumentParser.cpp 105 KB

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