-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertCss.js
93 lines (75 loc) · 2.25 KB
/
insertCss.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*! Isomorphic Style Loader | MIT License | https://github.com/kriasoft/isomorphic-style-loader */
'use strict';
var inserted = {};
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
return String.fromCharCode("0x" + p1);
}));
}
function removeCss(ids) {
ids.forEach(function (id) {
if (--inserted[id] <= 0) {
var elem = document.getElementById(id);
if (elem) {
elem.parentNode.removeChild(elem);
}
}
});
}
function insertCss(styles, _temp) {
var _ref = _temp === void 0 ? {} : _temp,
_ref$replace = _ref.replace,
replace = _ref$replace === void 0 ? false : _ref$replace,
_ref$prepend = _ref.prepend,
prepend = _ref$prepend === void 0 ? false : _ref$prepend,
_ref$prefix = _ref.prefix,
prefix = _ref$prefix === void 0 ? 's' : _ref$prefix;
var ids = [];
for (var i = 0; i < styles.length; i++) {
var _styles$i = styles[i],
moduleId = _styles$i[0],
css = _styles$i[1],
media = _styles$i[2],
sourceMap = _styles$i[3];
var id = "" + prefix + moduleId + "-" + i;
ids.push(id);
if (inserted[id]) {
if (!replace) {
inserted[id]++;
continue;
}
}
inserted[id] = 1;
var elem = document.getElementById(id);
var create = false;
if (!elem) {
create = true;
elem = document.createElement('style');
elem.setAttribute('type', 'text/css');
elem.id = id;
if (media) {
elem.setAttribute('media', media);
}
}
var cssText = css;
if (sourceMap && typeof btoa === 'function') {
cssText += "\n/*# sourceMappingURL=data:application/json;base64," + b64EncodeUnicode(JSON.stringify(sourceMap)) + "*/";
cssText += "\n/*# sourceURL=" + sourceMap.file + "?" + id + "*/";
}
if ('textContent' in elem) {
elem.textContent = cssText;
} else {
elem.styleSheet.cssText = cssText;
}
if (create) {
if (prepend) {
document.head.insertBefore(elem, document.head.childNodes[0]);
} else {
document.head.appendChild(elem);
}
}
}
return removeCss.bind(null, ids);
}
module.exports = insertCss;
//# sourceMappingURL=insertCss.js.map