( ampdoc, cssText, cb, opt_isRuntimeCss, opt_ext )
| 34 | * @return {!Element} |
| 35 | */ |
| 36 | export function installStylesForDoc( |
| 37 | ampdoc, |
| 38 | cssText, |
| 39 | cb, |
| 40 | opt_isRuntimeCss, |
| 41 | opt_ext |
| 42 | ) { |
| 43 | const cssRoot = ampdoc.getHeadNode(); |
| 44 | const style = insertStyleElement( |
| 45 | cssRoot, |
| 46 | maybeTransform(cssRoot, cssText), |
| 47 | opt_isRuntimeCss || false, |
| 48 | opt_ext || null |
| 49 | ); |
| 50 | |
| 51 | if (cb) { |
| 52 | const rootNode = ampdoc.getRootNode(); |
| 53 | // Styles aren't always available synchronously. E.g. if there is a |
| 54 | // pending style download, it will have to finish before the new |
| 55 | // style is visible. |
| 56 | // For this reason we poll until the style becomes available. |
| 57 | // Sync case. |
| 58 | if (styleLoaded(rootNode, style)) { |
| 59 | cb(style); |
| 60 | return style; |
| 61 | } |
| 62 | // Poll until styles are available. |
| 63 | const interval = setInterval(() => { |
| 64 | if (styleLoaded(rootNode, style)) { |
| 65 | clearInterval(interval); |
| 66 | cb(style); |
| 67 | } |
| 68 | }, 4); |
| 69 | } |
| 70 | return style; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Creates the properly configured style element. |
no test coverage detected