if ( preg_match( '/^[\r\n\t\x20-\x7e]+$/', $contents ) ) {
// Do not base64-encode non-binary files (sane SVGs).
// (This often produces longer URLs, but they compress better, yielding a net smaller size.)
- $uri = 'data:' . $type . ',' . rawurlencode( $contents );
+ $encoded = rawurlencode( $contents );
+ // Unencode some things that don't need to be encoded, to make the encoding smaller
+ $encoded = strtr( $encoded, [
+ '%20' => ' ', // Unencode spaces
+ '%2F' => '/', // Unencode slashes
+ '%3A' => ':', // Unencode colons
+ '%3D' => '=', // Unencode equals signs
+ '%22' => '"', // Unencode double quotes
+ '%0A' => ' ', // Change newlines to spaces
+ '%0D' => ' ', // Change carriage returns to spaces
+ '%09' => ' ', // Change tabs to spaces
+ ] );
+ // Consolidate runs of multiple spaces in a row
+ $encoded = preg_replace( '/ {2,}/', ' ', $encoded );
+ // Remove leading and trailing spaces
+ $encoded = preg_replace( '/^ | $/', '', $encoded );
+ $uri = 'data:' . $type . ',' . $encoded;
if ( !$ie8Compat || strlen( $uri ) < self::DATA_URI_SIZE_LIMIT ) {
return $uri;
}
return self::$mimeTypes[$ext];
}
- $realpath = realpath( $file );
- if (
- $realpath
- && function_exists( 'finfo_file' )
- && function_exists( 'finfo_open' )
- && defined( 'FILEINFO_MIME_TYPE' )
- ) {
- return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $realpath );
- }
-
- return false;
+ return mime_content_type( realpath( $file ) );
}
/**
if ( preg_match( '!^[\w\d:@/~.%+;,?&=-]+$!', $url ) ) {
return "url($url)";
} else {
- return 'url("' . strtr( $url, [ '\\' => '\\\\', '"' => '\\"' ] ) . '")';
+ return "url('" . strtr( $url, [ '\\' => '\\\\', "'" => "\\'" ] ) . "')";
}
}
return false;
}
+ /**
+ * @codeCoverageIgnore
+ */
private static function getUrlRegex() {
static $urlRegex;
if ( $urlRegex === null ) {