Skip to content

Commit

Permalink
Prevent minification of 8 character hex to 3 characters, instead mini…
Browse files Browse the repository at this point in the history
…fy to 4 characters
  • Loading branch information
bennothommo committed Jul 5, 2024
1 parent 75cc8af commit 1249d92
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Assetic/Filter/StylesheetMinifyFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Assetic\Filter;

use Assetic\Asset\AssetInterface;
use Assetic\Contracts\Asset\AssetInterface;

/**
* Stylesheet Minify Filter
Expand Down Expand Up @@ -52,8 +52,11 @@ protected function minify($css)
// Strips units if value is 0 (converts 0px to 0), ignores values inside ()
$css = preg_replace('/(:| )(\.?)0(em|ex|px|in|cm|mm|pt|pc)(?![^\(]*\))/i', '${1}0', $css);

// Shorten 8-character hex color codes to 4-character where possible
$css = preg_replace('/#([a-f0-9])\\1([a-f0-9])\\2([a-f0-9])\\3([a-f0-9])\\4/i', '#\1\2\3\4', $css);

// Shortern 6-character hex color codes to 3-character where possible
$css = preg_replace('/#([a-f0-9])\\1([a-f0-9])\\2([a-f0-9])\\3/i', '#\1\2\3', $css);
$css = preg_replace('/#([a-f0-9])\\1([a-f0-9])\\2([a-f0-9])\\3(?![a-f0-9])/i', '#\1\2\3', $css);

return trim($css);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Assetic/Test/Filter/StylesheetMinifyFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,15 @@ public function testAttributeSelectorsWithLess()

$this->assertEquals($output, $mockAsset->getContent());
}

public function testHexMinification()
{
$asset = new \Assetic\Asset\StringAsset('body { color: #00000088; background: #ffffff; border: #012345; box-shadow: 0 0 0 1px #01234567; }');
$asset->load();

$filter = new StylesheetMinifyFilter();
$filter->filterDump($asset);

$this->assertEquals('body{color:#0008;background:#fff;border:#012345;box-shadow:0 0 0 1px #01234567}', $asset->getContent());
}
}

0 comments on commit 1249d92

Please sign in to comment.