const EMBED_REGEX = '\/\*\s*\@embed\s*\*\/';
const COMMENT_REGEX = '\/\*.*?\*\/';
- /** @var array List of common image files extensions and MIME-types */
+ /** @var string[] List of common image files extensions and MIME-types */
protected static $mimeTypes = [
'gif' => 'image/gif',
'jpe' => 'image/jpeg',
*
* @param string $source CSS stylesheet source to process
* @param string $path File path where the source was read from
- * @return array List of local file references
+ * @return string[] List of local file references
*/
public static function getLocalFileReferences( $source, $path ) {
$stripped = preg_replace( '/' . self::COMMENT_REGEX . '/s', '', $source );
* @param bool $ie8Compat By default, a data URI will only be produced if it can be made short
* enough to fit in Internet Explorer 8 (and earlier) URI length limit (32,768 bytes). Pass
* `false` to remove this limitation.
- * @return string|bool Image contents encoded as a data URI or false.
+ * @return string|false Image contents encoded as a data URI or false.
*/
public static function encodeImageAsDataURI( $file, $type = null, $ie8Compat = true ) {
// Fast-fail for files that definitely exceed the maximum data URI length
* @param string $contents File contents to encode.
* @param string $type File's MIME type.
* @param bool $ie8Compat See encodeImageAsDataURI().
- * @return string|bool Image contents encoded as a data URI or false.
+ * @return string|false Image contents encoded as a data URI or false.
*/
public static function encodeStringAsDataURI( $contents, $type, $ie8Compat = true ) {
// Try #1: Non-encoded data URI
/**
* Serialize a string (escape and quote) for use as a CSS string value.
- * http://www.w3.org/TR/2013/WD-cssom-20131205/#serialize-a-string
+ * https://www.w3.org/TR/2016/WD-cssom-1-20160317/#serialize-a-string
*
* @param string $value
* @return string
- * @throws Exception
*/
public static function serializeStringValue( $value ) {
- if ( strstr( $value, "\0" ) ) {
- throw new Exception( "Invalid character in CSS string" );
- }
- $value = strtr( $value, [ '\\' => '\\\\', '"' => '\\"' ] );
- $value = preg_replace_callback( '/[\x01-\x1f\x7f-\x9f]/', function ( $match ) {
+ $value = strtr( $value, [ "\0" => "\\fffd ", '\\' => '\\\\', '"' => '\\"' ] );
+ $value = preg_replace_callback( '/[\x01-\x1f\x7f]/', function ( $match ) {
return '\\' . base_convert( ord( $match[0] ), 10, 16 ) . ' ';
}, $value );
return '"' . $value . '"';
* @return bool
*/
protected static function isLocalUrl( $maybeUrl ) {
- if ( $maybeUrl !== '' && $maybeUrl[0] === '/' && !self::isRemoteUrl( $maybeUrl ) ) {
- return true;
- }
- return false;
+ return isset( $maybeUrl[1] ) && $maybeUrl[0] === '/' && $maybeUrl[1] !== '/';
}
/**
// 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'];
}
return $data;
}
}
- if ( method_exists( 'OutputPage', 'transformFilePath' ) ) {
+ if ( class_exists( OutputPage::class ) ) {
$url = OutputPage::transformFilePath( $remote, $local, $file );
} else {
// Add version parameter as the first five hex digits
public static function minify( $css ) {
return trim(
str_replace(
- [ '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ],
- [ ';', ':', '{', '{', ',', '}', '}' ],
+ [ '; ', ': ', ' {', '{ ', ', ', '} ', ';}', '( ', ' )', '[ ', ' ]' ],
+ [ ';', ':', '{', '{', ',', '}', '}', '(', ')', '[', ']' ],
preg_replace( [ '/\s+/', '/\/\*.*?\*\//s' ], [ ' ', '' ], $css )
)
);