HTMLDocumentParser.cpp 96 KB

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