Merge "Fix some omitted colons in Spanish magic word l10n"
[lhc/web/wiklou.git] / includes / libs / CSSMin.php
index 68e30eb..e3a3e2c 100644 (file)
@@ -53,6 +53,7 @@ class CSSMin {
                'tif' => 'image/tiff',
                'tiff' => 'image/tiff',
                'xbm' => 'image/x-xbitmap',
+               'svg' => 'image/svg+xml',
        );
 
        /* Static Methods */
@@ -60,23 +61,38 @@ class CSSMin {
        /**
         * Gets a list of local file paths which are referenced in a CSS style sheet
         *
+        * This function will always return an empty array if the second parameter is not given or null
+        * for backwards-compatibility.
+        *
         * @param string $source CSS data to remap
         * @param string $path File path where the source was read from (optional)
         * @return array List of local file references
         */
        public static function getLocalFileReferences( $source, $path = null ) {
+               if ( $path === null ) {
+                       return array();
+               }
+
+               $path = rtrim( $path, '/' ) . '/';
                $files = array();
+
                $rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
                if ( preg_match_all( '/' . self::URL_REGEX . '/', $source, $matches, $rFlags ) ) {
                        foreach ( $matches as $match ) {
-                               $file = ( isset( $path )
-                                       ? rtrim( $path, '/' ) . '/'
-                                       : '' ) . "{$match['file'][0]}";
+                               $url = $match['file'][0];
 
-                               // Only proceed if we can access the file
-                               if ( !is_null( $path ) && file_exists( $file ) ) {
-                                       $files[] = $file;
+                               // Skip fully-qualified and protocol-relative URLs and data URIs
+                               if ( substr( $url, 0, 2 ) === '//' || parse_url( $url, PHP_URL_SCHEME ) ) {
+                                       break;
                                }
+
+                               $file = $path . $url;
+                               // Skip non-existent files
+                               if ( file_exists( $file ) ) {
+                                       break;
+                               }
+
+                               $files[] = $file;
                        }
                }
                return $files;
@@ -140,6 +156,24 @@ class CSSMin {
                return false;
        }
 
+       /**
+        * Build a CSS 'url()' value for the given URL, quoting parentheses (and other funny characters)
+        * and escaping quotes as necessary.
+        *
+        * @param string $url URL to process
+        * @return string 'url()' value, usually just `"url($url)"`, quoted/escaped if necessary
+        */
+       public static function buildUrlValue( $url ) {
+               // The list below has been crafted to match URLs such as:
+               //   scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s
+               //   data:image/png;base64,R0lGODlh/+==
+               if ( preg_match( '!^[\w\d:@/~.%+;,?&=-]+$!', $url ) ) {
+                       return "url($url)";
+               } else {
+                       return 'url("' . strtr( $url, array( '\\' => '\\\\', '"' => '\\"' ) ) . '")';
+               }
+       }
+
        /**
         * Remaps CSS URL paths and automatically embeds data URIs for CSS rules or url() values
         * preceded by an / * @embed * / comment.
@@ -168,7 +202,8 @@ class CSSMin {
 
                // 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];
 
@@ -181,14 +216,14 @@ class CSSMin {
 
                        $ruleWithRemapped = preg_replace_callback( $pattern, function ( $match ) use ( $local, $remote ) {
                                $remapped = CSSMin::remapOne( $match['file'], $match['query'], $local, $remote, false );
-                               return "url({$remapped})";
+                               return CSSMin::buildUrlValue( $remapped );
                        }, $rule );
 
                        if ( $embedData ) {
                                $ruleWithEmbedded = preg_replace_callback( $pattern, function ( $match ) use ( $embedAll, $local, $remote ) {
                                        $embed = $embedAll || $match['embed'];
                                        $embedded = CSSMin::remapOne( $match['file'], $match['query'], $local, $remote, $embed );
-                                       return "url({$embedded})";
+                                       return CSSMin::buildUrlValue( $embedded );
                                }, $rule );
                        }
 
@@ -203,8 +238,6 @@ class CSSMin {
                                return $ruleWithRemapped;
                        }
                }, $source );
-
-               return $source;
        }
 
        /**
@@ -218,50 +251,49 @@ class CSSMin {
         * @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 );
-               if ( $urlScheme ) {
-                       return $file;
+               // 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
+               if ( substr( $url, 0, 2 ) === '//' || parse_url( $url, PHP_URL_SCHEME ) ) {
+                       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;
                }
        }