HTMLDocumentParser.cpp 104 KB

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