diff --git a/custom-elements/adopted-callback.html b/custom-elements/adopted-callback.html
index 1ed4634dba9ff8..8a10f2f1e6accd 100644
--- a/custom-elements/adopted-callback.html
+++ b/custom-elements/adopted-callback.html
@@ -127,7 +127,7 @@
calls = [];
doc.documentElement.appendChild(shadowRoot);
- assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']);
+ assert_array_equals(calls, ['adopted', document, doc, 'disconnected', 'connected']);
});
}, 'Moving the shadow host\'s shadow of a custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback');
diff --git a/dom/nodes/adoption.window.js b/dom/nodes/adoption.window.js
index ad90aaf375a521..fffe254368b82c 100644
--- a/dom/nodes/adoption.window.js
+++ b/dom/nodes/adoption.window.js
@@ -1,10 +1,15 @@
// Testing DocumentFragment with host separately as it has a different node document by design
-test(() => {
+function create(doc, localName = "div") {
+ return doc.createElementNS("http://www.w3.org/1999/xhtml", localName);
+}
+
+test(t => {
const df = document.createElement("template").content;
const child = df.appendChild(new Text('hi'));
assert_not_equals(df.ownerDocument, document);
const nodeDocument = df.ownerDocument;
document.body.appendChild(df);
+ t.add_cleanup(() => child.remove());
assert_equals(df.childNodes.length, 0);
assert_equals(child.ownerDocument, document);
assert_equals(df.ownerDocument, nodeDocument);
@@ -27,26 +32,30 @@ test(() => {
},
{
"name": "ShadowRoot",
- "creator": doc => doc.createElementNS("http://www.w3.org/1999/xhtml", "div").attachShadow({mode: "closed"})
+ "creator": doc => create(doc).attachShadow({mode: "closed"})
}
-].forEach(dfTest => {
- test(() => {
+].forEach(({ name, creator }) => {
+ test(t => {
const doc = new Document();
- const df = dfTest.creator(doc);
+ const df = creator(doc);
const child = df.appendChild(new Text('hi'));
assert_equals(df.ownerDocument, doc);
-
document.body.appendChild(df);
+ t.add_cleanup(() => child.remove());
assert_equals(df.childNodes.length, 0);
assert_equals(child.ownerDocument, document);
- assert_equals(df.ownerDocument, doc);
- }, `appendChild() and ${dfTest.name}`);
+ if (name === "ShadowRoot") {
+ assert_equals(df.ownerDocument, doc);
+ } else {
+ assert_equals(df.ownerDocument, document);
+ }
+ }, `appendChild() and ${name}`);
test(() => {
const doc = new Document();
- const df = dfTest.creator(doc);
+ const df = creator(doc);
const child = df.appendChild(new Text('hi'));
- if (dfTest.name === "ShadowRoot") {
+ if (name === "ShadowRoot") {
assert_throws_dom("HierarchyRequestError", () => document.adoptNode(df));
} else {
document.adoptNode(df);
@@ -54,5 +63,20 @@ test(() => {
assert_equals(child.ownerDocument, document);
assert_equals(df.ownerDocument, document);
}
- }, `adoptNode() and ${dfTest.name}`);
+ }, `adoptNode() and ${name}`);
});
+
+test(t => {
+ const doc = new Document();
+ const host = create(doc);
+ const shadow = host.attachShadow({mode: "closed"});
+ const childHost = shadow.appendChild(create(doc, "body"));
+ const childShadow = childHost.attachShadow({mode: "closed"});
+ const descendant = childShadow.appendChild(new Text("hi"));
+ document.body.appendChild(shadow);
+ t.add_cleanup(() => childHost.remove());
+ assert_equals(shadow.childNodes.length, 0);
+ assert_equals(childHost.ownerDocument, document);
+ assert_equals(childShadow.ownerDocument, document);
+ assert_equals(descendant.ownerDocument, document);
+}, "Nested ShadowRoots");