-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
230 lines (179 loc) · 5.94 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
var debug = require('debug')('rework-inherit')
exports = module.exports = function (options) {
return function inherit(style) {
return new Inherit(style, options || {})
}
}
exports.Inherit = Inherit
function Inherit(style, options) {
if (!(this instanceof Inherit))
return new Inherit(style, options);
options = options || {}
this.propertyRegExp = options.propertyRegExp
|| /^(inherit|extend)s?$/i
var rules = this.rules = style.rules
this.matches = {}
for (var i = 0; i < rules.length; i++) {
var rule = rules[i]
if (rule.rules) {
// Media queries
this.inheritMedia(rule)
if (!rule.rules.length) rules.splice(i--, 1);
} else if (rule.selectors) {
// Regular rules
this.inheritRules(rule)
if (!rule.declarations.length) rules.splice(i--, 1);
}
}
this.removePlaceholders()
}
Inherit.prototype.inheritMedia = function (mediaRule) {
var rules = mediaRule.rules
var query = mediaRule.media
for (var i = 0; i < rules.length; i++) {
var rule = rules[i]
if (!rule.selectors) continue;
var additionalRules = this.inheritMediaRules(rule, query)
if (!rule.declarations.length) rules.splice(i--, 1);
// I don't remember why I'm using apply here.
;[].splice.apply(rules, [i, 0].concat(additionalRules))
i += additionalRules.length
}
}
Inherit.prototype.inheritMediaRules = function (rule, query) {
var declarations = rule.declarations
var selectors = rule.selectors
var appendRules = []
for (var i = 0; i < declarations.length; i++) {
var decl = declarations[i]
// Could be comments
if (decl.type !== 'declaration') continue;
if (!this.propertyRegExp.test(decl.property)) continue;
decl.value.split(',').map(trim).forEach(function (val) {
// Should probably just use concat here
;[].push.apply(appendRules, this.inheritMediaRule(val, selectors, query));
}, this)
declarations.splice(i--, 1)
}
return appendRules
}
Inherit.prototype.inheritMediaRule = function (val, selectors, query) {
var matchedRules = this.matches[val] || this.matchRules(val)
var alreadyMatched = matchedRules.media[query]
var matchedQueryRules = alreadyMatched || this.matchQueryRule(val, query)
if (!matchedQueryRules.rules.length)
throw new Error('Failed to extend as media query from ' + val + '.');
debug('extend %j in @media %j with %j', selectors, query, val);
this.appendSelectors(matchedQueryRules, val, selectors)
return alreadyMatched
? []
: matchedQueryRules.rules.map(getRule)
}
Inherit.prototype.inheritRules = function (rule) {
var declarations = rule.declarations
var selectors = rule.selectors
for (var i = 0; i < declarations.length; i++) {
var decl = declarations[i]
// Could be comments
if (decl.type !== 'declaration') continue;
if (!this.propertyRegExp.test(decl.property)) continue;
decl.value.split(',').map(trim).forEach(function (val) {
this.inheritRule(val, selectors)
}, this)
declarations.splice(i--, 1)
}
}
Inherit.prototype.inheritRule = function (val, selectors) {
var matchedRules = this.matches[val] || this.matchRules(val)
if (!matchedRules.rules.length)
throw new Error('Failed to extend from ' + val + '.');
debug('extend %j with %j', selectors, val);
this.appendSelectors(matchedRules, val, selectors)
}
Inherit.prototype.matchQueryRule = function (val, query) {
var matchedRules = this.matches[val] || this.matchRules(val)
return matchedRules.media[query] = {
media: query,
rules: matchedRules.rules.map(function (rule) {
return {
selectors: rule.selectors,
declarations: rule.declarations,
rule: {
type: 'rule',
selectors: [],
declarations: rule.declarations
}
}
})
}
}
Inherit.prototype.matchRules = function (val) {
var matchedRules = this.matches[val] = {
rules: [],
media: {}
}
this.rules.forEach(function (rule) {
if (!rule.selectors) return;
var matchedSelectors = rule.selectors.filter(function (selector) {
return selector.match(replaceRegExp(val))
})
if (!matchedSelectors.length) return;
matchedRules.rules.push({
selectors: matchedSelectors,
declarations: rule.declarations,
rule: rule
})
})
return matchedRules
}
Inherit.prototype.appendSelectors = function (matchedRules, val, selectors) {
matchedRules.rules.forEach(function (matchedRule) {
// Selector to actually inherit
var selectorReference = matchedRule.rule.selectors
matchedRule.selectors.forEach(function (matchedSelector) {
;[].push.apply(selectorReference, selectors.map(function (selector) {
return replaceSelector(matchedSelector, val, selector)
}))
})
})
}
// Placeholders are not allowed in media queries
Inherit.prototype.removePlaceholders = function () {
var rules = this.rules
for (var i = 0; i < rules.length; i++) {
var selectors = rules[i].selectors
if (!selectors) continue;
for (var j = 0; j < selectors.length; j++) {
var selector = selectors[j]
if (~selector.indexOf('%')) selectors.splice(j--, 1);
}
if (!selectors.length) rules.splice(i--, 1);
}
}
function replaceSelector(matchedSelector, val, selector) {
return matchedSelector.replace(replaceRegExp(val), function (_, first, last) {
return first + selector + last
})
}
function isPlaceholder(val) {
return val[0] === '%';
}
function replaceRegExp(val) {
var expression = escapeRegExp(val) + '($|\\s|\\>|\\+|~|\\:|\\[)';
var expressionPrefix = '(^|\\s|\\>|\\+|~)';
if (isPlaceholder(val)) {
// We just want to match an empty group here to preserve the arguments we
// may be expecting in a RegExp match.
expressionPrefix = '()';
}
return new RegExp(expressionPrefix + expression, 'g');
}
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")
}
function trim(x) {
return x.trim()
}
function getRule(x) {
return x.rule
}