Skip to content

Commit

Permalink
Refactored minifier regex
Browse files Browse the repository at this point in the history
This now ignores comments that start with an exclamation point /* ! */

Lighten up some method args
  • Loading branch information
daftspunk committed Apr 15, 2021
1 parent 159f0c5 commit 8f07d31
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 40 deletions.
22 changes: 8 additions & 14 deletions src/Assetic/Filter/StylesheetMinify.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use October\Rain\Assetic\Filter\FilterInterface;

/**
* Minify CSS Filter
* StylesheetMinify CSS Filter
* Class used to compress stylesheet css files.
*
* @package october/parse
Expand All @@ -22,32 +22,29 @@ public function filterDump(AssetInterface $asset)
}

/**
* Minifies CSS
* minify CSS
* @var $css string CSS code to minify.
* @return string Minified CSS.
*/
protected function minify($css)
{
// Step 1: Special rule for Tailwind CSS
$css = str_replace('/*!*/ /*!*/', '___CSSMIN___SPACE___', $css);

// Normalize whitespace in a smart way
$css = preg_replace('/\s{2,}/', ' ', $css);

// Remove spaces before and after comment
$css = preg_replace('/(\s+)(\/\*(.*?)\*\/)(\s+)/', '$2', $css);
$css = preg_replace('/(\s+)(\/\*[^!](.*?)\*\/)(\s+)/', '$2', $css);

// Remove comment blocks, everything between /* and */
$css = preg_replace('#/\*.*?\*/#s', '', $css);
// Remove comment blocks, everything between /* and */, ignore /*! comments
$css = preg_replace('#/\*[^\!].*?\*/#s', '', $css);

// Remove ; before }
$css = preg_replace('/;(?=\s*})/', '', $css);

// Remove space after , : ; { } */ >
$css = preg_replace('/(,|:|;|\{|}|\*\/|>) /', '$1', $css);
// Remove space after , : ; { } */ >, but not after !*/
$css = preg_replace('/(,|:|;|\{|}|[^!]\*\/|>) /', '$1', $css);

// Remove space before , ; { } >
$css = preg_replace('/(,|;|\{|}|>)/', '$1', $css);
$css = preg_replace('/ (,|;|\{|}|>)/', '$1', $css);

// Remove newline before } >
$css = preg_replace('/(\r\n|\r|\n)(})/', '$2', $css);
Expand All @@ -59,9 +56,6 @@ protected function minify($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);

// Step 2: Special rule for Tailwind CSS
$css = str_replace('___CSSMIN___SPACE___', ' ', $css);

return trim($css);
}
}
7 changes: 2 additions & 5 deletions src/Html/BlockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function endBlock(bool $append = false)
/**
* set a content of the layout block.
*/
public function set(string $name, string $content)
public function set(string $name, $content)
{
$this->put($name);
echo $content;
Expand All @@ -84,7 +84,7 @@ public function set(string $name, string $content)
/**
* append a content of the layout block
*/
public function append(string $name, string $content)
public function append(string $name, $content)
{
if (!isset($this->blocks[$name])) {
$this->blocks[$name] = null;
Expand All @@ -95,9 +95,6 @@ public function append(string $name, string $content)

/**
* placeholder returns the layout block contents and deletes the block from memory.
* @param string $name Specifies the block name.
* @param string $default Specifies a default block value to use if the block requested is not exists.
* @return string
*/
public function placeholder(string $name, string $default = null): ?string
{
Expand Down
80 changes: 59 additions & 21 deletions tests/Assetic/StylesheetMinifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class StylesheetMinifyTest extends TestCase
{
public function testSpaceRemoval()
{
$input = 'body {width: calc(99.9% * 1/1 - 0px); height: 0px;}';
$output = 'body {width:calc(99.9% * 1/1 - 0px);height:0px}';
$input = 'body{width: calc(99.9% * 1/1 - 0px); height: 0px;}';
$output = 'body{width:calc(99.9% * 1/1 - 0px);height:0px}';

$mockAsset = new MockAsset($input);
$result = new StylesheetMinify();
Expand All @@ -20,22 +20,24 @@ public function testSpaceRemoval()

public function testEmptyClassPreserve()
{
$input = '.view { /*
* Text
*/
/*
* Links
*/
/*
* Table
*/
/*
* Table cell
*/
/*
* Images
*/ }';
$output = '.view {}';
$input = <<<CSS
.view { /*
* Text
*/
/*
* Links
*/
/*
* Table
*/
/*
* Table cell
*/
/*
* Images
*/ }
CSS;
$output = '.view{}';

$mockAsset = new MockAsset($input);
$result = new StylesheetMinify();
Expand All @@ -44,10 +46,22 @@ public function testEmptyClassPreserve()
$this->assertEquals($output, $mockAsset->getContent());
}

public function testSpecialCommentPreservation()
{
$input = 'body {/*! Keep me */}';
$output = 'body{/*! Keep me */}';

$mockAsset = new MockAsset($input);
$result = new StylesheetMinify();
$result->filterDump($mockAsset);

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

public function testCommentRemoval()
{
$input = 'body {/* First comment */} /* Second comment */';
$output = 'body {}';
$input = 'body{/* First comment */} /* Second comment */';
$output = 'body{}';

$mockAsset = new MockAsset($input);
$result = new StylesheetMinify();
Expand All @@ -59,7 +73,7 @@ public function testCommentRemoval()
public function testCommentPreservationInVar()
{
$input = '--ring-inset: var(--empty, /*!*/ /*!*/);';
$output = '--ring-inset:var(--empty, );';
$output = '--ring-inset:var(--empty,/*!*/ /*!*/);';

$mockAsset = new MockAsset($input);
$result = new StylesheetMinify();
Expand All @@ -79,4 +93,28 @@ public function testUnitPreservationInVar()

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

public function testAttributeSelectorsWithLess()
{
$input = <<<CSS
[class^="icon-"]:before,
[class*=" icon-"]:before {
speak: none;
}
/* makes the font 33% larger relative to the icon container */
.icon-large:before {
speak: initial;
}
CSS;

$output = <<<CSS
[class^="icon-"]:before,[class*=" icon-"]:before{speak:none}.icon-large:before{speak:initial}
CSS;

$mockAsset = new MockAsset($input);
$result = new StylesheetMinify();
$result->filterDump($mockAsset);

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

0 comments on commit 8f07d31

Please sign in to comment.