Files
sousa-gecko/testing/web-platform/tests/shadow-dom/attach-shadow-non-html-namespace.html
T
Daniel Clark 0a809e3ebc Bug 2047053 [wpt PR 60570] - Add counter and experimental check for attachShadow on non-HTML element, a=testonly
Automatic update from web-platform-tests
Add counter and experimental check for attachShadow on non-HTML element

Per step 1 of the DOM spec's "attach a shadow root" [1],
Element.attachShadow() should check that the element's namespace is
the HTML namespace before allowing shadow root attachment.

Add a UseCounter to see how often attachShadow is called for non-
HTML elements, and add the check to disallow it behind an
experimental flag.

[1] https://dom.spec.whatwg.org/#concept-attach-a-shadow-root

Bug: 521896365
Change-Id: I5253eee863920a1ea7f7de48086355318b2982bd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7915190
Reviewed-by: Mason Freed <masonf@chromium.org>
Commit-Queue: Dan Clark <daniec@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#1645719}

--

wpt-commits: 29ce1bf0110baff4d787f2e042642868e06e6ee6
wpt-pr: 60570
2026-06-16 11:36:02 +00:00

60 lines
2.3 KiB
HTML

<!DOCTYPE html>
<title>attachShadow on non-HTML namespace elements with valid local names</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/resources/common.js"></script>
<script>
// Step 1 of "attach a shadow root" [1] requires:
// "If shadow host's namespace is not the HTML namespace, then throw a
// NotSupportedError DOMException."
// [1] https://dom.spec.whatwg.org/#concept-attach-a-shadow-root
const non_html_namespaces = [
{ uri: "http://www.w3.org/2000/svg", label: "SVG namespace" },
{ uri: "http://www.w3.org/1998/Math/MathML", label: "MathML namespace" },
{ uri: "http://www.w3.org/1999/xlink", label: "XLink namespace" },
{ uri: "http://www.w3.org/XML/1998/namespace", label: "XML namespace" },
{ uri: "http://example.com/custom", label: "custom namespace" },
{ uri: "", label: "empty string namespace" },
{ uri: null, label: "null namespace" },
];
const modes = ["open", "closed"];
for (const { uri, label } of non_html_namespaces) {
for (const mode of modes) {
for (const name of HTML5_SHADOW_ALLOWED_ELEMENTS) {
test(() => {
const el = document.createElementNS(uri, name);
assert_throws_dom("NotSupportedError", () => {
el.attachShadow({ mode });
});
}, `attachShadow({mode: "${mode}"}) should throw on ${label} element with local name '${name}'`);
}
test(() => {
const el = document.createElementNS(uri, "my-element");
assert_throws_dom("NotSupportedError", () => {
el.attachShadow({ mode });
});
}, `attachShadow({mode: "${mode}"}) should throw on ${label} element with custom element name 'my-element'`);
}
}
for (const mode of modes) {
for (const name of HTML5_SHADOW_ALLOWED_ELEMENTS) {
test(() => {
const el = document.createElementNS("http://www.w3.org/1999/xhtml", name);
const shadow = el.attachShadow({ mode });
assert_true(shadow instanceof ShadowRoot);
}, `attachShadow({mode: "${mode}"}) should succeed on HTML-namespace '${name}'`);
}
test(() => {
const el = document.createElementNS("http://www.w3.org/1999/xhtml", "my-element");
const shadow = el.attachShadow({ mode });
assert_true(shadow instanceof ShadowRoot);
}, `attachShadow({mode: "${mode}"}) should succeed on HTML-namespace custom element 'my-element'`);
}
</script>