Skip to content

CSS minifier merges past a rule that sets the same property after an escaped or URL paren #973

Description

@gaborbernat

The CSS minifier merges a rule into an earlier one past a rule that sets the same property, so the cascade changes. Both cases keep color:red on b and fold the second a rule into the first, which lets green beat red on an element matching both selectors:

from turbohtml.clean import minify_css

minify_css("a{color:blue}b{c:d\\(;color:red}a{color:green}")
# 'a{color:blue;color:green}b{c:d\\(;color:red}'
minify_css("a{color:blue}b{c:url(x(y);color:red}a{color:green}")
# 'a{color:blue;color:green}b{c:url(x(y);color:red}'

Expected: each input comes back with the three rules in order, since b sets color between the two a rules.

The merge check reads property names back out of each rendered body with a character scan, while the parser splits declarations by token. An escaped \( is part of an ident token, and the ( inside url(x(y) belongs to the URL token, but the scan counts both as an open paren and never sees the ; that ends the declaration, so it misses color. #972 bounds the scan and fixes the [; and stray ) cases; these two need the names taken from the token stream, as tdewolff/parse, css-tree and lightningcss do.

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions