CSSMin: Mangle whitespace in embedded SVGs

Convert newlines and tabs to spaces (which can be unencoded),
and consolidate runs of multiple spaces into a single space.
Also remove any leading and trailing spaces that might result
(most files end in a newline, for example).

Bug: T175318
Change-Id: Ic66c6acb37079cae84dd80ab2d5f2c829cf2df96
This commit is contained in:
Roan Kattouw 2017-09-14 11:32:07 -07:00 committed by Krinkle
parent 5e3165f008
commit dfd42d2653
2 changed files with 9 additions and 2 deletions

View file

@ -150,7 +150,14 @@ class CSSMin {
'%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;

View file

@ -271,9 +271,9 @@ class CSSMinTest extends MediaWikiTestCase {
// data: URIs for red.gif, green.gif, circle.svg
$red = 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=';
$green = 'data:image/gif;base64,R0lGODlhAQABAIAAAACAADAAACwAAAAAAQABAAACAkQBADs=';
$svg = 'data:image/svg+xml,%3C%3Fxml version="1.0" encoding="UTF-8"%3F%3E%0A'
$svg = 'data:image/svg+xml,%3C%3Fxml version="1.0" encoding="UTF-8"%3F%3E '
. '%3Csvg xmlns="http://www.w3.org/2000/svg" width="8" height='
. '"8"%3E%0A%09%3Ccircle cx="4" cy="4" r="2"/%3E%0A%3C/svg%3E%0A';
. '"8"%3E %3Ccircle cx="4" cy="4" r="2"/%3E %3C/svg%3E';
// @codingStandardsIgnoreStart Generic.Files.LineLength
return [