fix(html): create wrappers for empty html innerHTML

This commit is contained in:
ldm0
2026-09-02 05:23:15 +08:00
committed by Donough Liu
parent e1788dd216
commit 0a90591d7c
2 changed files with 29 additions and 1 deletions
@@ -763,7 +763,14 @@ impl DocumentRuntime {
.node(target)
.map(|node| node.child_ids(self.dom_host().dom()).collect::<Vec<_>>())
.unwrap_or_default();
if html.is_empty() && existing_children.is_empty() {
let empty_html_context_creates_document_wrappers = self
.dom_host()
.node(context)
.is_some_and(|node| node.is_html_element_named("html"));
if html.is_empty()
&& existing_children.is_empty()
&& !empty_html_context_creates_document_wrappers
{
return false;
}
let Some(fragment) = self.build_fragment_from_html(
@@ -7663,6 +7663,27 @@ fn document_element_inner_html_fragment_preserves_html_context_wrappers() {
assert_eq!(result, "HEAD|BODY");
}
#[test]
fn empty_detached_html_inner_html_creates_head_and_body() {
let mut vm = new_storage_test_vm("https://example.com/");
let result = vm
.eval(
r#"
(() => {
const html = document.createElement("html");
html.innerHTML = "";
return Array.from(html.childNodes)
.map(node => node.nodeName)
.join("|");
})()
"#,
)
.expect("empty detached HTML innerHTML probe should evaluate");
assert_eq!(result, "HEAD|BODY");
}
#[test]
fn child_window_text_replacement_setters_dispatch_disconnected_callbacks() {
let mut vm = new_storage_test_vm("https://example.com/");