fix(html): share template contents owner documents

This commit is contained in:
ldm0
2026-11-19 00:13:42 +08:00
parent 9f0ab44a65
commit 4f8967addc
2 changed files with 95 additions and 0 deletions
+62
View File
@@ -833,6 +833,68 @@ mod tests {
assert_eq!(element_chain_depth(&host, root), DEEP_TREE_DEPTH);
}
#[test]
fn template_adoption_retargets_nested_contents_to_the_shared_inert_document() {
let mut host = test_host();
let target_document = host.create_detached_html_document();
let template = host.create_element("template");
let contents = host
.node(template)
.and_then(Node::as_element)
.and_then(Element::template_contents)
.expect("template contents");
let source_contents_document = host
.node(contents)
.and_then(Node::owner_document)
.expect("source contents document");
let nested_template = host.create_element("template");
let nested_contents = host
.node(nested_template)
.and_then(Node::as_element)
.and_then(Element::template_contents)
.expect("nested template contents");
assert!(host.append_child(contents, nested_template));
assert_eq!(
host.node(nested_template).and_then(Node::owner_document),
Some(source_contents_document)
);
assert_eq!(
host.node(nested_contents).and_then(Node::owner_document),
Some(source_contents_document)
);
assert_eq!(host.adopt_node(target_document, template), Some(template));
let target_contents_document = host
.node(contents)
.and_then(Node::owner_document)
.expect("target contents document");
assert_ne!(target_contents_document, source_contents_document);
assert_ne!(target_contents_document, target_document);
assert_eq!(
host.node(nested_template).and_then(Node::owner_document),
Some(target_contents_document)
);
assert_eq!(
host.node(nested_contents).and_then(Node::owner_document),
Some(target_contents_document)
);
let sibling_template = host.create_element("template");
assert_eq!(
host.adopt_node(target_document, sibling_template),
Some(sibling_template)
);
let sibling_contents_document = host
.node(sibling_template)
.and_then(Node::as_element)
.and_then(Element::template_contents)
.and_then(|contents| host.node(contents))
.and_then(Node::owner_document)
.expect("sibling contents document");
assert_eq!(sibling_contents_document, target_contents_document);
}
#[test]
fn deep_normalize_walks_iteratively() {
let mut host = test_host();
@@ -1310,3 +1310,36 @@ fn range_insert_node_rejects_ancestor_without_splitting_text() {
assert_eq!(result, "HierarchyRequestError|3|true|abc|1|true|0");
}
#[test]
fn detached_templates_share_their_inert_owner_document() {
let mut vm = new_storage_test_vm("https://detached-template-shared-owner.test/");
let result = vm
.eval(
r#"
(() => {
const doc = document.implementation.createHTMLDocument('');
const first = doc.createElement('template');
const second = doc.createElement('template');
const owner = first.content.ownerDocument;
const nested = owner.createElement('template');
first.content.append(nested);
return [
owner !== doc,
owner.defaultView === null,
Object.prototype.toString.call(owner) === '[object Document]',
owner instanceof Document,
!(owner instanceof HTMLDocument),
second.content.ownerDocument === owner,
nested.ownerDocument === owner,
nested.content.ownerDocument === owner,
nested.content.ownerDocument.defaultView === null
].join('|');
})()
"#,
)
.expect("detached templates should share one inert owner document");
assert_eq!(result, "true|true|true|true|true|true|true|true|true");
}