Skip to content

Fix whitespace handling for media query keywords in @media rules#208

Merged
bartveneman merged 2 commits into
mainfrom
claude/fix-media-query-minify-vzopy
May 16, 2026
Merged

Fix whitespace handling for media query keywords in @media rules#208
bartveneman merged 2 commits into
mainfrom
claude/fix-media-query-minify-vzopy

Conversation

@bartveneman
Copy link
Copy Markdown
Member

Summary

This PR fixes whitespace handling in @media rule prelude formatting to ensure necessary spaces are preserved between media query keywords (and, or, not, only) and their following elements (parentheses or media types).

Key Changes

  • Core fix in src/lib/index.ts: Added a new regex replacement rule in format_atrule_prelude() that enforces whitespace between media/supports keywords and opening parentheses: \b(and|or|not|only)\(/gi$1 (
  • Test coverage in test/api.test.ts: Updated existing test expectation and added three new test cases to verify correct spacing for and, not, and or keywords with parentheses, both in normal and minified modes
  • Comprehensive test coverage in test/atrules.test.ts: Added 8 new minification tests covering various media query scenarios:
    • @media all and screen (keyword-to-keyword spacing)
    • @media screen and (min-width: 100px) (keyword-to-feature spacing)
    • @media (min-width: 100px) and (max-width: 200px) (feature-to-feature spacing)
    • @media not (color) and @media not screen (not keyword spacing)
    • @media only screen (only keyword spacing)
    • @media (min-width: 100px) or (max-width: 200px) (or keyword spacing)

Implementation Details

The fix uses a word boundary regex pattern (\b) to specifically target media query keywords and ensures a space is inserted between the keyword and any following opening parenthesis. This prevents invalid CSS like and( or not( while maintaining proper minification by removing unnecessary spaces elsewhere in the prelude.

https://claude.ai/code/session_01FnNEbE8DRPAFamgWRW4B7L

CSS media query keywords (and, or, not, only) must be separated from
adjacent parentheses by whitespace. Without a space, `and(condition)` is
parsed as a function call rather than the keyword followed by a
media condition, making the at-rule invalid.

Add a replaceAll pass to insert the required space whenever one of
these keywords is directly followed by `(`, and expand test coverage
to include `@media all and screen {}` and related keyword combinator
patterns.

https://claude.ai/code/session_01FnNEbE8DRPAFamgWRW4B7L
@codecov
Copy link
Copy Markdown

codecov Bot commented May 16, 2026

Bundle Report

Changes will increase total bundle size by 46 bytes (0.27%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
formatCss-esm 14.4kB 46 bytes (0.32%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: formatCss-esm

Assets Changed:

Asset Name Size Change Total Size Change (%)
index.js 46 bytes 12.88kB 0.36%

Files in index.js:

  • ./src/lib/index.ts → Total Size: 12.41kB

@codecov
Copy link
Copy Markdown

codecov Bot commented May 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.80%. Comparing base (fcd6855) to head (b7ff003).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #208   +/-   ##
=======================================
  Coverage   96.80%   96.80%           
=======================================
  Files           2        2           
  Lines         344      344           
  Branches      124      124           
=======================================
  Hits          333      333           
  Misses         10       10           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bartveneman bartveneman merged commit 53000db into main May 16, 2026
6 checks passed
@bartveneman bartveneman deleted the claude/fix-media-query-minify-vzopy branch May 16, 2026 11:46
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.

2 participants