All files / dom index.js

16.67% Statements 3/18
0% Branches 0/17
0% Functions 0/3
17.65% Lines 3/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38        1x               1x   1x                                              
// DOM variant of insert-style
// insertStyle, { getStyle, removeStyle }
 
 
export const getStyle = (elm) => {
  if ('styleSheet' in elm && 'cssText' in elm.styleSheet) {
    return elm.styleSheet.cssText; // IE8
  }
 
  return elm.textContent;
};
 
export const removeStyle = elm => elm.parentNode.removeChild(elm);
 
const insertStyle = (css, options = {}) => {
  const elm = options.element || document.createElement('style');
  elm.setAttribute('type', 'text/css');
 
  if ('styleSheet' in elm && 'cssText' in elm.styleSheet) {
    elm.styleSheet.cssText = css; // IE8
  } else {
    elm.textContent = css;
  }
 
  if (!options.element) {
    const head = document.getElementsByTagName('head')[0];
    if (options && options.prepend) {
      head.insertBefore(elm, head.childNodes[0]);
    } else {
      head.appendChild(elm);
    }
  }
 
  return elm;
};
 
export default insertStyle;