oops, don't delete char before the slash, followup to r83902
[lhc/web/wiklou.git] / includes / libs / CSSMin.php
1 <?php
2 /*
3 * Copyright 2010 Wikimedia Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software distributed
12 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
13 * OF ANY KIND, either express or implied. See the License for the
14 * specific language governing permissions and limitations under the License.
15 */
16
17 /**
18 * Transforms CSS data
19 *
20 * This class provides minification, URL remapping, URL extracting, and data-URL embedding.
21 *
22 * @file
23 * @version 0.1.1 -- 2010-09-11
24 * @author Trevor Parscal <tparscal@wikimedia.org>
25 * @copyright Copyright 2010 Wikimedia Foundation
26 * @license http://www.apache.org/licenses/LICENSE-2.0
27 */
28 class CSSMin {
29
30 /* Constants */
31
32 /**
33 * Maximum file size to still qualify for in-line embedding as a data-URI
34 *
35 * 24,576 is used because Internet Explorer has a 32,768 byte limit for data URIs,
36 * which when base64 encoded will result in a 1/3 increase in size.
37 */
38 const EMBED_SIZE_LIMIT = 24576;
39 const URL_REGEX = 'url\(\s*[\'"]?(?P<file>[^\?\)\'"]*)(?P<query>\??[^\)\'"]*)[\'"]?\s*\)';
40
41 /* Protected Static Members */
42
43 /** @var array List of common image files extensions and mime-types */
44 protected static $mimeTypes = array(
45 'gif' => 'image/gif',
46 'jpe' => 'image/jpeg',
47 'jpeg' => 'image/jpeg',
48 'jpg' => 'image/jpeg',
49 'png' => 'image/png',
50 'tif' => 'image/tiff',
51 'tiff' => 'image/tiff',
52 'xbm' => 'image/x-xbitmap',
53 );
54
55 /* Static Methods */
56
57 /**
58 * Gets a list of local file paths which are referenced in a CSS style sheet
59 *
60 * @param $source string CSS data to remap
61 * @param $path string File path where the source was read from (optional)
62 * @return array List of local file references
63 */
64 public static function getLocalFileReferences( $source, $path = null ) {
65 $files = array();
66 $rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
67 if ( preg_match_all( '/' . self::URL_REGEX . '/', $source, $matches, $rFlags ) ) {
68 foreach ( $matches as $match ) {
69 $file = ( isset( $path )
70 ? rtrim( $path, '/' ) . '/'
71 : '' ) . "{$match['file'][0]}";
72
73 // Only proceed if we can access the file
74 if ( !is_null( $path ) && file_exists( $file ) ) {
75 $files[] = $file;
76 }
77 }
78 }
79 return $files;
80 }
81
82 protected static function getMimeType( $file ) {
83 $realpath = realpath( $file );
84 // Try a couple of different ways to get the mime-type of a file, in order of
85 // preference
86 if (
87 $realpath
88 && function_exists( 'finfo_file' )
89 && function_exists( 'finfo_open' )
90 && defined( 'FILEINFO_MIME_TYPE' )
91 ) {
92 // As of PHP 5.3, this is how you get the mime-type of a file; it uses the Fileinfo
93 // PECL extension
94 return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $realpath );
95 } else if ( function_exists( 'mime_content_type' ) ) {
96 // Before this was deprecated in PHP 5.3, this was how you got the mime-type of a file
97 return mime_content_type( $file );
98 } else {
99 // Worst-case scenario has happened, use the file extension to infer the mime-type
100 $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
101 if ( isset( self::$mimeTypes[$ext] ) ) {
102 return self::$mimeTypes[$ext];
103 }
104 }
105 return false;
106 }
107
108 /**
109 * Remaps CSS URL paths and automatically embeds data URIs for URL rules
110 * preceded by an /* @embed * / comment
111 *
112 * @param $source string CSS data to remap
113 * @param $local string File path where the source was read from
114 * @param $remote string URL path to the file
115 * @param $embed ???
116 * @return string Remapped CSS data
117 */
118 public static function remap( $source, $local, $remote, $embed = true ) {
119 $pattern = '/((?P<embed>\s*\/\*\s*\@embed\s*\*\/)(?P<pre>[^\;\}]*))?' .
120 self::URL_REGEX . '(?P<post>[^;]*)[\;]?/';
121 $offset = 0;
122 while ( preg_match( $pattern, $source, $match, PREG_OFFSET_CAPTURE, $offset ) ) {
123 // Skip absolute URIs
124 if ( preg_match( '/^https?:\/\//', $match['file'][0] ) ) {
125 // Move the offset to the end of the match, leaving it alone
126 $offset = $match[0][1] + strlen( $match[0][0] );
127 continue;
128 }
129 // URLs with absolute paths like /w/index.php need to be expanded
130 // to absolute URLs but otherwise left alone
131 if ( $match['file'][0] !== '' && $match['file'][0][0] === '/' ) {
132 // Replace the file path with an expanded URL
133 $source = substr_replace( $source, wfExpandUrl( $match['file'][0] ),
134 $match['file'][1], strlen( $match['file'][0] )
135 );
136 // Move the offset to the end of the match, leaving it alone
137 $offset = $match[0][1] + strlen( $match[0][0] );
138 continue;
139 }
140 // Shortcuts
141 $embed = $match['embed'][0];
142 $pre = $match['pre'][0];
143 $post = $match['post'][0];
144 $query = $match['query'][0];
145 $url = "{$remote}/{$match['file'][0]}";
146 $file = "{$local}/{$match['file'][0]}";
147 // bug 27052 - Guard against double slashes, because foo//../bar
148 // apparently resolves to foo/bar on (some?) clients
149 $url = preg_replace( '#([^:])//+#', '\1/', $url );
150 $replacement = false;
151 if ( $local !== false && file_exists( $file ) ) {
152 // Add version parameter as a time-stamp in ISO 8601 format,
153 // using Z for the timezone, meaning GMT
154 $url .= '?' . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) );
155 // Embedding requires a bit of extra processing, so let's skip that if we can
156 if ( $embed ) {
157 $type = self::getMimeType( $file );
158 // Detect when URLs were preceeded with embed tags, and also verify file size is
159 // below the limit
160 if (
161 $type
162 && $match['embed'][1] > 0
163 && filesize( $file ) < self::EMBED_SIZE_LIMIT
164 ) {
165 // Strip off any trailing = symbols (makes browsers freak out)
166 $data = base64_encode( file_get_contents( $file ) );
167 // Build 2 CSS properties; one which uses a base64 encoded data URI in place
168 // of the @embed comment to try and retain line-number integrity, and the
169 // other with a remapped an versioned URL and an Internet Explorer hack
170 // making it ignored in all browsers that support data URIs
171 $replacement = "{$pre}url(data:{$type};base64,{$data}){$post};";
172 $replacement .= "{$pre}url({$url}){$post}!ie;";
173 }
174 }
175 if ( $replacement === false ) {
176 // Assume that all paths are relative to $remote, and make them absolute
177 $replacement = "{$embed}{$pre}url({$url}){$post};";
178 }
179 } else if ( $local === false ) {
180 // Assume that all paths are relative to $remote, and make them absolute
181 $replacement = "{$embed}{$pre}url({$url}{$query}){$post};";
182 }
183 if ( $replacement !== false ) {
184 // Perform replacement on the source
185 $source = substr_replace(
186 $source, $replacement, $match[0][1], strlen( $match[0][0] )
187 );
188 // Move the offset to the end of the replacement in the source
189 $offset = $match[0][1] + strlen( $replacement );
190 continue;
191 }
192 // Move the offset to the end of the match, leaving it alone
193 $offset = $match[0][1] + strlen( $match[0][0] );
194 }
195 return $source;
196 }
197
198 /**
199 * Removes whitespace from CSS data
200 *
201 * @param $css string CSS data to minify
202 * @return string Minified CSS data
203 */
204 public static function minify( $css ) {
205 return trim(
206 str_replace(
207 array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
208 array( ';', ':', '{', '{', ',', '}', '}' ),
209 preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )
210 )
211 );
212 }
213 }