MCPcopy Index your code
hub / github.com/ampproject/amphtml / scopeShadowCss

Function scopeShadowCss

src/shadow-embed.js:215–241  ·  view source on GitHub ↗
(shadowRoot, css)

Source from the content-addressed store, hash-verified

213 * @visibleForTesting
214 */
215export function scopeShadowCss(shadowRoot, css) {
216 const id = devAssert(shadowRoot['id']);
217 const doc = shadowRoot.ownerDocument;
218 let rules = null;
219 // Try to use a separate document.
220 try {
221 rules = getStylesheetRules(doc.implementation.createHTMLDocument(''), css);
222 } catch (e) {
223 // Ignore.
224 }
225 // Try to use the current document.
226 if (!rules) {
227 try {
228 rules = getStylesheetRules(doc, css);
229 } catch (e) {
230 // Ignore.
231 }
232 }
233
234 // No rules could be parsed - return css as is.
235 if (!rules) {
236 return css;
237 }
238
239 // Patch selectors.
240 return ShadowCSS.scopeRules(rules, `.${id}`, transformRootSelectors);
241}
242
243/**
244 * Replaces top-level selectors such as `html` and `body` with their polyfill

Callers 2

scopeFunction · 0.90
transformShadowCssFunction · 0.85

Calls 2

devAssertFunction · 0.90
getStylesheetRulesFunction · 0.85

Tested by

no test coverage detected