Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adoption and DocumentFragment #16348

Merged
merged 3 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name shadowRoot is very confusing. It should be documentFragment or something.

shadowRoot.appendChild(instance);
document.body.appendChild(host);

calls = [];
doc.documentElement.appendChild(shadowRoot);
if (documentName === "the document of the template elements") {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rniwa this seems a little ugly btw, but I wasn't sure how to avoid it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about doc === host.content.ownerDocument ?

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}`);
});
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<meta name="author" title="Takayoshi Kochi" href="mailto:kochi@chromium.org">
<meta name="assert" title="host-including inclusive ancestor should be checked for template content">
<link rel="help" href="https://dom.spec.whatwg.org/#concept-tree-host-including-inclusive-ancestor">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<div id=log></div>
<div id="parent">
<template id="tmpl"><span>Happy Templating!</span></template>
</div>
</body>
<script>
test(() => {
var parent = document.getElementById('parent');
Expand Down Expand Up @@ -53,12 +45,11 @@
assert_not_equals(new_doc, tmpl_doc);

// Try moving tmpl.content to new_doc and check the results.
var new_node = new_doc.adoptNode(tmpl.content);
assert_equals(new_node.ownerDocument, new_doc);
const tmplContentNodeDocument = tmpl.content.ownerDocument;
const tmplContentAdoptResult = new_doc.adoptNode(tmpl.content);
assert_equals(tmpl.content, tmplContentAdoptResult);
assert_equals(tmpl.ownerDocument, document);
assert_equals(tmpl.content.ownerDocument, new_doc);
assert_not_equals(tmpl.content.ownerDocument, tmpl_doc);
assert_not_equals(tmpl.content.ownerDocument, document);
assert_equals(tmpl.content.ownerDocument, tmplContentNodeDocument);

// Hierarchy checks at various combinations.
assert_throws('HierarchyRequestError', () => {
Expand All @@ -79,7 +70,7 @@
assert_equals(tmpl.content.firstChild, span,
'<span> should be kept until it is removed, even after ' +
'adopted to another document.');
new_doc.body.appendChild(new_node);
new_doc.body.appendChild(tmpl.content);
assert_equals(tmpl.content.firstChild, null,
'<span> should be removed from template content.');
assert_equals(tmpl_content_reference, tmpl.content,
Expand All @@ -88,4 +79,3 @@
}, 'Template content should throw exception when its ancestor in ' +
'a different document but connected via host is being append.');
</script>
</html>