Skip to content

fix(css): keep whitespace before pseudo-classes and pseudo-elements - #2564

Open
Yahiro025 wants to merge 1 commit into
beautifier:mainfrom
Yahiro025:fix-css-whitespace-before-pseudo
Open

Yahiro025 wants to merge 1 commit into
beautifier:mainfrom
Yahiro025:fix-css-whitespace-before-pseudo

Conversation

@Yahiro025

@Yahiro025 Yahiro025 commented Nov 10, 2026 •

Copy link
Copy Markdown

Description

  • Source branch in your fork has meaningful name (not main)

Fixes Issue: #2563

Whitespace before a pseudo-class or pseudo-element is a descendant combinator, so removing it changes which elements a rule matches. The selector branch only looked for a literal space, so a newline or a tab was dropped:

css('a\n:hover { color: red }')       // 'a:hover { ... }'
css('ul\n\t::marker { color: red }')  // 'ul::marker { ... }'

The whitespace before the current token is already read into isAfterSpace, so the branch now keeps the space for any whitespace run. The existing literal-space lookup stays for whitespace consumed by an earlier token. Sass and Less parent references (&:hover) and existing single-space selectors are unchanged.

Tests are added to test/data/css/tests.js, which feeds the JS and Python suites. Without the change, the four new cases fail in both implementations (a:hover, ul::marker, h1:any-link, &:hover); with it they pass. The full JS source suite (39,301 JS / 4,847 CSS / 12,378 HTML tests) and the Python CSS suite pass.

Before Merge Checklist

  • JavaScript implementation
  • Python implementation (NA if HTML beautifier)
  • Added Tests to data file(s)
  • Added command-line option(s) (NA if
  • README.md documents new feature/option(s)

Whitespace before a pseudo-class or pseudo-element is a descendant
combinator, so removing it changes which elements a rule matches.
The selector branch only checked for a literal space, so a newline or
tab was dropped. Use the whitespace already read before the current
token and keep the existing space lookup for whitespace consumed by an
earlier token.

JS and Python implementations are updated together, with tests in the
shared CSS test data.

This branch has not been deployed

No deployments
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