Skip to content

Fix CSS minifier selector whitespace - #90

Merged
samdark merged 1 commit into
masterfrom
fix/css-minifier-selector-whitespace
Jul 4, 2026
Merged

Fix CSS minifier selector whitespace#90
samdark merged 1 commit into
masterfrom
fix/css-minifier-selector-whitespace

Conversation

@samdark

@samdark samdark commented Jul 4, 2026

Copy link
Copy Markdown
Member

Summary

  • Preserve required CSS whitespace between descendant/pseudo selectors and multi-token values during asset minification
  • Keep colon minification limited to spacing after colons so selectors such as .content :is(...) remain valid
  • Add regression coverage for selector and value whitespace

Verification

  • make build-docs
  • make test -- --filter AssetMinifierTest
  • make test -- tests/Unit/Build/AssetMinifierTest.php tests/Unit/Build/ThemeAssetCopierTest.php tests/Unit/Build/ContentAssetCopierTest.php tests/Unit/Build/AssetFileWriterTest.php
  • make test
  • Playwright inspection of docs header at 1440, 1024, 640, and 390 px

Summary by CodeRabbit

  • Bug Fixes
    • Improved CSS minification so spacing around selectors and values is preserved correctly, avoiding broken output in cases like advanced selectors and calculated values.
  • Tests
    • Added coverage for CSS minification edge cases to confirm whitespace is handled safely during compression.

Copilot AI review requested due to automatic review settings July 4, 2026 09:01
@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d4cfd9de-567b-4a5f-8142-486ac82ce2cd

📥 Commits

Reviewing files that changed from the base of the PR and between 2bafb6b and fc28887.

📒 Files selected for processing (2)
  • src/Build/AssetMinifier.php
  • tests/Unit/Build/AssetMinifierTest.php

📝 Walkthrough

Walkthrough

The CSS minification logic in AssetMinifier was modified to exclude colons from whitespace-stripping punctuation handling and add a dedicated regex for collapsing whitespace after colons. The needsCssSpace helper was simplified using str_contains checks, removing the isIdentifierChar helper. A corresponding test was added.

Changes

CSS Minification Whitespace Fix

Layer / File(s) Summary
Regex and spacing logic changes
src/Build/AssetMinifier.php
Excludes : from punctuation whitespace-removal regex, adds a new regex to trim whitespace after colons, and rewrites needsCssSpace with str_contains-based exclusions, removing isIdentifierChar.
Test coverage for whitespace preservation
tests/Unit/Build/AssetMinifierTest.php
Adds a test verifying minify() preserves whitespace in selectors/values including :has(...), :is(...), and calc(...).

Estimated code review effort: 2 (Simple) | ~10 minutes

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/css-minifier-selector-whitespace

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@samdark
samdark merged commit 7af33e9 into master Jul 4, 2026
6 of 8 checks passed
@samdark
samdark deleted the fix/css-minifier-selector-whitespace branch July 4, 2026 09:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the CSS minification logic to avoid breaking selector semantics and value parsing by preserving whitespace that is syntactically required (e.g., descendant combinators and multi-token values), and adds a regression test to cover these cases.

Changes:

  • Split CSS punctuation trimming so colon handling only removes whitespace after : (avoids breaking selectors like .content :is(...)).
  • Simplify whitespace-preservation logic in the CSS whitespace-collapser to retain necessary spaces more reliably.
  • Add PHPUnit regression coverage for selector/value whitespace preservation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Build/AssetMinifier.php Adjusts CSS whitespace/punctuation minification rules to preserve required selector/value whitespace.
tests/Unit/Build/AssetMinifierTest.php Adds regression coverage ensuring minification keeps required whitespace in selectors and multi-token values.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 40 to 43
$content = self::collapseCssWhitespace($content);
$content = (string) preg_replace('/\s*([{}:;,>~=\[\]])\s*/', '$1', $content);
$content = (string) preg_replace('/\s*([{};,>~=\[\]])\s*/', '$1', $content);
$content = (string) preg_replace('/:\s*/', ':', $content);
$content = (string) preg_replace('/;}/', '}', $content);
Comment on lines +54 to +73
public function testMinifiesCssWithoutBreakingSelectorOrValueWhitespace(): void
{
$css = <<<'CSS'
.site-header .container {
display: grid;
padding: 0 .125rem;
}

.container:has(.docs-layout) {
max-width: calc(100% - (var(--page-gutter) * 2));
}

.content :is(h1, h2) .header-anchor {
margin: 1rem 1.25rem;
}
CSS;

assertSame(
'.site-header .container{display:grid;padding:0 .125rem}.container:has(.docs-layout){max-width:calc(100% - (var(--page-gutter) * 2))}.content :is(h1,h2) .header-anchor{margin:1rem 1.25rem}',
AssetMinifier::minify('app.css', $css),
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