From 27efb3b140b6557f41b7ac2e613bdcac36354fc7 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 25 Apr 2025 09:36:57 +0200 Subject: [PATCH] LibWeb: When declarative shadow attachment fails, continue in right spot If attachment fails for whatever reason (e.g the host element is not allowed to be a host), the HTML spec tells us to insert the template element anyway and proceed. Before this change, we were recomputing the insertion location at this point, which caused it to be *inside* the template element. Inserting the template element into itself didn't work, and so the DOM would end up incorrect. The fix here is to simply use the insertion point we determined earlier in the same function, before putting a template element on the stack of open elements. We already do this elsewhere. Fixes at least 228 subtests on WPT. :^) --- Libraries/LibWeb/HTML/Parser/HTMLParser.cpp | 5 +- .../declarative-shadow-dom-attachment.txt | 460 +++++++++--------- 2 files changed, 234 insertions(+), 231 deletions(-) diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp index cca09a2a3e2..144061d5856 100644 --- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp +++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp @@ -1114,7 +1114,10 @@ void HTMLParser::handle_in_head(HTMLToken& token) auto result = declarative_shadow_host_element.attach_a_shadow_root(mode, clonable, serializable, delegates_focus, Bindings::SlotAssignmentMode::Named); if (result.is_error()) { report_exception(Bindings::exception_to_throw_completion(vm(), result.release_error()), realm()); - insert_an_element_at_the_adjusted_insertion_location(template_); + // FIXME: We do manual "insert before" instead of "insert an element at the adjusted insertion location" here + // Otherwise, the new insertion location will be inside the template's contents, which is not what we want here. + // This might be a spec bug(?) + adjusted_insertion_location.parent->insert_before(*template_, adjusted_insertion_location.insert_before_sibling); return; } diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/declarative/declarative-shadow-dom-attachment.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/declarative/declarative-shadow-dom-attachment.txt index 45af316ab1d..b4bee671987 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/declarative/declarative-shadow-dom-attachment.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/declarative/declarative-shadow-dom-attachment.txt @@ -2,8 +2,8 @@ Harness status: OK Found 648 tests -324 Pass -324 Fail +552 Pass +96 Fail Pass Declarative Shadow DOM as a child of
, with mode=open, delegatesFocus=false. Should be safelisted. Pass Declarative Shadow DOM as a child of