MCPcopy
hub / github.com/ampproject/amphtml / createShadowRoot

Function createShadowRoot

src/shadow-embed.js:37–95  ·  view source on GitHub ↗
(hostElement)

Source from the content-addressed store, hash-verified

35 * @return {!ShadowRoot}
36 */
37export 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;

Callers 8

test-ampdoc.jsFile · 0.90
test-runtime.jsFile · 0.90
attachFunction · 0.90
attachShadowDoc_Method · 0.90

Calls 9

getWinFunction · 0.90
isShadowCssSupportedFunction · 0.90
installCssTransformerFunction · 0.90
createShadowRootPolyfillFunction · 0.85
transformShadowCssFunction · 0.85
forEachMethod · 0.45
pushMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected