-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathscoper.js
131 lines (103 loc) · 3.45 KB
/
scoper.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* global exports */
function scoperInit() {
var style = document.createElement("style");
style.appendChild(document.createTextNode(""));
document.head.appendChild(style);
style.sheet.insertRule("body { visibility: hidden; }", 0);
}
function scoper(css, prefix) {
var re = new RegExp("([^\r\n,{}]+)(,(?=[^}]*{)|\s*{)", "g");
css = css.replace(re, function(g0, g1, g2) {
if (g1.match(/^\s*(@media|@.*keyframes|to|from|@font-face|1?[0-9]?[0-9])/)) {
return g1 + g2;
}
if (g1.match(/:scope/)) {
g1 = g1.replace(/([^\s]*):scope/, function(h0, h1) {
if (h1 === "") {
return "> *";
} else {
return "> " + h1;
}
});
}
g1 = g1.replace(/^(\s*)/, "$1" + prefix + " ");
return g1 + g2;
});
return css;
}
function scoperProcess() {
var styles = document.body.querySelectorAll("style[scoped]");
if (styles.length === 0) {
document.getElementsByTagName("body")[0].style.visibility = "visible";
return;
}
var head = document.head || document.getElementsByTagName("head")[0];
for (var i = 0; i < styles.length; i++) {
var style = styles[i];
var css = style.innerHTML;
if (css && (style.parentElement.nodeName !== "BODY")) {
var id = "scoper-" + i;
var prefix = "#" + id;
var wrapper = document.createElement("span");
wrapper.id = id;
var parent = style.parentNode;
var grandparent = parent.parentNode;
grandparent.replaceChild(wrapper, parent);
wrapper.appendChild(parent);
style.parentNode.removeChild(style);
var newstyle = document.createElement("style");
newstyle.setAttribute('data-scoped-style-for', id);
var csses = scoper(css, prefix);
if (newstyle.styleSheet){
newstyle.styleSheet.cssText = csses;
} else {
newstyle.appendChild(document.createTextNode(csses));
}
head.appendChild(newstyle);
}
}
document.getElementsByTagName("body")[0].style.visibility = "visible";
}
function scoperReset() {
var styles = document.head.querySelectorAll("style[data-scoped-style-for]");
for (var i = 0; i < styles.length; i++) {
var style = styles[i];
var wrapperElementId = style.getAttribute('data-scoped-style-for');
var wrapperEl = document.getElementById(wrapperElementId);
if(wrapperEl) { // Node may have disappeared, in that case nothing should happen
var css = style.innerHTML;
var resettedCss = css.replace("#"+wrapperElementId+" ", "");
var parent = wrapperEl.parentNode;
var targetEl = wrapperEl.childNodes[0];
parent.replaceChild(targetEl, wrapperEl);
var scopedStyle = document.createElement("style");
scopedStyle.setAttribute("scoped", "true");
if (scopedStyle.styleSheet){
scopedStyle.styleSheet.cssText = resettedCss;
} else {
scopedStyle.appendChild(document.createTextNode(resettedCss));
}
targetEl.appendChild(scopedStyle);
}
style.parentNode.removeChild(style);
}
}
function scoperRestart() {
scoperReset();
scoperProcess();
}
(function() {
"use strict";
if (typeof document === "undefined" || ("scoped" in document.createElement("style"))) {
return;
}
scoperInit();
if (document.readyState === "complete" || document.readyState === "loaded") {
scoperProcess();
} else {
document.addEventListener("DOMContentLoaded", scoperProcess);
}
}());
if (typeof exports !== "undefined") {
exports.scoper = scoper;
}