-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
205 lines (167 loc) · 5.6 KB
/
index.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import jsincss from 'https://unpkg.com/jsincss/index.vanilla.js'
export default function(
plugins = {}
) {
plugins = Object.assign(
{
stylesheet: {},
rule: {}
},
plugins
)
const foundRules = []
// For each stylesheet in the CSSOM
Array.from(document.styleSheets)
// Stylesheet must have accessible cssRules
// Note: this excludes stylesheets inaccessible to JS due to CORS
.filter(stylesheet => {
try {
stylesheet.cssRules
return true
}
catch (error) { }
})
.forEach(stylesheet =>
// For each rule in the stylesheet
Array.from(stylesheet.cssRules).forEach(rule => {
// If JS-powered style rule
if (rule.type === 1 && rule.selectorText.includes('[--')) {
// selector[]
const selector = /(.*)\[--.+\]/.test(rule.selectorText)
&& rule.selectorText.match(/(.*)\[--.+\]/)[1]
.replace(/([>~+]|\|\|)\s*$/, '$1 *')
.replace(/\\(?![0-9a-fA-F\n]{1,6}|\n|$)/g, '')
|| '*'
// [plugin]
const plugin = rule.selectorText
.replace(/.*\[--([^=]+).*\]/, '$1')
.replace(/-([a-z])/g, (string, match) => match.toUpperCase())
// If we have a rule plugin with the same name
if (plugins.rule[plugin]) {
// [="(args)"]
const args = /.*\[--.+="(.*)"\]/.test(rule.selectorText)
&& JSON.parse(
'['
+ rule.selectorText
.match(/.*\[--.+="(.*)"\]/)[1]
.replace(/\\"/g, '"')
+ ']'
)
|| ''
// { declarations }
const declarations = rule.cssText
.substring(rule.selectorText.length)
.trim()
.slice(1, -1)
.trim()
// If rule defines custom --selector and --events properties
if (
[
'--selector',
'--events'
].every(prop => Array.from(rule.style).includes(prop))
) {
const customSelector = rule.style.getPropertyValue('--selector').trim()
// Push a rule with custom events to output
jsincss(
event => plugins.rule[plugin](
selector,
...args,
declarations
),
(
customSelector === 'window'
? window
: customSelector.slice(1, -1)
),
JSON.parse(rule.style.getPropertyValue('--events'))
)
} else {
// Otherwise push a rule to output
foundRules.push(
event => plugins.rule[plugin](
selector,
...args,
declarations
)
)
}
}
// If JS-powered @supports rule
} else if (
rule.type === 12
&& rule.conditionText.includes('--')
) {
// plugin()
const plugin = rule.conditionText
.replace(/\(*--([^(]+)\(.+\)\)*/, '$1')
.replace(/-([a-z])/g, (string, match) => match.toUpperCase())
// If we have an at-rule plugin with the same name
if (plugins.stylesheet[plugin]) {
// (args)
const args = /--[^(]+(.*)/.test(rule.conditionText)
&& JSON.parse(
'['
+ rule.conditionText
.replace(/^\((.+)\)$/g, '$1')
.replace(/^[^(]*\((.*)\)$/, '$1')
+ ']'
)
|| ''
// { body }
const body = rule.cssText
.substring(`@supports `.length + rule.conditionText.length)
.trim()
.slice(1, -1)
// If group body rule contains a top-level rule for [--options]
// And that rule contains custom --selector and --events properties
if (
Array.from(rule.cssRules).find(rule => rule.selectorText === '[--options]')
&& [
'--selector',
'--events'
].every(prop =>
Array.from(rule.cssRules)
.reverse()
.find(rule => rule.selectorText === '[--options]')
.style
.getPropertyValue(prop) !== null
)
) {
const props = Array.from(rule.cssRules)
.reverse()
.find(rule => rule.selectorText === '[--options]')
.style
const customSelector = props.getPropertyValue('--selector').trim()
// Push a stylesheet with custom events to output
jsincss(
event => plugins.stylesheet[plugin](
...args,
body.trim().replace(/\n/g, '\n ')
),
(
customSelector === 'window'
? window
: customSelector.slice(1, -1)
),
JSON.parse(props.getPropertyValue('--events'))
)
} else {
// Otherwise push a stylesheet to output
foundRules.push(
event => plugins.stylesheet[plugin](
...args,
body.trim().replace(/\n/g, '\n ')
)
)
}
}
}
})
)
return jsincss(event =>
foundRules
.map(func => func(event))
.join('')
)
}