diff --git a/Userland/Libraries/LibWeb/HTML/Parser/HTMLDocumentParser.cpp b/Userland/Libraries/LibWeb/HTML/Parser/HTMLDocumentParser.cpp index fd7a1655a3..0b63e9af4d 100644 --- a/Userland/Libraries/LibWeb/HTML/Parser/HTMLDocumentParser.cpp +++ b/Userland/Libraries/LibWeb/HTML/Parser/HTMLDocumentParser.cpp @@ -457,17 +457,34 @@ NonnullRefPtr HTMLDocumentParser::create_element_for(const HTMLTok return element; } -RefPtr HTMLDocumentParser::insert_foreign_element(const HTMLToken& token, const FlyString& namespace_) +// https://html.spec.whatwg.org/multipage/parsing.html#insert-a-foreign-element +NonnullRefPtr HTMLDocumentParser::insert_foreign_element(const HTMLToken& token, const FlyString& namespace_) { auto adjusted_insertion_location = find_appropriate_place_for_inserting_node(); + + // FIXME: Pass in adjusted_insertion_location.parent as the intended parent. auto element = create_element_for(token, namespace_); - // 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); + + auto pre_insertion_validity = adjusted_insertion_location.parent->ensure_pre_insertion_validity(element, adjusted_insertion_location.insert_before_sibling); + + // NOTE: If it's not possible to insert the element at the adjusted insertion location, the element is simply dropped. + if (!pre_insertion_validity.is_exception()) { + if (!m_parsing_fragment) { + // FIXME: push a new element queue onto element's relevant agent's custom element reactions stack. + } + + adjusted_insertion_location.parent->insert_before(element, adjusted_insertion_location.insert_before_sibling); + + if (!m_parsing_fragment) { + // FIXME: pop the element queue from element's relevant agent's custom element reactions stack, and invoke custom element reactions in that queue. + } + } + m_stack_of_open_elements.push(element); return element; } -RefPtr HTMLDocumentParser::insert_html_element(const HTMLToken& token) +NonnullRefPtr HTMLDocumentParser::insert_html_element(const HTMLToken& token) { return insert_foreign_element(token, Namespace::HTML); } diff --git a/Userland/Libraries/LibWeb/HTML/Parser/HTMLDocumentParser.h b/Userland/Libraries/LibWeb/HTML/Parser/HTMLDocumentParser.h index 4abbb74b72..4f1c4f2214 100644 --- a/Userland/Libraries/LibWeb/HTML/Parser/HTMLDocumentParser.h +++ b/Userland/Libraries/LibWeb/HTML/Parser/HTMLDocumentParser.h @@ -127,8 +127,8 @@ private: DOM::Text* find_character_insertion_node(); void flush_character_insertions(); - RefPtr insert_foreign_element(const HTMLToken&, const FlyString&); - RefPtr insert_html_element(const HTMLToken&); + NonnullRefPtr insert_foreign_element(const HTMLToken&, const FlyString&); + NonnullRefPtr insert_html_element(const HTMLToken&); DOM::Element& current_node(); DOM::Element& adjusted_current_node(); DOM::Element& node_before_current_node();