[wp-trac] [WordPress Trac] #18321: Stop minimizing CSS files and add a simple regexp in load-scripts.php to remove comments when concatenating

WordPress Trac wp-trac at lists.automattic.com
Mon Aug 8 09:01:05 UTC 2011


#18321: Stop minimizing CSS files and add a simple regexp in load-scripts.php to
remove comments when concatenating
-------------------------+-----------------------------
 Reporter:  azaozz       |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Future Release
Component:  General      |     Version:
 Severity:  normal       |  Resolution:
 Keywords:               |
-------------------------+-----------------------------
Changes (by GaryJ):

 * cc: gary@… (added)


Comment:

 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.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/18321#comment:2>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list