MCPcopy
hub / github.com/Khan/aphrodite / injectStyleTag

Function injectStyleTag

src/inject.js:25–59  ·  view source on GitHub ↗
(cssRules /* : string[] */)

Source from the content-addressed store, hash-verified

23// tag on it if that exists in the DOM. This could be used for e.g. reusing the
24// same style tag that server-side rendering inserts.
25const injectStyleTag = (cssRules /* : string[] */) => {
26 if (styleTag == null) {
27 // Try to find a style tag with the `data-aphrodite` attribute first.
28 styleTag = ((document.querySelector("style[data-aphrodite]") /* : any */) /* : ?HTMLStyleElement */);
29
30 // If that doesn't work, generate a new style tag.
31 if (styleTag == null) {
32 // Taken from
33 // http://stackoverflow.com/questions/524696/how-to-create-a-style-tag-with-javascript
34 const head = document.head || document.getElementsByTagName('head')[0];
35 styleTag = document.createElement('style');
36
37 styleTag.type = 'text/css';
38 styleTag.setAttribute("data-aphrodite", "");
39 head.appendChild(styleTag);
40 }
41 }
42
43 // $FlowFixMe
44 const sheet = ((styleTag.styleSheet || styleTag.sheet /* : any */) /* : CSSStyleSheet */);
45
46 if (sheet.insertRule) {
47 let numRules = sheet.cssRules.length;
48 cssRules.forEach((rule) => {
49 try {
50 sheet.insertRule(rule, numRules);
51 numRules += 1;
52 } catch(e) {
53 // The selector for this rule wasn't compatible with the browser
54 }
55 });
56 } else {
57 styleTag.innerText = (styleTag.innerText || '') + cssRules.join('');
58 }
59};
60
61// Custom handlers for stringifying CSS values that have side effects
62// (such as fontFamily, which can cause @font-face rules to be injected)

Callers 1

flushToStyleTagFunction · 0.85

Calls 1

forEachMethod · 0.80

Tested by

no test coverage detected