1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports["default"] = void 0;
|
7 |
|
8 | var containers = [];
|
9 |
|
10 | var styleElements = [];
|
11 |
|
12 | var usage = 'insert-css: You need to provide a CSS string. Usage: insertCss(cssString[, options]).';
|
13 |
|
14 | function createStyleElement() {
|
15 | var styleElement = document.createElement('style');
|
16 | styleElement.setAttribute('type', 'text/css');
|
17 | return styleElement;
|
18 | }
|
19 |
|
20 |
|
21 | function insertCss(css, options) {
|
22 | options = options || {};
|
23 |
|
24 | if (css === undefined) {
|
25 | throw new Error(usage);
|
26 | }
|
27 |
|
28 | var position = options.prepend === true ? 'prepend' : 'append';
|
29 | var container = options.container !== undefined ? options.container : document.querySelector('head');
|
30 | var containerId = containers.indexOf(container);
|
31 |
|
32 | if (containerId === -1) {
|
33 | containerId = containers.push(container) - 1;
|
34 | styleElements[containerId] = {};
|
35 | }
|
36 |
|
37 |
|
38 | var styleElement;
|
39 |
|
40 | if (styleElements[containerId] !== undefined && styleElements[containerId][position] !== undefined) {
|
41 | styleElement = styleElements[containerId][position];
|
42 | } else {
|
43 | styleElement = styleElements[containerId][position] = createStyleElement();
|
44 |
|
45 | if (position === 'prepend') {
|
46 | container.insertBefore(styleElement, container.childNodes[0]);
|
47 | } else {
|
48 | container.appendChild(styleElement);
|
49 | }
|
50 | }
|
51 |
|
52 |
|
53 | if (css.charCodeAt(0) === 0xfeff) {
|
54 | css = css.substr(1, css.length);
|
55 | }
|
56 |
|
57 |
|
58 | if (styleElement.styleSheet) {
|
59 | styleElement.styleSheet.cssText += css;
|
60 | } else {
|
61 | styleElement.textContent += css;
|
62 | }
|
63 |
|
64 | return styleElement;
|
65 | }
|
66 |
|
67 | var _default = insertCss;
|
68 | exports["default"] = _default; |
\ | No newline at end of file |