ericmckean / minify

Automatically exported from code.google.com/p/minify
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

CSS Compressor should remove rules with empty declaration blocks #203

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Minify version:
PHP version: 2.1.4

What steps will reproduce the problem?
1. insert an empty selector in the source css file like p{}

a {color:#aaa}
p {}
q {color:#bbb}

Expected output:
a{color:#aaa}q{color:bbb}

Actual output:
a{color:#aaa}p{}q{color:bbb}

Did any unit tests FAIL?
No

Please provide any additional information below.
Especially for long selector paths this can save many bytes, example: 
.cellpaddingtopbottom>tbody>tr>td {}

In which cases can this happen?
Where css developers read PageSpeed help pages 'Writing Efficient CSS' 
<http://developer.mozilla.org/en/Writing_Efficient_CSS>. This could lead them 
to group the selectors by type of rule:
1. id rules
2. class rules
3. tag rules
4. universal rules
and within each group sort by key selector, and further follow more specific 
overrides:
a {color:blue}
   .class a {color:grey}
   .class a {color:green}

In such cases you can easily have selectors where you have nothing to set for 
the "parent seletor name" but for readability you would like to keep its name 
there, like:
a {}
   .class a {color:grey}
   .class a {color:green}

Original issue reported on code.google.com by cerieljacobs@gmail.com on 3 Nov 2010 at 1:33

GoogleCodeExporter commented 9 years ago
In other words (simplified): 

find:    }*{} 
replace: }

Original comment by cerieljacobs@gmail.com on 3 Nov 2010 at 1:37

GoogleCodeExporter commented 9 years ago
Sounds good to me. 

This might be safe near the end of processing:
$css = preg_replace('/}[^\/}]+{}/', '}', $css);
That is, remove the selection only if the area between } and {:
* has no "/" (probably the start of a preserved comment)
* has no "}" (this could be near/in a media block)

TODO:
Add tests to /min_unit_tests/_test_files/css/styles.css
Add expectation to /min_unit_tests/_test_files/css/styles.min.css
Add code at line 153 of /min/lib/Minify/CSS/Compressor.php#153

Feel free to submit patches :)

Original comment by mrclay....@gmail.com on 3 Nov 2010 at 2:50