-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
css_decls.go
410 lines (373 loc) · 11.8 KB
/
css_decls.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package css_parser
import (
"github.com/evanw/esbuild/internal/compat"
"github.com/evanw/esbuild/internal/css_ast"
"github.com/evanw/esbuild/internal/css_lexer"
"github.com/evanw/esbuild/internal/logger"
)
func (p *parser) commaToken(loc logger.Loc) css_ast.Token {
t := css_ast.Token{
Loc: loc,
Kind: css_lexer.TComma,
Text: ",",
}
if !p.options.minifyWhitespace {
t.Whitespace = css_ast.WhitespaceAfter
}
return t
}
func expandTokenQuad(tokens []css_ast.Token, allowedIdent string) (result [4]css_ast.Token, ok bool) {
n := len(tokens)
if n < 1 || n > 4 {
return
}
// Don't do this if we encounter any unexpected tokens such as "var()"
for i := 0; i < n; i++ {
if t := tokens[i]; !t.Kind.IsNumeric() && (t.Kind != css_lexer.TIdent || allowedIdent == "" || t.Text != allowedIdent) {
return
}
}
result[0] = tokens[0]
if n > 1 {
result[1] = tokens[1]
} else {
result[1] = result[0]
}
if n > 2 {
result[2] = tokens[2]
} else {
result[2] = result[0]
}
if n > 3 {
result[3] = tokens[3]
} else {
result[3] = result[1]
}
ok = true
return
}
func compactTokenQuad(a css_ast.Token, b css_ast.Token, c css_ast.Token, d css_ast.Token, minifyWhitespace bool) []css_ast.Token {
tokens := []css_ast.Token{a, b, c, d}
if tokens[3].EqualIgnoringWhitespace(tokens[1]) {
if tokens[2].EqualIgnoringWhitespace(tokens[0]) {
if tokens[1].EqualIgnoringWhitespace(tokens[0]) {
tokens = tokens[:1]
} else {
tokens = tokens[:2]
}
} else {
tokens = tokens[:3]
}
}
for i := range tokens {
var whitespace css_ast.WhitespaceFlags
if !minifyWhitespace || i > 0 {
whitespace |= css_ast.WhitespaceBefore
}
if i+1 < len(tokens) {
whitespace |= css_ast.WhitespaceAfter
}
tokens[i].Whitespace = whitespace
}
return tokens
}
func (p *parser) processDeclarations(rules []css_ast.Rule) (rewrittenRules []css_ast.Rule) {
margin := boxTracker{key: css_ast.DMargin, keyText: "margin", allowAuto: true}
padding := boxTracker{key: css_ast.DPadding, keyText: "padding", allowAuto: false}
inset := boxTracker{key: css_ast.DInset, keyText: "inset", allowAuto: true}
borderRadius := borderRadiusTracker{}
rewrittenRules = make([]css_ast.Rule, 0, len(rules))
// Don't automatically generate the "inset" property if it's not supported
if p.options.unsupportedCSSFeatures.Has(compat.InsetProperty) {
inset.key = css_ast.DUnknown
inset.keyText = ""
}
for _, rule := range rules {
rewrittenRules = append(rewrittenRules, rule)
decl, ok := rule.Data.(*css_ast.RDeclaration)
if !ok {
continue
}
switch decl.Key {
case css_ast.DBackgroundColor,
css_ast.DBorderBlockEndColor,
css_ast.DBorderBlockStartColor,
css_ast.DBorderBottomColor,
css_ast.DBorderColor,
css_ast.DBorderInlineEndColor,
css_ast.DBorderInlineStartColor,
css_ast.DBorderLeftColor,
css_ast.DBorderRightColor,
css_ast.DBorderTopColor,
css_ast.DCaretColor,
css_ast.DColor,
css_ast.DColumnRuleColor,
css_ast.DFill,
css_ast.DFloodColor,
css_ast.DLightingColor,
css_ast.DOutlineColor,
css_ast.DStopColor,
css_ast.DStroke,
css_ast.DTextDecorationColor,
css_ast.DTextEmphasisColor:
if len(decl.Value) == 1 {
decl.Value[0] = p.lowerColor(decl.Value[0])
if p.options.minifySyntax {
t := decl.Value[0]
if hex, ok := parseColor(t); ok {
decl.Value[0] = p.mangleColor(t, hex)
}
}
}
case css_ast.DAnimation:
p.processAnimationShorthand(decl.Value)
case css_ast.DAnimationName:
if len(decl.Value) == 1 {
p.processAnimationName(&decl.Value[0])
}
case css_ast.DFont:
if p.options.minifySyntax {
decl.Value = p.mangleFont(decl.Value)
}
case css_ast.DFontFamily:
if p.options.minifySyntax {
if value, ok := p.mangleFontFamily(decl.Value); ok {
decl.Value = value
}
}
case css_ast.DFontWeight:
if len(decl.Value) == 1 && p.options.minifySyntax {
decl.Value[0] = p.mangleFontWeight(decl.Value[0])
}
case css_ast.DTransform:
if p.options.minifySyntax {
decl.Value = p.mangleTransforms(decl.Value)
}
case css_ast.DBoxShadow:
if p.options.minifySyntax {
decl.Value = p.mangleBoxShadows(decl.Value)
}
// Margin
case css_ast.DMargin:
if p.options.minifySyntax {
margin.mangleSides(rewrittenRules, decl, p.options.minifyWhitespace)
}
case css_ast.DMarginTop:
if p.options.minifySyntax {
margin.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxTop)
}
case css_ast.DMarginRight:
if p.options.minifySyntax {
margin.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxRight)
}
case css_ast.DMarginBottom:
if p.options.minifySyntax {
margin.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxBottom)
}
case css_ast.DMarginLeft:
if p.options.minifySyntax {
margin.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxLeft)
}
// Padding
case css_ast.DPadding:
if p.options.minifySyntax {
padding.mangleSides(rewrittenRules, decl, p.options.minifyWhitespace)
}
case css_ast.DPaddingTop:
if p.options.minifySyntax {
padding.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxTop)
}
case css_ast.DPaddingRight:
if p.options.minifySyntax {
padding.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxRight)
}
case css_ast.DPaddingBottom:
if p.options.minifySyntax {
padding.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxBottom)
}
case css_ast.DPaddingLeft:
if p.options.minifySyntax {
padding.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxLeft)
}
// Inset
case css_ast.DInset:
if p.options.unsupportedCSSFeatures.Has(compat.InsetProperty) {
if decls, ok := p.lowerInset(rule.Loc, decl); ok {
rewrittenRules = rewrittenRules[:len(rewrittenRules)-1]
for i := range decls {
rewrittenRules = append(rewrittenRules, decls[i])
if p.options.minifySyntax {
inset.mangleSide(rewrittenRules, decls[i].Data.(*css_ast.RDeclaration), p.options.minifyWhitespace, i)
}
}
break
}
}
if p.options.minifySyntax {
inset.mangleSides(rewrittenRules, decl, p.options.minifyWhitespace)
}
case css_ast.DTop:
if p.options.minifySyntax {
inset.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxTop)
}
case css_ast.DRight:
if p.options.minifySyntax {
inset.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxRight)
}
case css_ast.DBottom:
if p.options.minifySyntax {
inset.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxBottom)
}
case css_ast.DLeft:
if p.options.minifySyntax {
inset.mangleSide(rewrittenRules, decl, p.options.minifyWhitespace, boxLeft)
}
// Border radius
case css_ast.DBorderRadius:
if p.options.minifySyntax {
borderRadius.mangleCorners(rewrittenRules, decl, p.options.minifyWhitespace)
}
case css_ast.DBorderTopLeftRadius:
if p.options.minifySyntax {
borderRadius.mangleCorner(rewrittenRules, decl, p.options.minifyWhitespace, borderRadiusTopLeft)
}
case css_ast.DBorderTopRightRadius:
if p.options.minifySyntax {
borderRadius.mangleCorner(rewrittenRules, decl, p.options.minifyWhitespace, borderRadiusTopRight)
}
case css_ast.DBorderBottomRightRadius:
if p.options.minifySyntax {
borderRadius.mangleCorner(rewrittenRules, decl, p.options.minifyWhitespace, borderRadiusBottomRight)
}
case css_ast.DBorderBottomLeftRadius:
if p.options.minifySyntax {
borderRadius.mangleCorner(rewrittenRules, decl, p.options.minifyWhitespace, borderRadiusBottomLeft)
}
}
if prefixes, ok := p.options.cssPrefixData[decl.Key]; ok {
if (prefixes & compat.WebkitPrefix) != 0 {
rewrittenRules = p.insertPrefixedDeclaration(rewrittenRules, "-webkit-", rule.Loc, decl)
}
if (prefixes & compat.KhtmlPrefix) != 0 {
rewrittenRules = p.insertPrefixedDeclaration(rewrittenRules, "-khtml-", rule.Loc, decl)
}
if (prefixes & compat.MozPrefix) != 0 {
rewrittenRules = p.insertPrefixedDeclaration(rewrittenRules, "-moz-", rule.Loc, decl)
}
if (prefixes & compat.MsPrefix) != 0 {
rewrittenRules = p.insertPrefixedDeclaration(rewrittenRules, "-ms-", rule.Loc, decl)
}
if (prefixes & compat.OPrefix) != 0 {
rewrittenRules = p.insertPrefixedDeclaration(rewrittenRules, "-o-", rule.Loc, decl)
}
}
}
// Compact removed rules
if p.options.minifySyntax {
end := 0
for _, rule := range rewrittenRules {
if rule.Data != nil {
rewrittenRules[end] = rule
end++
}
}
rewrittenRules = rewrittenRules[:end]
}
return
}
func (p *parser) insertPrefixedDeclaration(rules []css_ast.Rule, prefix string, loc logger.Loc, decl *css_ast.RDeclaration) []css_ast.Rule {
keyText := prefix + decl.KeyText
// Don't insert a prefixed declaration if there already is one
for i := len(rules) - 2; i >= 0; i-- {
if prev, ok := rules[i].Data.(*css_ast.RDeclaration); ok && prev.Key == css_ast.DUnknown {
if prev.KeyText == keyText {
// We found a previous declaration with a matching prefixed property.
// The value is ignored, which matches the behavior of "autoprefixer".
return rules
}
if p, d := len(prev.KeyText), len(decl.KeyText); p > d && prev.KeyText[p-d-1] == '-' && prev.KeyText[p-d:] == decl.KeyText {
// Continue through a run of prefixed properties with the same name
continue
}
}
break
}
// Additional special cases for when the prefix applies
switch decl.Key {
case css_ast.DBackgroundClip:
// The prefix is only needed for "background-clip: text"
if len(decl.Value) != 1 || decl.Value[0].Kind != css_lexer.TIdent || decl.Value[0].Text != "text" {
return rules
}
case css_ast.DPosition:
// The prefix is only needed for "position: sticky"
if len(decl.Value) != 1 || decl.Value[0].Kind != css_lexer.TIdent || decl.Value[0].Text != "sticky" {
return rules
}
}
value := css_ast.CloneTokensWithoutImportRecords(decl.Value)
// Additional special cases for how to transform the contents
switch decl.Key {
case css_ast.DPosition:
// The prefix applies to the value, not the property
keyText = decl.KeyText
value[0].Text = "-webkit-sticky"
case css_ast.DUserSelect:
// The prefix applies to the value as well as the property
if prefix == "-moz-" && len(value) == 1 && value[0].Kind == css_lexer.TIdent && value[0].Text == "none" {
value[0].Text = "-moz-none"
}
}
// Overwrite the latest declaration with the prefixed declaration
rules[len(rules)-1] = css_ast.Rule{Loc: loc, Data: &css_ast.RDeclaration{
KeyText: keyText,
KeyRange: decl.KeyRange,
Value: value,
Important: decl.Important,
}}
// Re-add the latest declaration after the inserted declaration
rules = append(rules, css_ast.Rule{Loc: loc, Data: decl})
return rules
}
func (p *parser) lowerInset(loc logger.Loc, decl *css_ast.RDeclaration) ([]css_ast.Rule, bool) {
if tokens, ok := expandTokenQuad(decl.Value, ""); ok {
mask := ^css_ast.WhitespaceAfter
if p.options.minifyWhitespace {
mask = 0
}
for i := range tokens {
tokens[i].Whitespace &= mask
}
return []css_ast.Rule{
{Loc: loc, Data: &css_ast.RDeclaration{
KeyText: "top",
KeyRange: decl.KeyRange,
Key: css_ast.DTop,
Value: tokens[0:1],
Important: decl.Important,
}},
{Loc: loc, Data: &css_ast.RDeclaration{
KeyText: "right",
KeyRange: decl.KeyRange,
Key: css_ast.DRight,
Value: tokens[1:2],
Important: decl.Important,
}},
{Loc: loc, Data: &css_ast.RDeclaration{
KeyText: "bottom",
KeyRange: decl.KeyRange,
Key: css_ast.DBottom,
Value: tokens[2:3],
Important: decl.Important,
}},
{Loc: loc, Data: &css_ast.RDeclaration{
KeyText: "left",
KeyRange: decl.KeyRange,
Key: css_ast.DLeft,
Value: tokens[3:4],
Important: decl.Important,
}},
}, true
}
return nil, false
}