Index: branches/resourceloader/phase3/includes/CSSMin.php |
— | — | @@ -14,50 +14,67 @@ |
15 | 15 | |
16 | 16 | /* Static Methods */ |
17 | 17 | |
18 | | - /* |
| 18 | + /** |
| 19 | + * Gets a list of local file paths which are referenced in a CSS style sheet |
| 20 | + * |
| 21 | + * @param $source string CSS data to remap |
| 22 | + * @param $path string File path where the source was read from |
| 23 | + * @return array List of local file references |
| 24 | + */ |
| 25 | + public static function getLocalFileReferences( $source, $path ) { |
| 26 | + $pattern = '/url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)/'; |
| 27 | + $files = array(); |
| 28 | + if ( preg_match_all( $pattern, $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER ) ) { |
| 29 | + foreach ( $matches as $match ) { |
| 30 | + $file = "{$path}/{$match['file'][0]}"; |
| 31 | + // Only proceed if we can access the file |
| 32 | + if ( file_exists( $file ) ) { |
| 33 | + $files[] = $file; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + return $files; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
19 | 41 | * Remaps CSS URL paths and automatically embeds data URIs for URL rules preceded by an /* @embed * / comment |
20 | 42 | * |
21 | 43 | * @param $source string CSS data to remap |
22 | 44 | * @param $path string File path where the source was read from |
| 45 | + * @return string Remapped CSS data |
23 | 46 | */ |
24 | 47 | public static function remap( $source, $path ) { |
25 | | - $pattern = '/((?<embed>\s*\/\*\s*\@embed\s*\*\/)(?<rule>[^\;\}]*))?url\((?<file>[^)]*)\)(?<extra>[^;]*)[\;]?/'; |
| 48 | + $pattern = '/((?<embed>\s*\/\*\s*\@embed\s*\*\/)(?<rule>[^\;\}]*))?url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)(?<extra>[^;]*)[\;]?/'; |
26 | 49 | $offset = 0; |
27 | 50 | 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; |
| 51 | + // Shortcuts |
| 52 | + $embed = $match['embed'][0]; |
| 53 | + $rule = $match['rule'][0]; |
| 54 | + $extra = $match['extra'][0]; |
| 55 | + $file = "{$path}/{$match['file'][0]}"; |
| 56 | + // Only proceed if we can access the file |
| 57 | + if ( file_exists( $file ) ) { |
| 58 | + // Add version parameter as a time-stamp in ISO 8601 format, using Z for the timezone, meaning GMT |
| 59 | + $url = "{$file}?" . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) ); |
| 60 | + // Detect when URLs were preceeded with embed tags, and also verify file size is below the limit |
| 61 | + if ( $match['embed'][1] > 0 && filesize( $file ) < self::EMBED_SIZE_LIMIT ) { |
| 62 | + // If we ever get to PHP 5.3, we should use the Fileinfo extension instead of mime_content_type |
| 63 | + $type = mime_content_type( $file ); |
| 64 | + // Strip off any trailing = symbols (makes browsers freak out) |
| 65 | + $data = rtrim( base64_encode( file_get_contents( $file ) ), '=' ); |
| 66 | + // Build 2 CSS properties; one which uses a base64 encoded data URI in place of the @embed |
| 67 | + // comment to try and retain line-number integrity , and the other with a remapped an versioned |
| 68 | + // URL and an Internet Explorer hack making it ignored in all browsers that support data URIs |
| 69 | + $replacement = "{$rule}url(data:{$type};base64,{$data}){$extra};{$rule}url({$url}){$extra}!ie;"; |
| 70 | + } else { |
| 71 | + // Build a CSS property with a remapped and versioned URL |
| 72 | + $replacement = "{$embed}{$rule}url({$url}){$extra};"; |
61 | 73 | } |
| 74 | + // Perform replacement on the source |
| 75 | + $source = substr_replace( $source, $replacement, $match[0][1], strlen( $match[0][0] ) ); |
| 76 | + // Move the offset to the end of the replacement in the source |
| 77 | + $offset = $match[0][1] + strlen( $replacement ); |
| 78 | + continue; |
62 | 79 | } |
63 | 80 | // Move the offset to the end of the match, leaving it alone |
64 | 81 | $offset = $match[0][1] + strlen( $match[0][0] ); |
— | — | @@ -65,10 +82,11 @@ |
66 | 83 | return $source; |
67 | 84 | } |
68 | 85 | |
69 | | - /* |
| 86 | + /** |
70 | 87 | * Removes whitespace from CSS data |
71 | 88 | * |
72 | 89 | * @param $source string CSS data to minify |
| 90 | + * @return string Minified CSS data |
73 | 91 | */ |
74 | 92 | public static function minify( $css ) { |
75 | 93 | return trim( |