From: Elliott Eggleston Date: Sat, 19 Sep 2015 19:32:09 +0000 (-0400) Subject: Strip comments before gathering url(...) refs in CSS X-Git-Tag: 1.31.0-rc.0~9958^2 X-Git-Url: http://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=c737e48d7f0ae02b512b0a0c1223a2114974daf6;p=lhc%2Fweb%2Fwiklou.git Strip comments before gathering url(...) refs in CSS 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 --- diff --git a/includes/libs/CSSMin.php b/includes/libs/CSSMin.php index 26489214a0..5a8c4c7bc1 100644 --- a/includes/libs/CSSMin.php +++ b/includes/libs/CSSMin.php @@ -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 index 0000000000..744a14c730 --- /dev/null +++ b/tests/phpunit/data/css/comments.css @@ -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); } +*/ diff --git a/tests/phpunit/structure/ResourcesTest.php b/tests/phpunit/structure/ResourcesTest.php index 69f452503d..eefc926a77 100644 --- a/tests/phpunit/structure/ResourcesTest.php +++ b/tests/phpunit/structure/ResourcesTest.php @@ -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