github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/test/integration/test-fixtures/npm-lock/node_modules/insert-css/index.js (about) 1 var containers = []; // will store container HTMLElement references 2 var styleElements = []; // will store {prepend: HTMLElement, append: HTMLElement} 3 4 var usage = 'insert-css: You need to provide a CSS string. Usage: insertCss(cssString[, options]).'; 5 6 function insertCss(css, options) { 7 options = options || {}; 8 9 if (css === undefined) { 10 throw new Error(usage); 11 } 12 13 var position = options.prepend === true ? 'prepend' : 'append'; 14 var container = options.container !== undefined ? options.container : document.querySelector('head'); 15 var containerId = containers.indexOf(container); 16 17 // first time we see this container, create the necessary entries 18 if (containerId === -1) { 19 containerId = containers.push(container) - 1; 20 styleElements[containerId] = {}; 21 } 22 23 // try to get the correponding container + position styleElement, create it otherwise 24 var styleElement; 25 26 if (styleElements[containerId] !== undefined && styleElements[containerId][position] !== undefined) { 27 styleElement = styleElements[containerId][position]; 28 } else { 29 styleElement = styleElements[containerId][position] = createStyleElement(); 30 31 if (position === 'prepend') { 32 container.insertBefore(styleElement, container.childNodes[0]); 33 } else { 34 container.appendChild(styleElement); 35 } 36 } 37 38 // strip potential UTF-8 BOM if css was read from a file 39 if (css.charCodeAt(0) === 0xFEFF) { css = css.substr(1, css.length); } 40 41 // actually add the stylesheet 42 if (styleElement.styleSheet) { 43 styleElement.styleSheet.cssText += css 44 } else { 45 styleElement.textContent += css; 46 } 47 48 return styleElement; 49 }; 50 51 function createStyleElement() { 52 var styleElement = document.createElement('style'); 53 styleElement.setAttribute('type', 'text/css'); 54 return styleElement; 55 } 56 57 module.exports = insertCss; 58 module.exports.insertCss = insertCss;