* Creates the properly configured style element. * @param {!Element|!ShadowRoot} cssRoot * @param {string} cssText * @param {boolean} isRuntimeCss * @param {?string} ext * @return {!Element}
(cssRoot, cssText, isRuntimeCss, ext)
| 79 | * @return {!Element} |
| 80 | */ |
| 81 | function insertStyleElement(cssRoot, cssText, isRuntimeCss, ext) { |
| 82 | let styleMap = cssRoot[STYLE_MAP_PROP]; |
| 83 | if (!styleMap) { |
| 84 | styleMap = cssRoot[STYLE_MAP_PROP] = map(); |
| 85 | } |
| 86 | |
| 87 | const isExtCss = |
| 88 | !isRuntimeCss && ext && ext != 'amp-custom' && ext != 'amp-keyframes'; |
| 89 | const key = isRuntimeCss |
| 90 | ? 'amp-runtime' |
| 91 | : isExtCss |
| 92 | ? `amp-extension=${ext}` |
| 93 | : null; |
| 94 | |
| 95 | // Check if it has already been created or discovered. |
| 96 | if (key) { |
| 97 | const existing = getExistingStyleElement(cssRoot, styleMap, key); |
| 98 | // If we find a `link[rel=stylesheet]` to an extensions css, it is |
| 99 | // prioritized and not overwritten as the document would most likely |
| 100 | // be a transformed document and that the extension will also most likely |
| 101 | // be an extension without a CSS to install for optimization. |
| 102 | if (existing) { |
| 103 | // Only overwrite the textContent if it is a `style` tag. |
| 104 | if (existing.tagName == 'STYLE' && existing.textContent !== cssText) { |
| 105 | existing.textContent = cssText; |
| 106 | } |
| 107 | return existing; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Create the new style element and append to cssRoot. |
| 112 | const doc = cssRoot.ownerDocument || cssRoot; |
| 113 | const style = doc.createElement('style'); |
| 114 | style./*OK*/ textContent = cssText; |
| 115 | let afterElement = null; |
| 116 | // Make sure that we place style tags after the main runtime CSS. Otherwise |
| 117 | // the order is random. |
| 118 | if (isRuntimeCss) { |
| 119 | style.setAttribute('amp-runtime', ''); |
| 120 | } else if (isExtCss) { |
| 121 | style.setAttribute('amp-extension', ext || ''); |
| 122 | afterElement = dev().assertElement( |
| 123 | getExistingStyleElement(cssRoot, styleMap, 'amp-runtime') |
| 124 | ); |
| 125 | } else { |
| 126 | if (ext) { |
| 127 | style.setAttribute(ext, ''); |
| 128 | } |
| 129 | afterElement = cssRoot.lastChild; |
| 130 | } |
| 131 | insertAfterOrAtStart(cssRoot, style, afterElement); |
| 132 | if (key) { |
| 133 | styleMap[key] = style; |
| 134 | } |
| 135 | return style; |
| 136 | } |
| 137 | |
| 138 | /** |
no test coverage detected