Merge "rc_old/new_len null for CategoryMembership RC change"
[lhc/web/wiklou.git] / includes / libs / CSSMin.php
1 <?php
2 /**
3 * Minification of CSS stylesheets.
4 *
5 * Copyright 2010 Wikimedia Foundation
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
17 *
18 * @file
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
23 */
24
25 /**
26 * Transforms CSS data
27 *
28 * This class provides minification, URL remapping, URL extracting, and data-URL embedding.
29 */
30 class CSSMin {
31
32 /* Constants */
33
34 /** @var string Strip marker for comments. **/
35 const PLACEHOLDER = "\x7fPLACEHOLDER\x7f";
36
37 /**
38 * Internet Explorer data URI length limit. See encodeImageAsDataURI().
39 */
40 const DATA_URI_SIZE_LIMIT = 32768;
41 const URL_REGEX = 'url\(\s*[\'"]?(?P<file>[^\?\)\'"]*?)(?P<query>\?[^\)\'"]*?|)[\'"]?\s*\)';
42 const EMBED_REGEX = '\/\*\s*\@embed\s*\*\/';
43 const COMMENT_REGEX = '\/\*.*?\*\/';
44
45 /* Protected Static Members */
46
47 /** @var array List of common image files extensions and MIME-types */
48 protected static $mimeTypes = [
49 'gif' => 'image/gif',
50 'jpe' => 'image/jpeg',
51 'jpeg' => 'image/jpeg',
52 'jpg' => 'image/jpeg',
53 'png' => 'image/png',
54 'tif' => 'image/tiff',
55 'tiff' => 'image/tiff',
56 'xbm' => 'image/x-xbitmap',
57 'svg' => 'image/svg+xml',
58 ];
59
60 /* Static Methods */
61
62 /**
63 * Gets a list of local file paths which are referenced in a CSS style sheet.
64 *
65 * If you wish non-existent files to be listed too, use getAllLocalFileReferences().
66 *
67 * For backwards-compatibility, if the second parameter is not given or null,
68 * this function will return an empty array instead of erroring out.
69 *
70 * @param string $source CSS stylesheet source to process
71 * @param string $path File path where the source was read from
72 * @return array List of local file references
73 */
74 public static function getLocalFileReferences( $source, $path = null ) {
75 if ( $path === null ) {
76 return [];
77 }
78
79 $files = self::getAllLocalFileReferences( $source, $path );
80
81 // Skip non-existent files
82 $files = array_filter( $files, function ( $file ) {
83 return file_exists( $file );
84 } );
85
86 return $files;
87 }
88
89 /**
90 * Gets a list of local file paths which are referenced in a CSS style sheet, including
91 * non-existent files.
92 *
93 * @param string $source CSS stylesheet source to process
94 * @param string $path File path where the source was read from
95 * @return array List of local file references
96 */
97 public static function getAllLocalFileReferences( $source, $path ) {
98 $stripped = preg_replace( '/' . self::COMMENT_REGEX . '/s', '', $source );
99 $path = rtrim( $path, '/' ) . '/';
100 $files = [];
101
102 $rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
103 if ( preg_match_all( '/' . self::URL_REGEX . '/', $stripped, $matches, $rFlags ) ) {
104 foreach ( $matches as $match ) {
105 $url = $match['file'][0];
106
107 // Skip fully-qualified and protocol-relative URLs and data URIs
108 if ( substr( $url, 0, 2 ) === '//' || parse_url( $url, PHP_URL_SCHEME ) ) {
109 break;
110 }
111
112 $files[] = $path . $url;
113 }
114 }
115 return $files;
116 }
117
118 /**
119 * Encode an image file as a data URI.
120 *
121 * If the image file has a suitable MIME type and size, encode it as a data URI, base64-encoded
122 * for binary files or just percent-encoded otherwise. Return false if the image type is
123 * unfamiliar or file exceeds the size limit.
124 *
125 * @param string $file Image file to encode.
126 * @param string|null $type File's MIME type or null. If null, CSSMin will
127 * try to autodetect the type.
128 * @param bool $ie8Compat By default, a data URI will only be produced if it can be made short
129 * enough to fit in Internet Explorer 8 (and earlier) URI length limit (32,768 bytes). Pass
130 * `false` to remove this limitation.
131 * @return string|bool Image contents encoded as a data URI or false.
132 */
133 public static function encodeImageAsDataURI( $file, $type = null, $ie8Compat = true ) {
134 // Fast-fail for files that definitely exceed the maximum data URI length
135 if ( $ie8Compat && filesize( $file ) >= self::DATA_URI_SIZE_LIMIT ) {
136 return false;
137 }
138
139 if ( $type === null ) {
140 $type = self::getMimeType( $file );
141 }
142 if ( !$type ) {
143 return false;
144 }
145
146 return self::encodeStringAsDataURI( file_get_contents( $file ), $type, $ie8Compat );
147 }
148
149 /**
150 * Encode file contents as a data URI with chosen MIME type.
151 *
152 * The URI will be base64-encoded for binary files or just percent-encoded otherwise.
153 *
154 * @since 1.25
155 *
156 * @param string $contents File contents to encode.
157 * @param string $type File's MIME type.
158 * @param bool $ie8Compat See encodeImageAsDataURI().
159 * @return string|bool Image contents encoded as a data URI or false.
160 */
161 public static function encodeStringAsDataURI( $contents, $type, $ie8Compat = true ) {
162 // Try #1: Non-encoded data URI
163 // The regular expression matches ASCII whitespace and printable characters.
164 if ( preg_match( '/^[\r\n\t\x20-\x7e]+$/', $contents ) ) {
165 // Do not base64-encode non-binary files (sane SVGs).
166 // (This often produces longer URLs, but they compress better, yielding a net smaller size.)
167 $uri = 'data:' . $type . ',' . rawurlencode( $contents );
168 if ( !$ie8Compat || strlen( $uri ) < self::DATA_URI_SIZE_LIMIT ) {
169 return $uri;
170 }
171 }
172
173 // Try #2: Encoded data URI
174 $uri = 'data:' . $type . ';base64,' . base64_encode( $contents );
175 if ( !$ie8Compat || strlen( $uri ) < self::DATA_URI_SIZE_LIMIT ) {
176 return $uri;
177 }
178
179 // A data URI couldn't be produced
180 return false;
181 }
182
183 /**
184 * Serialize a string (escape and quote) for use as a CSS string value.
185 * http://www.w3.org/TR/2013/WD-cssom-20131205/#serialize-a-string
186 *
187 * @param string $value
188 * @return string
189 * @throws Exception
190 */
191 public static function serializeStringValue( $value ) {
192 if ( strstr( $value, "\0" ) ) {
193 throw new Exception( "Invalid character in CSS string" );
194 }
195 $value = strtr( $value, [ '\\' => '\\\\', '"' => '\\"' ] );
196 $value = preg_replace_callback( '/[\x01-\x1f\x7f-\x9f]/', function ( $match ) {
197 return '\\' . base_convert( ord( $match[0] ), 10, 16 ) . ' ';
198 }, $value );
199 return '"' . $value . '"';
200 }
201
202 /**
203 * @param $file string
204 * @return bool|string
205 */
206 public static function getMimeType( $file ) {
207 $realpath = realpath( $file );
208 if (
209 $realpath
210 && function_exists( 'finfo_file' )
211 && function_exists( 'finfo_open' )
212 && defined( 'FILEINFO_MIME_TYPE' )
213 ) {
214 return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $realpath );
215 }
216
217 // Infer the MIME-type from the file extension
218 $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
219 if ( isset( self::$mimeTypes[$ext] ) ) {
220 return self::$mimeTypes[$ext];
221 }
222
223 return false;
224 }
225
226 /**
227 * Build a CSS 'url()' value for the given URL, quoting parentheses (and other funny characters)
228 * and escaping quotes as necessary.
229 *
230 * See http://www.w3.org/TR/css-syntax-3/#consume-a-url-token
231 *
232 * @param string $url URL to process
233 * @return string 'url()' value, usually just `"url($url)"`, quoted/escaped if necessary
234 */
235 public static function buildUrlValue( $url ) {
236 // The list below has been crafted to match URLs such as:
237 // scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s
238 // data:image/png;base64,R0lGODlh/+==
239 if ( preg_match( '!^[\w\d:@/~.%+;,?&=-]+$!', $url ) ) {
240 return "url($url)";
241 } else {
242 return 'url("' . strtr( $url, [ '\\' => '\\\\', '"' => '\\"' ] ) . '")';
243 }
244 }
245
246 /**
247 * Remaps CSS URL paths and automatically embeds data URIs for CSS rules
248 * or url() values preceded by an / * @embed * / comment.
249 *
250 * @param string $source CSS data to remap
251 * @param string $local File path where the source was read from
252 * @param string $remote URL path to the file
253 * @param bool $embedData If false, never do any data URI embedding,
254 * even if / * @embed * / is found.
255 * @return string Remapped CSS data
256 */
257 public static function remap( $source, $local, $remote, $embedData = true ) {
258 // High-level overview:
259 // * For each CSS rule in $source that includes at least one url() value:
260 // * Check for an @embed comment at the start indicating that all URIs should be embedded
261 // * For each url() value:
262 // * Check for an @embed comment directly preceding the value
263 // * If either @embed comment exists:
264 // * Embedding the URL as data: URI, if it's possible / allowed
265 // * Otherwise remap the URL to work in generated stylesheets
266
267 // Guard against trailing slashes, because "some/remote/../foo.png"
268 // resolves to "some/remote/foo.png" on (some?) clients (bug 27052).
269 if ( substr( $remote, -1 ) == '/' ) {
270 $remote = substr( $remote, 0, -1 );
271 }
272
273 // Disallow U+007F DELETE, which is illegal anyway, and which
274 // we use for comment placeholders.
275 $source = str_replace( "\x7f", "?", $source );
276
277 // Replace all comments by a placeholder so they will not interfere with the remapping.
278 // Warning: This will also catch on anything looking like the start of a comment between
279 // quotation marks (e.g. "foo /* bar").
280 $comments = [];
281
282 $pattern = '/(?!' . CSSMin::EMBED_REGEX . ')(' . CSSMin::COMMENT_REGEX . ')/s';
283
284 $source = preg_replace_callback(
285 $pattern,
286 function ( $match ) use ( &$comments ) {
287 $comments[] = $match[ 0 ];
288 return CSSMin::PLACEHOLDER . ( count( $comments ) - 1 ) . 'x';
289 },
290 $source
291 );
292
293 // Note: This will not correctly handle cases where ';', '{' or '}'
294 // appears in the rule itself, e.g. in a quoted string. You are advised
295 // not to use such characters in file names. We also match start/end of
296 // the string to be consistent in edge-cases ('@import url(…)').
297 $pattern = '/(?:^|[;{])\K[^;{}]*' . CSSMin::URL_REGEX . '[^;}]*(?=[;}]|$)/';
298
299 $source = preg_replace_callback(
300 $pattern,
301 function ( $matchOuter ) use ( $local, $remote, $embedData ) {
302 $rule = $matchOuter[0];
303
304 // Check for global @embed comment and remove it. Allow other comments to be present
305 // before @embed (they have been replaced with placeholders at this point).
306 $embedAll = false;
307 $rule = preg_replace(
308 '/^((?:\s+|' .
309 CSSMin::PLACEHOLDER .
310 '(\d+)x)*)' .
311 CSSMin::EMBED_REGEX .
312 '\s*/',
313 '$1',
314 $rule,
315 1,
316 $embedAll
317 );
318
319 // Build two versions of current rule: with remapped URLs
320 // and with embedded data: URIs (where possible).
321 $pattern = '/(?P<embed>' . CSSMin::EMBED_REGEX . '\s*|)' . CSSMin::URL_REGEX . '/';
322
323 $ruleWithRemapped = preg_replace_callback(
324 $pattern,
325 function ( $match ) use ( $local, $remote ) {
326 $remapped = CSSMin::remapOne( $match['file'], $match['query'], $local, $remote, false );
327
328 return CSSMin::buildUrlValue( $remapped );
329 },
330 $rule
331 );
332
333 if ( $embedData ) {
334 // Remember the occurring MIME types to avoid fallbacks when embedding some files.
335 $mimeTypes = [];
336
337 $ruleWithEmbedded = preg_replace_callback(
338 $pattern,
339 function ( $match ) use ( $embedAll, $local, $remote, &$mimeTypes ) {
340 $embed = $embedAll || $match['embed'];
341 $embedded = CSSMin::remapOne(
342 $match['file'],
343 $match['query'],
344 $local,
345 $remote,
346 $embed
347 );
348
349 $url = $match['file'] . $match['query'];
350 $file = $local . $match['file'];
351 if (
352 !self::isRemoteUrl( $url ) && !self::isLocalUrl( $url )
353 && file_exists( $file )
354 ) {
355 $mimeTypes[ CSSMin::getMimeType( $file ) ] = true;
356 }
357
358 return CSSMin::buildUrlValue( $embedded );
359 },
360 $rule
361 );
362
363 // Are all referenced images SVGs?
364 $needsEmbedFallback = $mimeTypes !== [ 'image/svg+xml' => true ];
365 }
366
367 if ( !$embedData || $ruleWithEmbedded === $ruleWithRemapped ) {
368 // We're not embedding anything, or we tried to but the file is not embeddable
369 return $ruleWithRemapped;
370 } elseif ( $embedData && $needsEmbedFallback ) {
371 // Build 2 CSS properties; one which uses a data URI in place of the @embed comment, and
372 // the other with a remapped and versioned URL with an Internet Explorer 6 and 7 hack
373 // making it ignored in all browsers that support data URIs
374 return "$ruleWithEmbedded;$ruleWithRemapped!ie";
375 } else {
376 // Look ma, no fallbacks! This is for files which IE 6 and 7 don't support anyway: SVG.
377 return $ruleWithEmbedded;
378 }
379 }, $source );
380
381 // Re-insert comments
382 $pattern = '/' . CSSMin::PLACEHOLDER . '(\d+)x/';
383 $source = preg_replace_callback( $pattern, function( $match ) use ( &$comments ) {
384 return $comments[ $match[1] ];
385 }, $source );
386
387 return $source;
388
389 }
390
391 /**
392 * Is this CSS rule referencing a remote URL?
393 *
394 * @param string $maybeUrl
395 * @return bool
396 */
397 protected static function isRemoteUrl( $maybeUrl ) {
398 if ( substr( $maybeUrl, 0, 2 ) === '//' || parse_url( $maybeUrl, PHP_URL_SCHEME ) ) {
399 return true;
400 }
401 return false;
402 }
403
404 /**
405 * Is this CSS rule referencing a local URL?
406 *
407 * @param string $maybeUrl
408 * @return bool
409 */
410 protected static function isLocalUrl( $maybeUrl ) {
411 if ( $maybeUrl !== '' && $maybeUrl[0] === '/' && !self::isRemoteUrl( $maybeUrl ) ) {
412 return true;
413 }
414 return false;
415 }
416
417 /**
418 * Remap or embed a CSS URL path.
419 *
420 * @param string $file URL to remap/embed
421 * @param string $query
422 * @param string $local File path where the source was read from
423 * @param string $remote URL path to the file
424 * @param bool $embed Whether to do any data URI embedding
425 * @return string Remapped/embedded URL data
426 */
427 public static function remapOne( $file, $query, $local, $remote, $embed ) {
428 // The full URL possibly with query, as passed to the 'url()' value in CSS
429 $url = $file . $query;
430
431 // Expand local URLs with absolute paths like /w/index.php to possibly protocol-relative URL, if
432 // wfExpandUrl() is available. (This will not be the case if we're running outside of MW.)
433 if ( self::isLocalUrl( $url ) && function_exists( 'wfExpandUrl' ) ) {
434 return wfExpandUrl( $url, PROTO_RELATIVE );
435 }
436
437 // Pass thru fully-qualified and protocol-relative URLs and data URIs, as well as local URLs if
438 // we can't expand them.
439 if ( self::isRemoteUrl( $url ) || self::isLocalUrl( $url ) ) {
440 return $url;
441 }
442
443 if ( $local === false ) {
444 // Assume that all paths are relative to $remote, and make them absolute
445 $url = $remote . '/' . $url;
446 } else {
447 // We drop the query part here and instead make the path relative to $remote
448 $url = "{$remote}/{$file}";
449 // Path to the actual file on the filesystem
450 $localFile = "{$local}/{$file}";
451 if ( file_exists( $localFile ) ) {
452 if ( $embed ) {
453 $data = self::encodeImageAsDataURI( $localFile );
454 if ( $data !== false ) {
455 return $data;
456 }
457 }
458 if ( method_exists( 'OutputPage', 'transformFilePath' ) ) {
459 $url = OutputPage::transformFilePath( $remote, $local, $file );
460 } else {
461 // Add version parameter as the first five hex digits
462 // of the MD5 hash of the file's contents.
463 $url .= '?' . substr( md5_file( $localFile ), 0, 5 );
464 }
465 }
466 // If any of these conditions failed (file missing, we don't want to embed it
467 // or it's not embeddable), return the URL (possibly with ?timestamp part)
468 }
469 if ( function_exists( 'wfRemoveDotSegments' ) ) {
470 $url = wfRemoveDotSegments( $url );
471 }
472 return $url;
473 }
474
475 /**
476 * Removes whitespace from CSS data
477 *
478 * @param string $css CSS data to minify
479 * @return string Minified CSS data
480 */
481 public static function minify( $css ) {
482 return trim(
483 str_replace(
484 [ '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ],
485 [ ';', ':', '{', '{', ',', '}', '}' ],
486 preg_replace( [ '/\s+/', '/\/\*.*?\*\//s' ], [ ' ', '' ], $css )
487 )
488 );
489 }
490 }