HTMLDocumentParser.cpp 95 KB

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