Opened 14 years ago
Closed 12 years ago
#18321 closed enhancement (wontfix)
Stop minimizing CSS files and add a simple regexp in load-scripts.php to remove comments when concatenating
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | |
| Component: | Build/Test Tools | Keywords: | |
| Focuses: | Cc: |
Description
As the title, differences in gzipped .dev.css and .css files are minimal. If we remove comments on the fly in load-scripts.php (has to be very fast regexp) we wouldn't need to pre-minimize css files.
Change History (3)
#2
@
14 years ago
- Cc gary@… added
Here's a rework of a function I'm using for simplified minifying. For use as a potential starting point for someone (I'm sure it could be optimised):
/**
* Quick and dirty way to mostly minify CSS.
*
* @param string $css String of CSS to minify.
* @return string
*/
function wp_minify_css( $css ) {
// Normalize whitespace
$css = preg_replace( '/\s+/', ' ', $css );
// Remove comment blocks, everything between /* and */, unless
// preserved with /*! ... */
$css = preg_replace( '/\/\*[^\!](.*?)\*\//', '', $css );
// Remove space after , : ; { }
$css = preg_replace( '/(,|:|;|\{|}) /', '$1', $css );
// Remove space before , ; { }
$css = preg_replace( '/ (,|;|\{|})/', '$1', $css );
// Strips leading 0 on decimal values (converts 0.5px into .5px)
$css = preg_replace( '/(:| )0\.([0-9]+)(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}.${2}${3}', $css );
// Strips units if value is 0 (converts 0px to 0)
$css = preg_replace( '/(:| )(\.?)0(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}0', $css );
// Converts all zeros value into short-hand
$css = preg_replace( '/0 0 0 0/', '0', $css );
return apply_filters( 'wp_minify_css', $css );
}
It would be good if something like this was kept as a function re-usable by themes / plugins, rather than them having to include a heavy-weight 3rd-party library to minify strings of CSS.
Note: See
TracTickets for help on using
tickets.
It would certainly make changesets easier to review.