Fix preserving CSS custom properties during minification

This commit is contained in:
Joost de Valk
2026-09-03 07:15:21 +02:00
parent a3cacca368
commit 09a18e6d0f
2 changed files with 41 additions and 0 deletions

View File

@@ -592,6 +592,12 @@ class CssVariablesMinifierPlugin extends aCssMinifierPlugin
for ($i = 0, $l = count($m[0]); $i < $l; $i++)
{
$variable = trim($m[1][$i]);
// Native CSS custom properties are resolved by the browser and must
// not be treated as variables from CssMin's @variables syntax.
if (substr($variable, 0, 2) === "--")
{
continue;
}
foreach ($mediaTypes as $mediaType)
{
if (isset($this->variables[$mediaType], $this->variables[$mediaType][$variable]))

35
tests/Unit/CssMinTest.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class CssMinTest extends TestCase
{
public function testNativeCssCustomPropertiesArePreserved()
{
$css = ':root { --sidebar-width: 360px; } .sidebar { width: var(--sidebar-width); }';
$this->assertSame(
':root{--sidebar-width:360px}.sidebar{width:var(--sidebar-width)}',
\CssMin::minify($css)
);
}
public function testNativeCssCustomPropertiesInsideClampArePreserved()
{
$css = '.sidebar { width: clamp(280px, 25vw, var(--sidebar-width)); }';
$this->assertSame(
'.sidebar{width:clamp(280px, 25vw, var(--sidebar-width))}',
\CssMin::minify($css)
);
}
public function testLegacyCssMinVariablesAreStillResolved()
{
$css = '@variables { sidebarWidth: 360px; } .sidebar { width: var(sidebarWidth); }';
$this->assertSame('.sidebar{width:360px}', \CssMin::minify($css));
}
}