*/
public static function encodeStringAsDataURI( $contents, $type, $ie8Compat = true ) {
// Try #1: Non-encoded data URI
+
+ // Remove XML declaration, it's not needed with data URI usage
+ $contents = preg_replace( "/<\\?xml.*?\\?>/", '', $contents );
// The regular expression matches ASCII whitespace and printable characters.
if ( preg_match( '/^[\r\n\t\x20-\x7e]+$/', $contents ) ) {
// Do not base64-encode non-binary files (sane SVGs).
$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;
// is only supported in PHP 5.6. Use a getter method for now.
$urlRegex = '(' .
// Unquoted url
- 'url\(\s*(?P<file0>[^\'"][^\?\)]*?)(?P<query0>\?[^\)]*?|)\s*\)' .
+ 'url\(\s*(?P<file0>[^\'"][^\?\)]+?)(?P<query0>\?[^\)]*?|)\s*\)' .
// Single quoted url
- '|url\(\s*\'(?P<file1>[^\?\']*?)(?P<query1>\?[^\']*?|)\'\s*\)' .
+ '|url\(\s*\'(?P<file1>[^\?\']+?)(?P<query1>\?[^\']*?|)\'\s*\)' .
// Double quoted url
- '|url\(\s*"(?P<file2>[^\?"]*?)(?P<query2>\?[^"]*?|)"\s*\)' .
+ '|url\(\s*"(?P<file2>[^\?"]+?)(?P<query2>\?[^"]*?|)"\s*\)' .
')';
}
return $urlRegex;
$match['file'] = $match['file1'];
$match['query'] = $match['query1'];
} else {
+ if ( !isset( $match['file2'] ) || $match['file2'][1] === -1 ) {
+ throw new Exception( 'URL must be non-empty' );
+ }
$match['file'] = $match['file2'];
$match['query'] = $match['query2'];
}
$match['file'] = $match['file1'];
$match['query'] = $match['query1'];
} else {
+ if ( !isset( $match['file2'] ) || $match['file2'] === '' ) {
+ throw new Exception( 'URL must be non-empty' );
+ }
$match['file'] = $match['file2'];
$match['query'] = $match['query2'];
}
public static function minify( $css ) {
return trim(
str_replace(
- [ '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ],
- [ ';', ':', '{', '{', ',', '}', '}' ],
+ [ '; ', ': ', ' {', '{ ', ', ', '} ', ';}', '( ', ' )', '[ ', ' ]' ],
+ [ ';', ':', '{', '{', ',', '}', '}', '(', ')', '[', ']' ],
preg_replace( [ '/\s+/', '/\/\*.*?\*\//s' ], [ ' ', '' ], $css )
)
);