35 const PLACEHOLDER =
"\x7fPLACEHOLDER\x7f";
50 'jpe' =>
'image/jpeg',
51 'jpeg' =>
'image/jpeg',
52 'jpg' =>
'image/jpeg',
54 'tif' =>
'image/tiff',
55 'tiff' =>
'image/tiff',
56 'xbm' =>
'image/x-xbitmap',
57 'svg' =>
'image/svg+xml',
70 $stripped = preg_replace(
'/' . self::COMMENT_REGEX .
'/s',
'',
$source );
74 $rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
75 if ( preg_match_all(
'/' . self::getUrlRegex() .
'/', $stripped,
$matches, $rFlags ) ) {
78 $url = $match[
'file'][0];
83 substr( $url, 0, 2 ) ===
'//' ||
84 parse_url( $url, PHP_URL_SCHEME ) ||
85 substr( $url, 0, 9 ) ===
'#default#'
90 $files[] =
$path . $url;
113 if ( $ie8Compat && filesize( $file ) >= self::DATA_URI_SIZE_LIMIT ) {
117 if (
$type ===
null ) {
142 if ( preg_match(
'/^[\r\n\t\x20-\x7e]+$/', $contents ) ) {
145 $encoded = rawurlencode( $contents );
147 $encoded = strtr( $encoded, [
153 $uri =
'data:' .
$type .
',' . $encoded;
154 if ( !$ie8Compat || strlen( $uri ) < self::DATA_URI_SIZE_LIMIT ) {
160 $uri =
'data:' .
$type .
';base64,' . base64_encode( $contents );
161 if ( !$ie8Compat || strlen( $uri ) < self::DATA_URI_SIZE_LIMIT ) {
178 if ( strstr(
$value,
"\0" ) ) {
179 throw new Exception(
"Invalid character in CSS string" );
181 $value = strtr(
$value, [
'\\' =>
'\\\\',
'"' =>
'\\"' ] );
182 $value = preg_replace_callback(
'/[\x01-\x1f\x7f-\x9f]/',
function ( $match ) {
183 return '\\' . base_convert( ord( $match[0] ), 10, 16 ) .
' ';
185 return '"' .
$value .
'"';
194 $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
195 if ( isset( self::$mimeTypes[
$ext] ) ) {
196 return self::$mimeTypes[
$ext];
199 return mime_content_type( realpath( $file ) );
215 if ( preg_match(
'!^[\w\d:@/~.%+;,?&=-]+$!', $url ) ) {
218 return 'url("' . strtr( $url, [
'\\' =>
'\\\\',
'"' =>
'\\"' ] ) .
'")';
233 public static function remap(
$source, $local, $remote, $embedData =
true ) {
245 if ( substr( $remote, -1 ) ==
'/' ) {
246 $remote = substr( $remote, 0, -1 );
258 $pattern =
'/(?!' . self::EMBED_REGEX .
')(' . self::COMMENT_REGEX .
')/s';
260 $source = preg_replace_callback(
262 function ( $match )
use ( &$comments ) {
263 $comments[] = $match[ 0 ];
264 return CSSMin::PLACEHOLDER . (
count( $comments ) - 1 ) .
'x';
275 $source = preg_replace_callback(
277 function ( $matchOuter )
use ( $local, $remote, $embedData ) {
278 $rule = $matchOuter[0];
283 $rule = preg_replace(
285 CSSMin::PLACEHOLDER .
299 $ruleWithRemapped = preg_replace_callback(
301 function ( $match )
use ( $local, $remote ) {
304 $remapped =
CSSMin::remapOne( $match[
'file'], $match[
'query'], $local, $remote,
false );
314 $ruleWithEmbedded = preg_replace_callback(
316 function ( $match )
use ( $embedAll, $local, $remote, &
$mimeTypes ) {
319 $embed = $embedAll || $match[
'embed'];
328 $url = $match[
'file'] . $match[
'query'];
329 $file =
"{$local}/{$match['file']}";
331 !self::isRemoteUrl( $url ) && !self::isLocalUrl( $url )
332 && file_exists( $file )
346 if ( !$embedData || $ruleWithEmbedded === $ruleWithRemapped ) {
348 return $ruleWithRemapped;
349 } elseif ( $embedData && $needsEmbedFallback ) {
353 return "$ruleWithEmbedded;$ruleWithRemapped!ie";
356 return $ruleWithEmbedded;
361 $pattern =
'/' . self::PLACEHOLDER .
'(\d+)x/';
362 $source = preg_replace_callback( $pattern,
function ( $match )
use ( &$comments ) {
363 return $comments[ $match[1] ];
376 if ( substr( $maybeUrl, 0, 2 ) ===
'//' || parse_url( $maybeUrl, PHP_URL_SCHEME ) ) {
389 if ( $maybeUrl !==
'' && $maybeUrl[0] ===
'/' && !self::isRemoteUrl( $maybeUrl ) ) {
400 if ( $urlRegex ===
null ) {
422 'url\(\s*(?P<file0>[^\'"][^\?\)]*?)(?P<query0>\?[^\)]*?|)\s*\)' .
424 '|url\(\s*\'(?P<file1>[^\?\']*?)(?P<query1>\?[^\']*?|)\'\s*\)' .
426 '|url\(\s*"(?P<file2>[^\?"]*?)(?P<query2>\?[^"]*?|)"\s*\)' .
433 if (
$flags & PREG_SET_ORDER ) {
437 if ( isset( $match[
'file0'] ) && $match[
'file0'][1] !== -1 ) {
438 $match[
'file'] = $match[
'file0'];
439 $match[
'query'] = $match[
'query0'];
440 } elseif ( isset( $match[
'file1'] ) && $match[
'file1'][1] !== -1 ) {
441 $match[
'file'] = $match[
'file1'];
442 $match[
'query'] = $match[
'query1'];
444 $match[
'file'] = $match[
'file2'];
445 $match[
'query'] = $match[
'query2'];
448 if ( isset( $match[
'file0'] ) && $match[
'file0'] !==
'' ) {
449 $match[
'file'] = $match[
'file0'];
450 $match[
'query'] = $match[
'query0'];
451 } elseif ( isset( $match[
'file1'] ) && $match[
'file1'] !==
'' ) {
452 $match[
'file'] = $match[
'file1'];
453 $match[
'query'] = $match[
'query1'];
455 $match[
'file'] = $match[
'file2'];
456 $match[
'query'] = $match[
'query2'];
477 if ( self::isLocalUrl( $url ) && function_exists(
'wfExpandUrl' ) ) {
485 self::isRemoteUrl( $url ) ||
486 self::isLocalUrl( $url ) ||
487 substr( $url, 0, 9 ) ===
'#default#'
492 if ( $local ===
false ) {
494 $url = $remote .
'/' . $url;
497 $url =
"{$remote}/{$file}";
499 $localFile =
"{$local}/{$file}";
500 if ( file_exists( $localFile ) ) {
503 if ( $data !==
false ) {
507 if ( method_exists(
'OutputPage',
'transformFilePath' ) ) {
512 $url .=
'?' . substr( md5_file( $localFile ), 0, 5 );
518 if ( function_exists(
'wfRemoveDotSegments' ) ) {
533 [
'; ',
': ',
' {',
'{ ',
', ',
'} ',
';}' ],
534 [
';',
':',
'{',
'{',
',',
'}',
'}' ],
535 preg_replace( [
'/\s+/',
'/\/\*.*?\*\//s' ], [
' ',
'' ],
$css )