CSSMin: Trim whitespace from attribute selectors and url tokens
authorFomafix <fomafix@googlemail.com>
Wed, 6 Dec 2017 16:16:41 +0000 (17:16 +0100)
committerKrinkle <krinklemail@gmail.com>
Thu, 22 Feb 2018 23:41:14 +0000 (23:41 +0000)
* Trim whitespaces after opening and before closing parentheses and
  brackets.
* Ensure by test case that the invalid "url (" will not become the
  valid "url(" by minification.

This change also prevents the parsing problem in Firefox 57.0
https://bugzilla.mozilla.org/1418152 which is fixed in Firefox 57.0.1.

Change-Id: I804477ba7c6363f0e964fc8c7c0bc74d2d4c1a0d

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

index f2c7ed2..3d1c8b8 100644 (file)
@@ -535,8 +535,8 @@ class CSSMin {
        public static function minify( $css ) {
                return trim(
                        str_replace(
-                               [ '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ],
-                               [ ';', ':', '{', '{', ',', '}', '}' ],
+                               [ '; ', ': ', ' {', '{ ', ', ', '} ', ';}', '( ', ' )', '[ ', ' ]' ],
+                               [ ';', ':', '{', '{', ',', '}', '}', '(', ')', '[', ']' ],
                                preg_replace( [ '/\s+/', '/\/\*.*?\*\//s' ], [ ' ', '' ], $css )
                        )
                );
index 89cf68f..667eb0a 100644 (file)
@@ -149,6 +149,12 @@ class CSSMinTest extends MediaWikiTestCase {
                        [ "foo { content: '\"'; }", "foo{content:'\"'}" ],
                        // - Whitespace in string values
                        [ 'foo { content: " "; }', 'foo{content:" "}' ],
+
+                       // Whitespaces after opening and before closing parentheses and brackets
+                       [ 'a:not( [ href ] ) { prop: url( foobar.png ); }', 'a:not([href]){prop:url(foobar.png)}' ],
+
+                       // Ensure that the invalid "url (" will not become the valid "url(" by minification
+                       [ 'foo { prop: url ( foobar.png ); }', 'foo{prop:url (foobar.png)}' ],
                ];
        }