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. :^)
This commit is contained in:
Andreas Kling 2025-04-25 09:36:57 +02:00 committed by Andreas Kling
parent 1102a31866
commit 27efb3b140
Notes: github-actions[bot] 2025-04-25 09:02:11 +00:00
2 changed files with 234 additions and 231 deletions

View file

@ -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;
}