// Note: This will not correctly handle cases where ';', '{' or '}' appears in the rule itself,
// e.g. in a quoted string. You are advised not to use such characters in file names.
- $pattern = '/[;{]\K[^;}]*' . CSSMin::URL_REGEX . '[^;}]*(?=[;}])/';
+ // We also match start/end of the string to be consistent in edge-cases ('@import url(…)').
+ $pattern = '/(?:^|[;{])\K[^;{}]*' . CSSMin::URL_REGEX . '[^;}]*(?=[;}]|$)/';
return preg_replace_callback( $pattern, function ( $matchOuter ) use ( $local, $remote, $embedData ) {
$rule = $matchOuter[0];
* @return string Remapped/embedded URL data
*/
public static function remapOne( $file, $query, $local, $remote, $embed ) {
- // Skip fully-qualified URLs and data URIs
- $urlScheme = parse_url( $file, PHP_URL_SCHEME );
+ // The full URL possibly with query, as passed to the 'url()' value in CSS
+ $url = $file . $query;
+
+ // Skip fully-qualified and protocol-relative URLs and data URIs
+ $urlScheme = substr( $url, 0, 2 ) === '//' || parse_url( $url, PHP_URL_SCHEME );
if ( $urlScheme ) {
- return $file;
+ return $url;
}
// URLs with absolute paths like /w/index.php need to be expanded
// to absolute URLs but otherwise left alone
- if ( $file !== '' && $file[0] === '/' ) {
+ if ( $url !== '' && $url[0] === '/' ) {
// Replace the file path with an expanded (possibly protocol-relative) URL
// ...but only if wfExpandUrl() is even available.
// This will not be the case if we're running outside of MW
if ( function_exists( 'wfExpandUrl' ) ) {
- return wfExpandUrl( $file, PROTO_RELATIVE );
+ return wfExpandUrl( $url, PROTO_RELATIVE );
} else {
- return $file;
+ return $url;
}
}
- $url = "{$remote}/{$file}";
- $file = "{$local}/{$file}";
-
- $replacement = false;
-
- if ( $local !== false && file_exists( $file ) ) {
- // Add version parameter as a time-stamp in ISO 8601 format,
- // using Z for the timezone, meaning GMT
- $url .= '?' . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) );
- if ( $embed ) {
- $data = self::encodeImageAsDataURI( $file );
- if ( $data !== false ) {
- return $data;
- } else {
- return $url;
- }
- } else {
- // Assume that all paths are relative to $remote, and make them absolute
- return $url;
- }
- } elseif ( $local === false ) {
+ if ( $local === false ) {
// Assume that all paths are relative to $remote, and make them absolute
- return $url . $query;
+ return $remote . '/' . $url;
} else {
- return $file;
+ // We drop the query part here and instead make the path relative to $remote
+ $url = "{$remote}/{$file}";
+ // Path to the actual file on the filesystem
+ $localFile = "{$local}/{$file}";
+ if ( file_exists( $localFile ) ) {
+ // Add version parameter as a time-stamp in ISO 8601 format,
+ // using Z for the timezone, meaning GMT
+ $url .= '?' . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $localFile ), -2 ) );
+ if ( $embed ) {
+ $data = self::encodeImageAsDataURI( $localFile );
+ if ( $data !== false ) {
+ return $data;
+ }
+ }
+ }
+ // If any of these conditions failed (file missing, we don't want to embed it
+ // or it's not embeddable), return the URL (possibly with ?timestamp part)
+ return $url;
}
}