HTMLDocumentParser.cpp 96 KB

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