Automatic update from web-platform-tests Fix importNode fallback registry not propagating to nested null-registry descendants (#59409) When importNode clones a tree with a provided customElementRegistry fallback, the fallback should be used for all descendants with null registries. Previously, Element::CloneWithChildren passed the *resolved* registry (which could be an intermediate ancestor's own registry) as the fallback for children, instead of propagating the original fallback_registry through the entire clone tree. For example, given: container(null) > e1(registry1) > e1Null(null), calling importNode(container, {customElementRegistry: registry}) would incorrectly assign registry1 to e1Null instead of registry. Fix: Add a separate fallback_registry parameter to CloneWithChildren so the resolved registry is used for element creation while the original fallback propagates unchanged to all descendants. Also pass fallback_registry when cloning shadow root children instead of nullptr. Bug: 491031515 Change-Id: I43d67d0d574431b284260eb1581a6f74a8fee651 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7770294 Commit-Queue: Jayson Chen <jaysonchen@microsoft.com> Reviewed-by: Joey Arhar <jarhar@chromium.org> Cr-Commit-Position: refs/heads/main@{#1619147} Co-authored-by: Jayson Chen <jaysonchen@microsoft.com> -- wpt-commits: 5095f1e101f587e4f9643186146c5a003a979427 wpt-pr: 59409
222 lines
11 KiB
HTML
222 lines
11 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
|
|
<link rel="help" href="https://github.com/whatwg/html/issues/10854">
|
|
<script src="/resources/testharness.js"></script>
|
|
<script src="/resources/testharnessreport.js"></script>
|
|
</head>
|
|
<body>
|
|
<div id="host">
|
|
<template shadowrootmode="open" shadowrootclonable="true" shadowrootcustomelementregistry>
|
|
<div>
|
|
<some-element></some-element>
|
|
<other-element></other-element>
|
|
</div>
|
|
</template>
|
|
</div>
|
|
<template id="template">
|
|
<some-element>
|
|
<template shadowrootmode="closed" shadowrootclonable="true" shadowrootcustomelementregistry>
|
|
<div>
|
|
<some-element></some-element>
|
|
<other-element></other-element>
|
|
</div>
|
|
</template>
|
|
</some-element>
|
|
</template>
|
|
<div id="root">
|
|
<span></span>
|
|
</div>
|
|
<script>
|
|
|
|
const scopedRegistry = new CustomElementRegistry();
|
|
const emptyRegistry = new CustomElementRegistry();
|
|
class GlobalSomeElement extends HTMLElement {
|
|
elementInternals;
|
|
|
|
constructor() {
|
|
super();
|
|
this.elementInternals = this.attachInternals();
|
|
if (this.elementInternals.shadowRoot)
|
|
scopedRegistry.initialize(this.elementInternals.shadowRoot);
|
|
}
|
|
};
|
|
class GlobalOtherElement extends HTMLElement {};
|
|
class ScopedSomeElement extends HTMLElement {};
|
|
customElements.define('some-element', GlobalSomeElement);
|
|
customElements.define('other-element', GlobalOtherElement);
|
|
scopedRegistry.define('some-element', ScopedSomeElement);
|
|
|
|
test(() => {
|
|
assert_true(document.importNode(document.createElement('some-element')) instanceof GlobalSomeElement);
|
|
}, 'importNode should clone using the global regsitry by default');
|
|
|
|
test(() => {
|
|
assert_true(document.importNode(document.createElement('some-element'), {customElementRegistry: scopedRegistry}) instanceof GlobalSomeElement);
|
|
}, 'importNode should clone using target\'s registry if non-null');
|
|
|
|
test(() => {
|
|
assert_true(document.importNode(document.implementation.createHTMLDocument().createElement('some-element'), {customElementRegistry: scopedRegistry}) instanceof ScopedSomeElement);
|
|
}, 'importNode should clone using the specified registry if target\'s registry is null');
|
|
|
|
test(() => {
|
|
const clone = document.importNode(host, {selfOnly: false, customElementRegistry: scopedRegistry});
|
|
assert_equals(clone.shadowRoot.querySelector('some-element').__proto__.constructor.name, 'HTMLElement');
|
|
assert_false(clone.shadowRoot.querySelector('some-element') instanceof GlobalSomeElement);
|
|
assert_false(clone.shadowRoot.querySelector('some-element') instanceof ScopedSomeElement);
|
|
assert_true(clone.shadowRoot.querySelector('other-element') instanceof HTMLElement);
|
|
assert_false(clone.shadowRoot.querySelector('other-element') instanceof GlobalOtherElement);
|
|
}, 'importNode should preserve null-ness of custom element registry');
|
|
|
|
test(() => {
|
|
const clone = document.importNode(host.shadowRoot.querySelector('div'), {selfOnly: false});
|
|
assert_equals(clone.customElementRegistry, window.customElements);
|
|
assert_true(clone.querySelector('some-element') instanceof GlobalSomeElement);
|
|
assert_true(clone.querySelector('other-element') instanceof GlobalOtherElement);
|
|
}, 'importNode should clone a shadow host with a declarative shadow DOM using the global registry by default');
|
|
|
|
test(() => {
|
|
const clone = document.importNode(host.shadowRoot.querySelector('div'), {selfOnly: false, customElementRegistry: scopedRegistry});
|
|
assert_equals(clone.customElementRegistry, scopedRegistry);
|
|
assert_true(clone.querySelector('some-element') instanceof ScopedSomeElement);
|
|
assert_false(clone.querySelector('other-element') instanceof GlobalOtherElement);
|
|
}, 'importNode should clone a shadow host with a declarative shadow DOM using a specified scoped registry');
|
|
|
|
test(() => {
|
|
const element = document.createElement('div', {customElementRegistry: emptyRegistry});
|
|
element.innerHTML = '<some-element></some-element><other-element></other-element>';
|
|
const clone = document.importNode(element, {selfOnly: false, customElementRegistry: scopedRegistry});
|
|
assert_equals(clone.customElementRegistry, emptyRegistry);
|
|
assert_true(clone.querySelector('some-element') instanceof HTMLElement);
|
|
assert_false(clone.querySelector('some-element') instanceof GlobalSomeElement);
|
|
assert_false(clone.querySelector('some-element') instanceof ScopedSomeElement);
|
|
assert_true(clone.querySelector('other-element') instanceof HTMLElement);
|
|
assert_false(clone.querySelector('other-element') instanceof GlobalOtherElement);
|
|
}, 'importNode should clone using target\'s registry if non-null, including when it\'s not the global registry');
|
|
|
|
test(() => {
|
|
const template = document.createElement('template');
|
|
template.innerHTML = '<div><some-element>hello</some-element><other-element>world</other-element></div>';
|
|
assert_equals(template.content.querySelector('some-element').__proto__.constructor.name, 'HTMLElement');
|
|
assert_equals(template.content.querySelector('other-element').__proto__.constructor.name, 'HTMLElement');
|
|
const clone = document.importNode(template.content, {selfOnly: false});
|
|
assert_equals(clone.querySelector('some-element').customElementRegistry, window.customElements);
|
|
assert_equals(clone.querySelector('some-element').__proto__.constructor.name, 'GlobalSomeElement');
|
|
assert_equals(clone.querySelector('other-element').__proto__.constructor.name, 'GlobalOtherElement');
|
|
}, 'importNode should clone a template content using the global registry by default');
|
|
|
|
test(() => {
|
|
const template = document.createElement('template');
|
|
template.innerHTML = '<div><some-element>hello</some-element><other-element>world</other-element></div>';
|
|
assert_equals(template.content.querySelector('some-element').__proto__.constructor.name, 'HTMLElement');
|
|
assert_equals(template.content.querySelector('other-element').__proto__.constructor.name, 'HTMLElement');
|
|
const clone = document.importNode(template.content, {selfOnly: false, customElementRegistry: scopedRegistry});
|
|
assert_equals(clone.querySelector('some-element').customElementRegistry, scopedRegistry);
|
|
assert_equals(clone.querySelector('some-element').__proto__.constructor.name, 'ScopedSomeElement');
|
|
assert_equals(clone.querySelector('other-element').__proto__.constructor.name, 'HTMLElement');
|
|
}, 'importNode should clone a template content using a specified scoped registry');
|
|
|
|
test(() => {
|
|
const template = document.createElement('template');
|
|
template.innerHTML = `
|
|
<div>
|
|
<some-element>
|
|
<template>
|
|
<other-element>
|
|
hello
|
|
</other-element>
|
|
</template>
|
|
</some-element>
|
|
<other-element>
|
|
world
|
|
</other-element>
|
|
</div>`;
|
|
assert_equals(template.content.querySelector('some-element').__proto__.constructor.name, 'HTMLElement');
|
|
assert_equals(template.content.querySelector('other-element').__proto__.constructor.name, 'HTMLElement');
|
|
const clone = document.importNode(template.content, {selfOnly: false});
|
|
assert_equals(clone.querySelector('some-element').customElementRegistry, window.customElements);
|
|
assert_equals(clone.querySelector('some-element').__proto__.constructor.name, 'GlobalSomeElement');
|
|
const otherElementInTemplate = clone.querySelector('template').content.querySelector('other-element');
|
|
assert_equals(otherElementInTemplate.__proto__.constructor.name, 'HTMLElement');
|
|
assert_equals(clone.querySelector('other-element').__proto__.constructor.name, 'GlobalOtherElement');
|
|
}, 'importNode should clone a template content with a nested template element using a scoped registry');
|
|
|
|
test(() => {
|
|
const clone = document.importNode(root);
|
|
assert_false(clone.hasChildNodes());
|
|
}, "importNode: don't pass options argument");
|
|
|
|
test(() => {
|
|
const clone = document.importNode(root, false);
|
|
assert_false(clone.hasChildNodes());
|
|
}, "importNode: pass options argument with value false");
|
|
|
|
test(() => {
|
|
const clone = document.importNode(root, true);
|
|
assert_true(clone.hasChildNodes());
|
|
}, "importNode: pass options argument with value true");
|
|
|
|
test(() => {
|
|
const clone = document.importNode(root, undefined);
|
|
assert_false(clone.hasChildNodes());
|
|
}, "importNode: pass options argument with value undefined");
|
|
|
|
test(() => {
|
|
const clone = document.importNode(root, { });
|
|
assert_true(clone.hasChildNodes());
|
|
}, "importNode: pass options argument with value { }");
|
|
|
|
test(() => {
|
|
const clone = document.importNode(root, { selfOnly: false });
|
|
assert_true(clone.hasChildNodes());
|
|
}, "importNode: pass options argument with value { selfOnly: false }");
|
|
|
|
test(() => {
|
|
const clone = document.importNode(root, { selfOnly: true });
|
|
assert_false(clone.hasChildNodes());
|
|
}, "importNode: pass options argument with value { selfOnly: true }");
|
|
|
|
test(() => {
|
|
const clone = document.importNode(root, { customElementRegistry: scopedRegistry });
|
|
assert_true(clone.hasChildNodes());
|
|
}, "importNode: pass options argument with value { customElementRegistry: scopedRegistry }");
|
|
|
|
test(() => {
|
|
assert_throws_js(TypeError, () => document.importNode(root, { customElementRegistry: null }));
|
|
}, "importNode: pass options argument with value { customElementRegistry: null }");
|
|
|
|
test(() => {
|
|
const tagName = 'some-element';
|
|
const fallbackRegistry = new CustomElementRegistry();
|
|
class FallbackSomeElement extends HTMLElement {};
|
|
fallbackRegistry.define(tagName, FallbackSomeElement);
|
|
|
|
const container = document.createElement('div', {customElementRegistry: null});
|
|
const child = document.createElement(tagName, {customElementRegistry: scopedRegistry});
|
|
container.append(child);
|
|
const grandchild = document.createElement(tagName, {customElementRegistry: null});
|
|
child.append(grandchild);
|
|
|
|
assert_equals(container.customElementRegistry, null);
|
|
assert_equals(child.customElementRegistry, scopedRegistry);
|
|
assert_equals(grandchild.customElementRegistry, null);
|
|
|
|
const imported = document.importNode(container, {customElementRegistry: fallbackRegistry});
|
|
const importedChild = imported.firstElementChild;
|
|
const importedGrandchild = importedChild.firstElementChild;
|
|
|
|
assert_equals(imported.customElementRegistry, fallbackRegistry,
|
|
'imported container with null registry should use the fallback registry');
|
|
assert_equals(importedChild.customElementRegistry, scopedRegistry,
|
|
'imported child with non-null registry should retain its own registry');
|
|
assert_equals(importedGrandchild.customElementRegistry, fallbackRegistry,
|
|
'imported grandchild with null registry should use the fallback registry, not the intermediate ancestor\'s registry');
|
|
assert_true(importedGrandchild instanceof FallbackSomeElement,
|
|
'grandchild should be upgraded using the fallback registry');
|
|
}, 'importNode should use the provided fallback registry for null-registry descendants nested under non-null-registry ancestors');
|
|
|
|
</script>
|
|
</body>
|
|
</html>
|