Strip comments before gathering url(...) refs in CSS
authorElliott Eggleston <ejegg@ejegg.com>
Sat, 19 Sep 2015 19:32:09 +0000 (15:32 -0400)
committerElliott Eggleston <ejegg@ejegg.com>
Sat, 19 Sep 2015 20:10:53 +0000 (16:10 -0400)
Change I5a3cdeb7d53485f161ccf8133e76850cdf5b4579 introduced tests
for existence of all local files referenced in CSS.  It's currently
failing changes even when the referenced files have been commented
out.  This change ensures that commented-out files are not included
in the testing list.

Change-Id: I2ad74c13a9cf36e96a7d7451bf229856bcd18422

includes/libs/CSSMin.php
tests/phpunit/data/css/comments.css [new file with mode: 0644]
tests/phpunit/structure/ResourcesTest.php

index 2648921..5a8c4c7 100644 (file)
@@ -91,15 +91,16 @@ class CSSMin {
         * non-existent files.
         *
         * @param string $source CSS stylesheet source to process
-        * @param string $path File path where the source wa
+        * @param string $path File path where the source was read from
         * @return array List of local file references
         */
        public static function getAllLocalFileReferences( $source, $path ) {
+               $stripped = preg_replace( '/' . self::COMMENT_REGEX . '/s', '', $source );
                $path = rtrim( $path, '/' ) . '/';
                $files = array();
 
                $rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
-               if ( preg_match_all( '/' . self::URL_REGEX . '/', $source, $matches, $rFlags ) ) {
+               if ( preg_match_all( '/' . self::URL_REGEX . '/', $stripped, $matches, $rFlags ) ) {
                        foreach ( $matches as $match ) {
                                $url = $match['file'][0];
 
diff --git a/tests/phpunit/data/css/comments.css b/tests/phpunit/data/css/comments.css
new file mode 100644 (file)
index 0000000..744a14c
--- /dev/null
@@ -0,0 +1,7 @@
+/* url expressions in comments should be ignored */
+
+.selector { /*@noflip*/ background-image: /*@embed*/ url(not-commented.gif); }
+
+/*
+.selector { background-image: url(commented-out.gif); }
+*/
index 69f4525..eefc926 100644 (file)
@@ -118,6 +118,22 @@ class ResourcesTest extends MediaWikiTestCase {
                }
        }
 
+       /**
+        * CSSMin::getAllLocalFileReferences should ignore url(...) expressions
+        * that have been commented out.
+        */
+       public function testCommentedLocalFileReferences() {
+               $basepath = __DIR__ . '/../data/css/';
+               $css = file_get_contents( $basepath . 'comments.css' );
+               $files = CSSMin::getAllLocalFileReferences( $css, $basepath );
+               $expected = array( $basepath . 'not-commented.gif' );
+               $this->assertArrayEquals(
+                       $expected,
+                       $files,
+                       'Url(...) expression in comment should be omitted.'
+               );
+       }
+
        /**
         * Get all registered modules from ResouceLoader.
         * @return array