(hostElement)
| 35 | * @return {!ShadowRoot} |
| 36 | */ |
| 37 | export function createShadowRoot(hostElement) { |
| 38 | const win = getWin(hostElement); |
| 39 | |
| 40 | const existingRoot = hostElement.shadowRoot || hostElement.__AMP_SHADOW_ROOT; |
| 41 | if (existingRoot) { |
| 42 | if (self.trustedTypes && self.trustedTypes.createPolicy) { |
| 43 | // Create Trusted Types policy that only returns the empty string as |
| 44 | // TrustedHTML |
| 45 | const policy = self.trustedTypes.createPolicy( |
| 46 | 'shadow-embed#createShadowRoot', |
| 47 | { |
| 48 | createHTML: function (unused) { |
| 49 | return ''; |
| 50 | }, |
| 51 | } |
| 52 | ); |
| 53 | existingRoot./*OK*/ innerHTML = policy.createHTML(''); |
| 54 | } else { |
| 55 | existingRoot./*OK*/ innerHTML = ''; |
| 56 | } |
| 57 | return existingRoot; |
| 58 | } |
| 59 | |
| 60 | let shadowRoot; |
| 61 | const shadowDomSupported = getShadowDomSupportedVersion(); |
| 62 | if (shadowDomSupported == ShadowDomVersion_Enum.V1) { |
| 63 | shadowRoot = hostElement.attachShadow({mode: 'open'}); |
| 64 | if (!shadowRoot.styleSheets) { |
| 65 | Object.defineProperty(shadowRoot, 'styleSheets', { |
| 66 | get: function () { |
| 67 | const items = []; |
| 68 | shadowRoot.childNodes.forEach((child) => { |
| 69 | if (child.tagName === 'STYLE') { |
| 70 | items.push(child.sheet); |
| 71 | } |
| 72 | }); |
| 73 | return items; |
| 74 | }, |
| 75 | }); |
| 76 | } |
| 77 | } else if (shadowDomSupported == ShadowDomVersion_Enum.V0) { |
| 78 | shadowRoot = hostElement.createShadowRoot(); |
| 79 | } else { |
| 80 | shadowRoot = createShadowRootPolyfill(hostElement); |
| 81 | } |
| 82 | |
| 83 | if (!isShadowCssSupported()) { |
| 84 | const rootId = `i-amphtml-sd-${win.Math.floor(win.Math.random() * 10000)}`; |
| 85 | shadowRoot['id'] = rootId; |
| 86 | shadowRoot.host.classList.add(rootId); |
| 87 | |
| 88 | // CSS isolation. |
| 89 | installCssTransformer(shadowRoot, (css) => { |
| 90 | return transformShadowCss(shadowRoot, css); |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | return shadowRoot; |
no test coverage detected