Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for supports and layers in @import statements #429

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class CSS extends Minify
*/
protected $maxImportSize = 5;

protected $maxSupportsNesting = 5;

/**
* @var string[] valid import extensions
*/
Expand Down Expand Up @@ -146,6 +148,18 @@ protected function combineImports($source, $content, $parents)
# (optional) trailing whitespace
\s*

# layer
(?P<layer>layer(\((?P<layerName>[^\)]+)\))?)?

# (optional) trailing whitespace
\s*

# supports
(?P<supports>supports'.str_repeat('\([^)(]*(?:', $this->maxSupportsNesting).'\([^)(]*\)'.str_repeat('[^)(]*)*\)', $this->maxSupportsNesting).')?

# (optional) trailing whitespace
\s*

# (optional) media statement(s)
(?P<media>[^;]*)

Expand Down Expand Up @@ -178,6 +192,18 @@ protected function combineImports($source, $content, $parents)
# (optional) trailing whitespace
\s*

# layer
(?P<layer>layer(\((?P<layerName>[^\)]+)\))?)?

# (optional) trailing whitespace
\s*

# supports
(?P<supports>supports'.str_repeat('\([^)(]*(?:', $this->maxSupportsNesting).'\([^)(]*\)'.str_repeat('[^)(]*)*\)', $this->maxSupportsNesting).')?

# (optional) trailing whitespace
\s*

# (optional) media statement(s)
(?P<media>[^;]*)

Expand Down Expand Up @@ -225,6 +251,18 @@ protected function combineImports($source, $content, $parents)
$minifier->setImportExtensions($this->importExtensions);
$importContent = $minifier->execute($source, $parents);

// check if this is only valid for certain layers (named or unnamed)
if (!empty($match['layerName'])) {
$importContent = '@layer ' . $match['layerName'] . '{' . $importContent . '}';
} elseif (!empty($match['layer'])) {
$importContent = '@layer{' . $importContent . '}';
}

// check if this is only valid for certain layers (named or unnamed)
if (!empty($match['supports'])) {
$importContent = '@' . $match['supports'] . '{' . $importContent . '}';
}

// check if this is only valid for certain media
if (!empty($match['media'])) {
$importContent = '@media ' . $match['media'] . '{' . $importContent . '}';
Expand Down
34 changes: 34 additions & 0 deletions tests/CSS/CSSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MatthiasMullie\Minify\Tests\CSS;

use MatthiasMullie\Minify\Tests\CompatTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* CSS minifier test case.
Expand All @@ -21,6 +22,7 @@ protected function getMinifier()
*
* @dataProvider dataProvider
*/
#[dataProvider('dataProvider')]
public function testMinify($input, $expected)
{
$minifier = $this->getMinifier();
Expand All @@ -34,6 +36,7 @@ public function testMinify($input, $expected)
*
* @dataProvider dataProviderPaths
*/
#[dataProvider('dataProviderPaths')]
public function testConvertRelativePath($source, $target, $expected)
{
$minifier = $this->getMinifier();
Expand Down Expand Up @@ -877,6 +880,37 @@ public static function dataProvider()
'a{color:rgba(var(--bs-link-color-rgb),var(--bs-link-opacity,1));text-decoration:none}a:hover{--bs-link-color-rgb:var(--bs-link-hover-color-rgb)}a:not([href]):not([class]),a:not([href]):not([class]):hover{color:inherit;text-decoration:none}',
);

// https://github.com/matthiasmullie/minify/issues/428
$tests[] = array(
__DIR__ . '/sample/combine_imports/index7.css',
'@layer;@layer{body{color:red}}'
);

$tests[] = array(
__DIR__ . '/sample/combine_imports/index8.css',
'@layer testLayer1,testLayer2;@layer testLayer1{body{color:blue}}@layer testLayer2{body{color:red}}'
);

$tests[] = array(
__DIR__ . '/sample/combine_imports/index9.css',
'@supports(((display:grid) and (selector(h2>p))) or (not (display:flex))){body{color:red}}'
);

$tests[] = array(
__DIR__ . '/sample/combine_imports/index10.css',
'@layer;@supports(((display:grid) and (selector(h2>p))) or (not (display:flex))){@layer{body{color:red}}}'
);

$tests[] = array(
__DIR__ . '/sample/combine_imports/index11.css',
'@layer testLayer1;@supports(((display:grid) and (selector(h2>p))) or (not (display:flex))){@layer testLayer1{body{color:red}}}'
);

$tests[] = array(
__DIR__ . '/sample/combine_imports/index12.css',
'@layer testLayer1;@media screen and (orientation:landscape){@supports(((display:grid) and (selector(h2>p))) or (not (display:flex))){@layer testLayer1{body{color:red}}}}'
);

return $tests;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/CSS/sample/combine_imports/import2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: blue;
}
2 changes: 2 additions & 0 deletions tests/CSS/sample/combine_imports/index10.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@layer;
@import url('import.css') layer supports(((display: grid) and (selector(h2 > p))) or (not (display: flex)));
2 changes: 2 additions & 0 deletions tests/CSS/sample/combine_imports/index11.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@layer testLayer1;
@import url('import.css') layer(testLayer1) supports(((display: grid) and (selector(h2 > p))) or (not (display: flex)));
2 changes: 2 additions & 0 deletions tests/CSS/sample/combine_imports/index12.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@layer testLayer1;
@import url('import.css') layer(testLayer1) supports(((display: grid) and (selector(h2 > p))) or (not (display: flex))) screen and (orientation: landscape);
2 changes: 2 additions & 0 deletions tests/CSS/sample/combine_imports/index7.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@layer;
@import url('import.css') layer;
3 changes: 3 additions & 0 deletions tests/CSS/sample/combine_imports/index8.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@layer testLayer1, testLayer2;
@import url('import2.css') layer(testLayer1);
@import url('import.css') layer(testLayer2);
1 change: 1 addition & 0 deletions tests/CSS/sample/combine_imports/index9.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import url('import.css') supports(((display: grid) and (selector(h2 > p))) or (not (display: flex)));
2 changes: 2 additions & 0 deletions tests/JS/JSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MatthiasMullie\Minify\Tests\JS;

use MatthiasMullie\Minify\Tests\CompatTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* JS minifier test case.
Expand Down Expand Up @@ -45,6 +46,7 @@ public function testAddFile()
*
* @dataProvider dataProvider
*/
#[dataProvider('dataProvider')]
public function testMinify($input, $expected)
{
$minifier = $this->getMinifier();
Expand Down