Skip to content

Commit

Permalink
Adoption and DocumentFragment
Browse files Browse the repository at this point in the history
Tests for whatwg/dom#744.

The assumption here is an early return for DocumentFragment that has a host by adoptNode() and that adoption generally doesn't affect DocumentFragment otherwise (only its children).
  • Loading branch information
annevk committed Dec 7, 2019
1 parent 091fc52 commit 1b02a0b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
32 changes: 32 additions & 0 deletions custom-elements/adopted-callback.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,38 @@
});
}, 'Moving the shadow host of a custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback');

promise_test(function () {
return getDocument().then(function (doc) {
var instance = document.createElement('my-custom-element');
var host = document.createElement('div');
var shadowRoot = host.attachShadow({mode: 'closed'});
shadowRoot.appendChild(instance);
document.body.appendChild(host);

calls = [];
doc.documentElement.appendChild(shadowRoot);
assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']);
});
}, 'Moving the shadow host\'s shadow of a custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback');

promise_test(function () {
return getDocument().then(function (doc) {
var instance = document.createElement('my-custom-element');
var host = document.createElement('template');
var shadowRoot = host.content;
shadowRoot.appendChild(instance);
document.body.appendChild(host);

calls = [];
doc.documentElement.appendChild(shadowRoot);
if (documentName === "the document of the template elements") {
assert_array_equals(calls, ['connected']);
} else {
assert_array_equals(calls, ['adopted', shadowRoot.ownerDocument, doc, 'connected']);
}
});
}, 'Moving the <template>\'s content of a custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback');

promise_test(function () {
return getDocument().then(function (doc) {
var instance = document.createElement('my-custom-element');
Expand Down
58 changes: 58 additions & 0 deletions dom/nodes/adoption.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Testing DocumentFragment with host separately as it has a different node document by design
test(() => {
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);
assert_equals(df.childNodes.length, 0);
assert_equals(child.ownerDocument, document);
assert_equals(df.ownerDocument, nodeDocument);
}, `appendChild() and DocumentFragment with host`);

test(() => {
const df = document.createElement("template").content;
const child = df.appendChild(new Text('hi'));
const nodeDocument = df.ownerDocument;
document.adoptNode(df);
assert_equals(df.childNodes.length, 1);
assert_equals(child.ownerDocument, nodeDocument);
assert_equals(df.ownerDocument, nodeDocument);
}, `adoptNode() and DocumentFragment with host`);

[
{
"name": "DocumentFragment",
"creator": doc => doc.createDocumentFragment()
},
{
"name": "ShadowRoot",
"creator": doc => doc.createElementNS("http://www.w3.org/1999/xhtml", "div").attachShadow({mode: "closed"})
}
].forEach(dfTest => {
test(() => {
const doc = new Document();
const df = dfTest.creator(doc);
const child = df.appendChild(new Text('hi'));
assert_equals(df.ownerDocument, doc);

document.body.appendChild(df);
assert_equals(df.childNodes.length, 0);
assert_equals(child.ownerDocument, document);
assert_equals(df.ownerDocument, doc);
}, `appendChild() and ${dfTest.name}`);

test(() => {
const doc = new Document();
const df = dfTest.creator(doc);
const child = df.appendChild(new Text('hi'));
if (dfTest.name === "ShadowRoot") {
assert_throws("HierarchyRequestError", () => document.adoptNode(df));
} else {
document.adoptNode(df);
assert_equals(df.childNodes.length, 1);
assert_equals(child.ownerDocument, document);
assert_equals(df.ownerDocument, document);
}
}, `adoptNode() and ${dfTest.name}`);
});

0 comments on commit 1b02a0b

Please sign in to comment.