CSSMin: Mangle whitespace in embedded SVGs
authorRoan Kattouw <roan.kattouw@gmail.com>
Thu, 14 Sep 2017 18:32:07 +0000 (11:32 -0700)
committerKrinkle <krinklemail@gmail.com>
Tue, 19 Sep 2017 18:52:54 +0000 (18:52 +0000)
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

includes/libs/CSSMin.php
tests/phpunit/includes/libs/CSSMinTest.php

index adaae17..a9c021e 100644 (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;
index 62f990b..a770fa4 100644 (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 [