Index: branches/resourceloader/phase3/includes/CSSMin.php |
— | — | @@ -4,52 +4,79 @@ |
5 | 5 | |
6 | 6 | /* Constants */ |
7 | 7 | |
8 | | - const MAX_EMBED_SIZE = 1024; |
| 8 | + /** |
| 9 | + * Maximum file size to still qualify for in-line embedding as a data-URI |
| 10 | + * |
| 11 | + * 24,576 is used because Internet Explorer has a 32,768 byte limit for data URIs, which when base64 encoded will |
| 12 | + * result in a 1/4 increase in size. |
| 13 | + */ |
| 14 | + const EMBED_SIZE_LIMIT = 24576; |
9 | 15 | |
10 | 16 | /* Static Methods */ |
11 | 17 | |
12 | 18 | /* |
13 | 19 | * Remaps CSS URL paths and automatically embeds data URIs for URL rules preceded by an /* @embed * / comment |
| 20 | + * |
| 21 | + * @param $source string CSS data to remap |
| 22 | + * @param $path string File path where the source was read from |
14 | 23 | */ |
15 | 24 | public static function remap( $source, $path ) { |
16 | | - // Pre-process for URL rewriting |
| 25 | + $pattern = '/((?<embed>\s*\/\*\s*\@embed\s*\*\/)(?<rule>[^\;\}]*))?url\((?<file>[^)]*)\)(?<extra>[^;]*)[\;]?/'; |
17 | 26 | $offset = 0; |
18 | | - while ( preg_match( |
19 | | - '/((?<embed>\s*\/\*\s*\@embed\s*\*\/)(?<rule>[^\;\}]*))?url\((?<file>[^)]*)\)(?<extra>[^;]*)[\;]?/', |
20 | | - $source, $match, PREG_OFFSET_CAPTURE, $offset |
21 | | - ) ) { |
22 | | - $url = $path . '/' . trim( $match['file'][0], "'\"" ); |
23 | | - $file = preg_replace( '/([^\?]*)(.*)/', '$1', $url ); |
24 | | - $embed = $match['embed'][0]; |
25 | | - $rule = $match['rule'][0]; |
26 | | - $extra = $match['extra'][0]; |
27 | | - if ( $match['embed'][1] > 0 && file_exists( $file ) && filesize( $file ) <= self::MAX_EMBED_SIZE ) { |
28 | | - // If we ever get to PHP 5.3, we should use the Fileinfo extension instead of mime_content_type |
29 | | - $type = mime_content_type( $file ); |
30 | | - $data = rtrim( base64_encode( file_get_contents( $file ) ), '=' ); |
31 | | - $replacement = "{$rule}url(data:{$type};base64,{$data}){$extra};{$rule}url({$url}){$extra}!ie;"; |
32 | | - } else { |
33 | | - $replacement = "{$embed}{$rule}url({$url}){$extra};"; |
| 27 | + while ( preg_match( $pattern, $source, $match, PREG_OFFSET_CAPTURE, $offset ) ) { |
| 28 | + // Remove single or double quotes |
| 29 | + $url = trim( $match['file'][0], "'\"" ); |
| 30 | + // Only proceed if the URL is to a local file |
| 31 | + if ( !preg_match( '/^[a-zA-Z]*\:\/\//', $url ) ) { |
| 32 | + // Shortcuts |
| 33 | + $embed = $match['embed'][0]; |
| 34 | + $rule = $match['rule'][0]; |
| 35 | + $extra = $match['extra'][0]; |
| 36 | + // Strip query string from URL |
| 37 | + $file = "{$path}/" . preg_replace( '/([^\?]*)(.*)/', '$1', $url ); |
| 38 | + // Only proceed if we can access the file |
| 39 | + if ( file_exists( $file ) ) { |
| 40 | + // Add version parameter as a time-stamp in ISO 8601 format, using Z for the timezone, meaning GMT |
| 41 | + $url = "{$file}?" . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) ); |
| 42 | + // Detect when URLs were preceeded with embed tags, and also verify file size is below the limit |
| 43 | + if ( $match['embed'][1] > 0 && filesize( $file ) < self::EMBED_SIZE_LIMIT ) { |
| 44 | + // If we ever get to PHP 5.3, we should use the Fileinfo extension instead of mime_content_type |
| 45 | + $type = mime_content_type( $file ); |
| 46 | + // Strip off any trailing = symbols (makes browsers freak out) |
| 47 | + $data = rtrim( base64_encode( file_get_contents( $file ) ), '=' ); |
| 48 | + // Build 2 CSS properties; one which uses a base64 encoded data URI in place of the @embed |
| 49 | + // comment to try and retain line-number integrity , and the other with a remapped an versioned |
| 50 | + // URL and an Internet Explorer hack making it ignored in all browsers that support data URIs |
| 51 | + $replacement = "{$rule}url(data:{$type};base64,{$data}){$extra};{$rule}url({$url}){$extra}!ie;"; |
| 52 | + } else { |
| 53 | + // Build a CSS property with a remapped and versioned URL |
| 54 | + $replacement = "{$embed}{$rule}url({$url}){$extra};"; |
| 55 | + } |
| 56 | + // Perform replacement on the source |
| 57 | + $source = substr_replace( $source, $replacement, $match[0][1], strlen( $match[0][0] ) ); |
| 58 | + // Move the offset to the end of the replacement in the source |
| 59 | + $offset = $match[0][1] + strlen( $replacement ); |
| 60 | + continue; |
| 61 | + } |
34 | 62 | } |
35 | | - $source = substr_replace( $source, $replacement, $match[0][1], strlen( $match[0][0] ) ); |
36 | | - $offset = $match[0][1] + strlen( $replacement ); |
| 63 | + // Move the offset to the end of the match, leaving it alone |
| 64 | + $offset = $match[0][1] + strlen( $match[0][0] ); |
37 | 65 | } |
38 | 66 | return $source; |
39 | 67 | } |
40 | 68 | |
41 | 69 | /* |
42 | | - * As seen at http://www.lateralcode.com/css-minifier/ |
| 70 | + * Removes whitespace from CSS data |
| 71 | + * |
| 72 | + * @param $source string CSS data to minify |
43 | 73 | */ |
44 | 74 | public static function minify( $css ) { |
45 | | - $css = preg_replace( '#\s+#', ' ', $css ); |
46 | | - $css = preg_replace( '#/\*.*?\*/#s', '', $css ); |
47 | | - $css = str_replace( '; ', ';', $css ); |
48 | | - $css = str_replace( ': ', ':', $css ); |
49 | | - $css = str_replace( ' {', '{', $css ); |
50 | | - $css = str_replace( '{ ', '{', $css ); |
51 | | - $css = str_replace( ', ', ',', $css ); |
52 | | - $css = str_replace( '} ', '}', $css ); |
53 | | - $css = str_replace( ';}', '}', $css ); |
54 | | - return trim( $css ); |
| 75 | + return trim( |
| 76 | + str_replace( |
| 77 | + array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ), |
| 78 | + array( ';', ':', '{', '{', ',', '}', '}' ), |
| 79 | + preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css ) |
| 80 | + ) |
| 81 | + ); |
55 | 82 | } |
56 | 83 | } |
\ No newline at end of file |