-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
css.js
136 lines (127 loc) · 3.67 KB
/
css.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
132
133
134
/*! litejs.com/MIT-LICENSE.txt */
exports.CSSStyleDeclaration = CSSStyleDeclaration
exports.CSSStyleSheet = CSSStyleSheet
var clearFn = (_, q, str) => q ? (q == "\"" && str.indexOf("'") == -1 ? "'" + str + "'" : _) :
_.trim().replace(/[\t\n]/g, " ")
.replace(/ *([,;{}>~+\/]) */g, "$1")
.replace(/^ +|;(?=})/g, "")
.replace(/: +/g, ":")
, clear = s => s.replace(/(["'])((?:\\\1|.)*?)\1|[^"']+/g, clearFn).replace(/url\(("|')([^'"()\s]+)\1\)/g, "url($2)")
, styleHandler = {
get(style, prop) {
if (prop === "cssText") {
for (var out = [], i = style.length; i--; ) {
out[i] = style[i] + ":" + (style.__[i] || style[style[i]])
}
return out.join(";")
}
return style[prop] || ""
},
set(style, prop, val) {
if (prop === "cssText") {
var m, k
, re = /(?:^|;)\s*([-a-z]+)\s*:((?:("|')(?:\\.|(?!\3)[^\\])*?\3|[^"';])+)(?=;|$)/ig
, len = 0
, lastIdx = {}
for (; (m = re.exec(val)); ) {
k = m[1]
if (lastIdx[k] >= 0) style.__[lastIdx[k]] = style[k]
style[style[lastIdx[k] = len++] = k] = style[
k === "float" ? "cssFloat" : k.replace(/-([a-z])/g, (_, a) => a.toUpperCase())
] = clear(m[2])
}
style.length = len
} else {
if (!style[prop]) style[style.length++] = prop
style[prop] = style[prop === "cssFloat" ? "float" : prop.replace(/[A-Z]/g, "-$&").toLowerCase()] = clear(val)
}
}
}
, ruleTypes = {
style: {
get cssText() {
return this.style.length > 0 ? this.selectorText + "{" + this.style.cssText + "}" : ""
},
set cssText(text) {
var idx = text.indexOf("{")
this.selectorText = text.slice(0, idx).trim()
this.style = CSSStyleDeclaration(text.slice(idx + 1).replace(/}\s*/, ""), this)
}
},
import: {
get cssText() {
return clear(this.text)
},
set cssText(text) {
this.text = clear(text)
}
},
"}": {
get cssText() {
var body = "" + this.style
return body.length > 0 ? this.mediaText + "{\n" + body + "\n}" : ""
},
set cssText(text) {
var idx = text.indexOf("{")
this.mediaText = clear(text.slice(0, idx).trim())
this.style = new CSSStyleSheet({})
this.style.replaceSync(text.slice(idx + 1, -1))
}
},
";": {
get cssText() {
return clear(this.text)
},
set cssText(text) {
this.text = clear(text)
}
}
}
function CSSRule(text, parentStyleSheet, atType, parentRule = null) {
// Clear comments and trim
text = text.replace(/^\s+|\/\*[^*]*\*+([^/*][^*]*\*+)*\/|\s+$/g, "")
var type = text[0] === "@" && text.slice(1, text.indexOf(" ")) || "style"
, rule = Object.create(ruleTypes[type] || ruleTypes[type === "font-face" || type === "counter-style" ? "style" : atType])
rule.cssText = text
rule.parentStyleSheet = parentStyleSheet
rule.parentRule = parentRule
return rule
}
function CSSStyleDeclaration(text, parentRule = null) {
var style = new Proxy({__: {}, parentRule}, styleHandler)
style.cssText = text
return style
}
function CSSStyleSheet(opts) {
Object.assign(this, opts)
this.replaceSync(this.ownerNode ? this.ownerNode.textContent : "")
}
CSSStyleSheet.prototype = {
disabled: false,
type: "text/css",
deleteRule(idx) {
this.rules.splice(idx, 1)
},
insertRule(rule, idx) {
this.rules.splice(idx > -1 ? idx : this.rules.length, 0, CSSRule(rule, this))
},
replaceSync(text) {
var m
, sheet = this
, re = /((?:("|')(?:\\.|(?!\2)[^\\])*?\2|[^"'}{;])+)|./ig
, block = 0
, pos = 0
sheet.rules = sheet.cssRules = []
sheet.warnings = []
for (; (m = re.exec(text)); ) {
if (m[0] === "{") {
block++
} else if (m[0] === ";" && block === 0 || m[0] === "}" && --block < 1) {
sheet.rules.push(CSSRule(text.slice(pos, pos = re.lastIndex), sheet, m[0]))
}
}
},
toString() {
return this.rules.map(rule => rule.cssText).filter(Boolean).join("\n")
}
}