-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
css_nesting.go
333 lines (287 loc) · 11 KB
/
css_nesting.go
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package css_parser
import (
"github.com/evanw/esbuild/internal/css_ast"
"github.com/evanw/esbuild/internal/css_lexer"
)
func lowerNestingInRule(rule css_ast.Rule, results []css_ast.Rule) []css_ast.Rule {
switch r := rule.Data.(type) {
case *css_ast.RSelector:
// Filter out pseudo elements because they are ignored by nested style
// rules. This is because pseudo-elements are not valid within :is():
// https://www.w3.org/TR/selectors-4/#matches-pseudo. This restriction
// may be relaxed in the future, but this restriction hash shipped so
// we're stuck with it: https://github.com/w3c/csswg-drafts/issues/7433.
selectors := r.Selectors
n := 0
for _, sel := range selectors {
if !sel.UsesPseudoElement() {
selectors[n] = sel
n++
}
}
selectors = selectors[:n]
// Emit this selector before its nested children
start := len(results)
results = append(results, rule)
// Lower all children and filter out ones that become empty
context := lowerNestingContext{
parentSelectors: selectors,
loweredRules: results,
}
r.Rules = lowerNestingInRulesAndReturnRemaining(r.Rules, &context)
// Omit this selector entirely if it's now empty
if len(r.Rules) == 0 {
copy(context.loweredRules[start:], context.loweredRules[start+1:])
context.loweredRules = context.loweredRules[:len(context.loweredRules)-1]
}
return context.loweredRules
case *css_ast.RKnownAt:
var rules []css_ast.Rule
for _, child := range r.Rules {
rules = lowerNestingInRule(child, rules)
}
r.Rules = rules
case *css_ast.RAtLayer:
var rules []css_ast.Rule
for _, child := range r.Rules {
rules = lowerNestingInRule(child, rules)
}
r.Rules = rules
}
return append(results, rule)
}
// Lower all children and filter out ones that become empty
func lowerNestingInRulesAndReturnRemaining(rules []css_ast.Rule, context *lowerNestingContext) []css_ast.Rule {
n := 0
for _, child := range rules {
child = lowerNestingInRuleWithContext(child, context)
if child.Data != nil {
rules[n] = child
n++
}
}
return rules[:n]
}
type lowerNestingContext struct {
parentSelectors []css_ast.ComplexSelector
loweredRules []css_ast.Rule
}
func lowerNestingInRuleWithContext(rule css_ast.Rule, context *lowerNestingContext) css_ast.Rule {
switch r := rule.Data.(type) {
case *css_ast.RSelector:
// "a { & b {} }" => "a b {}"
// "a { &b {} }" => "a:is(b) {}"
// "a { &:hover {} }" => "a:hover {}"
// ".x { &b {} }" => "b.x {}"
// "a, b { .c, d {} }" => ":is(a, b) :is(.c, d) {}"
// "a, b { &.c, & d, e & {} }" => ":is(a, b).c, :is(a, b) d, e :is(a, b) {}"
// Pass 1: Canonicalize and analyze our selectors
canUseGroupDescendantCombinator := true // Can we do "parent «space» :is(...selectors)"?
canUseGroupSubSelector := true // Can we do "parent«nospace»:is(...selectors)"?
for i := range r.Selectors {
sel := &r.Selectors[i]
// Inject the implicit "&" now for simplicity later on
if sel.IsRelative() {
sel.Selectors = append([]css_ast.CompoundSelector{{HasNestingSelector: true}}, sel.Selectors...)
}
// Are all children of the form "& «something»"?
if len(sel.Selectors) < 2 || !sel.Selectors[0].IsSingleAmpersand() {
canUseGroupDescendantCombinator = false
}
// Are all children of the form "&«something»"?
if first := sel.Selectors[0]; !first.HasNestingSelector || first.IsSingleAmpersand() {
canUseGroupSubSelector = false
}
}
// Try to apply simplifications for shorter output
if canUseGroupDescendantCombinator {
// "& a, & b {}" => "& :is(a, b) {}"
for i := range r.Selectors {
sel := &r.Selectors[i]
sel.Selectors = sel.Selectors[1:]
}
merged := multipleComplexSelectorsToSingleComplexSelector(r.Selectors)
merged.Selectors = append([]css_ast.CompoundSelector{{HasNestingSelector: true}}, merged.Selectors...)
r.Selectors = []css_ast.ComplexSelector{merged}
} else if canUseGroupSubSelector {
// "&a, &b {}" => "&:is(a, b) {}"
for i := range r.Selectors {
sel := &r.Selectors[i]
sel.Selectors[0].HasNestingSelector = false
}
merged := multipleComplexSelectorsToSingleComplexSelector(r.Selectors)
merged.Selectors[0].HasNestingSelector = true
r.Selectors = []css_ast.ComplexSelector{merged}
}
// Pass 2: Substitue "&" for the parent selector
for i := range r.Selectors {
complex := &r.Selectors[i]
results := make([]css_ast.CompoundSelector, 0, len(complex.Selectors))
parent := multipleComplexSelectorsToSingleComplexSelector(context.parentSelectors)
for _, compound := range complex.Selectors {
results = substituteAmpersandsInCompoundSelector(compound, parent, results)
}
complex.Selectors = results
}
// Lower all child rules using our newly substituted selector
context.loweredRules = lowerNestingInRule(rule, context.loweredRules)
return css_ast.Rule{}
case *css_ast.RKnownAt:
childContext := lowerNestingContext{parentSelectors: context.parentSelectors}
r.Rules = lowerNestingInRulesAndReturnRemaining(r.Rules, &childContext)
// "div { @media screen { color: red } }" "@media screen { div { color: red } }"
if len(r.Rules) > 0 {
childContext.loweredRules = append([]css_ast.Rule{{Loc: rule.Loc, Data: &css_ast.RSelector{
Selectors: context.parentSelectors,
Rules: r.Rules,
}}}, childContext.loweredRules...)
}
// "div { @media screen { &:hover { color: red } } }" "@media screen { div:hover { color: red } }"
if len(childContext.loweredRules) > 0 {
r.Rules = childContext.loweredRules
context.loweredRules = append(context.loweredRules, rule)
}
return css_ast.Rule{}
case *css_ast.RAtLayer:
// Lower all children and filter out ones that become empty
childContext := lowerNestingContext{parentSelectors: context.parentSelectors}
r.Rules = lowerNestingInRulesAndReturnRemaining(r.Rules, &childContext)
// "div { @layer foo { color: red } }" "@layer foo { div { color: red } }"
if len(r.Rules) > 0 {
childContext.loweredRules = append([]css_ast.Rule{{Loc: rule.Loc, Data: &css_ast.RSelector{
Selectors: context.parentSelectors,
Rules: r.Rules,
}}}, childContext.loweredRules...)
}
// "div { @layer foo { &:hover { color: red } } }" "@layer foo { div:hover { color: red } }"
// "div { @layer foo {} }" => "@layer foo {}" (layers have side effects, so don't remove empty ones)
r.Rules = childContext.loweredRules
context.loweredRules = append(context.loweredRules, rule)
return css_ast.Rule{}
}
return rule
}
func substituteAmpersandsInCompoundSelector(sel css_ast.CompoundSelector, replacement css_ast.ComplexSelector, results []css_ast.CompoundSelector) []css_ast.CompoundSelector {
if sel.HasNestingSelector {
sel.HasNestingSelector = false
// Convert the replacement to a single compound selector
var single css_ast.CompoundSelector
if len(replacement.Selectors) == 1 || len(results) == 0 {
// ".foo { :hover & {} }" => ":hover .foo {}"
// ".foo .bar { &:hover {} }" => ".foo .bar:hover {}"
last := len(replacement.Selectors) - 1
results = append(results, replacement.Selectors[:last]...)
single = replacement.Selectors[last]
} else {
// ".foo .bar { :hover & {} }" => ":hover :is(.foo .bar) {}"
single = css_ast.CompoundSelector{
SubclassSelectors: []css_ast.SS{&css_ast.SSPseudoClass{
Name: "is",
Args: replacement.AppendToTokens(nil),
}},
}
}
var subclassSelectorPrefix []css_ast.SS
// Copy over the combinator, if any
sel.Combinator = single.Combinator
// Insert the type selector
if single.TypeSelector != nil {
if sel.TypeSelector != nil {
subclassSelectorPrefix = append(subclassSelectorPrefix, &css_ast.SSPseudoClass{
Name: "is",
Args: sel.TypeSelector.AppendToTokens(nil),
})
}
sel.TypeSelector = single.TypeSelector
}
// Insert the subclass selectors
subclassSelectorPrefix = append(subclassSelectorPrefix, single.SubclassSelectors...)
// Write the changes back
if len(subclassSelectorPrefix) > 0 {
sel.SubclassSelectors = append(subclassSelectorPrefix, sel.SubclassSelectors...)
}
}
// "div { :is(&.foo) {} }" => ":is(div.foo) {}"
for _, ss := range sel.SubclassSelectors {
if class, ok := ss.(*css_ast.SSPseudoClass); ok {
class.Args = substituteAmpersandsInTokens(class.Args, replacement)
}
}
return append(results, sel)
}
func substituteAmpersandsInTokens(tokens []css_ast.Token, replacement css_ast.ComplexSelector) []css_ast.Token {
foundAmpersand := false
for _, t := range tokens {
if t.Children != nil {
*t.Children = substituteAmpersandsInTokens(*t.Children, replacement)
}
if t.Kind == css_lexer.TDelimAmpersand {
foundAmpersand = true
}
}
// We only need to allocate if we find an ampersand
if !foundAmpersand {
return tokens
}
var results []css_ast.Token
replacementTokens := replacement.AppendToTokens(nil)
for _, t := range tokens {
if t.Kind != css_lexer.TDelimAmpersand {
results = append(results, t)
continue
}
insert := replacementTokens
// Try to avoid generating a bad substitution when "&" is in a weird place.
// This is necessary because we're operating on a list of tokens instead of
// a fully-parsed AST. Here's an example:
//
// "div { :is(.foo&) {} }" => ":is(.foo:is(div))"
// "div { :is(#foo&) {} }" => ":is(#foo:is(div))"
// "div { :is([foo]&) {} }" => ":is([foo]:is(div))"
// "div { :is(:hover&) {} }" => ":is(:hover:is(div))"
// "div { :is(:is(.foo)&) {} }" => ":is(:is(.foo):is(div))"
//
// There are likely a lot of edge cases with this that aren't covered. But
// this is probably fine because a) who would write code like this and b)
// PostCSS's nesting transform doesn't handle these edge cases at all.
if len(results) > 0 && len(insert) > 0 {
if first := insert[0]; first.Kind == css_lexer.TIdent && (first.Whitespace&css_ast.WhitespaceBefore) == 0 {
if last := results[len(results)-1]; (last.Whitespace & css_ast.WhitespaceAfter) == 0 {
switch last.Kind {
case css_lexer.TIdent, css_lexer.TOpenBracket, css_lexer.TFunction:
insert = []css_ast.Token{
{Kind: css_lexer.TColon, Text: ":"},
{Kind: css_lexer.TFunction, Text: "is", Children: &replacementTokens},
}
}
}
}
}
results = append(results, insert...)
}
return results
}
// Turn the list of selectors into a single selector by wrapping lists
// without a single element with ":is(...)". Note that this may result
// in an empty ":is()" selector (which matches nothing).
func multipleComplexSelectorsToSingleComplexSelector(selectors []css_ast.ComplexSelector) css_ast.ComplexSelector {
if len(selectors) == 1 {
return selectors[0]
}
// This must be non-nil so we print ":is()" instead of ":is"
tokens := []css_ast.Token{}
for i, sel := range selectors {
if i > 0 {
tokens = append(tokens, css_ast.Token{Kind: css_lexer.TComma, Text: ",", Whitespace: css_ast.WhitespaceAfter})
}
tokens = sel.AppendToTokens(tokens)
}
return css_ast.ComplexSelector{
Selectors: []css_ast.CompoundSelector{{
SubclassSelectors: []css_ast.SS{&css_ast.SSPseudoClass{
Name: "is",
Args: tokens,
}},
}},
}
}