| [9af201e] | 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | /* istanbul ignore next */
|
|---|
| 4 | var replaceText = function replaceText() {
|
|---|
| 5 | var textStore = [];
|
|---|
| 6 | return function replace(index, replacement) {
|
|---|
| 7 | textStore[index] = replacement;
|
|---|
| 8 | return textStore.filter(Boolean).join("\n");
|
|---|
| 9 | };
|
|---|
| 10 | }();
|
|---|
| 11 |
|
|---|
| 12 | /* istanbul ignore next */
|
|---|
| 13 | function apply(styleElement, index, remove, obj) {
|
|---|
| 14 | var css;
|
|---|
| 15 | if (remove) {
|
|---|
| 16 | css = "";
|
|---|
| 17 | } else {
|
|---|
| 18 | css = "";
|
|---|
| 19 | if (obj.supports) {
|
|---|
| 20 | css += "@supports (".concat(obj.supports, ") {");
|
|---|
| 21 | }
|
|---|
| 22 | if (obj.media) {
|
|---|
| 23 | css += "@media ".concat(obj.media, " {");
|
|---|
| 24 | }
|
|---|
| 25 | var needLayer = typeof obj.layer !== "undefined";
|
|---|
| 26 | if (needLayer) {
|
|---|
| 27 | css += "@layer".concat(obj.layer.length > 0 ? " ".concat(obj.layer) : "", " {");
|
|---|
| 28 | }
|
|---|
| 29 | css += obj.css;
|
|---|
| 30 | if (needLayer) {
|
|---|
| 31 | css += "}";
|
|---|
| 32 | }
|
|---|
| 33 | if (obj.media) {
|
|---|
| 34 | css += "}";
|
|---|
| 35 | }
|
|---|
| 36 | if (obj.supports) {
|
|---|
| 37 | css += "}";
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | // For old IE
|
|---|
| 42 | /* istanbul ignore if */
|
|---|
| 43 | if (styleElement.styleSheet) {
|
|---|
| 44 | styleElement.styleSheet.cssText = replaceText(index, css);
|
|---|
| 45 | } else {
|
|---|
| 46 | var cssNode = document.createTextNode(css);
|
|---|
| 47 | var childNodes = styleElement.childNodes;
|
|---|
| 48 | if (childNodes[index]) {
|
|---|
| 49 | styleElement.removeChild(childNodes[index]);
|
|---|
| 50 | }
|
|---|
| 51 | if (childNodes.length) {
|
|---|
| 52 | styleElement.insertBefore(cssNode, childNodes[index]);
|
|---|
| 53 | } else {
|
|---|
| 54 | styleElement.appendChild(cssNode);
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | var singletonData = {
|
|---|
| 59 | singleton: null,
|
|---|
| 60 | singletonCounter: 0
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | /* istanbul ignore next */
|
|---|
| 64 | function domAPI(options) {
|
|---|
| 65 | if (typeof document === "undefined") return {
|
|---|
| 66 | update: function update() {},
|
|---|
| 67 | remove: function remove() {}
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | // eslint-disable-next-line no-undef,no-use-before-define
|
|---|
| 71 | var styleIndex = singletonData.singletonCounter++;
|
|---|
| 72 | var styleElement =
|
|---|
| 73 | // eslint-disable-next-line no-undef,no-use-before-define
|
|---|
| 74 | singletonData.singleton || (
|
|---|
| 75 | // eslint-disable-next-line no-undef,no-use-before-define
|
|---|
| 76 | singletonData.singleton = options.insertStyleElement(options));
|
|---|
| 77 | return {
|
|---|
| 78 | update: function update(obj) {
|
|---|
| 79 | apply(styleElement, styleIndex, false, obj);
|
|---|
| 80 | },
|
|---|
| 81 | remove: function remove(obj) {
|
|---|
| 82 | apply(styleElement, styleIndex, true, obj);
|
|---|
| 83 | }
|
|---|
| 84 | };
|
|---|
| 85 | }
|
|---|
| 86 | module.exports = domAPI; |
|---|