From 89edb0e754b72bfc8db573e7ffea952d4e0c21e3 Mon Sep 17 00:00:00 2001 From: obedparla Date: Thu, 5 Nov 2020 00:26:54 +0100 Subject: [PATCH 1/8] Add stripEmptyProperties and improve stripEmptyTags --- src/CSS.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/CSS.php b/src/CSS.php index 89fcf1bb..f46db863 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -310,6 +310,7 @@ public function execute($path = null, $parents = array()) $this->extractCalcs(); $css = $this->replace($css); + $css = $this->stripEmptyProperties($css); $css = $this->stripWhitespace($css); $css = $this->shortenColors($css); $css = $this->shortenZeroes($css); @@ -617,11 +618,27 @@ protected function shortenZeroes($content) protected function stripEmptyTags($content) { $content = preg_replace('/(?<=^)[^\{\};]+\{\s*\}/', '', $content); - $content = preg_replace('/(?<=(\}|;))[^\{\};]+\{\s*\}/', '', $content); + // Remove empty tags/selectors + $content = preg_replace('/(?<=(\}|;|\{))[^\{\};]+\{\s*\}/', '', $content); + // Run it again to remove empty media queries that had the empty selectors before + $content = preg_replace('/(?<=(\}|;|\{))[^\{\};]+\{\s*\}/', '', $content); return $content; } + /** + * Strip empty properties from source code. + * i.e: 'color: ;' + * + * @param string $content + * + * @return string + */ + protected function stripEmptyProperties($content) + { + return preg_replace('/[a-z-:()]*:\s*;/', '', $content); + } + /** * Strip comments from source code. */ From db1d0a6b9958173e945639a9aefe477a2cf89e9d Mon Sep 17 00:00:00 2001 From: obedparla Date: Thu, 5 Nov 2020 00:28:33 +0100 Subject: [PATCH 2/8] Add test for stripping empty properties and better empty tags --- tests/css/CSSTest.php | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/tests/css/CSSTest.php b/tests/css/CSSTest.php index 76f80aba..8b9ea23a 100644 --- a/tests/css/CSSTest.php +++ b/tests/css/CSSTest.php @@ -502,7 +502,7 @@ public function dataProvider() calc(100% - 15px) calc(1em + 2px), calc(100% - 2.5em) 0.5em; }', - '.cvp-live-filter select{background-position:calc(100% - 20px) calc(1em + 2px),calc(100% - 15px) calc(1em + 2px),calc(100% - 2.5em) .5em}', + '.cvp-live-filter select{background-position:calc(100% - 20px) calc(1em + 2px),calc(100% - 15px) calc(1em + 2px),calc(100% - 2.5em) .5em}', ); // https://github.com/matthiasmullie/minify/issues/301 @@ -800,6 +800,42 @@ public function dataProvider() 'ul p{padding-left:calc((var(--icon-size) / 2) + var(--horisontal-space))}', ); + $tests[] = array(' +body{ + background: white; +} + +.classWithEmptyProperties { + color: ; + font-size: ; +} + +@media(max-width: 576px) { + .emptyClass { + } + + .nonEmptyClass { + display: flex; + } +} + +@media(max-width: 850px) { + .emptyClassWithEmptyProperties { + color: ; + font-size: ; + } + + #emptyId { + } +} + +@media(max-width: 1580px) { + #emptyId { + } +} +', 'body{background:#fff}@media(max-width:576px){.nonEmptyClass{display:flex}}', + ); + return $tests; } From 921df08623ab517521522021dd05887ca391c3f8 Mon Sep 17 00:00:00 2001 From: obedparla Date: Thu, 5 Nov 2020 00:45:16 +0100 Subject: [PATCH 3/8] Improve the strip properties function and test --- src/CSS.php | 2 +- tests/css/CSSTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CSS.php b/src/CSS.php index f46db863..94e559ca 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -636,7 +636,7 @@ protected function stripEmptyTags($content) */ protected function stripEmptyProperties($content) { - return preg_replace('/[a-z-:()]*:\s*;/', '', $content); + return preg_replace('/[a-z-:().#]*:\s*;/', '', $content); } /** diff --git a/tests/css/CSSTest.php b/tests/css/CSSTest.php index 8b9ea23a..88706b00 100644 --- a/tests/css/CSSTest.php +++ b/tests/css/CSSTest.php @@ -806,8 +806,9 @@ public function dataProvider() } .classWithEmptyProperties { - color: ; + color::hover: ; font-size: ; + font-weight::hover:not(#id.class): ; } @media(max-width: 576px) { @@ -822,7 +823,6 @@ public function dataProvider() @media(max-width: 850px) { .emptyClassWithEmptyProperties { color: ; - font-size: ; } #emptyId { From 43f64ba4c1c70b767710a512a9c2fa17b0a77a8c Mon Sep 17 00:00:00 2001 From: obedparla Date: Thu, 5 Nov 2020 01:00:31 +0100 Subject: [PATCH 4/8] Add comments to stripEmptyTags --- src/CSS.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/CSS.php b/src/CSS.php index 94e559ca..93a3c85f 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -617,10 +617,11 @@ protected function shortenZeroes($content) */ protected function stripEmptyTags($content) { + // Remove empty tags/selectors at the start of the file $content = preg_replace('/(?<=^)[^\{\};]+\{\s*\}/', '', $content); - // Remove empty tags/selectors + // Remove empty tags/selectors (classes, ids and so on) $content = preg_replace('/(?<=(\}|;|\{))[^\{\};]+\{\s*\}/', '', $content); - // Run it again to remove empty media queries that had the empty selectors before + // Run it again to remove any empty media queries that had the empty selectors before $content = preg_replace('/(?<=(\}|;|\{))[^\{\};]+\{\s*\}/', '', $content); return $content; From 78215e3bd0171f7715f1e1dce2c8e90204fc2ed6 Mon Sep 17 00:00:00 2001 From: Obed Marquez Parlapiano Date: Mon, 4 Oct 2021 19:46:40 +0200 Subject: [PATCH 5/8] fix(css minify): add uppercase letters so that it works with css vars --- src/CSS.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CSS.php b/src/CSS.php index 93a3c85f..ef38fa01 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -637,7 +637,7 @@ protected function stripEmptyTags($content) */ protected function stripEmptyProperties($content) { - return preg_replace('/[a-z-:().#]*:\s*;/', '', $content); + return preg_replace('/[a-zA-Z-:().#]*:\s*;/', '', $content); } /** From bec356ee2376069c9af935f874dcf8e14f7e0cd0 Mon Sep 17 00:00:00 2001 From: Obed Marquez Parlapiano Date: Mon, 4 Oct 2021 20:33:51 +0200 Subject: [PATCH 6/8] Revert "fix(css minify): add uppercase letters so that it works with css vars" This reverts commit 78215e3bd0171f7715f1e1dce2c8e90204fc2ed6. --- src/CSS.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CSS.php b/src/CSS.php index ef38fa01..93a3c85f 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -637,7 +637,7 @@ protected function stripEmptyTags($content) */ protected function stripEmptyProperties($content) { - return preg_replace('/[a-zA-Z-:().#]*:\s*;/', '', $content); + return preg_replace('/[a-z-:().#]*:\s*;/', '', $content); } /** From 9409fcbcdb3f6f409e4e0aa35fbc9d0a10eba384 Mon Sep 17 00:00:00 2001 From: Obed Marquez Parlapiano Date: Mon, 4 Oct 2021 20:38:08 +0200 Subject: [PATCH 7/8] fix: add uppercase letters so that it works with css vars - A var such as --myColor would break. --- src/CSS.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CSS.php b/src/CSS.php index 93a3c85f..ef38fa01 100644 --- a/src/CSS.php +++ b/src/CSS.php @@ -637,7 +637,7 @@ protected function stripEmptyTags($content) */ protected function stripEmptyProperties($content) { - return preg_replace('/[a-z-:().#]*:\s*;/', '', $content); + return preg_replace('/[a-zA-Z-:().#]*:\s*;/', '', $content); } /** From 21c7dc49d465d24785477470be7070151ca054b8 Mon Sep 17 00:00:00 2001 From: Obed Marquez Parlapiano Date: Mon, 4 Oct 2021 20:42:55 +0200 Subject: [PATCH 8/8] Tests: add tests for css var with lower and uppercase letters --- tests/css/CSSTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/css/CSSTest.php b/tests/css/CSSTest.php index 88706b00..b0a9c356 100644 --- a/tests/css/CSSTest.php +++ b/tests/css/CSSTest.php @@ -817,12 +817,15 @@ public function dataProvider() .nonEmptyClass { display: flex; + --myColorVar: #fff; + color: var(--myColorVar); } } @media(max-width: 850px) { .emptyClassWithEmptyProperties { color: ; + --myEmptyColor: ; } #emptyId { @@ -833,7 +836,7 @@ public function dataProvider() #emptyId { } } -', 'body{background:#fff}@media(max-width:576px){.nonEmptyClass{display:flex}}', +', 'body{background:#fff}@media(max-width:576px){.nonEmptyClass{display:flex;--myColorVar: #fff;color: var(--myColorVar)}}', ); return $tests;