-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
index.js
205 lines (186 loc) · 4.95 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
let parser = require('postcss-selector-parser')
function parse (str, rule) {
let nodes
let saver = parser(parsed => {
nodes = parsed
})
try {
saver.processSync(str)
} catch (e) {
if (str.includes(':')) {
throw rule ? rule.error('Missed semicolon') : e
} else {
throw rule ? rule.error(e.message) : e
}
}
return nodes.at(0)
}
function replace (nodes, parent) {
let replaced = false
nodes.each(i => {
if (i.type === 'nesting') {
let clonedParent = parent.clone()
if (i.value !== '&') {
i.replaceWith(parse(i.value.replace('&', clonedParent.toString())))
} else {
i.replaceWith(clonedParent)
}
replaced = true
} else if (i.nodes) {
if (replace(i, parent)) {
replaced = true
}
}
})
return replaced
}
function selectors (parent, child) {
let result = []
parent.selectors.forEach(i => {
let parentNode = parse(i, parent)
child.selectors.forEach(j => {
let node = parse(j, child)
let replaced = replace(node, parentNode)
if (!replaced) {
node.prepend(parser.combinator({ value: ' ' }))
node.prepend(parentNode.clone())
}
result.push(node.toString())
})
})
return result
}
function pickComment (comment, after) {
if (comment && comment.type === 'comment') {
after.after(comment)
return comment
} else {
return after
}
}
function atruleChilds (rule, atrule, bubbling) {
let children = []
atrule.each(child => {
if (child.type === 'comment') {
children.push(child)
} else if (child.type === 'decl') {
children.push(child)
} else if (child.type === 'rule' && bubbling) {
child.selectors = selectors(rule, child)
} else if (child.type === 'atrule') {
atruleChilds(rule, child, bubbling)
}
})
if (bubbling) {
if (children.length) {
let clone = rule.clone({ nodes: [] })
for (let child of children) {
clone.append(child)
}
atrule.prepend(clone)
}
}
}
function pickDeclarations (selector, declarations, after, Rule) {
let parent = new Rule({
selector,
nodes: []
})
for (let declaration of declarations) {
parent.append(declaration)
}
after.after(parent)
return parent
}
function atruleNames (defaults, custom) {
let list = {}
for (let i of defaults) {
list[i] = true
}
if (custom) {
for (let i of custom) {
let name = i.replace(/^@/, '')
list[name] = true
}
}
return list
}
module.exports = (opts = {}) => {
let bubble = atruleNames(['media', 'supports'], opts.bubble)
let unwrap = atruleNames(
[
'document',
'font-face',
'keyframes',
'-webkit-keyframes',
'-moz-keyframes'
],
opts.unwrap
)
let preserveEmpty = opts.preserveEmpty
return {
postcssPlugin: 'postcss-nested',
Rule (rule, { Rule }) {
let unwrapped = false
let after = rule
let copyDeclarations = false
let declarations = []
rule.each(child => {
if (child.type === 'rule') {
if (declarations.length) {
after = pickDeclarations(rule.selector, declarations, after, Rule)
declarations = []
}
copyDeclarations = true
unwrapped = true
child.selectors = selectors(rule, child)
after = pickComment(child.prev(), after)
after.after(child)
after = child
} else if (child.type === 'atrule') {
if (declarations.length) {
after = pickDeclarations(rule.selector, declarations, after, Rule)
declarations = []
}
if (child.name === 'at-root') {
unwrapped = true
atruleChilds(rule, child, false)
let nodes = child.nodes
if (child.params) {
nodes = new Rule({ selector: child.params, nodes })
}
after.after(nodes)
after = nodes
child.remove()
} else if (bubble[child.name]) {
copyDeclarations = true
unwrapped = true
atruleChilds(rule, child, true)
after = pickComment(child.prev(), after)
after.after(child)
after = child
} else if (unwrap[child.name]) {
copyDeclarations = true
unwrapped = true
atruleChilds(rule, child, false)
after = pickComment(child.prev(), after)
after.after(child)
after = child
} else if (copyDeclarations) {
declarations.push(child)
}
} else if (child.type === 'decl' && copyDeclarations) {
declarations.push(child)
}
})
if (declarations.length) {
after = pickDeclarations(rule.selector, declarations, after, Rule)
}
if (unwrapped && preserveEmpty !== true) {
rule.raws.semicolon = true
if (rule.nodes.length === 0) rule.remove()
}
}
}
}
module.exports.postcss = true