Skip to content

Commit 7db7ff7

Browse files
author
Steve Orvell
committed
add force option and use in DSD testing
1 parent 609a4ab commit 7db7ff7

3 files changed

Lines changed: 26 additions & 14 deletions

File tree

packages/scoped-custom-element-registry/src/scoped-custom-element-registry.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
declare interface PolyfillWindow {
2323
CustomElementRegistryPolyfill: {
24+
force?: boolean;
2425
formAssociated: Set<string>;
2526
nativeRegistry: CustomElementRegistry;
2627
hasCustomElementRegistry: boolean;
@@ -50,6 +51,8 @@ polyfillWindow['CustomElementRegistryPolyfill'][
5051
'formAssociated'
5152
] ??= new Set<string>();
5253

54+
const {force} = polyfillWindow['CustomElementRegistryPolyfill'];
55+
5356
Object.assign(
5457
polyfillWindow['CustomElementRegistryPolyfill'],
5558
(() => {
@@ -68,7 +71,7 @@ Object.assign(
6871
return {
6972
'hasCustomElementRegistry': reg,
7073
'hasNullCustomElementRegistry': nullReg,
71-
'inUse': !(reg && nullReg),
74+
'inUse': force || !(reg && nullReg),
7275
};
7376
})()
7477
);
@@ -153,19 +156,26 @@ type ParametersOf<
153156
// Use an IIFE to prevent polyfill use if native scoped registries are detected
154157
(() => {
155158
const {
159+
['force']: force,
156160
['inUse']: inUse,
157161
['hasCustomElementRegistry']: hasCustomElementRegistry,
158162
['hasNullCustomElementRegistry']: hasNullCustomElementRegistry,
159163
} = polyfillWindow['CustomElementRegistryPolyfill'];
160164
console.warn(
161-
`Scoped custom element registries polyfill ${
162-
hasCustomElementRegistry
163-
? `detected browser support ${
164-
hasNullCustomElementRegistry
165-
? `and did not load.`
166-
: `but loaded because null registry support was not detected.`
165+
`Scoped custom element registries polyfill is ${
166+
inUse ? 'in use' : 'not in use'
167+
}. ${
168+
force
169+
? 'The "force" flag was set to true.'
170+
: `The polyfill ${
171+
hasCustomElementRegistry
172+
? `detected browser support ${
173+
hasNullCustomElementRegistry
174+
? `and did not load.`
175+
: `but loaded because null registry support was not detected.`
176+
}`
177+
: 'did *not* detect browser support and loaded.'
167178
}`
168-
: 'did *not* detect browser support and loaded.'
169179
}`
170180
);
171181
if (!inUse) {

packages/scoped-custom-element-registry/test/DeclarativeShadowRoot.test.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<html>
22
<body>
3+
<script>
4+
window.CustomElementRegistryPolyfill = {
5+
force: true,
6+
};
7+
</script>
38
<script src="../scoped-custom-element-registry.min.js"></script>
49
<div id="host1">
510
<template shadowrootmode="open">
611
<dsd-element></dsd-element>
712
</template>
813
</div>
914
<div id="host2" polyfill-shadowrootcustomelementregistry>
10-
<template shadowrootmode="open" Xshadowrootcustomelementregistry>
15+
<!-- note, cannot use native attribute here if browser has native support -->
16+
<template shadowrootmode="open">
1117
<dsd-element></dsd-element>
1218
</template>
1319
</div>

packages/scoped-custom-element-registry/test/DeclarativeShadowRoot.test.html.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ describe('Declarative ShadowRoot', () => {
1212
expect(ce).to.be.instanceOf(customElements.get(ce.localName));
1313
});
1414

15-
it(`should *not* customize elements in null registry, should be browser ${
16-
navigator.userAgent
17-
} and polyfill ${JSON.stringify(
18-
window.CustomElementRegistryPolyfill
19-
)}`, () => {
15+
it('should *not* customize elements in null registry', () => {
2016
const host = document.getElementById('host2');
2117
expect(host.shadowRoot).not.to.be.null;
2218
expect(host.shadowRoot.customElementRegistry).to.be.null;

0 commit comments

Comments
 (0)