CSSMin: Remove XML declaration from SVGs
authorVolker E <volker.e@wikimedia.org>
Tue, 9 Jan 2018 03:05:40 +0000 (19:05 -0800)
committerVolker E <volker.e@wikimedia.org>
Fri, 12 Jan 2018 03:28:15 +0000 (19:28 -0800)
Removing unnecessary XML declaration prolog when embedding SVGs
as data URI and adding more complex elements & attributes to test.

Bug: T175318
Change-Id: Ibec8c2ab498554a6ad33b4420ae6258dcc80fcdf

includes/libs/CSSMin.php
tests/phpunit/data/cssmin/circle.svg
tests/phpunit/includes/libs/CSSMinTest.php

index e85f02d..f2c7ed2 100644 (file)
@@ -132,6 +132,9 @@ class CSSMin {
         */
        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).
@@ -151,6 +154,7 @@ class CSSMin {
                        $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 4f7af21..415d992 100644 (file)
@@ -1,4 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8">
+<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8" viewBox="0 0 8 8">
        <circle cx="4" cy="4" r="2"/>
-</svg>
+       <a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="?>">test</a>
+</svg>
\ No newline at end of file
index 81ceb59..89cf68f 100644 (file)
@@ -271,9 +271,10 @@ 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=%221.0%22 encoding=%22UTF-8%22%3F%3E '
-                       . '%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%228%22 height='
-                       . '%228%22%3E %3Ccircle cx=%224%22 cy=%224%22 r=%222%22/%3E %3C/svg%3E';
+               $svg = 'data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%228'
+                       . '%22 height=%228%22 viewBox=%220 0 8 8%22%3E %3Ccircle cx=%224%22 cy=%224%22 '
+                       . 'r=%222%22/%3E %3Ca xmlns:xlink=%22http://www.w3.org/1999/xlink%22 xlink:title='
+                       . '%22%3F%3E%22%3Etest%3C/a%3E %3C/svg%3E';
 
                // phpcs:disable Generic.Files.LineLength
                return [