-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (57 loc) · 1.75 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
module.exports = wrap
function wrap(options) {
options = options || {}
return function(stylesheet) {
var rules = stylesheet.rules
stylesheet.rules = rules.reduce(function(list, rule) {
if (rule.type !== 'document') return concat(list, rule)
var wrapper = getWrapper(rule.document)
if (!wrapper) return concat(list, rule)
var amperstand = wrapper.indexOf('&')
if (amperstand === -1) {
return concat(list, rule.rules.map(function(rule) {
rule.selectors = rule.selectors.map(function(selector) {
if (selector.indexOf('&') !== -1)
return selector.replace(/\&/g, wrapper)
return wrapper + ' ' + selector
})
return rule
}))
}
var prefix = wrapper.slice(0, amperstand)
var suffix = wrapper.slice(amperstand + 1)
var append = rule.rules.map(function(rule) {
if (prefix) {
rule.selectors = rule.selectors.map(function(sel) {
// special case for prefixing either classes/ids
// or elements
var element = !/[.#]/.test(sel[0])
return element
? prefix.replace(/[-_]+$/g, ' ') + sel
: prefix + sel.slice(1)
})
}
if (suffix) {
rule.selectors = rule.selectors.map(function(sel) {
return sel + suffix
})
}
return rule
})
return concat(list, append)
}, [])
return stylesheet
}
}
function concat(parent, child) {
if (Array.isArray(child)) {
Array.prototype.push.apply(parent, child)
} else {
parent.push(child)
}
return parent
}
function getWrapper(document) {
var match = document.match(/^\s*wrap\(([^)]+)\)/)
if (match) return match[1]
}