Skip to content

fix(css): keep the descendant combinator before a pseudo-class when minifying - #5451

Open
breken-ai wants to merge 1 commit into
honojs:mainfrom
breken-ai:fix/css-minify-descendant-pseudo
Open

breken-ai wants to merge 1 commit into
honojs:mainfrom
breken-ai:fix/css-minify-descendant-pseudo

Conversation

@breken-ai

Copy link
Copy Markdown

The CSS minifier removes whitespace on both sides of :. In a declaration that is fine, but in a selector the whitespace before a pseudo-class is the descendant combinator. Removing it changes which element the rule matches:

const cls = css`
  & :first-child {
    margin: 0;
  }
  .a :is(h1, h2) {
    color: red;
  }
`
// before: &:first-child{margin:0}.a:is(h1,h2){color:red}   (matches the element itself)
// after:  & :first-child{margin:0}.a :is(h1,h2){color:red}  (matches its descendants)

With this change the minifier removes only the whitespace after :. A single space before : is kept, and longer runs still collapse to one space as before. The minified output for the usual color: red and (min-width: 600px) does not change. Only the rare color : red style now keeps one space (color :red), which is still valid CSS.

The new preserve descendant combinator before pseudo-class case in src/helper/css/index.test.tsx fails on main with .css-123{&:first-child{margin:0}&:hover:is(h1,h2){color:red}} and passes with this change. vp test --project main passes (123 files), and tsc -p tsconfig.spec.json and vp check are clean.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • pnpm run check:fix to format and lint the code
  • Add TSDoc/JSDoc to document the code (no public API change)

🤖 Generated with Claude Code

…inifying

The minifier removed whitespace on both sides of ":", so in a nested
selector the space that means "descendant" was lost:

  & :first-child { margin: 0 }   ->  &:first-child{margin:0}
  .a :is(h1, h2) { color: red }  ->  .a:is(h1,h2){color:red}

That rule then matches the element itself instead of its descendants.
Only the whitespace after ":" is removed now.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant