123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601 |
- /*
- * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- //#define PARSER_DEBUG
- #include <AK/Utf32View.h>
- #include <LibWeb/DOM/Comment.h>
- #include <LibWeb/DOM/Document.h>
- #include <LibWeb/DOM/DocumentType.h>
- #include <LibWeb/DOM/ElementFactory.h>
- #include <LibWeb/DOM/Event.h>
- #include <LibWeb/DOM/HTMLFormElement.h>
- #include <LibWeb/DOM/HTMLHeadElement.h>
- #include <LibWeb/DOM/HTMLScriptElement.h>
- #include <LibWeb/DOM/Text.h>
- #include <LibWeb/Parser/HTMLDocumentParser.h>
- #include <LibWeb/Parser/HTMLToken.h>
- #define PARSE_ERROR() \
- do { \
- dbg() << "Parse error! " << __PRETTY_FUNCTION__ << " @ " << __LINE__; \
- } while (0)
- namespace Web {
- RefPtr<Document> parse_html_document(const StringView& data, const URL& url, const String& encoding)
- {
- HTMLDocumentParser parser(data, encoding);
- parser.run(url);
- return parser.document();
- }
- HTMLDocumentParser::HTMLDocumentParser(const StringView& input, const String& encoding)
- : m_tokenizer(input, encoding)
- {
- }
- HTMLDocumentParser::~HTMLDocumentParser()
- {
- }
- void HTMLDocumentParser::run(const URL& url)
- {
- m_document = adopt(*new Document);
- m_document->set_url(url);
- m_document->set_source(m_tokenizer.source());
- for (;;) {
- auto optional_token = m_tokenizer.next_token();
- if (!optional_token.has_value())
- break;
- auto& token = optional_token.value();
- #ifdef PARSER_DEBUG
- dbg() << "[" << insertion_mode_name() << "] " << token.to_string();
- #endif
- process_using_the_rules_for(m_insertion_mode, token);
- if (m_stop_parsing) {
- dbg() << "Stop parsing! :^)";
- break;
- }
- }
- flush_character_insertions();
- // "The end"
- auto scripts_to_execute_when_parsing_has_finished = m_document->take_scripts_to_execute_when_parsing_has_finished({});
- for (auto& script : scripts_to_execute_when_parsing_has_finished) {
- script.execute_script();
- }
- m_document->dispatch_event(Event::create("DOMContentLoaded"));
- auto scripts_to_execute_as_soon_as_possible = m_document->take_scripts_to_execute_as_soon_as_possible({});
- for (auto& script : scripts_to_execute_as_soon_as_possible) {
- script.execute_script();
- }
- }
- void HTMLDocumentParser::process_using_the_rules_for(InsertionMode mode, HTMLToken& token)
- {
- switch (mode) {
- case InsertionMode::Initial:
- handle_initial(token);
- break;
- case InsertionMode::BeforeHTML:
- handle_before_html(token);
- break;
- case InsertionMode::BeforeHead:
- handle_before_head(token);
- break;
- case InsertionMode::InHead:
- handle_in_head(token);
- break;
- case InsertionMode::InHeadNoscript:
- handle_in_head_noscript(token);
- break;
- case InsertionMode::AfterHead:
- handle_after_head(token);
- break;
- case InsertionMode::InBody:
- handle_in_body(token);
- break;
- case InsertionMode::AfterBody:
- handle_after_body(token);
- break;
- case InsertionMode::AfterAfterBody:
- handle_after_after_body(token);
- break;
- case InsertionMode::Text:
- handle_text(token);
- break;
- case InsertionMode::InTable:
- handle_in_table(token);
- break;
- case InsertionMode::InTableBody:
- handle_in_table_body(token);
- break;
- case InsertionMode::InRow:
- handle_in_row(token);
- break;
- case InsertionMode::InCell:
- handle_in_cell(token);
- break;
- case InsertionMode::InTableText:
- handle_in_table_text(token);
- break;
- case InsertionMode::InSelectInTable:
- handle_in_select_in_table(token);
- break;
- case InsertionMode::InSelect:
- handle_in_select(token);
- break;
- case InsertionMode::InCaption:
- handle_in_caption(token);
- break;
- case InsertionMode::InColumnGroup:
- handle_in_column_group(token);
- break;
- case InsertionMode::InTemplate:
- handle_in_template(token);
- break;
- case InsertionMode::InFrameset:
- handle_in_frameset(token);
- break;
- case InsertionMode::AfterFrameset:
- handle_after_frameset(token);
- break;
- case InsertionMode::AfterAfterFrameset:
- handle_after_after_frameset(token);
- break;
- default:
- ASSERT_NOT_REACHED();
- }
- }
- void HTMLDocumentParser::handle_initial(HTMLToken& token)
- {
- if (token.is_character() && token.is_parser_whitespace()) {
- return;
- }
- if (token.is_comment()) {
- auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
- document().append_child(move(comment));
- return;
- }
- if (token.is_doctype()) {
- auto doctype = adopt(*new DocumentType(document()));
- doctype->set_name(token.m_doctype.name.to_string());
- document().append_child(move(doctype));
- document().set_quirks_mode(token.m_doctype.force_quirks);
- m_insertion_mode = InsertionMode::BeforeHTML;
- return;
- }
- PARSE_ERROR();
- document().set_quirks_mode(true);
- m_insertion_mode = InsertionMode::BeforeHTML;
- process_using_the_rules_for(InsertionMode::BeforeHTML, token);
- }
- void HTMLDocumentParser::handle_before_html(HTMLToken& token)
- {
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_comment()) {
- auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
- document().append_child(move(comment));
- return;
- }
- if (token.is_character() && token.is_parser_whitespace()) {
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
- auto element = create_element_for(token);
- document().append_child(element);
- m_stack_of_open_elements.push(move(element));
- m_insertion_mode = InsertionMode::BeforeHead;
- return;
- }
- if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::head, HTML::TagNames::body, HTML::TagNames::html, HTML::TagNames::br)) {
- goto AnythingElse;
- }
- if (token.is_end_tag()) {
- PARSE_ERROR();
- return;
- }
- AnythingElse:
- auto element = create_element(document(), HTML::TagNames::html);
- document().append_child(element);
- m_stack_of_open_elements.push(element);
- // 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.
- m_insertion_mode = InsertionMode::BeforeHead;
- process_using_the_rules_for(InsertionMode::BeforeHead, token);
- return;
- }
- Element& HTMLDocumentParser::current_node()
- {
- return m_stack_of_open_elements.current_node();
- }
- Element& HTMLDocumentParser::node_before_current_node()
- {
- return m_stack_of_open_elements.elements().at(m_stack_of_open_elements.elements().size() - 2);
- }
- HTMLDocumentParser::AdjustedInsertionLocation HTMLDocumentParser::find_appropriate_place_for_inserting_node()
- {
- auto& target = current_node();
- 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)) {
- // FIXME: There's a bunch of steps for <template> elements here.
- auto last_table = m_stack_of_open_elements.last_element_with_tag_name(HTML::TagNames::table);
- if (!last_table)
- return { m_stack_of_open_elements.elements().first(), nullptr };
- if (last_table->parent_node())
- return { last_table->parent_node(), last_table };
- return { m_stack_of_open_elements.element_before(*last_table), nullptr };
- }
- return { target, nullptr };
- }
- NonnullRefPtr<Element> HTMLDocumentParser::create_element_for(HTMLToken& token)
- {
- auto element = create_element(document(), token.tag_name());
- for (auto& attribute : token.m_tag.attributes) {
- element->set_attribute(attribute.local_name_builder.to_string(), attribute.value_builder.to_string());
- }
- return element;
- }
- RefPtr<Element> HTMLDocumentParser::insert_html_element(HTMLToken& token)
- {
- auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
- auto element = create_element_for(token);
- // FIXME: Check if it's possible to insert `element` at `adjusted_insertion_location`
- adjusted_insertion_location.parent->insert_before(element, adjusted_insertion_location.insert_before_sibling);
- m_stack_of_open_elements.push(element);
- return element;
- }
- void HTMLDocumentParser::handle_before_head(HTMLToken& token)
- {
- if (token.is_character() && token.is_parser_whitespace()) {
- return;
- }
- if (token.is_comment()) {
- insert_comment(token);
- return;
- }
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::head) {
- auto element = insert_html_element(token);
- m_head_element = to<HTMLHeadElement>(element);
- m_insertion_mode = InsertionMode::InHead;
- return;
- }
- if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::head, HTML::TagNames::body, HTML::TagNames::html, HTML::TagNames::br)) {
- goto AnythingElse;
- }
- if (token.is_end_tag()) {
- PARSE_ERROR();
- return;
- }
- AnythingElse:
- HTMLToken fake_head_token;
- fake_head_token.m_type = HTMLToken::Type::StartTag;
- fake_head_token.m_tag.tag_name.append(HTML::TagNames::head);
- m_head_element = to<HTMLHeadElement>(insert_html_element(fake_head_token));
- m_insertion_mode = InsertionMode::InHead;
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- void HTMLDocumentParser::insert_comment(HTMLToken& token)
- {
- auto data = token.m_comment_or_character.data.to_string();
- auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
- adjusted_insertion_location.parent->insert_before(adopt(*new Comment(document(), data)), adjusted_insertion_location.insert_before_sibling);
- }
- void HTMLDocumentParser::handle_in_head(HTMLToken& token)
- {
- if (token.is_parser_whitespace()) {
- insert_character(token.codepoint());
- return;
- }
- if (token.is_comment()) {
- insert_comment(token);
- return;
- }
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::base, HTML::TagNames::basefont, HTML::TagNames::bgsound, HTML::TagNames::link)) {
- insert_html_element(token);
- m_stack_of_open_elements.pop();
- token.acknowledge_self_closing_flag_if_set();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::meta) {
- auto element = insert_html_element(token);
- m_stack_of_open_elements.pop();
- token.acknowledge_self_closing_flag_if_set();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::title) {
- insert_html_element(token);
- m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
- m_original_insertion_mode = m_insertion_mode;
- m_insertion_mode = InsertionMode::Text;
- return;
- }
- 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)) {
- parse_generic_raw_text_element(token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::noscript && !m_scripting_enabled) {
- insert_html_element(token);
- m_insertion_mode = InsertionMode::InHeadNoscript;
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::script) {
- auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
- auto element = create_element_for(token);
- auto& script_element = to<HTMLScriptElement>(*element);
- script_element.set_parser_document({}, document());
- script_element.set_non_blocking({}, false);
- if (m_parsing_fragment) {
- TODO();
- }
- if (m_invoked_via_document_write) {
- TODO();
- }
- adjusted_insertion_location.parent->insert_before(element, adjusted_insertion_location.insert_before_sibling, false);
- m_stack_of_open_elements.push(element);
- m_tokenizer.switch_to({}, HTMLTokenizer::State::ScriptData);
- m_original_insertion_mode = m_insertion_mode;
- m_insertion_mode = InsertionMode::Text;
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::head) {
- m_stack_of_open_elements.pop();
- m_insertion_mode = InsertionMode::AfterHead;
- return;
- }
- if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::body, HTML::TagNames::html, HTML::TagNames::br)) {
- goto AnythingElse;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::template_) {
- // FIXME: Support this properly
- insert_html_element(token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
- // FIXME: Support this properly
- ASSERT(current_node().tag_name() == HTML::TagNames::template_);
- m_stack_of_open_elements.pop();
- return;
- }
- if ((token.is_start_tag() && token.tag_name() == HTML::TagNames::head) || token.is_end_tag()) {
- PARSE_ERROR();
- return;
- }
- AnythingElse:
- m_stack_of_open_elements.pop();
- m_insertion_mode = InsertionMode::AfterHead;
- process_using_the_rules_for(m_insertion_mode, token);
- }
- void HTMLDocumentParser::handle_in_head_noscript(HTMLToken& token)
- {
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::noscript) {
- m_stack_of_open_elements.pop();
- m_insertion_mode = InsertionMode::InHead;
- return;
- }
- 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))) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::br) {
- goto AnythingElse;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::head, HTML::TagNames::noscript)) {
- PARSE_ERROR();
- return;
- }
- AnythingElse:
- PARSE_ERROR();
- m_stack_of_open_elements.pop();
- m_insertion_mode = InsertionMode::InHead;
- process_using_the_rules_for(m_insertion_mode, token);
- }
- void HTMLDocumentParser::parse_generic_raw_text_element(HTMLToken& token)
- {
- insert_html_element(token);
- m_tokenizer.switch_to({}, HTMLTokenizer::State::RAWTEXT);
- m_original_insertion_mode = m_insertion_mode;
- m_insertion_mode = InsertionMode::Text;
- }
- Text* HTMLDocumentParser::find_character_insertion_node()
- {
- auto adjusted_insertion_location = find_appropriate_place_for_inserting_node();
- if (adjusted_insertion_location.insert_before_sibling) {
- TODO();
- }
- if (adjusted_insertion_location.parent->is_document())
- return nullptr;
- if (adjusted_insertion_location.parent->last_child() && adjusted_insertion_location.parent->last_child()->is_text())
- return to<Text>(adjusted_insertion_location.parent->last_child());
- auto new_text_node = adopt(*new Text(document(), ""));
- adjusted_insertion_location.parent->append_child(new_text_node);
- return new_text_node;
- }
- void HTMLDocumentParser::flush_character_insertions()
- {
- if (m_character_insertion_builder.is_empty())
- return;
- m_character_insertion_node->set_data(m_character_insertion_builder.to_string());
- m_character_insertion_node->parent()->children_changed();
- m_character_insertion_builder.clear();
- }
- void HTMLDocumentParser::insert_character(u32 data)
- {
- auto node = find_character_insertion_node();
- if (node == m_character_insertion_node) {
- m_character_insertion_builder.append(Utf32View { &data, 1 });
- return;
- }
- if (!m_character_insertion_node) {
- m_character_insertion_node = node;
- m_character_insertion_builder.append(Utf32View { &data, 1 });
- return;
- }
- flush_character_insertions();
- m_character_insertion_node = node;
- m_character_insertion_builder.append(Utf32View { &data, 1 });
- }
- void HTMLDocumentParser::handle_after_head(HTMLToken& token)
- {
- if (token.is_character() && token.is_parser_whitespace()) {
- insert_character(token.codepoint());
- return;
- }
- if (token.is_comment()) {
- insert_comment(token);
- return;
- }
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::body) {
- insert_html_element(token);
- m_frameset_ok = false;
- m_insertion_mode = InsertionMode::InBody;
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::frameset) {
- insert_html_element(token);
- m_insertion_mode = InsertionMode::InFrameset;
- return;
- }
- 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)) {
- PARSE_ERROR();
- m_stack_of_open_elements.push(*m_head_element);
- process_using_the_rules_for(InsertionMode::InHead, token);
- m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) {
- return entry.ptr() == m_head_element;
- });
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::body, HTML::TagNames::html, HTML::TagNames::br)) {
- goto AnythingElse;
- }
- if ((token.is_start_tag() && token.tag_name() == HTML::TagNames::head) || token.is_end_tag()) {
- PARSE_ERROR();
- return;
- }
- AnythingElse:
- HTMLToken fake_body_token;
- fake_body_token.m_type = HTMLToken::Type::StartTag;
- fake_body_token.m_tag.tag_name.append(HTML::TagNames::body);
- insert_html_element(fake_body_token);
- m_insertion_mode = InsertionMode::InBody;
- process_using_the_rules_for(m_insertion_mode, token);
- }
- void HTMLDocumentParser::generate_implied_end_tags(const FlyString& exception)
- {
- 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))
- m_stack_of_open_elements.pop();
- }
- void HTMLDocumentParser::close_a_p_element()
- {
- generate_implied_end_tags(HTML::TagNames::p);
- if (current_node().tag_name() != HTML::TagNames::p) {
- PARSE_ERROR();
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::p);
- }
- void HTMLDocumentParser::handle_after_body(HTMLToken& token)
- {
- if (token.is_character() && token.is_parser_whitespace()) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_comment()) {
- auto data = token.m_comment_or_character.data.to_string();
- auto& insertion_location = m_stack_of_open_elements.first();
- insertion_location.append_child(adopt(*new Comment(document(), data)));
- return;
- }
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::html) {
- if (m_parsing_fragment) {
- TODO();
- }
- m_insertion_mode = InsertionMode::AfterAfterBody;
- return;
- }
- if (token.is_end_of_file()) {
- stop_parsing();
- return;
- }
- PARSE_ERROR();
- m_insertion_mode = InsertionMode::InBody;
- process_using_the_rules_for(InsertionMode::InBody, token);
- }
- void HTMLDocumentParser::handle_after_after_body(HTMLToken& token)
- {
- if (token.is_comment()) {
- auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
- document().append_child(move(comment));
- return;
- }
- if (token.is_doctype() || token.is_parser_whitespace() || (token.is_start_tag() && token.tag_name() == HTML::TagNames::html)) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_end_of_file()) {
- stop_parsing();
- return;
- }
- PARSE_ERROR();
- m_insertion_mode = InsertionMode::InBody;
- process_using_the_rules_for(m_insertion_mode, token);
- }
- void HTMLDocumentParser::reconstruct_the_active_formatting_elements()
- {
- // FIXME: This needs to care about "markers"
- if (m_list_of_active_formatting_elements.is_empty())
- return;
- if (m_list_of_active_formatting_elements.entries().last().is_marker())
- return;
- if (m_stack_of_open_elements.contains(*m_list_of_active_formatting_elements.entries().last().element))
- return;
- ssize_t index = m_list_of_active_formatting_elements.entries().size() - 1;
- RefPtr<Element> entry = m_list_of_active_formatting_elements.entries().at(index).element;
- ASSERT(entry);
- Rewind:
- if (index == 0) {
- goto Create;
- }
- --index;
- entry = m_list_of_active_formatting_elements.entries().at(index).element;
- ASSERT(entry);
- if (!m_stack_of_open_elements.contains(*entry))
- goto Rewind;
- Advance:
- ++index;
- entry = m_list_of_active_formatting_elements.entries().at(index).element;
- ASSERT(entry);
- Create:
- // FIXME: Hold on to the real token!
- HTMLToken fake_token;
- fake_token.m_type = HTMLToken::Type::StartTag;
- fake_token.m_tag.tag_name.append(entry->tag_name());
- auto new_element = insert_html_element(fake_token);
- m_list_of_active_formatting_elements.entries().at(index).element = *new_element;
- if (index != (ssize_t)m_list_of_active_formatting_elements.entries().size() - 1)
- goto Advance;
- }
- HTMLDocumentParser::AdoptionAgencyAlgorithmOutcome HTMLDocumentParser::run_the_adoption_agency_algorithm(HTMLToken& token)
- {
- auto subject = token.tag_name();
- // If the current node is an HTML element whose tag name is subject,
- // and the current node is not in the list of active formatting elements,
- // then pop the current node off the stack of open elements, and return.
- if (current_node().tag_name() == subject && !m_list_of_active_formatting_elements.contains(current_node())) {
- m_stack_of_open_elements.pop();
- return AdoptionAgencyAlgorithmOutcome::DoNothing;
- }
- size_t outer_loop_counter = 0;
- //OuterLoop:
- if (outer_loop_counter >= 8)
- return AdoptionAgencyAlgorithmOutcome::DoNothing;
- ++outer_loop_counter;
- auto formatting_element = m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker(subject);
- if (!formatting_element)
- return AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps;
- if (!m_stack_of_open_elements.contains(*formatting_element)) {
- PARSE_ERROR();
- // FIXME: If formatting element is not in the stack of open elements,
- // then this is a parse error; remove the element from the list, and return.
- TODO();
- }
- if (!m_stack_of_open_elements.has_in_scope(*formatting_element)) {
- PARSE_ERROR();
- return AdoptionAgencyAlgorithmOutcome::DoNothing;
- }
- if (formatting_element != ¤t_node()) {
- PARSE_ERROR();
- }
- RefPtr<Element> furthest_block = m_stack_of_open_elements.topmost_special_node_below(*formatting_element);
- if (!furthest_block) {
- while (¤t_node() != formatting_element)
- m_stack_of_open_elements.pop();
- m_stack_of_open_elements.pop();
- m_list_of_active_formatting_elements.remove(*formatting_element);
- return AdoptionAgencyAlgorithmOutcome::DoNothing;
- }
- // FIXME: Implement the rest of the AAA :^)
- TODO();
- }
- bool HTMLDocumentParser::is_special_tag(const FlyString& tag_name)
- {
- return tag_name.is_one_of(
- HTML::TagNames::address,
- HTML::TagNames::applet,
- HTML::TagNames::area,
- HTML::TagNames::article,
- HTML::TagNames::aside,
- HTML::TagNames::base,
- HTML::TagNames::basefont,
- HTML::TagNames::bgsound,
- HTML::TagNames::blockquote,
- HTML::TagNames::body,
- HTML::TagNames::br,
- HTML::TagNames::button,
- HTML::TagNames::caption,
- HTML::TagNames::center,
- HTML::TagNames::col,
- HTML::TagNames::colgroup,
- HTML::TagNames::dd,
- HTML::TagNames::details,
- HTML::TagNames::dir,
- HTML::TagNames::div,
- HTML::TagNames::dl,
- HTML::TagNames::dt,
- HTML::TagNames::embed,
- HTML::TagNames::fieldset,
- HTML::TagNames::figcaption,
- HTML::TagNames::figure,
- HTML::TagNames::footer,
- HTML::TagNames::form,
- HTML::TagNames::frame,
- HTML::TagNames::frameset,
- HTML::TagNames::h1,
- HTML::TagNames::h2,
- HTML::TagNames::h3,
- HTML::TagNames::h4,
- HTML::TagNames::h5,
- HTML::TagNames::h6,
- HTML::TagNames::head,
- HTML::TagNames::header,
- HTML::TagNames::hgroup,
- HTML::TagNames::hr,
- HTML::TagNames::html,
- HTML::TagNames::iframe,
- HTML::TagNames::img,
- HTML::TagNames::input,
- HTML::TagNames::keygen,
- HTML::TagNames::li,
- HTML::TagNames::link,
- HTML::TagNames::listing,
- HTML::TagNames::main,
- HTML::TagNames::marquee,
- HTML::TagNames::menu,
- HTML::TagNames::meta,
- HTML::TagNames::nav,
- HTML::TagNames::noembed,
- HTML::TagNames::noframes,
- HTML::TagNames::noscript,
- HTML::TagNames::object,
- HTML::TagNames::ol,
- HTML::TagNames::p,
- HTML::TagNames::param,
- HTML::TagNames::plaintext,
- HTML::TagNames::pre,
- HTML::TagNames::script,
- HTML::TagNames::section,
- HTML::TagNames::select,
- HTML::TagNames::source,
- HTML::TagNames::style,
- HTML::TagNames::summary,
- HTML::TagNames::table,
- HTML::TagNames::tbody,
- HTML::TagNames::td,
- HTML::TagNames::template_,
- HTML::TagNames::textarea,
- HTML::TagNames::tfoot,
- HTML::TagNames::th,
- HTML::TagNames::thead,
- HTML::TagNames::title,
- HTML::TagNames::tr,
- HTML::TagNames::track,
- HTML::TagNames::ul,
- HTML::TagNames::wbr,
- HTML::TagNames::xmp);
- }
- void HTMLDocumentParser::handle_in_body(HTMLToken& token)
- {
- if (token.is_character()) {
- if (token.codepoint() == 0) {
- PARSE_ERROR();
- return;
- }
- if (token.is_parser_whitespace()) {
- reconstruct_the_active_formatting_elements();
- insert_character(token.codepoint());
- return;
- }
- reconstruct_the_active_formatting_elements();
- insert_character(token.codepoint());
- m_frameset_ok = false;
- return;
- }
- if (token.is_comment()) {
- insert_comment(token);
- return;
- }
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
- PARSE_ERROR();
- if (m_stack_of_open_elements.contains(HTML::TagNames::template_))
- return;
- for (auto& attribute : token.m_tag.attributes) {
- if (current_node().has_attribute(attribute.local_name_builder.string_view()))
- continue;
- current_node().set_attribute(attribute.local_name_builder.to_string(), attribute.value_builder.to_string());
- }
- return;
- }
- 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)) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::body) {
- PARSE_ERROR();
- if (m_stack_of_open_elements.elements().size() == 1
- || node_before_current_node().tag_name() != HTML::TagNames::body
- || m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
- return;
- }
- m_frameset_ok = false;
- for (auto& attribute : token.m_tag.attributes) {
- if (node_before_current_node().has_attribute(attribute.local_name_builder.string_view()))
- continue;
- node_before_current_node().set_attribute(attribute.local_name_builder.to_string(), attribute.value_builder.to_string());
- }
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::frameset) {
- TODO();
- }
- if (token.is_end_of_file()) {
- // FIXME: If the stack of template insertion modes is not empty,
- // then process the token using the rules for the "in template" insertion mode.
- // FIXME: If there is a node in the stack of open elements that is not either
- // a dd element, a dt element, an li element, an optgroup element, an option element,
- // a p element, an rb element, an rp element, an rt element, an rtc element,
- // a tbody element, a td element, a tfoot element, a th element, a thead element,
- // a tr element, the body element, or the html element, then this is a parse error.
- stop_parsing();
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::body) {
- if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::body)) {
- PARSE_ERROR();
- return;
- }
- for (auto& node : m_stack_of_open_elements.elements()) {
- 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)) {
- PARSE_ERROR();
- break;
- }
- }
- m_insertion_mode = InsertionMode::AfterBody;
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::html) {
- if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::body)) {
- PARSE_ERROR();
- return;
- }
- for (auto& node : m_stack_of_open_elements.elements()) {
- 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)) {
- PARSE_ERROR();
- break;
- }
- }
- m_insertion_mode = InsertionMode::AfterBody;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- 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)) {
- if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
- close_a_p_element();
- insert_html_element(token);
- return;
- }
- 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)) {
- if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
- close_a_p_element();
- 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)) {
- PARSE_ERROR();
- m_stack_of_open_elements.pop();
- }
- insert_html_element(token);
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::pre, HTML::TagNames::listing)) {
- if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
- close_a_p_element();
- insert_html_element(token);
- m_frameset_ok = false;
- // If the next token is a U+000A LINE FEED (LF) character token,
- // then ignore that token and move on to the next one.
- // (Newlines at the start of pre blocks are ignored as an authoring convenience.)
- auto next_token = m_tokenizer.next_token();
- if (next_token.has_value() && next_token.value().is_character() && next_token.value().codepoint() == '\n') {
- // Ignore it.
- } else {
- process_using_the_rules_for(m_insertion_mode, next_token.value());
- }
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::form) {
- if (m_form_element && !m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
- PARSE_ERROR();
- return;
- }
- if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
- close_a_p_element();
- auto element = insert_html_element(token);
- if (!m_stack_of_open_elements.contains(HTML::TagNames::template_))
- m_form_element = to<HTMLFormElement>(*element);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::li) {
- m_frameset_ok = false;
- for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
- RefPtr<Element> node = m_stack_of_open_elements.elements()[i];
- if (node->tag_name() == HTML::TagNames::li) {
- generate_implied_end_tags(HTML::TagNames::li);
- if (current_node().tag_name() != HTML::TagNames::li) {
- PARSE_ERROR();
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::li);
- break;
- }
- if (is_special_tag(node->tag_name()) && !node->tag_name().is_one_of(HTML::TagNames::address, HTML::TagNames::div, HTML::TagNames::p))
- break;
- }
- if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
- close_a_p_element();
- insert_html_element(token);
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt)) {
- m_frameset_ok = false;
- for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
- RefPtr<Element> node = m_stack_of_open_elements.elements()[i];
- if (node->tag_name() == HTML::TagNames::dd) {
- generate_implied_end_tags(HTML::TagNames::dd);
- if (current_node().tag_name() != HTML::TagNames::dd) {
- PARSE_ERROR();
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::dd);
- break;
- }
- if (node->tag_name() == HTML::TagNames::dt) {
- generate_implied_end_tags(HTML::TagNames::dt);
- if (current_node().tag_name() != HTML::TagNames::dt) {
- PARSE_ERROR();
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::dt);
- break;
- }
- if (is_special_tag(node->tag_name()) && !node->tag_name().is_one_of(HTML::TagNames::address, HTML::TagNames::div, HTML::TagNames::p))
- break;
- }
- if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
- close_a_p_element();
- insert_html_element(token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::plaintext) {
- if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
- close_a_p_element();
- insert_html_element(token);
- m_tokenizer.switch_to({}, HTMLTokenizer::State::PLAINTEXT);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::button) {
- if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::button)) {
- PARSE_ERROR();
- generate_implied_end_tags();
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::button);
- }
- reconstruct_the_active_formatting_elements();
- insert_html_element(token);
- m_frameset_ok = false;
- return;
- }
- 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)) {
- if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
- PARSE_ERROR();
- return;
- }
- generate_implied_end_tags();
- if (current_node().tag_name() != token.tag_name()) {
- PARSE_ERROR();
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::form) {
- if (!m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
- auto node = m_form_element;
- m_form_element = nullptr;
- if (!node || !m_stack_of_open_elements.has_in_scope(*node)) {
- PARSE_ERROR();
- return;
- }
- generate_implied_end_tags();
- if (¤t_node() != node) {
- PARSE_ERROR();
- }
- m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) { return entry.ptr() == node.ptr(); });
- } else {
- if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::form)) {
- PARSE_ERROR();
- return;
- }
- generate_implied_end_tags();
- if (current_node().tag_name() != HTML::TagNames::form) {
- PARSE_ERROR();
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::form);
- }
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::p) {
- if (!m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p)) {
- PARSE_ERROR();
- HTMLToken fake_p_token;
- fake_p_token.m_type = HTMLToken::Type::StartTag;
- fake_p_token.m_tag.tag_name.append(HTML::TagNames::p);
- insert_html_element(fake_p_token);
- }
- close_a_p_element();
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::li) {
- if (!m_stack_of_open_elements.has_in_list_item_scope(HTML::TagNames::li)) {
- PARSE_ERROR();
- return;
- }
- generate_implied_end_tags(HTML::TagNames::li);
- if (current_node().tag_name() != HTML::TagNames::li) {
- PARSE_ERROR();
- dbg() << "Expected <li> current node, but had <" << current_node().tag_name() << ">";
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::li);
- return;
- }
- if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::dd, HTML::TagNames::dt)) {
- if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
- PARSE_ERROR();
- return;
- }
- generate_implied_end_tags(token.tag_name());
- if (current_node().tag_name() != token.tag_name()) {
- PARSE_ERROR();
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
- return;
- }
- 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)) {
- if (!m_stack_of_open_elements.has_in_scope(HTML::TagNames::h1)
- && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h2)
- && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h3)
- && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h4)
- && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h5)
- && !m_stack_of_open_elements.has_in_scope(HTML::TagNames::h6)) {
- PARSE_ERROR();
- return;
- }
- generate_implied_end_tags();
- if (current_node().tag_name() != token.tag_name()) {
- PARSE_ERROR();
- }
- for (;;) {
- auto popped_element = m_stack_of_open_elements.pop();
- 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))
- break;
- }
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::a) {
- if (auto* element = m_list_of_active_formatting_elements.last_element_with_tag_name_before_marker(HTML::TagNames::a)) {
- PARSE_ERROR();
- if (run_the_adoption_agency_algorithm(token) == AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps)
- goto AnyOtherEndTag;
- m_list_of_active_formatting_elements.remove(*element);
- m_stack_of_open_elements.elements().remove_first_matching([&](auto& entry) {
- return entry.ptr() == element;
- });
- }
- reconstruct_the_active_formatting_elements();
- auto element = insert_html_element(token);
- m_list_of_active_formatting_elements.add(*element);
- return;
- }
- 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)) {
- reconstruct_the_active_formatting_elements();
- auto element = insert_html_element(token);
- m_list_of_active_formatting_elements.add(*element);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::nobr) {
- reconstruct_the_active_formatting_elements();
- if (m_stack_of_open_elements.has_in_scope(HTML::TagNames::nobr)) {
- PARSE_ERROR();
- run_the_adoption_agency_algorithm(token);
- reconstruct_the_active_formatting_elements();
- }
- auto element = insert_html_element(token);
- m_list_of_active_formatting_elements.add(*element);
- return;
- }
- 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)) {
- if (run_the_adoption_agency_algorithm(token) == AdoptionAgencyAlgorithmOutcome::RunAnyOtherEndTagSteps)
- goto AnyOtherEndTag;
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::applet, HTML::TagNames::marquee, HTML::TagNames::object)) {
- reconstruct_the_active_formatting_elements();
- insert_html_element(token);
- m_list_of_active_formatting_elements.add_marker();
- m_frameset_ok = false;
- return;
- }
- if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::applet, HTML::TagNames::marquee, HTML::TagNames::object)) {
- if (!m_stack_of_open_elements.has_in_scope(token.tag_name())) {
- PARSE_ERROR();
- return;
- }
- generate_implied_end_tags();
- if (current_node().tag_name() != token.tag_name()) {
- PARSE_ERROR();
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
- m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::table) {
- if (!document().in_quirks_mode()) {
- if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
- close_a_p_element();
- }
- insert_html_element(token);
- m_frameset_ok = false;
- m_insertion_mode = InsertionMode::InTable;
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::br) {
- token.drop_attributes();
- goto BRStartTag;
- }
- 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)) {
- BRStartTag:
- reconstruct_the_active_formatting_elements();
- insert_html_element(token);
- m_stack_of_open_elements.pop();
- token.acknowledge_self_closing_flag_if_set();
- m_frameset_ok = false;
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::input) {
- reconstruct_the_active_formatting_elements();
- insert_html_element(token);
- m_stack_of_open_elements.pop();
- token.acknowledge_self_closing_flag_if_set();
- auto type_attribute = token.attribute(HTML::AttributeNames::type);
- if (type_attribute.is_null() || !type_attribute.equals_ignoring_case("hidden")) {
- m_frameset_ok = false;
- }
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::param, HTML::TagNames::source, HTML::TagNames::track)) {
- insert_html_element(token);
- m_stack_of_open_elements.pop();
- token.acknowledge_self_closing_flag_if_set();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::hr) {
- if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p))
- close_a_p_element();
- insert_html_element(token);
- m_stack_of_open_elements.pop();
- token.acknowledge_self_closing_flag_if_set();
- m_frameset_ok = false;
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::image) {
- // Parse error. Change the token's tag name to HTML::TagNames::img and reprocess it. (Don't ask.)
- PARSE_ERROR();
- token.m_tag.tag_name.clear();
- token.m_tag.tag_name.append(HTML::TagNames::img);
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::textarea) {
- insert_html_element(token);
- m_tokenizer.switch_to({}, HTMLTokenizer::State::RCDATA);
- // If the next token is a U+000A LINE FEED (LF) character token,
- // then ignore that token and move on to the next one.
- // (Newlines at the start of pre blocks are ignored as an authoring convenience.)
- auto next_token = m_tokenizer.next_token();
- m_original_insertion_mode = m_insertion_mode;
- m_frameset_ok = false;
- m_insertion_mode = InsertionMode::Text;
- if (next_token.has_value() && next_token.value().is_character() && next_token.value().codepoint() == '\n') {
- // Ignore it.
- } else {
- process_using_the_rules_for(m_insertion_mode, next_token.value());
- }
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::xmp) {
- if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p)) {
- close_a_p_element();
- }
- reconstruct_the_active_formatting_elements();
- m_frameset_ok = false;
- parse_generic_raw_text_element(token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::iframe) {
- m_frameset_ok = false;
- parse_generic_raw_text_element(token);
- return;
- }
- if (token.is_start_tag() && ((token.tag_name() == HTML::TagNames::noembed) || (token.tag_name() == HTML::TagNames::noscript && m_scripting_enabled))) {
- parse_generic_raw_text_element(token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::select) {
- reconstruct_the_active_formatting_elements();
- insert_html_element(token);
- m_frameset_ok = false;
- switch (m_insertion_mode) {
- case InsertionMode::InTable:
- case InsertionMode::InCaption:
- case InsertionMode::InTableBody:
- case InsertionMode::InRow:
- case InsertionMode::InCell:
- m_insertion_mode = InsertionMode::InSelectInTable;
- break;
- default:
- m_insertion_mode = InsertionMode::InSelect;
- break;
- }
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::optgroup, HTML::TagNames::option)) {
- if (current_node().tag_name() == HTML::TagNames::option)
- m_stack_of_open_elements.pop();
- reconstruct_the_active_formatting_elements();
- insert_html_element(token);
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::rb, HTML::TagNames::rtc)) {
- if (m_stack_of_open_elements.has_in_scope(HTML::TagNames::ruby))
- generate_implied_end_tags();
- if (current_node().tag_name() != HTML::TagNames::ruby)
- PARSE_ERROR();
- insert_html_element(token);
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::rp, HTML::TagNames::rt)) {
- if (m_stack_of_open_elements.has_in_scope(HTML::TagNames::ruby))
- generate_implied_end_tags(HTML::TagNames::rtc);
- if (current_node().tag_name() != HTML::TagNames::rtc || current_node().tag_name() != HTML::TagNames::ruby)
- PARSE_ERROR();
- insert_html_element(token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::math) {
- dbg() << "<math> element encountered.";
- reconstruct_the_active_formatting_elements();
- adjust_mathml_attributes(token);
- adjust_foreign_attributes(token);
- // FIXME: this should insert a foreign element, but lets just insert it normally for now :^)
- insert_html_element(token);
- if (token.is_self_closing()) {
- m_stack_of_open_elements.pop();
- token.acknowledge_self_closing_flag_if_set();
- }
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::svg) {
- dbg() << "<svg> element encountered.";
- reconstruct_the_active_formatting_elements();
- adjust_svg_attributes(token);
- adjust_foreign_attributes(token);
- // FIXME: this should insert a foreign element, but lets just insert it normally for now :^)
- insert_html_element(token);
- if (token.is_self_closing()) {
- m_stack_of_open_elements.pop();
- token.acknowledge_self_closing_flag_if_set();
- }
- return;
- }
- 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))) {
- PARSE_ERROR();
- return;
- }
- // Any other start tag
- if (token.is_start_tag()) {
- reconstruct_the_active_formatting_elements();
- insert_html_element(token);
- return;
- }
- if (token.is_end_tag()) {
- AnyOtherEndTag:
- RefPtr<Element> node;
- for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
- node = m_stack_of_open_elements.elements()[i];
- if (node->tag_name() == token.tag_name()) {
- generate_implied_end_tags(token.tag_name());
- if (node != current_node()) {
- PARSE_ERROR();
- }
- while (¤t_node() != node) {
- m_stack_of_open_elements.pop();
- }
- m_stack_of_open_elements.pop();
- break;
- }
- if (is_special_tag(node->tag_name())) {
- PARSE_ERROR();
- return;
- }
- }
- return;
- }
- }
- void HTMLDocumentParser::adjust_mathml_attributes(HTMLToken& token)
- {
- token.adjust_attribute_name("definitionurl", "definitionURL");
- }
- void HTMLDocumentParser::adjust_svg_attributes(HTMLToken& token)
- {
- token.adjust_attribute_name("attributename", "attributeName");
- token.adjust_attribute_name("attributetype", "attributeType");
- token.adjust_attribute_name("basefrequency", "baseFrequency");
- token.adjust_attribute_name("baseprofile", "baseProfile");
- token.adjust_attribute_name("calcmode", "calcMode");
- token.adjust_attribute_name("clippathunits", "clipPathUnits");
- token.adjust_attribute_name("diffuseconstant", "diffuseConstant");
- token.adjust_attribute_name("edgemode", "edgeMode");
- token.adjust_attribute_name("filterunits", "filterUnits");
- token.adjust_attribute_name("glyphref", "glyphRef");
- token.adjust_attribute_name("gradienttransform", "gradientTransform");
- token.adjust_attribute_name("gradientunits", "gradientUnits");
- token.adjust_attribute_name("kernelmatrix", "kernelMatrix");
- token.adjust_attribute_name("kernelunitlength", "kernelUnitLength");
- token.adjust_attribute_name("keypoints", "keyPoints");
- token.adjust_attribute_name("keysplines", "keySplines");
- token.adjust_attribute_name("keytimes", "keyTimes");
- token.adjust_attribute_name("lengthadjust", "lengthAdjust");
- token.adjust_attribute_name("limitingconeangle", "limitingConeAngle");
- token.adjust_attribute_name("markerheight", "markerHeight");
- token.adjust_attribute_name("markerunits", "markerUnits");
- token.adjust_attribute_name("markerwidth", "markerWidth");
- token.adjust_attribute_name("maskcontentunits", "maskContentUnits");
- token.adjust_attribute_name("maskunits", "maskUnits");
- token.adjust_attribute_name("numoctaves", "numOctaves");
- token.adjust_attribute_name("pathlength", "pathLength");
- token.adjust_attribute_name("patterncontentunits", "patternContentUnits");
- token.adjust_attribute_name("patterntransform", "patternTransform");
- token.adjust_attribute_name("patternunits", "patternUnits");
- token.adjust_attribute_name("pointsatx", "pointsAtX");
- token.adjust_attribute_name("pointsaty", "pointsAtY");
- token.adjust_attribute_name("pointsatz", "pointsAtZ");
- token.adjust_attribute_name("preservealpha", "preserveAlpha");
- token.adjust_attribute_name("preserveaspectratio", "preserveAspectRatio");
- token.adjust_attribute_name("primitiveunits", "primitiveUnits");
- token.adjust_attribute_name("refx", "refX");
- token.adjust_attribute_name("refy", "refY");
- token.adjust_attribute_name("repeatcount", "repeatCount");
- token.adjust_attribute_name("repeatdur", "repeatDur");
- token.adjust_attribute_name("requiredextensions", "requiredExtensions");
- token.adjust_attribute_name("requiredfeatures", "requiredFeatures");
- token.adjust_attribute_name("specularconstant", "specularConstant");
- token.adjust_attribute_name("specularexponent", "specularExponent");
- token.adjust_attribute_name("spreadmethod", "spreadMethod");
- token.adjust_attribute_name("startoffset", "startOffset");
- token.adjust_attribute_name("stddeviation", "stdDeviation");
- token.adjust_attribute_name("stitchtiles", "stitchTiles");
- token.adjust_attribute_name("surfacescale", "surfaceScale");
- token.adjust_attribute_name("systemlanguage", "systemLanguage");
- token.adjust_attribute_name("tablevalues", "tableValues");
- token.adjust_attribute_name("targetx", "targetX");
- token.adjust_attribute_name("targety", "targetY");
- token.adjust_attribute_name("textlength", "textLength");
- token.adjust_attribute_name("viewbox", "viewBox");
- token.adjust_attribute_name("viewtarget", "viewTarget");
- token.adjust_attribute_name("xchannelselector", "xChannelSelector");
- token.adjust_attribute_name("ychannelselector", "yChannelSelector");
- token.adjust_attribute_name("zoomandpan", "zoomAndPan");
- }
- void HTMLDocumentParser::adjust_foreign_attributes(HTMLToken& token)
- {
- auto xlink_namespace = "http://www.w3.org/1999/xlink";
- token.adjust_foreign_attribute("xlink:actuate", "xlink", "actuate", xlink_namespace);
- token.adjust_foreign_attribute("xlink:arcrole", "xlink", "arcrole", xlink_namespace);
- token.adjust_foreign_attribute("xlink:href", "xlink", "href", xlink_namespace);
- token.adjust_foreign_attribute("xlink:role", "xlink", "role", xlink_namespace);
- token.adjust_foreign_attribute("xlink:show", "xlink", "show", xlink_namespace);
- token.adjust_foreign_attribute("xlink:title", "xlink", "title", xlink_namespace);
- token.adjust_foreign_attribute("xlink:type", "xlink", "type", xlink_namespace);
- auto xml_namespace = "http://www.w3.org/XML/1998/namespace";
- token.adjust_foreign_attribute("xml:lang", "xml", "lang", xml_namespace);
- token.adjust_foreign_attribute("xml:space", "xml", "space", xml_namespace);
- auto xmlns_namespace = "http://www.w3.org/2000/xmlns/";
- token.adjust_foreign_attribute("xmlns", "", "xmlns", xmlns_namespace);
- token.adjust_foreign_attribute("xmlns:xlink", "xmlns", "xlink", xmlns_namespace);
- }
- void HTMLDocumentParser::increment_script_nesting_level()
- {
- ++m_script_nesting_level;
- }
- void HTMLDocumentParser::decrement_script_nesting_level()
- {
- ASSERT(m_script_nesting_level);
- --m_script_nesting_level;
- }
- void HTMLDocumentParser::handle_text(HTMLToken& token)
- {
- if (token.is_character()) {
- insert_character(token.codepoint());
- return;
- }
- if (token.is_end_of_file()) {
- PARSE_ERROR();
- if (current_node().tag_name() == HTML::TagNames::script)
- to<HTMLScriptElement>(current_node()).set_already_started({}, true);
- m_stack_of_open_elements.pop();
- m_insertion_mode = m_original_insertion_mode;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::script) {
- NonnullRefPtr<HTMLScriptElement> script = to<HTMLScriptElement>(current_node());
- m_stack_of_open_elements.pop();
- m_insertion_mode = m_original_insertion_mode;
- // FIXME: Handle tokenizer insertion point stuff here.
- increment_script_nesting_level();
- script->prepare_script({});
- decrement_script_nesting_level();
- if (script_nesting_level() == 0)
- m_parser_pause_flag = false;
- // FIXME: Handle tokenizer insertion point stuff here too.
- while (document().pending_parsing_blocking_script()) {
- if (script_nesting_level() != 0) {
- m_parser_pause_flag = true;
- // FIXME: Abort the processing of any nested invocations of the tokenizer,
- // yielding control back to the caller. (Tokenization will resume when
- // the caller returns to the "outer" tree construction stage.)
- TODO();
- } else {
- auto the_script = document().take_pending_parsing_blocking_script({});
- m_tokenizer.set_blocked(true);
- // FIXME: If the parser's Document has a style sheet that is blocking scripts
- // or the script's "ready to be parser-executed" flag is not set:
- // spin the event loop until the parser's Document has no style sheet
- // that is blocking scripts and the script's "ready to be parser-executed"
- // flag is set.
- if (the_script->failed_to_load())
- return;
- ASSERT(the_script->is_ready_to_be_parser_executed());
- if (m_aborted)
- return;
- m_tokenizer.set_blocked(false);
- // FIXME: Handle tokenizer insertion point stuff here too.
- ASSERT(script_nesting_level() == 0);
- increment_script_nesting_level();
- the_script->execute_script();
- decrement_script_nesting_level();
- ASSERT(script_nesting_level() == 0);
- m_parser_pause_flag = false;
- // FIXME: Handle tokenizer insertion point stuff here too.
- }
- }
- return;
- }
- if (token.is_end_tag()) {
- m_stack_of_open_elements.pop();
- m_insertion_mode = m_original_insertion_mode;
- return;
- }
- TODO();
- }
- void HTMLDocumentParser::clear_the_stack_back_to_a_table_context()
- {
- while (!current_node().tag_name().is_one_of(HTML::TagNames::table, HTML::TagNames::template_, HTML::TagNames::html))
- m_stack_of_open_elements.pop();
- }
- void HTMLDocumentParser::clear_the_stack_back_to_a_table_row_context()
- {
- while (!current_node().tag_name().is_one_of(HTML::TagNames::tr, HTML::TagNames::template_, HTML::TagNames::html))
- m_stack_of_open_elements.pop();
- }
- void HTMLDocumentParser::clear_the_stack_back_to_a_table_body_context()
- {
- while (!current_node().tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead, HTML::TagNames::template_, HTML::TagNames::html))
- m_stack_of_open_elements.pop();
- }
- void HTMLDocumentParser::handle_in_row(HTMLToken& token)
- {
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::th, HTML::TagNames::td)) {
- clear_the_stack_back_to_a_table_row_context();
- insert_html_element(token);
- m_insertion_mode = InsertionMode::InCell;
- m_list_of_active_formatting_elements.add_marker();
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::tr) {
- if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tr)) {
- PARSE_ERROR();
- return;
- }
- clear_the_stack_back_to_a_table_row_context();
- m_stack_of_open_elements.pop();
- m_insertion_mode = InsertionMode::InTableBody;
- return;
- }
- 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))
- || (token.is_end_tag() && token.tag_name() == HTML::TagNames::table)) {
- if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tr)) {
- PARSE_ERROR();
- return;
- }
- clear_the_stack_back_to_a_table_row_context();
- m_stack_of_open_elements.pop();
- m_insertion_mode = InsertionMode::InTableBody;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead)) {
- if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
- PARSE_ERROR();
- return;
- }
- if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tr)) {
- return;
- }
- clear_the_stack_back_to_a_table_row_context();
- m_stack_of_open_elements.pop();
- m_insertion_mode = InsertionMode::InTableBody;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- 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)) {
- PARSE_ERROR();
- return;
- }
- process_using_the_rules_for(InsertionMode::InTable, token);
- }
- void HTMLDocumentParser::close_the_cell()
- {
- generate_implied_end_tags();
- if (!current_node().tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
- PARSE_ERROR();
- }
- while (!current_node().tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th))
- m_stack_of_open_elements.pop();
- m_stack_of_open_elements.pop();
- m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
- m_insertion_mode = InsertionMode::InRow;
- }
- void HTMLDocumentParser::handle_in_cell(HTMLToken& token)
- {
- if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
- if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
- PARSE_ERROR();
- return;
- }
- generate_implied_end_tags();
- if (current_node().tag_name() != token.tag_name()) {
- PARSE_ERROR();
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(token.tag_name());
- m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
- m_insertion_mode = InsertionMode::InRow;
- return;
- }
- 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)) {
- 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)) {
- PARSE_ERROR();
- return;
- }
- close_the_cell();
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- 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)) {
- PARSE_ERROR();
- return;
- }
- 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)) {
- if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
- PARSE_ERROR();
- return;
- }
- close_the_cell();
- // Reprocess the token.
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- process_using_the_rules_for(InsertionMode::InBody, token);
- }
- void HTMLDocumentParser::handle_in_table_text(HTMLToken& token)
- {
- if (token.is_character()) {
- if (token.codepoint() == 0) {
- PARSE_ERROR();
- return;
- }
- m_pending_table_character_tokens.append(token);
- return;
- }
- for (auto& pending_token : m_pending_table_character_tokens) {
- ASSERT(pending_token.is_character());
- if (!pending_token.is_parser_whitespace()) {
- // FIXME: If any of the tokens in the pending table character tokens list
- // are character tokens that are not ASCII whitespace, then this is a parse error:
- // reprocess the character tokens in the pending table character tokens list using
- // the rules given in the "anything else" entry in the "in table" insertion mode.
- TODO();
- }
- }
- for (auto& pending_token : m_pending_table_character_tokens) {
- insert_character(pending_token.codepoint());
- }
- m_insertion_mode = m_original_insertion_mode;
- process_using_the_rules_for(m_insertion_mode, token);
- }
- void HTMLDocumentParser::handle_in_table_body(HTMLToken& token)
- {
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::tr) {
- clear_the_stack_back_to_a_table_body_context();
- insert_html_element(token);
- m_insertion_mode = InsertionMode::InRow;
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::th, HTML::TagNames::td)) {
- PARSE_ERROR();
- clear_the_stack_back_to_a_table_body_context();
- HTMLToken fake_tr_token;
- fake_tr_token.m_type = HTMLToken::Type::StartTag;
- fake_tr_token.m_tag.tag_name.append(HTML::TagNames::tr);
- insert_html_element(fake_tr_token);
- m_insertion_mode = InsertionMode::InRow;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead)) {
- if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name())) {
- PARSE_ERROR();
- return;
- }
- clear_the_stack_back_to_a_table_body_context();
- m_stack_of_open_elements.pop();
- m_insertion_mode = InsertionMode::InTable;
- return;
- }
- 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))
- || (token.is_end_tag() && token.tag_name() == HTML::TagNames::table)) {
- if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tbody)
- && !m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::thead)
- && !m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::tfoot)) {
- PARSE_ERROR();
- return;
- }
- clear_the_stack_back_to_a_table_body_context();
- m_stack_of_open_elements.pop();
- m_insertion_mode = InsertionMode::InTable;
- process_using_the_rules_for(InsertionMode::InTable, token);
- return;
- }
- 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)) {
- PARSE_ERROR();
- return;
- }
- process_using_the_rules_for(InsertionMode::InTable, token);
- }
- void HTMLDocumentParser::handle_in_table(HTMLToken& token)
- {
- 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)) {
- m_pending_table_character_tokens.clear();
- m_original_insertion_mode = m_insertion_mode;
- m_insertion_mode = InsertionMode::InTableText;
- process_using_the_rules_for(InsertionMode::InTableText, token);
- return;
- }
- if (token.is_comment()) {
- insert_comment(token);
- return;
- }
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::caption) {
- clear_the_stack_back_to_a_table_context();
- m_list_of_active_formatting_elements.add_marker();
- insert_html_element(token);
- m_insertion_mode = InsertionMode::InCaption;
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::colgroup) {
- clear_the_stack_back_to_a_table_context();
- insert_html_element(token);
- m_insertion_mode = InsertionMode::InColumnGroup;
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::col) {
- clear_the_stack_back_to_a_table_context();
- HTMLToken fake_colgroup_token;
- fake_colgroup_token.m_type = HTMLToken::Type::StartTag;
- fake_colgroup_token.m_tag.tag_name.append(HTML::TagNames::colgroup);
- insert_html_element(fake_colgroup_token);
- m_insertion_mode = InsertionMode::InColumnGroup;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::tfoot, HTML::TagNames::thead)) {
- clear_the_stack_back_to_a_table_context();
- insert_html_element(token);
- m_insertion_mode = InsertionMode::InTableBody;
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th, HTML::TagNames::tr)) {
- clear_the_stack_back_to_a_table_context();
- HTMLToken fake_tbody_token;
- fake_tbody_token.m_type = HTMLToken::Type::StartTag;
- fake_tbody_token.m_tag.tag_name.append(HTML::TagNames::tbody);
- insert_html_element(fake_tbody_token);
- m_insertion_mode = InsertionMode::InTableBody;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::table) {
- PARSE_ERROR();
- if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::table))
- return;
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::table);
- reset_the_insertion_mode_appropriately();
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::table) {
- if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::table)) {
- PARSE_ERROR();
- return;
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::table);
- reset_the_insertion_mode_appropriately();
- return;
- }
- 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)) {
- PARSE_ERROR();
- return;
- }
- if ((token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::style, HTML::TagNames::script, HTML::TagNames::template_))
- || (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_)) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::input) {
- auto type_attribute = token.attribute(HTML::AttributeNames::type);
- if (type_attribute.is_null() || !type_attribute.equals_ignoring_case("hidden")) {
- goto AnythingElse;
- }
- PARSE_ERROR();
- insert_html_element(token);
- // FIXME: Is this the correct interpretation of "Pop that input element off the stack of open elements."?
- // Because this wording is the first time it's seen in the spec.
- // Other times it's worded as: "Immediately pop the current node off the stack of open elements."
- m_stack_of_open_elements.pop();
- token.acknowledge_self_closing_flag_if_set();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::form) {
- PARSE_ERROR();
- if (m_form_element || m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
- return;
- }
- m_form_element = to<HTMLFormElement>(insert_html_element(token));
- // FIXME: See previous FIXME, as this is the same situation but for form.
- m_stack_of_open_elements.pop();
- return;
- }
- if (token.is_end_of_file()) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- AnythingElse:
- PARSE_ERROR();
- m_foster_parenting = true;
- process_using_the_rules_for(InsertionMode::InBody, token);
- m_foster_parenting = false;
- }
- void HTMLDocumentParser::handle_in_select_in_table(HTMLToken& token)
- {
- 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)) {
- PARSE_ERROR();
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
- reset_the_insertion_mode_appropriately();
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- 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)) {
- PARSE_ERROR();
- if (!m_stack_of_open_elements.has_in_table_scope(token.tag_name()))
- return;
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
- reset_the_insertion_mode_appropriately();
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- process_using_the_rules_for(InsertionMode::InSelect, token);
- }
- void HTMLDocumentParser::handle_in_select(HTMLToken& token)
- {
- if (token.is_character()) {
- if (token.codepoint() == 0) {
- PARSE_ERROR();
- return;
- }
- insert_character(token.codepoint());
- return;
- }
- if (token.is_comment()) {
- insert_comment(token);
- return;
- }
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::option) {
- if (current_node().tag_name() == HTML::TagNames::option) {
- m_stack_of_open_elements.pop();
- }
- insert_html_element(token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::optgroup) {
- if (current_node().tag_name() == HTML::TagNames::option) {
- m_stack_of_open_elements.pop();
- }
- if (current_node().tag_name() == HTML::TagNames::optgroup) {
- m_stack_of_open_elements.pop();
- }
- insert_html_element(token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::optgroup) {
- if (current_node().tag_name() == HTML::TagNames::option && node_before_current_node().tag_name() == HTML::TagNames::optgroup)
- m_stack_of_open_elements.pop();
- if (current_node().tag_name() == HTML::TagNames::optgroup) {
- m_stack_of_open_elements.pop();
- } else {
- PARSE_ERROR();
- return;
- }
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::option) {
- if (current_node().tag_name() == HTML::TagNames::option) {
- m_stack_of_open_elements.pop();
- } else {
- PARSE_ERROR();
- return;
- }
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::select) {
- if (!m_stack_of_open_elements.has_in_select_scope(HTML::TagNames::select)) {
- PARSE_ERROR();
- return;
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
- reset_the_insertion_mode_appropriately();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::select) {
- PARSE_ERROR();
- if (!m_stack_of_open_elements.has_in_select_scope(HTML::TagNames::select))
- return;
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
- reset_the_insertion_mode_appropriately();
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::input, HTML::TagNames::keygen, HTML::TagNames::textarea)) {
- PARSE_ERROR();
- if (!m_stack_of_open_elements.has_in_select_scope(HTML::TagNames::select)) {
- return;
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::select);
- reset_the_insertion_mode_appropriately();
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::script, HTML::TagNames::template_)) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- if (token.is_end_of_file()) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- PARSE_ERROR();
- }
- void HTMLDocumentParser::handle_in_caption(HTMLToken& token)
- {
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::caption) {
- if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::caption)) {
- PARSE_ERROR();
- return;
- }
- generate_implied_end_tags();
- if (current_node().tag_name() != HTML::TagNames::caption)
- PARSE_ERROR();
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::caption);
- m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
- m_insertion_mode = InsertionMode::InTable;
- return;
- }
- 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))
- || (token.is_end_tag() && token.tag_name() == HTML::TagNames::table)) {
- if (!m_stack_of_open_elements.has_in_table_scope(HTML::TagNames::caption)) {
- PARSE_ERROR();
- return;
- }
- generate_implied_end_tags();
- if (current_node().tag_name() != HTML::TagNames::caption)
- PARSE_ERROR();
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::caption);
- m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
- m_insertion_mode = InsertionMode::InTable;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- 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)) {
- PARSE_ERROR();
- return;
- }
- process_using_the_rules_for(InsertionMode::InBody, token);
- }
- void HTMLDocumentParser::handle_in_column_group(HTMLToken& token)
- {
- if (token.is_character() && token.is_parser_whitespace()) {
- insert_character(token.codepoint());
- return;
- }
- if (token.is_comment()) {
- insert_comment(token);
- return;
- }
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::col) {
- insert_html_element(token);
- m_stack_of_open_elements.pop();
- token.acknowledge_self_closing_flag_if_set();
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::colgroup) {
- if (current_node().tag_name() != HTML::TagNames::colgroup) {
- PARSE_ERROR();
- return;
- }
- m_stack_of_open_elements.pop();
- m_insertion_mode = InsertionMode::InTable;
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::col) {
- PARSE_ERROR();
- return;
- }
- if ((token.is_start_tag() || token.is_end_tag()) && token.tag_name() == HTML::TagNames::template_) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- if (token.is_end_of_file()) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (current_node().tag_name() != HTML::TagNames::colgroup) {
- PARSE_ERROR();
- return;
- }
- m_stack_of_open_elements.pop();
- m_insertion_mode = InsertionMode::InTable;
- process_using_the_rules_for(m_insertion_mode, token);
- }
- void HTMLDocumentParser::handle_in_template(HTMLToken& token)
- {
- if (token.is_character() || token.is_comment() || token.is_doctype()) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- 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)) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::template_) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- 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)) {
- m_stack_of_template_insertion_modes.take_last();
- m_stack_of_template_insertion_modes.append(InsertionMode::InTable);
- m_insertion_mode = InsertionMode::InTable;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::col) {
- m_stack_of_template_insertion_modes.take_last();
- m_stack_of_template_insertion_modes.append(InsertionMode::InColumnGroup);
- m_insertion_mode = InsertionMode::InColumnGroup;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::tr) {
- m_stack_of_template_insertion_modes.take_last();
- m_stack_of_template_insertion_modes.append(InsertionMode::InTableBody);
- m_insertion_mode = InsertionMode::InTableBody;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
- m_stack_of_template_insertion_modes.take_last();
- m_stack_of_template_insertion_modes.append(InsertionMode::InRow);
- m_insertion_mode = InsertionMode::InRow;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_start_tag()) {
- m_stack_of_template_insertion_modes.take_last();
- m_stack_of_template_insertion_modes.append(InsertionMode::InBody);
- m_insertion_mode = InsertionMode::InBody;
- process_using_the_rules_for(m_insertion_mode, token);
- return;
- }
- if (token.is_end_tag()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_end_of_file()) {
- if (!m_stack_of_open_elements.contains(HTML::TagNames::template_)) {
- stop_parsing();
- } else {
- PARSE_ERROR();
- }
- m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::template_);
- m_list_of_active_formatting_elements.clear_up_to_the_last_marker();
- m_stack_of_template_insertion_modes.take_last();
- reset_the_insertion_mode_appropriately();
- process_using_the_rules_for(m_insertion_mode, token);
- }
- }
- void HTMLDocumentParser::handle_in_frameset(HTMLToken& token)
- {
- if (token.is_character() && token.is_parser_whitespace()) {
- insert_character(token.codepoint());
- return;
- }
- if (token.is_comment()) {
- insert_comment(token);
- return;
- }
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::frameset) {
- insert_html_element(token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::frameset) {
- // FIXME: If the current node is the root html element, then this is a parse error; ignore the token. (fragment case)
- m_stack_of_open_elements.pop();
- if (m_parsing_fragment && current_node().tag_name() != HTML::TagNames::frameset) {
- m_insertion_mode = InsertionMode::AfterFrameset;
- }
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::frame) {
- insert_html_element(token);
- m_stack_of_open_elements.pop();
- token.acknowledge_self_closing_flag_if_set();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::noframes) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- if (token.is_end_of_file()) {
- //FIXME: If the current node is not the root html element, then this is a parse error.
- stop_parsing();
- return;
- }
- PARSE_ERROR();
- }
- void HTMLDocumentParser::handle_after_frameset(HTMLToken& token)
- {
- if (token.is_character() && token.is_parser_whitespace()) {
- insert_character(token.codepoint());
- return;
- }
- if (token.is_comment()) {
- insert_comment(token);
- return;
- }
- if (token.is_doctype()) {
- PARSE_ERROR();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_end_tag() && token.tag_name() == HTML::TagNames::html) {
- m_insertion_mode = InsertionMode::AfterAfterFrameset;
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::noframes) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- if (token.is_end_of_file()) {
- stop_parsing();
- return;
- }
- PARSE_ERROR();
- }
- void HTMLDocumentParser::handle_after_after_frameset(HTMLToken& token)
- {
- if (token.is_comment()) {
- auto comment = adopt(*new Comment(document(), token.m_comment_or_character.data.to_string()));
- document().append_child(move(comment));
- return;
- }
- if (token.is_doctype() || token.is_parser_whitespace() || (token.is_start_tag() && token.tag_name() == HTML::TagNames::html)) {
- process_using_the_rules_for(InsertionMode::InBody, token);
- return;
- }
- if (token.is_end_of_file()) {
- stop_parsing();
- return;
- }
- if (token.is_start_tag() && token.tag_name() == HTML::TagNames::noframes) {
- process_using_the_rules_for(InsertionMode::InHead, token);
- return;
- }
- PARSE_ERROR();
- }
- void HTMLDocumentParser::reset_the_insertion_mode_appropriately()
- {
- for (ssize_t i = m_stack_of_open_elements.elements().size() - 1; i >= 0; --i) {
- RefPtr<Element> node = m_stack_of_open_elements.elements().at(i);
- if (node->tag_name() == HTML::TagNames::select) {
- TODO();
- }
- if (node->tag_name().is_one_of(HTML::TagNames::td, HTML::TagNames::th)) {
- m_insertion_mode = InsertionMode::InCell;
- return;
- }
- if (node->tag_name() == HTML::TagNames::tr) {
- m_insertion_mode = InsertionMode::InRow;
- return;
- }
- if (node->tag_name().is_one_of(HTML::TagNames::tbody, HTML::TagNames::thead, HTML::TagNames::tfoot)) {
- m_insertion_mode = InsertionMode::InTableBody;
- return;
- }
- if (node->tag_name() == HTML::TagNames::caption) {
- m_insertion_mode = InsertionMode::InCaption;
- return;
- }
- if (node->tag_name() == HTML::TagNames::colgroup) {
- m_insertion_mode = InsertionMode::InColumnGroup;
- return;
- }
- if (node->tag_name() == HTML::TagNames::table) {
- m_insertion_mode = InsertionMode::InTable;
- return;
- }
- if (node->tag_name() == HTML::TagNames::template_) {
- TODO();
- }
- if (node->tag_name() == HTML::TagNames::body) {
- m_insertion_mode = InsertionMode::InBody;
- return;
- }
- if (node->tag_name() == HTML::TagNames::frameset) {
- m_insertion_mode = InsertionMode::InFrameset;
- if (m_parsing_fragment) {
- TODO();
- }
- return;
- }
- if (node->tag_name() == HTML::TagNames::html) {
- TODO();
- }
- }
- m_insertion_mode = InsertionMode::InBody;
- if (m_parsing_fragment) {
- TODO();
- }
- }
- const char* HTMLDocumentParser::insertion_mode_name() const
- {
- switch (m_insertion_mode) {
- #define __ENUMERATE_INSERTION_MODE(mode) \
- case InsertionMode::mode: \
- return #mode;
- ENUMERATE_INSERTION_MODES
- #undef __ENUMERATE_INSERTION_MODE
- }
- ASSERT_NOT_REACHED();
- }
- Document& HTMLDocumentParser::document()
- {
- return *m_document;
- }
- }
|