forked from webpack-contrib/style-loader
/
styleDomAPI.js
47 lines (39 loc) · 1007 Bytes
/
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
/* istanbul ignore next */
function apply(style, options, obj) {
let css = obj.css;
const media = obj.media;
const sourceMap = obj.sourceMap;
if (media) {
style.setAttribute("media", media);
} else {
style.removeAttribute("media");
}
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, style, options.insertOptions);
}
function removeStyleElement(style) {
// istanbul ignore if
if (style.parentNode === null) {
return false;
}
style.parentNode.removeChild(style);
}
/* istanbul ignore next */
function domAPI(options) {
const style = options.insertStyleElement(options);
return {
update: (obj) => {
apply(style, options, obj);
},
remove: () => {
removeStyleElement(style);
},
};
}
module.exports = domAPI;