Skip to content

Commit

Permalink
switch css minify from in-place to a new array
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 25, 2023
1 parent a4e2a1e commit 33322d2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
54 changes: 28 additions & 26 deletions internal/css_parser/css_decls.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ func compactTokenQuad(a css_ast.Token, b css_ast.Token, c css_ast.Token, d css_a
return tokens
}

func (p *parser) processDeclarations(rules []css_ast.Rule) []css_ast.Rule {
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))

for i, rule := range rules {
for _, rule := range rules {
rewrittenRules = append(rewrittenRules, rule)
decl, ok := rule.Data.(*css_ast.RDeclaration)
if !ok {
continue
Expand Down Expand Up @@ -153,104 +155,104 @@ func (p *parser) processDeclarations(rules []css_ast.Rule) []css_ast.Rule {
// Margin
case css_ast.DMargin:
if p.options.MinifySyntax {
margin.mangleSides(rules, decl, i, p.options.MinifyWhitespace)
margin.mangleSides(rewrittenRules, decl, p.options.MinifyWhitespace)
}
case css_ast.DMarginTop:
if p.options.MinifySyntax {
margin.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxTop)
margin.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxTop)
}
case css_ast.DMarginRight:
if p.options.MinifySyntax {
margin.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxRight)
margin.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxRight)
}
case css_ast.DMarginBottom:
if p.options.MinifySyntax {
margin.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxBottom)
margin.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxBottom)
}
case css_ast.DMarginLeft:
if p.options.MinifySyntax {
margin.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxLeft)
margin.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxLeft)
}

// Padding
case css_ast.DPadding:
if p.options.MinifySyntax {
padding.mangleSides(rules, decl, i, p.options.MinifyWhitespace)
padding.mangleSides(rewrittenRules, decl, p.options.MinifyWhitespace)
}
case css_ast.DPaddingTop:
if p.options.MinifySyntax {
padding.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxTop)
padding.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxTop)
}
case css_ast.DPaddingRight:
if p.options.MinifySyntax {
padding.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxRight)
padding.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxRight)
}
case css_ast.DPaddingBottom:
if p.options.MinifySyntax {
padding.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxBottom)
padding.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxBottom)
}
case css_ast.DPaddingLeft:
if p.options.MinifySyntax {
padding.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxLeft)
padding.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxLeft)
}

// Inset
case css_ast.DInset:
if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MinifySyntax {
inset.mangleSides(rules, decl, i, p.options.MinifyWhitespace)
inset.mangleSides(rewrittenRules, decl, p.options.MinifyWhitespace)
}
case css_ast.DTop:
if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MinifySyntax {
inset.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxTop)
inset.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxTop)
}
case css_ast.DRight:
if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MinifySyntax {
inset.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxRight)
inset.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxRight)
}
case css_ast.DBottom:
if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MinifySyntax {
inset.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxBottom)
inset.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxBottom)
}
case css_ast.DLeft:
if !p.options.UnsupportedCSSFeatures.Has(compat.InsetProperty) && p.options.MinifySyntax {
inset.mangleSide(rules, decl, i, p.options.MinifyWhitespace, boxLeft)
inset.mangleSide(rewrittenRules, decl, p.options.MinifyWhitespace, boxLeft)
}

// Border radius
case css_ast.DBorderRadius:
if p.options.MinifySyntax {
borderRadius.mangleCorners(rules, decl, i, p.options.MinifyWhitespace)
borderRadius.mangleCorners(rewrittenRules, decl, p.options.MinifyWhitespace)
}
case css_ast.DBorderTopLeftRadius:
if p.options.MinifySyntax {
borderRadius.mangleCorner(rules, decl, i, p.options.MinifyWhitespace, borderRadiusTopLeft)
borderRadius.mangleCorner(rewrittenRules, decl, p.options.MinifyWhitespace, borderRadiusTopLeft)
}
case css_ast.DBorderTopRightRadius:
if p.options.MinifySyntax {
borderRadius.mangleCorner(rules, decl, i, p.options.MinifyWhitespace, borderRadiusTopRight)
borderRadius.mangleCorner(rewrittenRules, decl, p.options.MinifyWhitespace, borderRadiusTopRight)
}
case css_ast.DBorderBottomRightRadius:
if p.options.MinifySyntax {
borderRadius.mangleCorner(rules, decl, i, p.options.MinifyWhitespace, borderRadiusBottomRight)
borderRadius.mangleCorner(rewrittenRules, decl, p.options.MinifyWhitespace, borderRadiusBottomRight)
}
case css_ast.DBorderBottomLeftRadius:
if p.options.MinifySyntax {
borderRadius.mangleCorner(rules, decl, i, p.options.MinifyWhitespace, borderRadiusBottomLeft)
borderRadius.mangleCorner(rewrittenRules, decl, p.options.MinifyWhitespace, borderRadiusBottomLeft)
}
}
}

// Compact removed rules
if p.options.MinifySyntax {
end := 0
for _, rule := range rules {
for _, rule := range rewrittenRules {
if rule.Data != nil {
rules[end] = rule
rewrittenRules[end] = rule
end++
}
}
rules = rules[:end]
rewrittenRules = rewrittenRules[:end]
}

return rules
return
}
8 changes: 4 additions & 4 deletions internal/css_parser/css_decls_border_radius.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (borderRadius *borderRadiusTracker) updateCorner(rules []css_ast.Rule, corn
borderRadius.corners[corner] = new
}

func (borderRadius *borderRadiusTracker) mangleCorners(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, minifyWhitespace bool) {
func (borderRadius *borderRadiusTracker) mangleCorners(rules []css_ast.Rule, decl *css_ast.RDeclaration, minifyWhitespace bool) {
// Reset if we see a change in the "!important" flag
if borderRadius.important != decl.Important {
borderRadius.corners = [4]borderRadiusCorner{}
Expand Down Expand Up @@ -87,7 +87,7 @@ func (borderRadius *borderRadiusTracker) mangleCorners(rules []css_ast.Rule, dec
firstToken: t,
secondToken: t,
unitSafety: unitSafety,
ruleIndex: uint32(index),
ruleIndex: uint32(len(rules) - 1),
})
}

Expand All @@ -105,7 +105,7 @@ func (borderRadius *borderRadiusTracker) mangleCorners(rules []css_ast.Rule, dec
borderRadius.compactRules(rules, decl.KeyRange, minifyWhitespace)
}

func (borderRadius *borderRadiusTracker) mangleCorner(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, minifyWhitespace bool, corner int) {
func (borderRadius *borderRadiusTracker) mangleCorner(rules []css_ast.Rule, decl *css_ast.RDeclaration, minifyWhitespace bool, corner int) {
// Reset if we see a change in the "!important" flag
if borderRadius.important != decl.Important {
borderRadius.corners = [4]borderRadiusCorner{}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (borderRadius *borderRadiusTracker) mangleCorner(rules []css_ast.Rule, decl
firstToken: firstToken,
secondToken: secondToken,
unitSafety: unitSafety,
ruleIndex: uint32(index),
ruleIndex: uint32(len(rules) - 1),
wasSingleRule: true,
})
borderRadius.compactRules(rules, decl.KeyRange, minifyWhitespace)
Expand Down
8 changes: 4 additions & 4 deletions internal/css_parser/css_decls_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (box *boxTracker) updateSide(rules []css_ast.Rule, side int, new boxSide) {
box.sides[side] = new
}

func (box *boxTracker) mangleSides(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, minifyWhitespace bool) {
func (box *boxTracker) mangleSides(rules []css_ast.Rule, decl *css_ast.RDeclaration, minifyWhitespace bool) {
// Reset if we see a change in the "!important" flag
if box.important != decl.Important {
box.sides = [4]boxSide{}
Expand All @@ -116,7 +116,7 @@ func (box *boxTracker) mangleSides(rules []css_ast.Rule, decl *css_ast.RDeclarat
}
box.updateSide(rules, side, boxSide{
token: t,
ruleIndex: uint32(index),
ruleIndex: uint32(len(rules) - 1),
unitSafety: unitSafety,
})
}
Expand All @@ -126,7 +126,7 @@ func (box *boxTracker) mangleSides(rules []css_ast.Rule, decl *css_ast.RDeclarat
}
}

func (box *boxTracker) mangleSide(rules []css_ast.Rule, decl *css_ast.RDeclaration, index int, minifyWhitespace bool, side int) {
func (box *boxTracker) mangleSide(rules []css_ast.Rule, decl *css_ast.RDeclaration, minifyWhitespace bool, side int) {
// Reset if we see a change in the "!important" flag
if box.important != decl.Important {
box.sides = [4]boxSide{}
Expand All @@ -144,7 +144,7 @@ func (box *boxTracker) mangleSide(rules []css_ast.Rule, decl *css_ast.RDeclarati
}
box.updateSide(rules, side, boxSide{
token: t,
ruleIndex: uint32(index),
ruleIndex: uint32(len(rules) - 1),
wasSingleRule: true,
unitSafety: unitSafety,
})
Expand Down

0 comments on commit 33322d2

Please sign in to comment.