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