/**
* 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;
return $ruleWithRemapped;
}
}, $source );
-
- return $source;
}
/**
$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 ) {
+ if ( substr( $url, 0, 2 ) === '//' || parse_url( $url, PHP_URL_SCHEME ) ) {
return $url;
}