Truly independent web browser
LibWeb: Synchronize HTML parser comments with the current spec
Andreas Kling2bcf93454be4
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex cf8b5b4f1e8..aa4714a468c 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -1503,13 +1503,14 @@ WebIDL::ExceptionOr<Document*> Document::open(Optional<Utf16String> const&, Opti// 17. Set the insertion point to point at just before the end of the input stream (which at this point will be empty).m_parser->tokenizer().update_insertion_point();- // 18. Set the parser's allow declarative shadow roots to true.+ // FIXME: Preserve the parser's inherited allow declarative shadow roots value; the document open steps do not+ // override it.m_parser->set_allow_declarative_shadow_roots(HTML::HTMLParser::AllowDeclarativeShadowRoots::Yes);- // 19. Update the current document readiness of document to "loading".+ // 18. Update the current document readiness of document to "loading".update_readiness(HTML::DocumentReadyState::Loading);- // 20. Return document.+ // 19. Return document.return this;}diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex 9b25941625c..f0b82a76826 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -4055,15 +4055,18 @@ bool Element::is_actually_disabled() const// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#fragment-parsing-algorithm-stepsWebIDL::ExceptionOr<GC::Ref<DOM::DocumentFragment>> Element::parse_fragment(Variant<GC::Ref<Element>, GC::Ref<DocumentFragment>> target, Utf16View markup, HTML::ParserScriptingMode scripting_mode, HTML::FragmentFastPath fast_path){- // 1. Assert: scriptingMode is either Inert or Fragment.+ // NB: This helper implements Legacy mode with no sanitizer configuration; callers supply scriptingMode.+ // The HTML fragment parser requires scriptingMode to be either Inert or Fragment.VERIFY(scripting_mode == HTML::ParserScriptingMode::Inert || scripting_mode == HTML::ParserScriptingMode::Fragment);- // 2. If target's node document is an XML document, then return the result of invoking the XML fragment parsing- // algorithm given target and markup.+ // 1. If mode is Legacy and target's node document is an XML document, then return the result of invoking the+ // XML fragment parsing algorithm given target and markup.if (target.visit([](auto node) { return node->document().is_xml_document(); }))return XMLFragmentParser::parse_xml_fragment(target, markup);- // 3. If context's node document is an XML document, then set newChildren to the result of invoking the XML fragment parsing algorithm given context and markup.+ // 8. Return the result of the HTML fragment parsing algorithm given target, markup, allowDeclarativeShadowRoots,+ // scriptingMode, and sanitizerConfig.+ // NB: Legacy mode sets allowDeclarativeShadowRoots to false and uses a null sanitizerConfig.return HTML::HTMLParser::parse_html_fragment(target, markup, HTML::HTMLParser::AllowDeclarativeShadowRoots::No, scripting_mode, fast_path);}diff --git a/Libraries/LibWeb/HTML/HTMLScriptElement.cpp b/Libraries/LibWeb/HTML/HTMLScriptElement.cppindex 140ef420b3a..4307aa90ddb 100644--- a/Libraries/LibWeb/HTML/HTMLScriptElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLScriptElement.cpp@@ -219,23 +219,24 @@ void HTMLScriptElement::prepare_script()m_force_async = true;}- // 5. Execute the Prepare the script text algorithm on el. If that algorithm threw an error, then return.+ // NB: The Trusted Types integration prepares the cached script text value before the HTML source-text step.if (prepare_script_text().is_exception())return;- // 6. Let source text be el’s script text value.+ // 5. Let source text be el's child text content.+ // NB: The Trusted Types integration supplies this through the cached script text value.auto source_text = m_script_text;- // 7. If el has no src attribute, and source text is the empty string, then return.+ // 6. If el has no src attribute, and source text is the empty string, then return.if (!has_attribute_ns({}, HTML::AttributeNames::src) && source_text.is_empty()) {return;}- // 8. If el is not connected, then return.+ // 7. If el is not connected, then return.if (!is_connected())return;- // 9. If any of the following are true:+ // 8. If any of the following are true:// - el has a type attribute whose value is the empty string;// - el has no type attribute but it has a language attribute and that attribute's value is the empty string; or// - el has neither a type attribute nor a language attribute@@ -250,7 +251,8 @@ void HTMLScriptElement::prepare_script()}// Otherwise, if el has a type attribute,else if (maybe_type_attribute.has_value()) {- // then let the script block's type string be the value of that attribute with leading and trailing ASCII whitespace stripped.+ // then let the script block's type string be the value of that attribute.+ // FIXME: Preserve whitespace in the type string; only the classic JavaScript MIME comparison strips it.auto trimmed_type_attribute = maybe_type_attribute->trim(Infra::ASCII_WHITESPACE);script_block_type = trimmed_type_attribute;}@@ -263,61 +265,62 @@ void HTMLScriptElement::prepare_script()script_block_type = builder.to_string();}- // 10. If the script block's type string is a JavaScript MIME type essence match,+ // 9. If the script block's type string is a JavaScript MIME type essence match, or el has a type attribute whose+ // value with leading and trailing ASCII whitespace stripped is a JavaScript MIME type essence match,if (MimeSniff::is_javascript_mime_type_essence_match(script_block_type)) {// then set el's type to "classic".m_script_type = ScriptType::Classic;}- // 11. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "module",+ // 10. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "module",else if (script_block_type.equals_ignoring_ascii_case(u"module"sv)) {// then set el's type to "module".m_script_type = ScriptType::Module;}- // 12. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "importmap",+ // 11. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "importmap",else if (script_block_type.equals_ignoring_ascii_case(u"importmap"sv)) {// then set el's type to "importmap".m_script_type = ScriptType::ImportMap;}- // FIXME: 13. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "speculationrules", then set el's type to "speculationrules".- // 14. Otherwise, return. (No script is executed, and el's type is left as null.)+ // FIXME: 12. Otherwise, if the script block's type string is an ASCII case-insensitive match for the string "speculationrules", then set el's type to "speculationrules".+ // 13. Otherwise, return. (No script is executed, and el's type is left as null.)else {VERIFY(m_script_type == ScriptType::Null);return;}- // 15. If parser document is non-null, then set el's parser document back to parser document and set el's force async to false.+ // 14. If parser document is non-null, then set el's parser document back to parser document and set el's force async to false.if (parser_document) {m_parser_document = parser_document;m_force_async = false;}- // 16. Set el's already started to true.+ // 15. Set el's already started to true.m_already_started = true;- // 17. Set el's preparation-time document to its node document.+ // 16. Set el's preparation-time document to its node document.m_preparation_time_document = &document();- // 18. If parser document is non-null, and parser document is not equal to el's preparation-time document, then return.+ // 17. If parser document is non-null, and parser document is not equal to el's preparation-time document, then return.if (parser_document != nullptr && parser_document != m_preparation_time_document) {dbgln("HTMLScriptElement: Refusing to run script because the parser document is not the same as the preparation time document.");return;}- // 19. If scripting is disabled for el, then return.+ // 18. If scripting is disabled for el, then return.if (is_scripting_disabled()) {dbgln("HTMLScriptElement: Refusing to run script because scripting is disabled.");return;}- // 20. If el has a nomodule content attribute and its type is "classic", then return.+ // 19. If el has a nomodule content attribute and its type is "classic", then return.if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::nomodule)) {dbgln("HTMLScriptElement: Refusing to run classic script because it has the nomodule attribute.");return;}- // FIXME: 21. Let cspType be "script speculationrules" if el's type is "speculationrules"; otherwise, "script".+ // FIXME: 20. Let cspType be "script speculationrules" if el's type is "speculationrules"; otherwise, "script".- // 22. If el does not have a src content attribute, and the Should element's inline behavior be blocked by Content+ // 21. If el does not have a src content attribute, and the Should element's inline behavior be blocked by Content// Security Policy? algorithm returns "Blocked" when given el, cspType, and source text, then return [CSP]if (!has_attribute_ns({}, AttributeNames::src)&& ContentSecurityPolicy::should_elements_inline_type_behavior_be_blocked_by_content_security_policy(*this, ContentSecurityPolicy::Directives::Directive::InlineType::Script, source_text.utf16_view()) == ContentSecurityPolicy::Directives::Directive::Result::Blocked) {@@ -325,7 +328,7 @@ void HTMLScriptElement::prepare_script()return;}- // 23. If el has an event attribute and a for attribute, and el's type is "classic", then:+ // 22. If el has an event attribute and a for attribute, and el's type is "classic", then:if (m_script_type == ScriptType::Classic && has_attribute(HTML::AttributeNames::event) && has_attribute(HTML::AttributeNames::for_)) {// 1. Let for be the value of el's' for attribute.auto for_ = attribute(HTML::AttributeNames::for_).value_or({});@@ -351,7 +354,7 @@ void HTMLScriptElement::prepare_script()}}- // 24. If el has a charset attribute, then let encoding be the result of getting an encoding from the value of the charset attribute.+ // 23. If el has a charset attribute, then let encoding be the result of getting an encoding from the value of the charset attribute.// If el does not have a charset attribute, or if getting an encoding failed, then let encoding be el's node document's the encoding.Optional<Utf16String> encoding;@@ -367,34 +370,34 @@ void HTMLScriptElement::prepare_script()VERIFY(encoding.has_value());- // 25. Let classic script CORS setting be the current state of el's crossorigin content attribute.+ // 24. Let classic script CORS setting be the current state of el's crossorigin content attribute.auto classic_script_cors_setting = m_crossorigin;- // 26. Let module script credentials mode be the CORS settings attribute credentials mode for el's crossorigin content attribute.+ // 25. Let module script credentials mode be the CORS settings attribute credentials mode for el's crossorigin content attribute.auto module_script_credential_mode = cors_settings_attribute_credentials_mode(m_crossorigin);- // 27. Let cryptographic nonce be el's [[CryptographicNonce]] internal slot's value.+ // 26. Let cryptographic nonce be el's [[CryptographicNonce]] internal slot's value.auto cryptographic_nonce = nonce();- // 28. If el has an integrity attribute, then let integrity metadata be that attribute's value.+ // 27. If el has an integrity attribute, then let integrity metadata be that attribute's value.// Otherwise, let integrity metadata be the empty string.Utf16String integrity_metadata;if (auto maybe_integrity = get_attribute_ns({}, HTML::AttributeNames::integrity); maybe_integrity.has_value()) {integrity_metadata = *maybe_integrity;}- // 29. Let referrer policy be the current state of el's referrerpolicy content attribute.+ // 28. Let referrer policy be the current state of el's referrerpolicy content attribute.auto referrer_policy = m_referrer_policy;- // 30. Let fetch priority be the current state of el's fetchpriority content attribute.+ // 29. Let fetch priority be the current state of el's fetchpriority content attribute.auto fetch_priority = Fetch::Infrastructure::request_priority_from_string(attribute(HTML::AttributeNames::fetchpriority).value_or({})).value_or(Fetch::Infrastructure::Request::Priority::Auto);- // 31. Let parser metadata be "parser-inserted" if el is parser-inserted, and "not-parser-inserted" otherwise.+ // 30. Let parser metadata be "parser-inserted" if el is parser-inserted, and "not-parser-inserted" otherwise.auto parser_metadata = is_parser_inserted()? Fetch::Infrastructure::Request::ParserMetadata::ParserInserted: Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted;- // 32. Let options be a script fetch options whose cryptographic nonce is cryptographic nonce,+ // 31. Let options be a script fetch options whose cryptographic nonce is cryptographic nonce,// integrity metadata is integrity metadata, parser metadata is parser metadata,// credentials mode is module script credentials mode, referrer policy is referrer policy,// and fetch priority is fetch priority.@@ -407,10 +410,10 @@ void HTMLScriptElement::prepare_script().fetch_priority = move(fetch_priority),};- // 33. Let settings object be el's node document's relevant settings object.+ // 32. Let settings object be el's node document's relevant settings object.auto& settings_object = document().relevant_settings_object();- // 34. If el has a src content attribute, then:+ // 33. If el has a src content attribute, then:if (has_attribute_ns({}, HTML::AttributeNames::src)) {// 1. If el's type is "importmap" or "speculationrules", then:// FIXME: Add "speculationrules" support.@@ -498,7 +501,7 @@ void HTMLScriptElement::prepare_script()}}- // 35. If el does not have a src content attribute:+ // 34. If el does not have a src content attribute:if (!has_attribute_ns({}, HTML::AttributeNames::src)) {// 1. Let base URL be el's node document's document base URL.auto base_url = document().base_url();@@ -544,7 +547,7 @@ void HTMLScriptElement::prepare_script()// FIXME: -> "speculationrules"}- // 36. If el's type is "classic" and el has a src attribute, or el's type is "module":+ // 35. If el's type is "classic" and el has a src attribute, or el's type is "module":if ((m_script_type == ScriptType::Classic && has_attribute_ns({}, HTML::AttributeNames::src)) || m_script_type == ScriptType::Module) {// 1. Assert: el's result is "uninitialized".// FIXME: I believe this step to be a spec bug, and it should be removed: https://github.com/whatwg/html/issues/8534@@ -618,7 +621,7 @@ void HTMLScriptElement::prepare_script()}}- // 37. Otherwise:+ // 36. Otherwise:else {// 1. Assert: el's result is not "uninitialized".VERIFY(!m_result.has<ResultState::Uninitialized>());diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cppindex 94980866113..b6c27cc1f27 100644--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp@@ -349,6 +349,13 @@ GC::Ref<DOM::Element> HTMLParser::create_element_for_rust_parser(Utf16FlyStringif (had_duplicate_attribute)element->set_had_duplicate_attribute_during_tokenization({});+ // 21. If element is a form-associated element and not a form-associated custom element, the form element pointer+ // is not null, the parser is not parsing template contents, the parser's fragment context element is null,+ // element is either not listed or doesn't have a form attribute, the intendedParent is in the same tree as+ // the element pointed to by the form element pointer, and the parser's insertion target redirection map[the+ // element pointed to by the form element pointer] does not exist, then associate element with the form+ // element pointed to by the form element pointer and set element's parser inserted flag.+ // FIXME: Check the fragment context element, the full parsing-template-contents predicate, and the redirection map.if (form_element && !has_template_element_on_stack) {auto* html_element = as_if<HTML::HTMLElement>(*element);if (html_element && html_element->is_form_associated_element() && !html_element->is_form_associated_custom_element()) {@@ -907,7 +914,7 @@ void HTMLParserEndState::advance_to_dom_content_loaded_phase()// FIXME: 4. Enable the client message queue of the ServiceWorkerContainer object whose associated service worker client is the Document object's relevant settings object.- // FIXME: 5. Invoke WebDriver BiDi DOM content loaded with the Document's browsing context, and a new WebDriver BiDi navigation status whose id is the Document object's navigation id, status is "pending", and url is the Document object's URL.+ // FIXME: 5. Invoke WebDriver BiDi DOM content loaded with the Document's browsing context, and a new WebDriver BiDi navigation status whose id is the Document object's during-loading navigation ID for WebDriver BiDi, status is "pending", and url is the Document object's URL.// NB: Only advance the state machine after this task has run, so that anything the DOMContentLoaded event// handlers did (e.g. starting loads that delay the load event) is visible to the remaining phases. This@@ -1028,9 +1035,9 @@ void HTMLParserEndState::complete()}}- // FIXME: 6. Invoke WebDriver BiDi load complete with the Document's browsing context, and a new WebDriver BiDi navigation status whose id is the Document object's navigation id, status is "complete", and url is the Document object's URL.+ // FIXME: 6. Invoke WebDriver BiDi load complete with the Document's browsing context, and a new WebDriver BiDi navigation status whose id is the Document object's during-loading navigation ID for WebDriver BiDi, status is "complete", and url is the Document object's URL.- // FIXME: 7. Set the Document object's navigation id to null.+ // FIXME: 7. Set the Document object's during-loading navigation ID for WebDriver BiDi to null.// 8. Set the Document's load timing info's load event end time to the current high resolution time given window.document->load_timing_info().load_event_end_time = HighResolutionTime::current_high_resolution_time(relevant_global_object(window));@@ -1068,7 +1075,8 @@ GC::Ref<DOM::Element> HTMLParser::create_element_for(Utf16FlyString const& local// The active speculative HTML parser runs synchronously to completion, so it is null whenever the real parser// invokes this algorithm. It emits speculative fetch candidates directly instead of producing mock elements.- // 2. Otherwise, optionally create a speculative mock element given namespace, token's tag name, and token's attributes.+ // 2. Otherwise, if the parser's parser sanitizer configuration is null, optionally create a speculative mock+ // element given namespace, token's tag name, and token's attributes.// We deliberately skip step 2, the active speculative parser already issues these fetches, so doing it again here// would be redundant.@@ -1085,23 +1093,35 @@ GC::Ref<DOM::Element> HTMLParser::create_element_for(Utf16FlyString const& localreturn {};};- // 5. Let is be the value of the "is" attribute in token, if such an attribute exists; otherwise null.+ // FIXME: 5. Let sanitizerConfig be the parser's parser sanitizer configuration.+ // FIXME: 6. Let sanitizerAction be "Keep".+ // FIXME: 7. If sanitizerConfig is not null, then set sanitizerAction to the result of getting the sanitizer action+ // for an element name given localName, namespace, and sanitizerConfig.++ // 8. Let is be the value of the "is" attribute in token, if such an attribute exists; otherwise null.Optional<Utf16FlyString> is_value;if (auto is_attribute = attribute_value(AttributeNames::is); is_attribute.has_value())is_value = Utf16FlyString::from_utf8_without_validation(*is_attribute);- // 6. Let registry be the result of looking up a custom element registry given intendedParent.+ // FIXME: 9. If is is non-null, sanitizerConfig is non-null, and the result of checking if a sanitizer config allows+ // an attribute given "is", null, is, localName, namespace, and sanitizerConfig is false, then set is to null.++ // 10. Let registry be the result of looking up a custom element registry given intendedParent.auto registry = look_up_a_custom_element_registry(intended_parent);- // 7. Let definition be the result of looking up a custom element definition given registry, namespace, localName,- // and is.+ // 11. Let creationRegistry be registry if sanitizerAction is "Keep"; otherwise null.+ // NB: Without a sanitizer configuration, creationRegistry is registry.++ // 12. Let definition be null if sanitizerAction is not "Keep"; otherwise the result of looking up a custom element+ // definition given registry, namespace, localName, and is.+ // FIXME: Handle sanitizer actions other than "Keep".auto definition = look_up_a_custom_element_definition(registry, namespace_, local_name, is_value);- // 8. Let willExecuteScript be true if definition is non-null and the parser was not created as part of the HTML- // fragment parsing algorithm; otherwise false.+ // 13. Let willExecuteScript be true if definition is non-null and the parser's fragment context element is null;+ // otherwise false.bool will_execute_script = definition && !m_parsing_fragment;- // 9. If willExecuteScript is true:+ // 14. If willExecuteScript is true:if (will_execute_script) {// 1. Increment document's throw-on-dynamic-markup-insertion counter.document->increment_throw_on_dynamic_markup_insertion_counter({});@@ -1115,8 +1135,9 @@ GC::Ref<DOM::Element> HTMLParser::create_element_for(Utf16FlyString const& localrelevant_similar_origin_window_agent(document).custom_element_reactions_stack.element_queue_stack.append({});}- // 10. Let element be the result of creating an element given document, localName, namespace, null, is,- // willExecuteScript, and registry.+ // 15. Let element be the result of creating an element given document, localName, namespace, prefix, is,+ // willExecuteScript, and creationRegistry.+ // NB: This helper currently implements only the default null prefix and the unsanitized creation registry.auto element = DOM::create_element(*document, local_name, namespace_, {}, is_value, will_execute_script, registry).release_value_but_fixme_should_propagate_errors();// AD-HOC: Let <link> elements know which document they were originally parsed for.@@ -1132,13 +1153,22 @@ GC::Ref<DOM::Element> HTMLParser::create_element_for(Utf16FlyString const& localif (auto* style_element = as_if<DOM::StyleElementBase>(*element))style_element->set_parser_document({}, document);- // 11. Append each attribute in the given token to element.+ // FIXME: 16. If sanitizerAction is "Replace with children", then set element's custom element registry to registry.++ // 17. For each attribute of token's attributes:element->ensure_attribute_capacity(attributes.size());for (auto const& attribute : attributes) {+ // FIXME: 1. If sanitizerConfig is not null:+ // 1. Let attrLocalName be attribute's local name if attribute is a namespaced attribute; otherwise+ // attribute's name.+ // 2. Let attrNamespace be attribute's namespace if attribute is a namespaced attribute; otherwise null.+ // 3. If the result of checking if a sanitizer config allows an attribute given attrLocalName,+ // attrNamespace, attribute's value, localName, namespace, and sanitizerConfig is false, then continue.Optional<Utf16FlyString> prefix;if (attribute.prefix_len != 0)prefix = Utf16FlyString::from_utf8_without_validation({ reinterpret_cast<char const*>(attribute.prefix_ptr), attribute.prefix_len });DOM::QualifiedName qualified_name { Utf16FlyString::from_raw(attribute.local_name), move(prefix), attribute_namespace_from_html_parser_ffi(attribute.namespace_) };+ // 2. Append attribute to element.element->append_attribute(move(qualified_name), utf8_string_from_ffi(attribute.value_ptr, attribute.value_len));}@@ -1153,7 +1183,7 @@ GC::Ref<DOM::Element> HTMLParser::create_element_for(Utf16FlyString const& local}}- // 12. If willExecuteScript is true:+ // 18. If willExecuteScript is true:if (will_execute_script) {// 1. Let queue be the result of popping from document's relevant agent's custom element reactions stack.// (This will be the same element queue as was pushed above.)@@ -1166,19 +1196,21 @@ GC::Ref<DOM::Element> HTMLParser::create_element_for(Utf16FlyString const& localdocument->decrement_throw_on_dynamic_markup_insertion_counter({});}- // FIXME: 13. If element has an xmlns attribute in the XMLNS namespace whose value is not exactly the same as the element's namespace, that is a parse error.+ // FIXME: 19. If element has an xmlns attribute in the XMLNS namespace whose value is not exactly the same as the element's namespace, that is a parse error.// Similarly, if element has an xmlns:xlink attribute in the XMLNS namespace whose value is not the XLink Namespace, that is a parse error.if (auto* html_element = as_if<HTML::HTMLElement>(*element)) {if (html_element->is_form_associated_element() && !html_element->is_form_associated_custom_element()) {- // 14. If element is a resettable element and not a form-associated custom element, then invoke its reset algorithm.+ // 20. If element is a resettable element and not a form-associated custom element, then invoke its reset algorithm.// (This initializes the element's value and checkedness based on the element's attributes.)if (html_element->is_resettable())html_element->reset_algorithm();}}- // 16. Return element.+ // NB: Step 21 (parser form association) is performed by create_element_for_rust_parser().++ // 22. Return element.return element;}@@ -1756,6 +1788,8 @@ GC::Ptr<DOM::DocumentFragment> HTMLParser::try_parse_html_fragment_fast(DOM::Ele// https://html.spec.whatwg.org/multipage/parsing.html#parsing-html-fragmentsWebIDL::ExceptionOr<GC::Ref<DOM::DocumentFragment>> HTMLParser::parse_html_fragment(Variant<GC::Ref<DOM::Element>, GC::Ref<DOM::DocumentFragment>> target, Utf16View input, AllowDeclarativeShadowRoots allow_declarative_shadow_roots, ParserScriptingMode scripting_mode, FragmentFastPath fast_path){+ // FIXME: Support the optional sanitizer configuration argument.+// 1. Assert: scriptingMode is either Inert or Fragment.VERIFY(scripting_mode == HTML::ParserScriptingMode::Inert || scripting_mode == HTML::ParserScriptingMode::Fragment);@@ -1781,12 +1815,14 @@ WebIDL::ExceptionOr<GC::Ref<DOM::DocumentFragment>> HTMLParser::parse_html_fragm// 6. If contextDocument is in quirks mode, then set document's mode to "quirks".// 7. Otherwise, if contextDocument is in limited-quirks mode, then set document's mode to "limited-quirks".- // 8. Create a new HTML parser whose allow declarative shadow roots is allowDeclarativeShadowRoots, and associate it with document.- // 9. If contextDocument's scripting is disabled, then set scriptingMode to Disabled.- // 10. Set the parser's scripting mode to scriptingMode.+ // 8. If contextDocument's scripting is disabled, then set scriptingMode to Disabled.if (context_document.is_scripting_disabled())scripting_mode = HTML::ParserScriptingMode::Disabled;+ // 9. Create a new HTML parser whose parser sanitizer configuration is configuration, allow declarative shadow+ // roots is allowDeclarativeShadowRoots, scripting mode is scriptingMode, and fragment context element is+ // context, and associate it with document.+ // NB: The currently supported calls use a null sanitizer configuration.// OPTIMIZATION: Use the context document directly while keeping the parser detached from its document parser state.// The detached root below supplies the temporary tree-builder state, while all output nodes are// created for their final document. This avoids initializing a complete Document for every fragment.@@ -1795,7 +1831,7 @@ WebIDL::ExceptionOr<GC::Ref<DOM::DocumentFragment>> HTMLParser::parse_html_fragmparser->set_allow_declarative_shadow_roots(allow_declarative_shadow_roots);parser->m_context_element = context; // FIXME: Is this needed?- // 11. Set the state of the HTML parser's tokenization stage as follows, switching on context:+ // 10. Set the state of the HTML parser's tokenization stage as follows, switching on context:bool const context_element_is_html = context->namespace_uri() == Namespace::HTML;// - title// - textarea@@ -1837,31 +1873,33 @@ WebIDL::ExceptionOr<GC::Ref<DOM::DocumentFragment>> HTMLParser::parse_html_fragmauto target_node = target.visit([](auto node) -> GC::Ref<DOM::Node> { return node; });- // 12. Let root be the result of creating an element given document, "html", the HTML namespace, null, null, false,+ // 11. Let root be the result of creating an element given document, "html", the HTML namespace, null, null, false,// and the result of looking up a custom element registry given target.auto root_registry = look_up_a_custom_element_registry(target_node);auto root = MUST(DOM::create_element(context_document, HTML::TagNames::html, Namespace::HTML, {}, {}, false, root_registry));- // 13. Append root to document.+ // 12. Append root to document.// OPTIMIZATION: Keep the root detached. It is only a sentinel for the tree builder, whose root insertions are// redirected to the output fragment.- // 14. Set up the HTML parser's stack of open elements so that it contains just the single element root.- // 15. Let fragment be a new DocumentFragment whose node document is target's node document.+ // 13. Set up the HTML parser's stack of open elements so that it contains just the single element root.+ // 14. Let fragment be the result of creating a document fragment given document if configuration is non-null;+ // otherwise target's node document.auto fragment = DOM::DocumentFragment::create(target_node->document());- // 16. Set the parser's root insertion target to fragment.+ // 15. Set the parser's insertion target redirection map[root] to (fragment, null).+ // NB: m_root_insertion_target represents the root entry of the insertion target redirection map.parser->m_root_insertion_target = fragment;- // 17. If context is a template element, then push "in template" onto the stack of template insertion modes so that+ // 16. If context is a template element, then push "in template" onto the stack of template insertion modes so that// it is the new current template insertion mode.- // 18. Create a start tag token whose name is the local name of context and whose attributes are the attributes of context.+ // 17. Create a start tag token whose name is the local name of context and whose attributes are the attributes of context.// Let this start tag token be the start tag token of context; e.g. for the purposes of determining if it is an// HTML integration point.- // 19. Reset the parser's insertion mode appropriately.+ // 18. Reset the parser's insertion mode appropriately.// NB: The parser will reference the context element as part of that algorithm.- // 20. Set the HTML parser's form element pointer to the nearest node to context that is a form element+ // 19. Set the HTML parser's form element pointer to the nearest node to context that is a form element// (going straight up the ancestor chain, and including the element itself, if it is a form element), if any.// (If there is no such form element, the form element pointer keeps its initial value, null.)parser->m_form_element = as_if<HTMLFormElement>(context);@@ -1916,10 +1954,15 @@ WebIDL::ExceptionOr<GC::Ref<DOM::DocumentFragment>> HTMLParser::parse_html_fragmallow_declarative_shadow_roots == AllowDeclarativeShadowRoots::Yes,parser->m_form_element ? reinterpret_cast<size_t>(parser->m_form_element.ptr()) : 0);- // 22. Place the input into the input stream for the HTML parser just created. The encoding confidence is irrelevant.- // 23. Start the HTML parser and let it run until it has consumed all the characters just inserted into the input stream.+ // 20. Place the input into the input stream for the HTML parser just created. The encoding confidence is irrelevant.+ // NB: The input was supplied when constructing the parser above.+ // 21. Start the HTML parser and let it run until it has consumed all the characters just inserted into the input stream.parser->run_until_completion();+ // 22. Adopt fragment into target's node document.+ // NB: The fragment is already in the target's node document.++ // 23. Return fragment.return fragment;}@@ -2241,8 +2284,9 @@ Utf16String HTMLParser::serialize_html_fragment(DOM::Node const& node, Serializa// -> If current node is a ProcessingInstructionauto& processing_instruction_node = as<DOM::ProcessingInstruction>(current_node);- // Append the literal string "<?" (U+003C LESS-THAN SIGN, U+003F QUESTION MARK), followed by the value of current node's target IDL attribute,- // followed by a single U+0020 SPACE character, followed by the value of current node's data IDL attribute, followed by a single U+003E GREATER-THAN SIGN character (>).+ // Append "<?", followed by the value of current node's target, U+0020 SPACE, the value of+ // current node's data, and "?>".+ // FIXME: Use "?>" as the processing instruction terminator.builder.append_ascii("<?"sv);builder.append(processing_instruction_node.target().view());builder.append_ascii(' ');@@ -2872,7 +2916,7 @@ extern "C" void ladybird_html_parser_insert_text(size_t parent, size_t offset, uauto insertion_location = node_and_offset_from_html_parser_ffi(parent, offset);auto& parent_node = *insertion_location.node;- // 3. If insertionLocation is in a Document node, then return.+ // 3. If targetParent is a Document node, then return.// NOTE: The DOM will not let Document nodes have Text node children, so they are dropped on the floor.if (parent_node.is_document())return;@@ -2987,7 +3031,8 @@ extern "C" void ladybird_html_parser_insert_node(size_t parent, size_t offset, s}// https://html.spec.whatwg.org/multipage/parsing.html#adoptionAgency-// NB: Steps 15 and 16, given the (target, refNode) pair computed by step 14.+// NB: Steps 20 and 21 of the outer loop, given the (target, refNode) pair computed by step 18.+// This callback implements the unsanitized case, in which lastNode is non-null.extern "C" void ladybird_html_parser_reinsert_last_node(size_t parent, size_t offset, size_t last_node){auto insertion_location = node_and_offset_from_html_parser_ffi(parent, offset);@@ -2995,11 +3040,12 @@ extern "C" void ladybird_html_parser_reinsert_last_node(size_t parent, size_t ofGC::Ptr<DOM::Node> ref_node = insertion_location.child_at_offset();auto& last_node_to_insert = node_from_html_parser_ffi(last_node);- // 15. If lastNode's parent is non-null, then remove lastNode.+ // 20. If lastNode is non-null and lastNode's parent is non-null, then remove lastNode.if (last_node_to_insert.parent())last_node_to_insert.remove();- // 16. If all of the following are true:+ // 21. If all of the following are true:+ // - lastNode is non-null;// - lastNode's parent is null;// - lastNode is not a host-including inclusive ancestor of target;// - target is not a Document node, or it does not have an element child; anddiff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rsindex b5082fc626f..4e3de9c2b46 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs@@ -419,16 +419,18 @@ impl ParserState {if fragment_context.form_element != 0 {self.form_element = Some(fragment_context.form_element);}- // 14. If context is a template element, then push "in template" onto the stack of template insertion modes+ // 16. If context is a template element, then push "in template" onto the stack of template insertion modes// so that it is the new current template insertion mode.if context_namespace == RustFfiHtmlNamespace::Html && context_local_name == tag_name!("template") {self.stack_of_template_insertion_modes.push(InsertionMode::InTemplate);}- // 15. Create a start tag token whose name is the local name of context and whose attributes are the attributes+ // 17. Create a start tag token whose name is the local name of context and whose attributes are the attributes// of context.- // The synthetic context token is represented by ParserState::context_element.+ // Let this start tag token be the start tag token of context; e.g. for the purposes of determining if it is+ // an HTML integration point.+ // NB: The synthetic context token is represented by ParserState::context_element.- // 16. Reset the parser's insertion mode appropriately.+ // 18. Reset the parser's insertion mode appropriately.self.reset_the_insertion_mode_appropriately();self.original_insertion_mode = self.insertion_mode;}@@ -625,8 +627,9 @@ impl TreeBuilder {let has_fast_data_run = unsafe { self.tokenizer.as_ref().has_fast_data_run() };if has_fast_data_run {self.reconstruct_the_active_formatting_elements();- // SAFETY: See above. Reconstructing the active formatting elements does not- // run the tokenizer, so the same Data-state run is still available.+ // SAFETY: See above.+ // FIXME: Recheck the tokenizer state after reconstruction; custom element reactions can abort+ // the parser, invalidating the previously available Data-state run.let contains_non_whitespace = unsafe {self.tokenizer.as_mut()@@ -764,10 +767,12 @@ impl TreeBuilder {self.parse_error("unexpected DOCTYPE token");}- // Append a DocumentType node to the Document node, with its name set to the name given in the DOCTYPE token,- // or the empty string if the name was missing; its public ID set to the public identifier given in the DOCTYPE token,- // or the empty string if the public identifier was missing; and its system ID set to the system identifier- // given in the DOCTYPE token, or the empty string if the system identifier was missing.+ // Let doctype be the result of creating a doctype given the Document node, the name given in the DOCTYPE+ // token (or the empty string if the name was missing), the public identifier given in the DOCTYPE token+ // (or the empty string if the public identifier was missing), and the system identifier given in the DOCTYPE+ // token (or the empty string if the system identifier was missing). Then, if the Document node has neither+ // a DocumentType child nor an element child, append doctype to the Document node.+ // FIXME: Check for an existing DocumentType or element child before appending the new doctype.let name = if doctype.missing_name {""} else {@@ -1006,34 +1011,35 @@ impl TreeBuilder {if token.is_start_tag_named(tag_name!("script")) {// Run these steps://- // 1. Let the adjustedInsertionLocation be the appropriate place for inserting a node.- // 2. Create an element for the token in the HTML namespace, with the intended parent being the element in- // which the adjustedInsertionLocation finds itself.- // 3. If the scripting mode is not Fragment, then set the element's parser document to the Document.- // NOTE: The Fragment scripting mode treats parser-inserted scripts as if they were not parser-inserted,- // allowing, for example, executing scripts when applying a fragment created by- // createContextualFragment().- // 4. Set the element's force async to false.- // NOTE: This ensures that, if the script is external, any document.write() calls in the script will execute- // in-line, instead of blowing the document away, as would happen in most other cases. It also- // prevents the script from executing until the end tag is seen.- // 5. If the parser's scripting mode is Inert, then set the script element's already started to true.+ // 1. Create an element for the token in the HTML namespace, with the intended parent being the target parent+ // of the appropriate place for inserting a node.+ // 2. If the scripting mode is not Fragment, then set the element's parser document to the Document and set+ // its force async to false.+ // NOTE: The Fragment scripting mode treats parser-inserted scripts as if they were not parser-inserted+ // (so external scripts execute asynchronously), allowing, for example, executing scripts when applying+ // a fragment created by createContextualFragment().+ // FIXME: Keep force async true in Fragment mode; the C++ element configuration currently clears it.+ // NOTE: Setting force async to false ensures that, if the script is external, any document.write() calls+ // in the script will execute in-line, instead of blowing the document away, as would happen in most+ // other cases. It also prevents the script from executing until the end tag is seen.+ // 3. If the parser's scripting mode is Inert, then set the script element's already started to true.// (fragment case)- // 6. If the parser was invoked via the document.write() or document.writeln() methods, then optionally set+ // 4. If the parser was invoked via the document.write() or document.writeln() methods, then optionally set// the script element's already started to true. (For example, the user agent might use this clause to// prevent execution of cross-origin scripts inserted via document.write() under slow network conditions,// or when the page has already taken a long time to load.)- // 7. Insert the newly created element at the adjusted insertion location given adjustedInsertionLocation.- // 8. Push the element onto the stack of open elements so that it is the new current node.+ // 5. Insert an element at the adjusted insertion location with the newly created element.+ // 6. Push the newly created element onto the stack of open elements so that it is the new current node.+ // NB: Creation, configuration, insertion, and the stack push are performed by the insertion helper.self.insert_html_element_for(&token);- // 9. Switch the tokenizer to the script data state.+ // 7. Switch the tokenizer to the script data state.self.switch_tokenizer_to(State::ScriptData);- // 10. Set the original insertion mode to the current insertion mode.+ // 8. Set the original insertion mode to the current insertion mode.self.original_insertion_mode = self.insertion_mode;- // 11. Switch the insertion mode to "text".+ // 9. Switch the insertion mode to "text".self.insertion_mode = InsertionMode::Text;return;}@@ -1213,6 +1219,8 @@ impl TreeBuilder {// An end tag whose tag name is one of: "body", "html", "br"if token.is_end_tag_one_of(&[tag_name!("body"), tag_name!("html"), tag_name!("br")]) {+ // Act as described in the "anything else" entry below.+ // FIXME: Set the frameset-ok flag to "ok" after inserting the implicit body element.self.insert_html_element_named("body");self.insertion_mode = InsertionMode::InBody;self.process_using_the_rules_for(InsertionMode::InBody, token);@@ -1231,6 +1239,7 @@ impl TreeBuilder {{// Insert an HTML element for a "body" start tag token with no attributes.self.insert_html_element_named("body");+ // FIXME: Set the frameset-ok flag to "ok".// Switch the insertion mode to "in body".self.insertion_mode = InsertionMode::InBody;// Reprocess the current token.@@ -1459,6 +1468,8 @@ impl TreeBuilder {}if token.is_start_tag_named(tag_name!("form")) {+ // FIXME: Use "parsing template contents", which also includes a template fragment context element.+ // https://html.spec.whatwg.org/multipage/parsing.html#parsing-template-contentslet has_template_element_on_stack = self.has_template_element_on_stack_of_open_elements();if self.form_element.is_some() && !has_template_element_on_stack {// Parse error. Ignore the token.@@ -1476,6 +1487,8 @@ impl TreeBuilder {}if token.is_end_tag_named(tag_name!("form")) {+ // FIXME: The current rule branches on whether the parser is parsing template contents, including the+ // template fragment-context case, rather than only checking the stack of open elements.if !self.has_template_element_on_stack_of_open_elements() {let node = self.form_element.take();if let Some(node) = node {@@ -1603,7 +1616,8 @@ impl TreeBuilder {return;}- // A start tag whose tag name is "ruby"+ // FIXME: A "ruby" start tag belongs to the "any other start tag" rule: reconstruct the active formatting+ // elements, then insert an HTML element for the token. It must not close an open paragraph here.if token.is_start_tag_named(tag_name!("ruby")) {if self.has_in_button_scope(tag_name!("p")) {self.close_a_p_element();@@ -2068,8 +2082,8 @@ impl TreeBuilder {// Otherwise, pop the current node from the stack of open elements.self.pop_current_node();- // If the parser was not created as part of the HTML fragment parsing algorithm (fragment case), and the- // current node is no longer a frameset element, then switch the insertion mode to "after frameset".+ // If the parser's fragment context element is null and the current node is no longer a frameset element,+ // then switch the insertion mode to "after frameset".if !self.parsing_fragment && !self.current_node_named(tag_name!("frameset")) {self.insertion_mode = InsertionMode::AfterFrameset;}@@ -2223,6 +2237,9 @@ impl TreeBuilder {if token.is_start_tag_named(tag_name!("form")) {// Parse error.self.parse_error("form start tag in in table insertion mode");+ // FIXME: If the form element pointer is not null and the parser is not parsing template contents, ignore+ // the token. Otherwise, insert an HTML element for the token, and, if the parser is not parsing+ // template contents, set the form element pointer to point to the element created.if self.form_element.is_some() || self.has_template_element_on_stack_of_open_elements() {return;}@@ -2683,7 +2700,7 @@ impl TreeBuilder {fn handle_in_cell(&mut self, token: Token) {// -> An end tag whose tag name is one of: "td", "th"if token.is_end_tag_one_of(&[tag_name!("td"), tag_name!("th")]) {- // If the stack of open elements does not have an element in table scope that is an HTML element with th+ // If the stack of open elements does not have an element in table scope that is an HTML element with the// same tag name as that of the token, then this is a parse error; ignore the token.if !self.has_in_table_scope(token.tag_name()) {self.parse_error("cell end tag without matching element in table scope");@@ -2987,8 +3004,8 @@ impl TreeBuilder {// -> An end tag whose tag name is "html"if token.is_end_tag_named(tag_name!("html")) {- // If the parser was created as part of the HTML fragment parsing algorithm, this is a parse error;- // ignore the token. (fragment case)+ // If the parser's fragment context element is non-null, this is a parse error; ignore the token.+ // (fragment case)if self.parsing_fragment {// Parse error. Ignore the token.self.parse_error("html end tag in after body insertion mode for fragment");@@ -3002,7 +3019,7 @@ impl TreeBuilder {// -> An end-of-file tokenif token.token_type == TokenType::EndOfFile {- // Process the token using the rules for the "in body" insertion mode.+ // Stop parsing.self.stop_parsing();return;}@@ -3139,7 +3156,9 @@ impl TreeBuilder {// -> A start tag whose tag name is one of: "b", "big", "blockquote", "body", "br", "center", "code", "dd", "div",// "dl", "dt", "em", "embed", "h1", "h2", "h3", "h4", "h5", "h6", "head", "hr", "i", "img", "li", "listing",// "menu", "meta", "nobr", "ol", "p", "pre", "ruby", "s", "small", "span", "strong", "strike", "sub", "sup",- // "table", "tt", "u", "ul", "var".+ // "table", "tt", "u", "ul", "var"+ // -> A start tag whose tag name is "font", if the token has any attributes named "color", "face", or "size"+ // -> An end tag whose tag name is "br", "p"if is_foreign_content_breakout_token(&token) {// Parse error.self.parse_error("HTML breakout token in foreign content");@@ -3159,29 +3178,27 @@ impl TreeBuilder {return;}- // FIXME: -> A start tag whose tag name is "font", if the token has any attributes named "color", "face", or "size"- // FIXME: -> An end tag whose tag name is "br", "p"-// -> Any other start tagif token.is_start_tag() {- // FIXME: If the adjusted current node is an element in the MathML namespace, adjust MathML attributes for the token.+ // If the adjusted current node is an element in the MathML namespace, adjust MathML attributes for the token.// (This fixes the case of MathML attributes that are not all lowercase.)- // FIXME: If the adjusted current node is an element in the SVG namespace, and the token's tag name is one of+ // If the adjusted current node is an element in the SVG namespace, and the token's tag name is one of// the ones in the first column of the following table, change the tag name to the name given in the// corresponding cell in the second column. (This fixes the case of SVG elements that are not all lowercase.)- // FIXME: If the adjusted current node is an element in the SVG namespace, adjust SVG attributes for the token.+ // If the adjusted current node is an element in the SVG namespace, adjust SVG attributes for the token.// (This fixes the case of SVG attributes that are not all lowercase.)- // FIXME: Adjust foreign attributes for the token. (This fixes the use of namespaced attributes, in particular+ // Adjust foreign attributes for the token. (This fixes the use of namespaced attributes, in particular// XLink in SVG.)let namespace_ = self.adjusted_current_node().map(|node| node.namespace_).unwrap_or(RustFfiHtmlNamespace::Html);- // Insert a foreign element for the token, with the adjusted current node's namespace and false.+ // Insert a foreign element for the token, with the adjusted current node's namespace.+ // NB: The insertion helper applies the tag-name and attribute adjustments described above.let element = self.insert_foreign_element_for(&token, namespace_, false);// If the token has its self-closing flag set, then run the appropriate steps from the following list:@@ -3191,7 +3208,7 @@ impl TreeBuilder {// "script" end tag below.// -> Otherwise// Pop the current node off the stack of open elements and acknowledge the token's self-closing flag.- // FIXME: This is not equivalent to acting as described as steps for "script" end tag below.+ // NB: Self-closing and explicit SVG script end tags use the same pending-script helper.self.pop_current_node();if namespace_ == RustFfiHtmlNamespace::Svg && *token.tag_name() == tag_name!("script") {self.process_svg_script(element);@@ -3364,9 +3381,9 @@ impl TreeBuilder {// https://html.spec.whatwg.org/multipage/parsing.html#adjusted-current-nodefn adjusted_current_node(&self) -> Option<&StackNode> {- // The adjusted current node is the context element if the parser was created as part of the HTML fragment parsing- // algorithm and the stack of open elements has only one element in it (fragment case); otherwise, the adjusted- // current node is the current node.+ // The adjusted current node is the parser's fragment context element if that element is non-null and the stack+ // of open elements has only one element in it (fragment case); otherwise, the adjusted current node is the+ // current node.if self.parsing_fragment && self.stack_of_open_elements.len() == 1 {return self.context_element.as_ref();}@@ -3375,7 +3392,7 @@ impl TreeBuilder {// https://html.spec.whatwg.org/multipage/parsing.html#insert-an-html-elementfn insert_html_element_named(&mut self, name: &str) -> usize {- // To insert an HTML element given a token token: insert a foreign element given token, the HTML namespace, and false.+ // To insert an HTML element given a token token: insert a foreign element given token and the HTML namespace.self.insert_foreign_element_for(&Token::synthetic_start_tag(name), RustFfiHtmlNamespace::Html, false)}@@ -3407,12 +3424,13 @@ impl TreeBuilder {// https://html.spec.whatwg.org/multipage/parsing.html#insert-an-html-elementfn insert_html_element_for(&mut self, token: &Token) -> usize {- // To insert an HTML element given a token token: insert a foreign element given token, the HTML namespace, and false.+ // To insert an HTML element given a token token: insert a foreign element given token and the HTML namespace.self.insert_foreign_element_for(token, RustFfiHtmlNamespace::Html, false)}// https://html.spec.whatwg.org/multipage/parsing.html#insert-a-foreign-element- // To insert a foreign element, given a token token, a string namespace, and a boolean onlyAddToElementStack:+ // To insert a foreign element, given a token token and a string namespace:+ // NB: only_add_to_element_stack is an implementation parameter used to defer template insertion.fn insert_foreign_element_for(&mut self,token: &Token,@@ -3420,7 +3438,8 @@ impl TreeBuilder {only_add_to_element_stack: bool,) -> usize {self.flush_character_insertions();- // 1. Let the adjustedInsertionLocation be the appropriate place for inserting a node.+ // 1. Let element be the result of creating an element for the token given token, namespace, and the target+ // parent of the appropriate place for inserting a node.let adjusted_insertion_location = self.appropriate_place_for_inserting_node(None);let local_name = adjusted_foreign_tag_name(token.tag_name(), namespace_);let mut attributes = std::mem::take(&mut self.attribute_buffers);@@ -3438,8 +3457,6 @@ impl TreeBuilder {} else {None};- // 2. Let element be the result of creating an element for the token given token, namespace, and the element in- // which the adjustedInsertionLocation finds itself.let element = self.create_element(adjusted_insertion_location.parent,namespace_,@@ -3457,8 +3474,10 @@ impl TreeBuilder {}}self.recycle_attribute_buffers(attributes);- // 3. If onlyAddToElementStack is false, then run insert an element at the adjusted insertion location with- // element.+ // 2. Insert an element at the adjusted insertion location with element.+ // NOTE: The location is computed anew in that algorithm, since creating element can run script that changes+ // the DOM.+ // NB: Deferred template insertion skips this step using the implementation parameter.if !only_add_to_element_stack {self.insert_element_at_adjusted_insertion_location(element, None);}@@ -3467,7 +3486,7 @@ impl TreeBuilder {} else {None};- // 4. Push element onto the stack of open elements so that it is the new current node.+ // 3. Push element onto the stack of open elements so that it is the new current node.self.stack_of_open_elements.push(StackNode {handle: element,local_name: local_name.clone(),@@ -3476,7 +3495,7 @@ impl TreeBuilder {attributes: owned_attributes,template_content,});- // 5. Return element.+ // 4. Return element.element}@@ -3651,10 +3670,24 @@ impl TreeBuilder {element: usize,insertion_location: Option<AdjustedInsertionLocation>,) {- // 1. Set insertionLocation to the adjusted insertion location given insertionLocation.+ // 1. Let (targetParent, referenceChild) be the adjusted insertion location given overrideLocation.+ // NB: The implementation names overrideLocation insertion_location.let insertion_location = self.adjusted_insertion_location(insertion_location);- // NB: Remaining steps implemented on C++ side.+ // FIXME: 2. If any of the following are true:+ // - element's parent is non-null;+ // - element is a host-including inclusive ancestor of targetParent; or+ // - targetParent is a Document node that already has an element child,+ // then return.+ // FIXME: 3. If alreadySanitized is false and the parser's parser sanitizer configuration is not null:+ // FIXME: Implement the sanitizer action, attribute assertions, and insertion-target redirection for this step.+ // FIXME: 4. Assert: ensure pre-insert validity given element, targetParent, referenceChild, and « » does not throw.+ // 5. If the parser's fragment context element is null, then push a new element queue onto element's relevant+ // agent's custom element reactions stack.+ // 6. Insert element into targetParent before referenceChild.+ // 7. If the parser's fragment context element is null, then pop the element queue from element's relevant agent's+ // custom element reactions stack, and invoke custom element reactions in that queue.+ // NB: Steps 5 through 7 are implemented on the C++ side.let queue_custom_element_reactions = !self.parsing_fragment;unsafe {ladybird_html_parser_insert_node(@@ -3737,21 +3770,40 @@ impl TreeBuilder {// 5. Push "in template" onto the stack of template insertion modes so that it is the new current template insertion mode.self.stack_of_template_insertion_modes.push(InsertionMode::InTemplate);- // 6. Let the adjustedInsertionLocation be the appropriate place for inserting a node.+ // FIXME: 6. Let declarativeShadowHostElement be the current node.+ // 7. Let hostIsRoot be true if declarativeShadowHostElement is the topmost element in the stack of open elements;+ // otherwise false.+ // FIXME: 8. Let template be the result of creating an element for the token given templateStartTag, the HTML+ // namespace, and the target parent of the appropriate place for inserting a node.+ // 9. Let insertionLocation be the appropriate place for inserting a node.+ // FIXME: Create template before computing insertionLocation and inspecting its attributes.+ // NB: The implementation still selects a host and mode before the deferred template-creation helper below.let adjusted_insertion_location = self.appropriate_place_for_inserting_node(None);- // 7. If any of the following are false:- // - templateStartTag's shadowrootmode is not in the None state;+ // 10. Push template onto the stack of open elements so that it is the new current node.+ // NB: The deferred template-creation helper below performs this push.+ // 11. Let sanitizerAction be "Keep".+ // FIXME: 12. If the parser's parser sanitizer configuration is not null, then set sanitizerAction to the result of+ // getting the sanitizer action for an element name given template's local name, template's namespace,+ // and the parser's parser sanitizer configuration.+ // 13. Switch on sanitizerAction:+ // FIXME: -> "Remove"+ // -> "Replace with children"+ // Return.+ // -> "Keep"+ // If template's shadowrootmode attribute is not in the None state:+ // FIXME: Read the mode and flags from the created template, rather than from templateStartTag.let mode = match token.attribute(attribute_name!("shadowrootmode")) {Some(value) if value.eq_ignore_ascii_case("open") => Some(RustFfiHtmlShadowRootMode::Open),Some(value) if value.eq_ignore_ascii_case("closed") => Some(RustFfiHtmlShadowRootMode::Closed),_ => None,};- // - document's allow declarative shadow roots is true; or+ // NB: The mode and allow-declarative-shadow-roots checks are combined with the host check below.let parser_allows_declarative_shadow_roots = self.allow_declarative_shadow_roots;- // - the adjusted current node is not the topmost element in the stack of open elements,+ // FIXME: Use the current node for declarativeShadowHostElement (step 6), rather than the adjusted current node.+ // A top-level template in a fragment must not attach a shadow root to its fragment context element.let adjusted_current_node = self.adjusted_current_node().map(|node| node.handle);let adjusted_current_node_is_not_topmost_element = adjusted_current_node.is_some_and(|adjusted_current_node| {self.stack_of_open_elements@@ -3759,26 +3811,38 @@ impl TreeBuilder {.is_none_or(|topmost_element| topmost_element.handle != adjusted_current_node)});- // then insert an HTML element for the token.+ // If any of the following are true:+ // - the parser's allow declarative shadow roots is false;+ // - hostIsRoot is true; or+ // - the parser's insertion target redirection map[declarativeShadowHostElement] exists,+ // then insert an element at the adjusted insertion location with template, insertionLocation, and true, and return.+ // FIXME: Check the insertion target redirection map when sanitizer redirection is supported.+ // NB: The ordinary-template case is also handled by this combined fallback.if mode.is_none() || !parser_allows_declarative_shadow_roots || !adjusted_current_node_is_not_topmost_element {+ // FIXME: Otherwise, if template has a for attribute:+ // 1. Let scope be the target parent of the adjusted insertion location given insertionLocation.+ // 2. If scope is the body element of its node document, then set scope to its parent.+ // 3. Let success be the result of preparing content patching given scope and template.+ // 4. If success is false, then insert an element at the adjusted insertion location with template,+ // insertionLocation, and true.+ // NB: Templates currently fall back to ordinary insertion without content-patching support.self.insert_html_element_for(token);return;}- // 8. Otherwise:- // 1. Let declarativeShadowHostElement be adjusted current node.+ // NB: Continue with the host selected by the checks above.let declarative_shadow_host_element = adjusted_current_node.unwrap();- // 2. Let template be the result of insert a foreign element for templateStartTag, with HTML namespace and true.+ // NB: This helper creates template (step 8) and pushes it (step 10) without inserting it into the DOM.let template = self.insert_foreign_element_for(token, RustFfiHtmlNamespace::Html, true);- // 3. Let mode be templateStartTag's shadowrootmode attribute's value.+ // Let mode be template's shadowrootmode attribute's value.let mode = mode.unwrap();- // 4. Let slotAssignment be "named".+ // Let slotAssignment be "named".let mut slot_assignment = RustFfiHtmlSlotAssignmentMode::Named;- // 5. If templateStartTag's shadowrootslotassignment attribute is in the Manual state, then set slotAssignment to "manual".+ // If template's shadowrootslotassignment attribute is in the Manual state, then set slotAssignment to "manual".if token.attribute(attribute_name!("shadowrootslotassignment")).is_some_and(|value| value.eq_ignore_ascii_case("manual"))@@ -3786,25 +3850,29 @@ impl TreeBuilder {slot_assignment = RustFfiHtmlSlotAssignmentMode::Manual;}- // 6. Let clonable be true if templateStartTag has a shadowrootclonable attribute; otherwise false.+ // Let clonable be true if template has a shadowrootclonable attribute; otherwise false.let clonable = token.has_attribute(attribute_name!("shadowrootclonable"));- // 7. Let serializable be true if templateStartTag has a shadowrootserializable attribute; otherwise false.+ // Let serializable be true if template has a shadowrootserializable attribute; otherwise false.let serializable = token.has_attribute(attribute_name!("shadowrootserializable"));- // 8. Let delegatesFocus be true if templateStartTag has a shadowrootdelegatesfocus attribute; otherwise false.+ // Let delegatesFocus be true if template has a shadowrootdelegatesfocus attribute; otherwise false.+ // NB: The spec reads delegatesFocus, serializable, slotAssignment, then clonable; these independent reads are+ // currently grouped in the order of the host helper's initialization fields.let delegates_focus = token.has_attribute(attribute_name!("shadowrootdelegatesfocus"));- // 9. If declarativeShadowHostElement is a shadow host, then insert an element at the adjusted insertion location with template and adjustedInsertionLocation.+ // If declarativeShadowHostElement is a shadow host, then insert an element at the adjusted insertion location+ // with template, insertionLocation, and true.if self.is_shadow_host(declarative_shadow_host_element) {self.insert_element_at_adjusted_insertion_location(template, Some(adjusted_insertion_location));}- // 10. Otherwise:+ // Otherwise:else {- // 1. Let registry be null if templateStartTag has a shadowrootcustomelementregistry attribute;- // otherwise declarativeShadowHostElement's node document's custom element registry.+ // 1. Let registry be null if template has a shadowrootcustomelementregistry attribute; otherwise+ // declarativeShadowHostElement's node document's custom element registry.- // 2. Attach a shadow root with declarativeShadowHostElement, mode, clonable, serializable, delegatesFocus, slotAssignment, and registry.+ // 2. Attach a shadow root with declarativeShadowHostElement, mode, delegatesFocus, serializable,+ // slotAssignment, clonable, and registry.let shadow_root = self.attach_declarative_shadow_root(DeclarativeShadowRootInit {host: declarative_shadow_host_element,mode,@@ -3818,7 +3886,7 @@ impl TreeBuilder {// If an exception is thrown, then catch it and:if shadow_root == 0 {- // 1. Insert an element at the adjusted insertion location with template and adjustedInsertionLocation.+ // 1. Insert an element at the adjusted insertion location with template, insertionLocation, and true.self.insert_element_at_adjusted_insertion_location(template, Some(adjusted_insertion_location));// 2. The user agent may report an error to the developer console.@@ -3829,15 +3897,18 @@ impl TreeBuilder {// 3. Let shadow be declarativeShadowHostElement's shadow root.- // 4. Set shadow's declarative to true.+ // 4. Set shadow's available to element internals to true.- // 5. Set template's template contents to shadow.+ // 5. Set shadow's declarative to true.+ // NB: The C++ attachment helper performs steps 3 through 5.++ // 6. Set template's template contents to shadow.self.set_template_content(template, shadow_root);self.stack_of_open_elements.last_mut().unwrap().template_content = Some(shadow_root);- // 6. Set shadow's available to element internals to true.-- // 7. If templateStartTag has a shadowrootcustomelementregistry attribute, then set shadow's keep custom element registry null to true.+ // 7. If template has a shadowrootcustomelementregistry attribute, then set shadow's keep custom element+ // registry null to true.+ // NB: The C++ attachment helper also sets this flag, using the value supplied from the original token.}}@@ -3858,13 +3929,23 @@ impl TreeBuilder {self.parse_error("template end tag with different current node");}- // 3. Pop elements from the stack of open elements until a template element has been popped from the stack.+ // FIXME: 3. Let template be the last template element in the stack of open elements.+ // FIXME: 4. If template's insertion target is not null:+ // 1. Let start be template's insertion start marker.+ // 2. Let end be template's insertion end marker.+ // 3. Assert: start is not null.+ // 4. If start's parent is not null, then remove start.+ // 5. If end is not null and end's parent is not null, then remove end.+ // 6. Set template's insertion target to null.+ // 7. Set template's insertion start marker to null.+ // 8. Set template's insertion end marker to null.+ // 5. Pop elements from the stack of open elements until a template element has been popped from the stack.self.pop_until_tag_name_has_been_popped(tag_name!("template"));- // 4. Clear the list of active formatting elements up to the last marker.+ // 6. Clear the list of active formatting elements up to the last marker.self.clear_the_list_of_active_formatting_elements_up_to_the_last_marker();- // 5. Pop the current template insertion mode off the stack of template insertion modes.+ // 7. Pop the current template insertion mode off the stack of template insertion modes.self.stack_of_template_insertion_modes.pop();- // 6. Reset the insertion mode appropriately.+ // 8. Reset the insertion mode appropriately.self.reset_the_insertion_mode_appropriately();}@@ -3879,17 +3960,19 @@ impl TreeBuilder {// https://html.spec.whatwg.org/multipage/parsing.html#insert-a-commentfn insert_comment(&mut self, data: &str, insertion_location: Option<AdjustedInsertionLocation>) {- // 1. Let data be the data given in the comment token being processed.+ // FIXME: 1. If the parser's parser sanitizer configuration is non-null and the parser's parser sanitizer+ // configuration does not allow comments, then return.+ // 2. Let data be the data given in the comment token being processed.self.flush_character_insertions();- // 2. Set insertionLocation to the adjusted insertion location given insertionLocation.+ // 3. Let (targetParent, referenceChild) be the adjusted insertion location given insertionLocation.let insertion_location = self.adjusted_insertion_location(insertion_location);- // 3. Create a Comment node whose data attribute is set to data and whose node document is the same as that of- // the node in which the adjusted insertion location finds itself.+ // 4. Let comment be the result of creating a comment node given targetParent's node document and data.+ // NB: The host creates the comment in the parser document; insertion adopts it into targetParent's document.let comment = self.create_comment(data);- // 4. Insert the newly created node at insertionLocation.+ // 5. Insert comment into targetParent before referenceChild.self.insert_node_at_insertion_location(insertion_location, comment);}@@ -3966,7 +4049,8 @@ impl TreeBuilder {let node = self.stack_of_open_elements.pop();if let Some(node) = node.as_ref() {// https://html.spec.whatwg.org/multipage/parsing.html#stack-of-open-elements- // When the current node is removed from the stack of open elements, process internal resource links.+ // FIXME: When the current node is removed from the stack of open elements, process internal resource links+ // given the current node's node document.//// AD-HOC: Callers flush buffered text before popping so option selectedcontent cloning sees up-to-date text.self.handle_element_popped(node);@@ -4228,11 +4312,10 @@ impl TreeBuilder {}// https://html.spec.whatwg.org/multipage/parsing.html#appropriate-place-for-inserting-a-node- // The appropriate place for inserting a node, optionally using a particular override target, is the position in an- // element returned by running the following steps:+ // To find the appropriate place for inserting a node, given an optional Node overrideTarget (default null), perform+ // the following steps. They return an insertion location.fn appropriate_place_for_inserting_node(&self, override_target: Option<usize>) -> AdjustedInsertionLocation {- // 1. If there was an override target specified, then let target be the override target.- // Otherwise, let target be the current node.+ // 1. Let targetParent be overrideTarget if it is not null; otherwise the current node.let target = override_target.unwrap_or_else(|| self.current_node_handle());let Some(target_node) = self.stack_of_open_elements.iter().find(|node| node.handle == target) else {return AdjustedInsertionLocation {@@ -4241,10 +4324,11 @@ impl TreeBuilder {};};- // 2. Determine the adjusted insertion location using the first matching steps from the following list:+ // 2. Let referenceChild be null.+ // NB: A null referenceChild is represented by APPEND_CHILD_OFFSET; otherwise offset is the child's index.let mut adjusted_insertion_location;- // -> If foster parenting is enabled and target is a table, tbody, tfoot, thead, or tr element+ // 3. If foster parenting is enabled and targetParent is a table, tbody, tfoot, thead, or tr element:// NOTE: Foster parenting happens when content is misnested in tables.if self.foster_parenting_enabled&& target_node.namespace_ == RustFfiHtmlNamespace::Html@@ -4256,19 +4340,18 @@ impl TreeBuilder {tag_name!("tr"),]){- // 1. Let last template be the last template element in the stack of open elements, if any.+ // 1. Let lastTemplateOrTable be the last template or table element in the stack of open elements if there+ // is one; otherwise null.+ // NB: Separate template and table searches find which is last in the stack.let last_template_index = self.stack_of_open_elements.iter().rposition(|node| {node.local_name == tag_name!("template") && node.namespace_ == RustFfiHtmlNamespace::Html});- // 2. Let last table be the last table element in the stack of open elements, if any.let last_table_index = self.stack_of_open_elements.iter().rposition(|node| {node.local_name == tag_name!("table") && node.namespace_ == RustFfiHtmlNamespace::Html});- // 3. If there is a last template and either there is no last table, or there is one, but last template is- // lower (more recently added) than last table in the stack of open elements, then let adjusted insertion- // location be inside last template's template contents, after its last child (if any), and abort these steps.+ // 3. If lastTemplateOrTable is a template element, then set targetParent to lastTemplateOrTable.if let Some(template_index) = last_template_index&& last_table_index.is_none_or(|table_index| template_index > table_index){@@ -4279,9 +4362,9 @@ impl TreeBuilder {};} else {match last_table_index {- // 4. If there is no last table, then let adjusted insertion location be inside the first element in- // the stack of open elements (the html element), after its last child (if any), and abort these- // steps. (fragment case)+ // 2. If lastTemplateOrTable is null, then return (the first element in the stack of open elements+ // (the html element), null). (fragment case)+ // NB: The common return below produces this insertion location.None => {let parent = self.stack_of_open_elements.first().map(|node| node.handle).unwrap_or(0);adjusted_insertion_location = AdjustedInsertionLocation {@@ -4292,19 +4375,18 @@ impl TreeBuilder {Some(table_index) => {let table = self.stack_of_open_elements[table_index].handle;let parent = self.parent_node(table);- // 5. If last table has a parent node, then let adjusted insertion location be inside last table's- // parent node, immediately before last table, and abort these steps.+ // 4. Otherwise, if lastTemplateOrTable's parent is not null, then set targetParent to+ // lastTemplateOrTable's parent and set referenceChild to lastTemplateOrTable.if parent != 0 {adjusted_insertion_location = AdjustedInsertionLocation {parent,offset: self.node_index(table),};} else {- // 6. Let previous element be the element immediately above last table in the stack of open- // elements.+ // 5. Otherwise, set targetParent to the element immediately above lastTemplateOrTable in+ // the stack of open elements.let previous_element = &self.stack_of_open_elements[table_index - 1];- // 7. Let adjusted insertion location be inside previous element, after its last child (if any).adjusted_insertion_location = AdjustedInsertionLocation {parent: previous_element.handle,offset: APPEND_CHILD_OFFSET,@@ -4317,18 +4399,21 @@ impl TreeBuilder {// NOTE: These steps are involved in part because it's possible for elements, the table element in this case// in particular, to have been moved by a script around in the DOM, or indeed removed from the DOM entirely,// after the element was inserted by the parser.- }- // -> Otherwise- else {- // Let adjusted insertion location be inside target, after its last child (if any).+ } else {+ // NB: When foster parenting does not apply, keep targetParent and the null referenceChild.adjusted_insertion_location = AdjustedInsertionLocation {parent: target_node.handle,offset: APPEND_CHILD_OFFSET,};}- // 3. If the adjusted insertion location is inside a template element, let it instead be inside the template- // element's template contents, after its last child (if any).+ // 4. If targetParent is not a template element, then return (targetParent, referenceChild).+ // 5. Let patchInsertionTarget be targetParent's insertion target.+ // 6. If patchInsertionTarget is null, then return (targetParent's template contents, null).+ // FIXME: 7. If targetParent's insertion end marker is not null and its parent is patchInsertionTarget, then+ // return (patchInsertionTarget, targetParent's insertion end marker).+ // FIXME: 8. Return (patchInsertionTarget, null).+ // NB: Templates currently always use their template contents; content-patching insertion targets are not stored.if let Some(node) = self.stack_of_open_elements.iter()@@ -4339,7 +4424,6 @@ impl TreeBuilder {adjusted_insertion_location.offset = APPEND_CHILD_OFFSET;}- // 4. Return the adjusted insertion location.adjusted_insertion_location}@@ -4349,19 +4433,21 @@ impl TreeBuilder {&self,insertion_location: Option<AdjustedInsertionLocation>,) -> AdjustedInsertionLocation {- // 1. Let overrideTarget be null if insertionLocation is null; otherwise the node in which insertionLocation finds itself.+ // 1. Let location be insertionLocation if insertionLocation is non-null; otherwise the appropriate place for+ // inserting a node.+ // FIXME: Preserve a supplied insertion location, including its reference child, instead of recomputing it.let override_target = insertion_location.map(|insertion_location| insertion_location.parent);- // 2. Let the adjustedInsertionLocation be the appropriate place for inserting a node given overrideTarget.+ // 2. Let (targetParent, refChild) be location.+ // NB: The implementation currently recomputes the location using only the supplied parent.let adjusted_insertion_location = self.appropriate_place_for_inserting_node(override_target);- // 3. If the node in which the adjustedInsertionLocation finds itself is the first element in the stack of open- // elements and the parser's root insertion target is non-null, then set the adjustedInsertionLocation to the- // parser's root insertion target, after its last child (if any).- let adjusted_insertion_location = self.apply_root_insertion_target(adjusted_insertion_location);-- // 4. Return the adjustedInsertionLocation.- adjusted_insertion_location+ // 3. If the parser's insertion target redirection map[targetParent] does not exist, then return location.+ // 4. Assert: refChild is null.+ // 5. Return the parser's insertion target redirection map[targetParent].+ // FIXME: Replace the root-only redirection with the insertion target redirection map, which also handles+ // elements replaced with children during sanitization.+ self.apply_root_insertion_target(adjusted_insertion_location)}fn apply_root_insertion_target(@@ -4403,7 +4489,7 @@ impl TreeBuilder {// 1. If there are already three elements in the list of active formatting elements after the last marker, if any, or anywhere in the list if there are no markers,// that have the same tag name, namespace, and attributes as element, then remove the earliest such element from the list of active formatting elements.- // For these purposes, the attributes must be compared as they were when the elements were created by the parser; two elements have the same attributes if all their parsed attributes+ // For these purposes, the attributes must be compared as they were on the tokens for which the elements were created; two elements have the same attributes if all their parsed attributes// can be paired such that the two attributes in each pair have identical names, namespaces, and values (the order of the attributes does not matter).let first_index_after_last_marker = self.active_formatting_elements_after_last_marker_start_index();let matching_entries = self.list_of_active_formatting_elements[first_index_after_last_marker..]@@ -4604,10 +4690,20 @@ impl TreeBuilder {let mut node_index = furthest_block_index;let mut last_node = furthest_block.handle;- // 12. Let innerLoopCounter be 0.+ // FIXME: 12. Let pendingRedirects be an empty list.+ // FIXME: 13. If the parser's parser sanitizer configuration is not null:+ // 1. Let action be the result of getting the sanitizer action for an element name given+ // lastNode's local name, lastNode's namespace, and the parser's parser sanitizer configuration.+ // 2. If action is not "Keep", then set lastNode to null.+ // 3. If action is "Replace with children", then append furthestBlock to pendingRedirects.++ // 14. Let innerLoopCounter be 0.let mut inner_loop_counter: u8 = 0;- // 13. While true:+ // 15. Let bookmarkMoved be false.+ // NB: Without sanitization, last_node == furthest_block.handle tracks whether the bookmark has moved.++ // 16. While true:loop {// 1. Increment innerLoopCounter by 1.inner_loop_counter += 1;@@ -4665,30 +4761,48 @@ impl TreeBuilder {template_content: None,};- // 7. If lastNode is furthestBlock,+ // 7. If bookmarkMoved is false, then move the aforementioned bookmark to be immediately after the new+ // node in the list of active formatting elements, and set bookmarkMoved to true.if last_node == furthest_block.handle {- // then move the aforementioned bookmark to be immediately after the new node in the list of active formatting elements.bookmark = active_index + 1;}- // 8. Append lastNode to node.+ // 8. Let nodeAction be "Keep".+ // FIXME: 9. If the parser's parser sanitizer configuration is not null, then set nodeAction to the result+ // of getting the sanitizer action for an element name given node's local name, node's namespace,+ // and the parser's parser sanitizer configuration.+ // FIXME: 10. If nodeAction is not "Replace with children":+ // 1. For each replacedElement of pendingRedirects: set the parser's insertion target+ // redirection map[replacedElement] to (node, null).+ // 2. Empty pendingRedirects.+ // 11. Switch on nodeAction:+ // -> "Keep"+ // If lastNode is non-null, then append lastNode to node. Set lastNode to node.+ // NB: The unsanitized path keeps lastNode non-null and always takes the "Keep" branch.self.append_child(new_element, last_node);-- // 9. Set lastNode to node.last_node = new_element;+ // FIXME: -> "Remove"+ // If lastNode is non-null and lastNode's parent is non-null, then remove lastNode. Set lastNode+ // to null.+ // FIXME: -> "Replace with children"+ // Append node to pendingRedirects.}- // 14. Let (target, refNode) be the adjusted insertion location given (commonAncestor, null).+ // 17. Let insertionLocation be the appropriate place for inserting a node given commonAncestor.+ // 18. Let (target, refNode) be the adjusted insertion location given insertionLocation.+ // NB: The adjusted-insertion-location helper currently performs the appropriate-place computation too.let adjusted_insertion_location = self.adjusted_insertion_location(Some(AdjustedInsertionLocation {parent: common_ancestor,offset: APPEND_CHILD_OFFSET,}));- // NB: Steps 15 and 16 implemented on C++ side.+ // FIXME: 19. For each replacedElement of pendingRedirects: set the parser's insertion target redirection+ // map[replacedElement] to (target, refNode).+ // NB: Steps 20 and 21 implemented on C++ side.self.reinsert_last_node_at_insertion_location(adjusted_insertion_location, last_node);- // 17. Create an element for the token for which formattingElement was created,- // in the HTML namespace, with furthestBlock as the intended parent.+ // 22. Let newElement be the result of creating an element for the token for which formattingElement was+ // created, in the HTML namespace, with furthestBlock as the intended parent.let Some(formatting_element_index) = self.list_of_active_formatting_elements.iter()@@ -4699,14 +4813,29 @@ impl TreeBuilder {let entry = self.list_of_active_formatting_elements[formatting_element_index].clone();let new_element = self.create_html_element_for_active_formatting_element(&entry, furthest_block.handle);- // 18. Take all of the child nodes of furthestBlock and append them to the element created in the last step.+ // FIXME: Perform steps 23 and 24 before the child mutations in step 27.+ // 25. Let sanitizerAction be "Keep".+ // FIXME: 26. If the parser's parser sanitizer configuration is not null, then set sanitizerAction to the result+ // of getting the sanitizer action for an element name given newElement's local name,+ // newElement's namespace, and the parser's parser sanitizer configuration.+ // 27. Switch on sanitizerAction:+ // FIXME: -> "Replace with children"+ // Set the parser's insertion target redirection map[newElement] to the adjusted insertion location+ // given (furthestBlock, null).+ // FIXME: -> "Remove"+ // Take all of the child nodes of furthestBlock and append them to newElement.+ // -> "Keep"+ // 1. Take all of the child nodes of furthestBlock and append them to newElement.self.move_all_children(furthest_block.handle, new_element);- // 19. Append that new element to furthestBlock.+ // FIXME: 2. Let (newElementParent, newElementRefNode) be the adjusted insertion location given+ // (furthestBlock, null).+ // 3. Insert newElement into newElementParent before newElementRefNode.+ // NB: The current unsanitized path appends directly to furthestBlock.self.append_child(furthest_block.handle, new_element);- // 20. Remove formattingElement from the list of active formatting elements,- // and insert the new element into the list of active formatting elements at the position of the aforementioned bookmark.+ // 23. Remove formattingElement from the list of active formatting elements, and insert newElement into the+ // list of active formatting elements at the position of the aforementioned bookmark.if formatting_element_index < bookmark {bookmark -= 1;}@@ -4722,8 +4851,8 @@ impl TreeBuilder {},);- // 21. Remove formattingElement from the stack of open elements, and insert the new element- // into the stack of open elements immediately below the position of furthestBlock in that stack.+ // 24. Remove formattingElement from the stack of open elements, and insert newElement into the stack of+ // open elements immediately below the position of furthestBlock in that stack.if let Some(formatting_element_stack_index) = self.stack_of_open_elements.iter()@@ -4749,6 +4878,13 @@ impl TreeBuilder {template_content: None,},);+ // FIXME: 28. Let belowNode be the element immediately below newElement in the stack of open elements, if any;+ // otherwise null.+ // FIXME: 29. While belowNode is non-null and the parser's insertion target redirection map[belowNode] exists:+ // 1. Set the parser's insertion target redirection map[belowNode] to the adjusted insertion location+ // given (newElement, null).+ // 2. Set belowNode to the element immediately below belowNode in the stack of open elements, if any;+ // otherwise null.}}diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/preload_scanner.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/preload_scanner.rsindex 578306c298e..adf3f998ff9 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/preload_scanner.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/preload_scanner.rs@@ -318,17 +318,19 @@ fn script_type_from_attributes(attributes: &[Attribute]) -> Option<ScriptType> {// - el has no type attribute but it has a language attribute and that attribute's value is the empty string; or// - el has neither a type attribute nor a language attribute,// then let the script block's type string for this script element be "text/javascript".- // Otherwise, if el has a type attribute, then let the script block's type string be the value of that attribute- // with leading and trailing ASCII whitespace stripped.+ // Otherwise, if el has a type attribute, then let the script block's type string be the value of that attribute.// Otherwise, el has a non-empty language attribute; let the script block's type string be the concatenation of// "text/" and the value of el's language attribute.+ // FIXME: Preserve whitespace in the type string; only the classic JavaScript MIME comparison strips it.let type_string: Cow<'_, [u8]> = match (type_attribute, language_attribute) {(Some(""), _) | (None, Some("")) | (None, None) => Cow::Borrowed(b"text/javascript"),(Some(type_attribute), _) => Cow::Borrowed(type_attribute.as_bytes().trim_ascii()),(None, Some(language)) => Cow::Owned([b"text/".as_slice(), language.as_bytes()].concat()),};- // 9. If the script block's type string is a JavaScript MIME type essence match, then set el's type to "classic".+ // 9. If the script block's type string is a JavaScript MIME type essence match, or el has a type attribute whose+ // value with leading and trailing ASCII whitespace stripped is a JavaScript MIME type essence match, then set+ // el's type to "classic".if JAVASCRIPT_MIME_TYPE_ESSENCES.iter().any(|essence| type_string.eq_ignore_ascii_case(essence))diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rsindex 8d6f0e6ad6e..ea8218dd94e 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs@@ -270,7 +270,8 @@ fn is_whitespace(c: u32) -> bool {matches!(c, 0x09 | 0x0A | 0x0C | 0x20)}-/// Numeric character reference replacement table (spec section 13.2.5.5).+/// Numeric character reference replacement table.+/// https://html.spec.whatwg.org/multipage/parsing.html#table-charref-overridesfn numeric_char_ref_replacement(code: u32) -> Option<u32> {match code {0x80 => Some(0x20AC),@@ -1114,7 +1115,8 @@ impl HtmlTokenizer {continue;}Some(0x3F) => {- // '?' - parse error+ // FIXME: Set the temporary buffer to the empty string. Switch to the processing instruction open state.+ // NB: Until processing instructions are supported, retain the bogus-comment fallback.self.create_new_token(TokenType::Comment);self.current_token.start_position = self.nth_last_position(2);self.reconsume(State::BogusComment);@@ -2602,7 +2604,8 @@ impl HtmlTokenizer {}},- // 13.2.5.72 Character reference state+ // https://html.spec.whatwg.org/multipage/parsing.html#character-reference-state+ // Character reference stateState::CharacterReference => {self.temporary_buffer.clear();self.temporary_buffer.push(0x26); // '&'@@ -2629,7 +2632,8 @@ impl HtmlTokenizer {}}- // 13.2.5.73 Named character reference state+ // https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state+ // Named character reference stateState::NamedCharacterReference => {// Insertion-point path: feed one character at a time.if self.stop_at_insertion_point && self.insertion_point.is_some() {@@ -2712,7 +2716,8 @@ impl HtmlTokenizer {}}- // 13.2.5.74 Ambiguous ampersand state+ // https://html.spec.whatwg.org/multipage/parsing.html#ambiguous-ampersand-state+ // Ambiguous ampersand stateState::AmbiguousAmpersand => match current_input_character {Some(cp) if is_ascii_alphanumeric(cp) => {if self.consumed_as_part_of_an_attribute() {@@ -2739,7 +2744,8 @@ impl HtmlTokenizer {}},- // 13.2.5.75 Numeric character reference state+ // https://html.spec.whatwg.org/multipage/parsing.html#numeric-character-reference-state+ // Numeric character reference stateState::NumericCharacterReference => {self.character_reference_code = 0;match current_input_character {@@ -2750,13 +2756,15 @@ impl HtmlTokenizer {continue;}_ => {+ // NB: DecimalCharacterReferenceStart implements the ASCII digit and anything-else cases of this state.self.reconsume(State::DecimalCharacterReferenceStart);continue;}}}- // 13.2.5.76 Hexadecimal character reference start state+ // https://html.spec.whatwg.org/multipage/parsing.html#hexadecimal-character-reference-start-state+ // Hexadecimal character reference start stateState::HexadecimalCharacterReferenceStart => match current_input_character {Some(cp) if is_ascii_hex_digit(cp) => {self.reconsume(State::HexadecimalCharacterReference);@@ -2773,14 +2781,16 @@ impl HtmlTokenizer {}},- // 13.2.5.77 Decimal character reference start state+ // NB: This implementation-only state handles the non-X/x cases of the numeric character reference state.State::DecimalCharacterReferenceStart => match current_input_character {Some(cp) if is_ascii_digit(cp) => {+ // Reconsume in the decimal character reference state.self.reconsume(State::DecimalCharacterReference);continue;}_ => {- // parse error+ // This is an absence-of-digits-in-numeric-character-reference parse error.+ // Flush code points consumed as a character reference. Reconsume in the return state.self.flush_codepoints_consumed_as_character_reference();self.reconsume_in_return_state(current_input_character);if !self.queued_tokens.is_empty() {@@ -2790,7 +2800,8 @@ impl HtmlTokenizer {}},- // 13.2.5.78 Hexadecimal character reference state+ // https://html.spec.whatwg.org/multipage/parsing.html#hexadecimal-character-reference-state+ // Hexadecimal character reference stateState::HexadecimalCharacterReference => match current_input_character {Some(cp) if is_ascii_digit(cp) => {self.character_reference_code =@@ -2818,7 +2829,8 @@ impl HtmlTokenizer {}},- // 13.2.5.79 Decimal character reference state+ // https://html.spec.whatwg.org/multipage/parsing.html#decimal-character-reference-state+ // Decimal character reference stateState::DecimalCharacterReference => match current_input_character {Some(cp) if is_ascii_digit(cp) => {self.character_reference_code =@@ -2836,7 +2848,8 @@ impl HtmlTokenizer {}},- // 13.2.5.80 Numeric character reference end state+ // https://html.spec.whatwg.org/multipage/parsing.html#numeric-character-reference-end-state+ // Numeric character reference end stateState::NumericCharacterReferenceEnd => {// Don't consumeif current_input_character.is_some() {