From 4f8967addc392d965e5ce2acda176bdee45e0594 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Sun, 30 Aug 2026 16:52:00 +0800 Subject: [PATCH] fix(html): share template contents owner documents --- moli-dom/src/native/host/clone.rs | 62 +++++++++++++++++++ .../tests/dom_xhr/dom/detached_documents.rs | 33 ++++++++++ 2 files changed, 95 insertions(+) diff --git a/moli-dom/src/native/host/clone.rs b/moli-dom/src/native/host/clone.rs index 5157a6c97d6..cb6ee68756e 100644 --- a/moli-dom/src/native/host/clone.rs +++ b/moli-dom/src/native/host/clone.rs @@ -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(); diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/detached_documents.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/detached_documents.rs index cae8cea40a1..683267a5759 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/detached_documents.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/detached_documents.rs @@ -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"); +}