3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 * This class provides minification, URL remapping, URL extracting, and data-URL embedding.
24 * @author Trevor Parscal
31 * Maximum file size to still qualify for in-line embedding as a data-URI
33 * 24,576 is used because Internet Explorer has a 32,768 byte limit for data URIs, which when base64 encoded will
34 * result in a 1/3 increase in size.
36 const EMBED_SIZE_LIMIT
= 24576;
41 * Gets a list of local file paths which are referenced in a CSS style sheet
43 * @param $source string CSS data to remap
44 * @param $path string File path where the source was read from (optional)
45 * @return array List of local file references
47 public static function getLocalFileReferences( $source, $path = null ) {
48 $pattern = '/url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)/';
51 if ( preg_match_all( $pattern, $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER
) ) {
52 foreach ( $matches as $match ) {
53 $file = ( isset( $path ) ?
rtrim( $path, '/' ) . '/' : '' ) . "{$match['file'][0]}";
55 // Only proceed if we can access the file
56 if ( file_exists( $file ) ) {
66 * Remaps CSS URL paths and automatically embeds data URIs for URL rules preceded by an /* @embed * / comment
68 * @param $source string CSS data to remap
69 * @param $path string File path where the source was read from
70 * @return string Remapped CSS data
72 public static function remap( $source, $path, $embed = true ) {
74 $pattern = '/((?<embed>\s*\/\*\s*\@embed\s*\*\/)(?<rule>[^\;\}]*))?url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)(?<extra>[^;]*)[\;]?/';
77 while ( preg_match( $pattern, $source, $match, PREG_OFFSET_CAPTURE
, $offset ) ) {
79 $embed = $match['embed'][0];
80 $rule = $match['rule'][0];
81 $extra = $match['extra'][0];
82 $file = "{$path}/{$match['file'][0]}";
84 // Only proceed if we can access the file
85 if ( file_exists( $file ) ) {
86 // Add version parameter as a time-stamp in ISO 8601 format, using Z for the timezone, meaning GMT
87 $url = "{$file}?" . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) );
89 // Detect when URLs were preceeded with embed tags, and also verify file size is below the limit
90 if ( $embed && $match['embed'][1] > 0 && filesize( $file ) < self
::EMBED_SIZE_LIMIT
) {
91 // If we ever get to PHP 5.3, we should use the Fileinfo extension instead of mime_content_type
92 $type = mime_content_type( $file );
93 // Strip off any trailing = symbols (makes browsers freak out)
94 $data = base64_encode( file_get_contents( $file ) );
95 // Build 2 CSS properties; one which uses a base64 encoded data URI in place of the @embed
96 // comment to try and retain line-number integrity , and the other with a remapped an versioned
97 // URL and an Internet Explorer hack making it ignored in all browsers that support data URIs
98 $replacement = "{$rule}url(data:{$type};base64,{$data}){$extra};{$rule}url({$url}){$extra}!ie;";
100 // Build a CSS property with a remapped and versioned URL
101 $replacement = "{$embed}{$rule}url({$url}){$extra};";
104 // Perform replacement on the source
105 $source = substr_replace( $source, $replacement, $match[0][1], strlen( $match[0][0] ) );
106 // Move the offset to the end of the replacement in the source
107 $offset = $match[0][1] +
strlen( $replacement );
110 // Move the offset to the end of the match, leaving it alone
111 $offset = $match[0][1] +
strlen( $match[0][0] );
118 * Removes whitespace from CSS data
120 * @param $source string CSS data to minify
121 * @return string Minified CSS data
123 public static function minify( $css ) {
126 array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
127 array( ';', ':', '{', '{', ',', '}', '}' ),
128 preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )