Skip to content

Commit 298943f

Browse files
committed
CSS: properly test for equal values, fixes #673
1 parent 1508f98 commit 298943f

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

css/css.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,21 @@ func (t Token) String() string {
8181
if len(t.Args) == 0 {
8282
return t.TokenType.String() + "(" + string(t.Data) + ")"
8383
}
84-
return fmt.Sprint(t.Args)
84+
85+
sb := strings.Builder{}
86+
sb.Write(t.Data)
87+
for _, arg := range t.Args {
88+
sb.WriteString(arg.String())
89+
}
90+
sb.WriteByte(')')
91+
return sb.String()
8592
}
8693

8794
// Equal returns true if both tokens are equal.
8895
func (t Token) Equal(t2 Token) bool {
8996
if t.TokenType == t2.TokenType && bytes.Equal(t.Data, t2.Data) && len(t.Args) == len(t2.Args) {
9097
for i := 0; i < len(t.Args); i++ {
91-
if t.Args[i].TokenType != t2.Args[i].TokenType || !bytes.Equal(t.Args[i].Data, t2.Args[i].Data) {
98+
if !t.Args[i].Equal(t2.Args[i]) {
9299
return false
93100
}
94101
}
@@ -405,7 +412,7 @@ func (c *cssMinifier) minifyDeclaration(property []byte, components []css.Token)
405412
}
406413

407414
values = c.minifyTokens(prop, 0, values)
408-
if len(values) > 0 {
415+
if 0 < len(values) {
409416
values = c.minifyProperty(prop, values)
410417
}
411418
c.writeDeclaration(values, important)

css/css_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func TestCSS(t *testing.T) {
7676

7777
// bugs
7878
{"a{@media screen and (min-width:1024px){ width: 40%; } & h1 { font-size: clamp(2.5rem, 1rem + 3vw, 3.5rem)}}", "a{@media screen and (min-width:1024px){width: 40%;}& h1 { font-size: clamp(2.5rem, 1rem + 3vw, 3.5rem)}}"}, // #602
79+
{"a{padding:calc(var(--dce-edge-xsmall,6px) - 2px) calc(var(--dce-button-horizontal-padding,18px) - 2px)}", "a{padding:calc(var(--dce-edge-xsmall,6px) - 2px)calc(var(--dce-button-horizontal-padding,18px) - 2px)}"}, // #673
7980
}
8081

8182
m := minify.New()
@@ -90,7 +91,7 @@ func TestCSS(t *testing.T) {
9091

9192
// coverage
9293
test.T(t, Token{css.IdentToken, []byte("data"), nil, 0, 0}.String(), "Ident(data)")
93-
test.T(t, Token{css.FunctionToken, nil, []Token{{css.IdentToken, []byte("data"), nil, 0, 0}}, 0, 0}.String(), "[Ident(data)]")
94+
test.T(t, Token{css.FunctionToken, []byte("func("), []Token{{css.IdentToken, []byte("data"), nil, 0, 0}}, 0, 0}.String(), "func(Ident(data))")
9495
}
9596

9697
func TestCSSInline(t *testing.T) {

0 commit comments

Comments
 (0)