Closed rictic closed 4 years ago
Hmm very good point. Suggestions for a better way to feature detect?
This works in Chrome canary with --enable-blink-features=DeclarativeShadowDOM
, but setting .innerHTML
to a string is not compatible with some trusted types CSP configs, so it's not what we'd generally recommend:
let hasNative;
export function hasNativeDeclarativeShadowRoots() {
if (hasNative === undefined) {
const div = document.createElement('div');
div.innerHTML = `<div><template shadowroot="open"></template></div>`;
hasNative = !!div.firstElementChild.shadowRoot;
}
return hasNative;
}
It would be good to have some reliable way to tell, so that polyfills of the feature can avoid walking the document for templates.
@domenic do you have any recommendations here? The original
'shadowRoot' in HTMLTemplateElement.prototype
suggestion already returns true. Have we, in the past, added hooks that are just for doing feature detection? I.e. I could see adding
'supportsDeclarativeShadowDOM' in HTMLTemplateElement.prototype
but that seems ugly.
HTMLTemplateElement.prototype.hasOwnProperty("shadowRoot");
Thanks - that works! I've updated the explainer accordingly, but I still need to implement this in Chromium.
The given function for feature detection:
This returns true for existing browsers, as
shadowRoot
is defined on Element.prototype