-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfeature_detect.ts
32 lines (30 loc) · 1.07 KB
/
feature_detect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
// lib.dom.ts is out of date, so declare our own parseFromString here.
interface DOMParser {
parseFromString(string: string, type: DOMParserSupportedType, options?: {
includeShadowRoots: boolean;
}): Document;
}
// This isn't ideal. Setting .innerHTML is not compatible with some
// TrustedTypes CSP policies. Discussion at:
// https://github.com/mfreed7/declarative-shadow-dom/issues/3
let hasNative: boolean|undefined;
export function hasNativeDeclarativeShadowRoots(): boolean {
if (hasNative === undefined) {
const html = `<div><template shadowrootmode="open"></template></div>`;
let fragment: Document;
if ('parseHTMLUnsafe' in Document) {
fragment = (Document as { parseHTMLUnsafe: (html: string) => Document }).parseHTMLUnsafe(html);
} else {
fragment = (new DOMParser() as DOMParser).parseFromString(html, 'text/html', {
includeShadowRoots: true
});
}
hasNative = !!fragment.querySelector('div')?.shadowRoot;
}
return hasNative;
}