/
styleDomAPI.js
69 lines (53 loc) · 1.32 KB
/
styleDomAPI.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* istanbul ignore next */
function apply(styleElement, options, obj) {
let css = "";
if (obj.supports) {
css += `@supports (${obj.supports}) {`;
}
if (obj.media) {
css += `@media ${obj.media} {`;
}
const needLayer = typeof obj.layer !== "undefined";
if (needLayer) {
css += `@layer${obj.layer.length > 0 ? ` ${obj.layer}` : ""} {`;
}
css += obj.css;
if (needLayer) {
css += "}";
}
if (obj.media) {
css += "}";
}
if (obj.supports) {
css += "}";
}
const sourceMap = obj.sourceMap;
if (sourceMap && typeof btoa !== "undefined") {
css += `\n/*# sourceMappingURL=data:application/json;base64,${btoa(
unescape(encodeURIComponent(JSON.stringify(sourceMap)))
)} */`;
}
// For old IE
/* istanbul ignore if */
options.styleTagTransform(css, styleElement, options.options);
}
function removeStyleElement(styleElement) {
// istanbul ignore if
if (styleElement.parentNode === null) {
return false;
}
styleElement.parentNode.removeChild(styleElement);
}
/* istanbul ignore next */
function domAPI(options) {
const styleElement = options.insertStyleElement(options);
return {
update: (obj) => {
apply(styleElement, options, obj);
},
remove: () => {
removeStyleElement(styleElement);
},
};
}
module.exports = domAPI;