Skip to content

Commit 237753b

Browse files
committed
Add new tests for customelementregistry content attribute and specifying null to createEleent and attachShadow per
- whatwg/dom#1424 - whatwg/dom#1423 Also update the existing tests to account for the behavior differences.`
1 parent 8293902 commit 237753b

7 files changed

+249
-9
lines changed

custom-elements/registries/Element-customElementRegistry-exceptions.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
<script src="/resources/testharnessreport.js"></script>
88
</head>
99
<body>
10-
<div id="host-window"><template shadowrootmode="open"><a-b></a-b></template></div>
11-
<div id="host-null"><template shadowrootmode="open" shadowrootcustomelementregistry=""><a-b></a-b></template></div>
1210
<script>
1311

1412
setup({allow_uncaught_exception : true});

custom-elements/registries/Element-customElementRegistry.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
const clone = document.getElementById('host-null').cloneNode(true);
4242
const ab = clone.shadowRoot.querySelector('a-b');
4343
document.body.appendChild(ab);
44-
assert_equals(ab.customElementRegistry, window.customElements);
44+
assert_equals(ab.customElementRegistry, null);
4545

4646
const frame = document.createElement('iframe');
4747
t.add_cleanup(() => frame.remove());

custom-elements/registries/ShadowRoot-init-customElementRegistry.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636
test(() => {
3737
const host = document.body.appendChild(document.createElement('div'));
3838
const shadowRoot = host.attachShadow({mode: 'closed', customElementRegistry: null});
39-
assert_equals(shadowRoot.customElementRegistry, window.customElements);
40-
}, 'attachShadow() should use the global registry when customElementRegistry is null (host uses global registry)');
39+
assert_equals(shadowRoot.customElementRegistry, null);
40+
}, 'attachShadow() should use null registry when customElementRegistry is null (host uses global registry)');
4141

4242
test(() => {
4343
const registry = new CustomElementRegistry;
4444
const host = document.body.appendChild(document.createElement('div', {customElementRegistry: registry}));
4545
const shadowRoot = host.attachShadow({mode: 'closed', customElementRegistry: null});
46-
assert_equals(shadowRoot.customElementRegistry, window.customElements);
47-
}, 'attachShadow() should use the global registry when customElementRegistry is null (host uses custom registry)');
46+
assert_equals(shadowRoot.customElementRegistry, null);
47+
}, 'attachShadow() should use null registry when customElementRegistry is null (host uses custom registry)');
4848

4949
test(() => {
5050
const registry = new CustomElementRegistry;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<meta name="author" title="Ryosuke Niwa" href="mailto:[email protected]"/>
5+
<link rel="help" href="https://html.spec.whatwg.org/multipage/custom-elements.html#dom-customelementregistry-initialize"/>
6+
<script src="/resources/testharness.js"></script>
7+
<script src="/resources/testharnessreport.js"></script>
8+
</head>
9+
<body>
10+
<script>
11+
<![CDATA[
12+
13+
customElements.define('c-d', class ABElement extends HTMLElement { });
14+
15+
]]>
16+
</script>
17+
<div id="container"><a-b customelementregistry=""></a-b><c-d customelementregistry=""></c-d></div>
18+
<script>
19+
<![CDATA[
20+
21+
const container = document.getElementById('container');
22+
test((test) => {
23+
assert_equals(container.querySelector('a-b').customElementRegistry, null);
24+
}, `XHTML parser should create a custom element candidate with null registry if customelementregistry is set`);
25+
26+
test((test) => {
27+
assert_equals(container.querySelector('c-d').customElementRegistry, null);
28+
}, `XHTML parser should create a defined custom element with null registry if customelementregistry is set`);
29+
30+
test((test) => {
31+
container.setHTMLUnsafe(`<a-b customelementregistry></a-b>`);
32+
assert_equals(container.querySelector('a-b').customElementRegistry, null);
33+
}, `XHTML fragment parser should create a custom element candidate with null registry if customelementregistry is set`);
34+
35+
test((test) => {
36+
container.setHTMLUnsafe(`<c-d></c-d>`);
37+
assert_equals(container.querySelector('c-d').customElementRegistry, window.customElements);
38+
container.setHTMLUnsafe(`<c-d customelementregistry></c-d>`);
39+
assert_equals(container.querySelector('c-d').customElementRegistry, null);
40+
}, `XHTML fragment parser should create a defined custom element with null registry if customelementregistry is set`);
41+
42+
]]>
43+
</script>
44+
</body>
45+
</html>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta name="author" title="Ryosuke Niwa" href="mailto:[email protected]">
5+
<link rel="help" href="https://html.spec.whatwg.org/multipage/custom-elements.html#dom-customelementregistry-initialize">
6+
<script src="/resources/testharness.js"></script>
7+
<script src="/resources/testharnessreport.js"></script>
8+
</head>
9+
<body>
10+
<script>
11+
12+
function makeIframe(test) {
13+
const iframe = document.createElement('iframe');
14+
document.body.appendChild(iframe);
15+
test.add_cleanup(() => iframe.remove());
16+
return [iframe.contentDocument, iframe.contentWindow];
17+
}
18+
19+
function makeIframeWithABElement(test) {
20+
const [doc, win] = makeIframe(test);
21+
win.customElements.define('a-b', class ABElement extends win.HTMLElement { });
22+
return [doc, win];
23+
}
24+
25+
function runBasicTests(makeIframe, elementName, elementDescription) {
26+
test((test) => {
27+
const [doc, win] = makeIframe(test);
28+
doc.documentElement.setHTMLUnsafe(`<${elementName} customelementregistry></${elementName}>`);
29+
assert_equals(doc.querySelector(elementName).customElementRegistry, null);
30+
}, `HTML parser should create a ${elementDescription} with null registry if customelementregistry is set`);
31+
32+
test((test) => {
33+
const [doc, win] = makeIframe(test);
34+
assert_equals(doc.createElement(elementName, {customElementRegistry: null}).customElementRegistry, null);
35+
assert_equals(doc.createElementNS('http://www.w3.org/1999/xhtml', 'div', {customElementRegistry: null}).customElementRegistry, null);
36+
}, `document.createElement should create a ${elementDescription} with null registry if customElement is set to null`);
37+
38+
test((test) => {
39+
const [doc, win] = makeIframe(test);
40+
const host = doc.createElement(elementName);
41+
const shadowRoot = host.attachShadow({mode: 'closed', customElementRegistry: null})
42+
assert_equals(shadowRoot.customElementRegistry, null);
43+
}, `attchShadow on a ${elementDescription} with null customElementRegistry should create a ShadowRoot with null registry`);
44+
45+
test((test) => {
46+
const [doc, win] = makeIframe(test);
47+
const element = doc.createElement(elementName);
48+
assert_equals(element.customElementRegistry, win.customElements);
49+
element.setAttribute('customelementregistry', '');
50+
assert_equals(element.customElementRegistry, win.customElements);
51+
}, `Setting customelementregistry content attribute after a ${elementDescription} had finishsed parsing should not set null registry`);
52+
53+
test((test) => {
54+
const [doc, win] = makeIframe(test);
55+
const element = doc.createElement(elementName, {customElementRegistry: null});
56+
assert_equals(element.customElementRegistry, null);
57+
assert_equals(element.cloneNode(false).customElementRegistry, null);
58+
59+
doc.documentElement.setHTMLUnsafe(`<${elementName} customelementregistry></${elementName}>`);
60+
assert_equals(doc.querySelector(elementName).customElementRegistry, null);
61+
assert_equals(doc.querySelector(elementName).cloneNode(true).customElementRegistry, null);
62+
}, `Cloning a ${elementDescription} with null regsitry should create an element with null registry`);
63+
}
64+
65+
runBasicTests(makeIframe, 'div', 'builtin element');
66+
runBasicTests(makeIframe, 'a-b', 'custom element candidate');
67+
runBasicTests(makeIframeWithABElement, 'a-b', 'custom element');
68+
69+
test((test) => {
70+
const [doc, win] = makeIframe(test);
71+
72+
const upgradeCandidate = doc.createElement('a-b');
73+
assert_equals(upgradeCandidate.customElementRegistry, win.customElements);
74+
75+
doc.body.setHTMLUnsafe('<a-b></a-b>');
76+
assert_equals(doc.querySelector('a-b').customElementRegistry, win.customElements);
77+
78+
assert_equals(upgradeCandidate.customElementRegistry, win.customElements);
79+
let customElementRegistryDuringConstruction = null;
80+
win.customElements.define('a-b', class ABElement extends win.HTMLElement {
81+
constructor() {
82+
super();
83+
this.setAttribute('customelementregistry', '');
84+
customElementRegistryDuringConstruction = this.customElementRegistry;
85+
this.removeAttribute('customelementregistry', '');
86+
}
87+
});
88+
assert_equals(customElementRegistryDuringConstruction, win.customElements);
89+
assert_equals(doc.querySelector('a-b').customElementRegistry, win.customElements);
90+
91+
const element = doc.createElement('a-b');
92+
assert_equals(customElementRegistryDuringConstruction, win.customElements);
93+
assert_equals(element.customElementRegistry, win.customElements);
94+
95+
doc.body.setHTMLUnsafe('<a-b></a-b>');
96+
assert_equals(customElementRegistryDuringConstruction, win.customElements);
97+
assert_equals(element.customElementRegistry, win.customElements);
98+
99+
win.customElements.upgrade(upgradeCandidate);
100+
assert_equals(customElementRegistryDuringConstruction, win.customElements);
101+
assert_equals(element.customElementRegistry, win.customElements);
102+
}, `Setting customelementregistry content attribute during constructor should not make it use null registry`);
103+
104+
</script>
105+
</body>
106+
</html>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta name="author" title="Ryosuke Niwa" href="mailto:[email protected]">
5+
<script src="/resources/testharness.js"></script>
6+
<script src="/resources/testharnessreport.js"></script>
7+
</head>
8+
<body>
9+
<script>
10+
11+
test(() => {
12+
assert_equals((new Document).createElement('a-b').customElementRegistry, null);
13+
assert_equals(document.implementation.createHTMLDocument().createElement('a-b').customElementRegistry, null);
14+
assert_equals(document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null).createElement('a-b').customElementRegistry, null);
15+
}, 'customElementRegistry of an upgrade candidate created in a document without a browsing context uses null regsitry by default');
16+
17+
function makeIframe(test) {
18+
const iframe = document.createElement('iframe');
19+
document.body.appendChild(iframe);
20+
test.add_cleanup(() => iframe.remove());
21+
return [iframe.contentDocument, iframe.contentWindow];
22+
}
23+
24+
test((test) => {
25+
const [doc, win] = makeIframe(test);
26+
doc.documentElement.setHTMLUnsafe('<div id="host"><template shadowrootmode="open" shadowrootclonable="true" shadowrootcustomelementregistry><a-b></a-b></template></div>');
27+
const hostClone = doc.getElementById('host').cloneNode(true);
28+
assert_equals(hostClone.shadowRoot.customElementRegistry, null);
29+
assert_equals(hostClone.shadowRoot.querySelector('a-b').customElementRegistry, null);
30+
}, 'Connecting a custom element candiate in a shadow root with a scoped custom element registry has null registry by default');
31+
32+
test((test) => {
33+
const [doc, win] = makeIframe(test);
34+
doc.documentElement.setHTMLUnsafe('<div id="host"><template shadowrootmode="open" shadowrootclonable="true" shadowrootcustomelementregistry><a-b></a-b></template></div>');
35+
win.customElements.define('a-b', class ABElement extends win.HTMLElement { });
36+
const hostClone = doc.getElementById('host').cloneNode(true);
37+
assert_equals(hostClone.shadowRoot.customElementRegistry, null);
38+
const candidate = hostClone.shadowRoot.querySelector('a-b');
39+
assert_equals(candidate.customElementRegistry, null);
40+
doc.body.appendChild(candidate);
41+
assert_equals(candidate.customElementRegistry, null);
42+
}, 'Connecting a custom element candiate with null registry does not set the registry');
43+
44+
test((test) => {
45+
const [doc, win] = makeIframe(test);
46+
doc.documentElement.setHTMLUnsafe('<div id="host"><template shadowrootmode="open" shadowrootclonable="true" shadowrootcustomelementregistry><a-b></a-b></template></div>');
47+
win.customElements.define('a-b', class ABElement extends win.HTMLElement { });
48+
const hostClone = doc.getElementById('host').cloneNode(true);
49+
const candidate = hostClone.shadowRoot.querySelector('a-b');
50+
const registry = new CustomElementRegistry;
51+
assert_equals(candidate.customElementRegistry, null);
52+
registry.initialize(hostClone.shadowRoot);
53+
assert_equals(candidate.customElementRegistry, registry);
54+
doc.body.appendChild(candidate);
55+
assert_equals(candidate.customElementRegistry, registry);
56+
57+
const element = doc.createElement('host', {customElementRegistry: registry});
58+
assert_equals(element.customElementRegistry, registry);
59+
doc.body.appendChild(element);
60+
assert_equals(element.customElementRegistry, registry);
61+
}, 'Connecting a custom element candiate with a scoped custom element registry does not change the registry');
62+
63+
test((test) => {
64+
const [doc, win] = makeIframe(test);
65+
doc.documentElement.setHTMLUnsafe('<div id="host1"><template shadowrootmode="open" shadowrootclonable="true" shadowrootcustomelementregistry><a-b></a-b></template></div>'
66+
+ '<div id="host2"><template shadowrootmode="open" shadowrootclonable="true" shadowrootcustomelementregistry><c-d></c-d></template></div>');
67+
win.customElements.define('a-b', class ABElement extends win.HTMLElement { });
68+
const clone1 = doc.getElementById('host1').cloneNode(true);
69+
const clone2 = doc.getElementById('host2').cloneNode(true);
70+
const candidate = clone1.shadowRoot.querySelector('a-b');
71+
assert_equals(candidate.customElementRegistry, null);
72+
assert_equals(clone2.shadowRoot.querySelector('c-d').customElementRegistry, null);
73+
const registry = new CustomElementRegistry;
74+
registry.initialize(clone2.shadowRoot);
75+
assert_equals(clone2.shadowRoot.querySelector('c-d').customElementRegistry, registry);
76+
clone2.shadowRoot.appendChild(candidate);
77+
assert_equals(candidate.customElementRegistry, null);
78+
}, 'Inserting a custom element candiate with null registry does not change the registry');
79+
80+
test((test) => {
81+
const [doc, win] = makeIframe(test);
82+
const registry = new CustomElementRegistry;
83+
const host = doc.createElement('div');
84+
const shadowRoot = host.attachShadow({mode: 'closed', customElementRegistry: registry});
85+
assert_equals(shadowRoot.customElementRegistry, registry);
86+
doc.body.appendChild(host);
87+
assert_equals(shadowRoot.customElementRegistry, registry);
88+
}, 'Inserting the shadow host of a shadow root with a scoped custom element registry does not change the registry');
89+
90+
</script>
91+
</body>
92+
</html>

custom-elements/registries/scoped-registry-define-upgrade-criteria.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,8 @@
123123

124124
test(t => {
125125
const name = nextCustomElementName();
126-
const node = document.body.appendChild(document.createElement(name));
127-
128126
const registry = new CustomElementRegistry;
127+
const node = document.body.appendChild(document.createElement(name, {customElementRegistry: registry}));
129128
const shadow = attachShadowForTest(t, registry);
130129
shadow.appendChild(node);
131130

0 commit comments

Comments
 (0)