Skip to content

Commit

Permalink
CSS: properly test for equal values, fixes #673
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Feb 26, 2024
1 parent 1508f98 commit 298943f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 10 additions & 3 deletions css/css.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,21 @@ func (t Token) String() string {
if len(t.Args) == 0 {
return t.TokenType.String() + "(" + string(t.Data) + ")"
}
return fmt.Sprint(t.Args)

sb := strings.Builder{}
sb.Write(t.Data)
for _, arg := range t.Args {
sb.WriteString(arg.String())
}
sb.WriteByte(')')
return sb.String()
}

// Equal returns true if both tokens are equal.
func (t Token) Equal(t2 Token) bool {
if t.TokenType == t2.TokenType && bytes.Equal(t.Data, t2.Data) && len(t.Args) == len(t2.Args) {
for i := 0; i < len(t.Args); i++ {
if t.Args[i].TokenType != t2.Args[i].TokenType || !bytes.Equal(t.Args[i].Data, t2.Args[i].Data) {
if !t.Args[i].Equal(t2.Args[i]) {
return false
}
}
Expand Down Expand Up @@ -405,7 +412,7 @@ func (c *cssMinifier) minifyDeclaration(property []byte, components []css.Token)
}

values = c.minifyTokens(prop, 0, values)
if len(values) > 0 {
if 0 < len(values) {
values = c.minifyProperty(prop, values)
}
c.writeDeclaration(values, important)
Expand Down
3 changes: 2 additions & 1 deletion css/css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func TestCSS(t *testing.T) {

// bugs
{"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
{"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
}

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

// coverage
test.T(t, Token{css.IdentToken, []byte("data"), nil, 0, 0}.String(), "Ident(data)")
test.T(t, Token{css.FunctionToken, nil, []Token{{css.IdentToken, []byte("data"), nil, 0, 0}}, 0, 0}.String(), "[Ident(data)]")
test.T(t, Token{css.FunctionToken, []byte("func("), []Token{{css.IdentToken, []byte("data"), nil, 0, 0}}, 0, 0}.String(), "func(Ident(data))")
}

func TestCSSInline(t *testing.T) {
Expand Down

0 comments on commit 298943f

Please sign in to comment.