3 * Minification of CSS stylesheets.
5 * Copyright 2010 Wikimedia Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software distributed
14 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
15 * OF ANY KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations under the License.
19 * @version 0.1.1 -- 2010-09-11
20 * @author Trevor Parscal <tparscal@wikimedia.org>
21 * @copyright Copyright 2010 Wikimedia Foundation
22 * @license http://www.apache.org/licenses/LICENSE-2.0
28 * This class provides minification, URL remapping, URL extracting, and data-URL embedding.
35 * Maximum file size to still qualify for in-line embedding as a data-URI
37 * 24,576 is used because Internet Explorer has a 32,768 byte limit for data URIs,
38 * which when base64 encoded will result in a 1/3 increase in size.
40 const EMBED_SIZE_LIMIT
= 24576;
41 const URL_REGEX
= 'url\(\s*[\'"]?(?P<file>[^\?\)\'"]*)(?P<query>\??[^\)\'"]*)[\'"]?\s*\)';
43 /* Protected Static Members */
45 /** @var array List of common image files extensions and mime-types */
46 protected static $mimeTypes = array(
48 'jpe' => 'image/jpeg',
49 'jpeg' => 'image/jpeg',
50 'jpg' => 'image/jpeg',
52 'tif' => 'image/tiff',
53 'tiff' => 'image/tiff',
54 'xbm' => 'image/x-xbitmap',
60 * Gets a list of local file paths which are referenced in a CSS style sheet
62 * @param string $source CSS data to remap
63 * @param string $path File path where the source was read from (optional)
64 * @return array List of local file references
66 public static function getLocalFileReferences( $source, $path = null ) {
68 $rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER
;
69 if ( preg_match_all( '/' . self
::URL_REGEX
. '/', $source, $matches, $rFlags ) ) {
70 foreach ( $matches as $match ) {
71 $file = ( isset( $path )
72 ?
rtrim( $path, '/' ) . '/'
73 : '' ) . "{$match['file'][0]}";
75 // Only proceed if we can access the file
76 if ( !is_null( $path ) && file_exists( $file ) ) {
88 protected static function getMimeType( $file ) {
89 $realpath = realpath( $file );
90 // Try a couple of different ways to get the mime-type of a file, in order of
94 && function_exists( 'finfo_file' )
95 && function_exists( 'finfo_open' )
96 && defined( 'FILEINFO_MIME_TYPE' )
98 // As of PHP 5.3, this is how you get the mime-type of a file; it uses the Fileinfo
100 return finfo_file( finfo_open( FILEINFO_MIME_TYPE
), $realpath );
101 } elseif ( function_exists( 'mime_content_type' ) ) {
102 // Before this was deprecated in PHP 5.3, this was how you got the mime-type of a file
103 return mime_content_type( $file );
105 // Worst-case scenario has happened, use the file extension to infer the mime-type
106 $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION
) );
107 if ( isset( self
::$mimeTypes[$ext] ) ) {
108 return self
::$mimeTypes[$ext];
115 * Remaps CSS URL paths and automatically embeds data URIs for URL rules
116 * preceded by an /* @embed * / comment
118 * @param string $source CSS data to remap
119 * @param string $local File path where the source was read from
120 * @param string $remote URL path to the file
121 * @param bool $embedData If false, never do any data URI embedding, even if / * @embed * / is found
122 * @return string Remapped CSS data
124 public static function remap( $source, $local, $remote, $embedData = true ) {
125 $pattern = '/((?P<embed>\s*\/\*\s*\@embed\s*\*\/)(?P<pre>[^\;\}]*))?' .
126 self
::URL_REGEX
. '(?P<post>[^;]*)[\;]?/';
128 while ( preg_match( $pattern, $source, $match, PREG_OFFSET_CAPTURE
, $offset ) ) {
129 // Skip fully-qualified URLs and data URIs
130 $urlScheme = parse_url( $match['file'][0], PHP_URL_SCHEME
);
132 // Move the offset to the end of the match, leaving it alone
133 $offset = $match[0][1] +
strlen( $match[0][0] );
136 // URLs with absolute paths like /w/index.php need to be expanded
137 // to absolute URLs but otherwise left alone
138 if ( $match['file'][0] !== '' && $match['file'][0][0] === '/' ) {
139 // Replace the file path with an expanded (possibly protocol-relative) URL
140 // ...but only if wfExpandUrl() is even available.
141 // This will not be the case if we're running outside of MW
143 if ( function_exists( 'wfExpandUrl' ) ) {
144 $expanded = wfExpandUrl( $match['file'][0], PROTO_RELATIVE
);
145 $origLength = strlen( $match['file'][0] );
146 $lengthIncrease = strlen( $expanded ) - $origLength;
147 $source = substr_replace( $source, $expanded,
148 $match['file'][1], $origLength
151 // Move the offset to the end of the match, leaving it alone
152 $offset = $match[0][1] +
strlen( $match[0][0] ) +
$lengthIncrease;
156 // Guard against double slashes, because "some/remote/../foo.png"
157 // resolves to "some/remote/foo.png" on (some?) clients (bug 27052).
158 if ( substr( $remote, -1 ) == '/' ) {
159 $remote = substr( $remote, 0, -1 );
163 $embed = $match['embed'][0];
164 $pre = $match['pre'][0];
165 $post = $match['post'][0];
166 $query = $match['query'][0];
167 $url = "{$remote}/{$match['file'][0]}";
168 $file = "{$local}/{$match['file'][0]}";
170 $replacement = false;
172 if ( $local !== false && file_exists( $file ) ) {
173 // Add version parameter as a time-stamp in ISO 8601 format,
174 // using Z for the timezone, meaning GMT
175 $url .= '?' . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) );
176 // Embedding requires a bit of extra processing, so let's skip that if we can
177 if ( $embedData && $embed ) {
178 $type = self
::getMimeType( $file );
179 // Detect when URLs were preceeded with embed tags, and also verify file size is
183 && $match['embed'][1] > 0
184 && filesize( $file ) < self
::EMBED_SIZE_LIMIT
186 // Strip off any trailing = symbols (makes browsers freak out)
187 $data = base64_encode( file_get_contents( $file ) );
188 // Build 2 CSS properties; one which uses a base64 encoded data URI in place
189 // of the @embed comment to try and retain line-number integrity, and the
190 // other with a remapped an versioned URL and an Internet Explorer hack
191 // making it ignored in all browsers that support data URIs
192 $replacement = "{$pre}url(data:{$type};base64,{$data}){$post};";
193 $replacement .= "{$pre}url({$url}){$post}!ie;";
196 if ( $replacement === false ) {
197 // Assume that all paths are relative to $remote, and make them absolute
198 $replacement = "{$embed}{$pre}url({$url}){$post};";
200 } elseif ( $local === false ) {
201 // Assume that all paths are relative to $remote, and make them absolute
202 $replacement = "{$embed}{$pre}url({$url}{$query}){$post};";
204 if ( $replacement !== false ) {
205 // Perform replacement on the source
206 $source = substr_replace(
207 $source, $replacement, $match[0][1], strlen( $match[0][0] )
209 // Move the offset to the end of the replacement in the source
210 $offset = $match[0][1] +
strlen( $replacement );
213 // Move the offset to the end of the match, leaving it alone
214 $offset = $match[0][1] +
strlen( $match[0][0] );
220 * Removes whitespace from CSS data
222 * @param string $css CSS data to minify
223 * @return string Minified CSS data
225 public static function minify( $css ) {
228 array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
229 array( ';', ':', '{', '{', ',', '}', '}' ),
230 preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )