HTMLDocumentParser.cpp 104 KB

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