-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx-style-apply.js
38 lines (34 loc) · 1.04 KB
/
x-style-apply.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
(() => {
/**
* x-style plugin that implements @apply, extracting the css rules from the class
* defined in the current document and inserting then into the new css.
*/
var applyRE = /@apply ([^;}$]*);?/g;
var resolvedClasses = {};
var resolveClass = (className) => {
if (className[0] !== ".") className = `.${className}`;
if (!resolvedClasses[className]) {
const matchingRules = [];
for (const stylesheet of document.styleSheets) {
for (const rule of stylesheet.cssRules) {
if (rule.selectorText === className) {
matchingRules.push(rule);
}
}
}
return matchingRules
.map((rule) => {
const text = rule.cssText;
return text.slice(text.indexOf("{") + 1, text.lastIndexOf("}"));
})
.join(" ");
}
return resolvedClasses[className];
};
var doApply = (css) => {
return css.replace(applyRE, (match, p1) => {
return p1.split(" ").map(resolveClass).join(" ");
});
};
xstyle.pre.push(doApply);
})();