Skip to content

Commit 62ca556

Browse files
Merge pull request #22 from matthewp/feat-detect
Fixes feature detection
2 parents d296799 + c264fd8 commit 62ca556

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/_implementation/feature_detect.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66

7+
// lib.dom.ts is out of date, so declare our own parseFromString here.
8+
interface DOMParser {
9+
parseFromString(string: string, type: DOMParserSupportedType, options?: {
10+
includeShadowRoots: boolean;
11+
}): Document;
12+
}
13+
714
// This isn't ideal. Setting .innerHTML is not compatible with some
815
// TrustedTypes CSP policies. Discussion at:
916
// https://github.com/mfreed7/declarative-shadow-dom/issues/3
1017
let hasNative: boolean|undefined;
1118
export function hasNativeDeclarativeShadowRoots(): boolean {
1219
if (hasNative === undefined) {
13-
const div = document.createElement('div');
14-
div.innerHTML = `<div><template shadowroot="open"></template></div>`;
15-
hasNative = !!div.firstElementChild!.shadowRoot;
20+
const html = `<div><template shadowroot="open"></template></div>`;
21+
const fragment = (new DOMParser() as DOMParser).parseFromString(html, 'text/html', {
22+
includeShadowRoots: true
23+
});
24+
hasNative = !!fragment.querySelector('div')?.shadowRoot;
1625
}
1726
return hasNative;
1827
}

0 commit comments

Comments
 (0)