3 * This file test the CSSMin library shipped with Mediawiki.
9 * @group ResourceLoader
12 class CSSMinTest
extends MediaWikiTestCase
{
14 protected function setUp() {
17 $server = 'http://doc.example.org';
19 $this->setMwGlobals( [
20 'wgServer' => $server,
21 'wgCanonicalServer' => $server,
26 * @dataProvider mimeTypeProvider
27 * @covers CSSMin::getMimeType
29 public function testGetMimeType( $fileContents, $fileExtension, $expected ) {
30 $fileName = wfTempDir() . DIRECTORY_SEPARATOR
. uniqid( 'MW_PHPUnit_CSSMinTest_' ) . '.'
32 $this->addTmpFiles( $fileName );
33 file_put_contents( $fileName, $fileContents );
34 $this->assertSame( $expected, CSSMin
::getMimeType( $fileName ) );
37 public function mimeTypeProvider() {
39 'JPEG with short extension' => [
44 'JPEG with long extension' => [
50 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
55 'PNG extension but JPEG content' => [
60 'JPEG extension but PNG content' => [
61 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
65 'PNG extension but SVG content' => [
66 '<?xml version="1.0"?><svg></svg>',
70 'SVG extension but PNG content' => [
71 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
76 'SVG with all headers' => [
77 '<?xml version="1.0"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
78 . '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>',
82 'SVG with XML header only' => [
83 '<?xml version="1.0"?><svg></svg>',
87 'SVG with DOCTYPE only' => [
88 '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
89 . '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>',
93 'SVG without any header' => [
102 * @dataProvider provideMinifyCases
103 * @covers CSSMin::minify
105 public function testMinify( $code, $expectedOutput ) {
106 $minified = CSSMin
::minify( $code );
111 'Minified output should be in the form expected.'
115 public static function provideMinifyCases() {
118 [ "\r\t\f \v\n\r", "" ],
119 [ "foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
123 [ "/*******\n foo\n *******/", "" ],
124 [ "/*!\n foo\n */", "" ],
126 // Inline comments in various different places
127 [ "/* comment */foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
128 [ "foo/* comment */, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
129 [ "foo,/* comment */ bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
130 [ "foo, bar/* comment */ {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
131 [ "foo, bar {\n\t/* comment */prop: value;\n}", "foo,bar{prop:value}" ],
132 [ "foo, bar {\n\tprop: /* comment */value;\n}", "foo,bar{prop:value}" ],
133 [ "foo, bar {\n\tprop: value /* comment */;\n}", "foo,bar{prop:value }" ],
134 [ "foo, bar {\n\tprop: value; /* comment */\n}", "foo,bar{prop:value; }" ],
136 // Keep track of things that aren't as minified as much as they
138 [ 'foo { prop: value ;}', 'foo{prop:value }' ],
139 [ 'foo { prop : value; }', 'foo{prop :value}' ],
140 [ 'foo { prop: value ; }', 'foo{prop:value }' ],
141 [ 'foo { font-family: "foo" , "bar"; }', 'foo{font-family:"foo" ,"bar"}' ],
142 [ "foo { src:\n\turl('foo') ,\n\turl('bar') ; }", "foo{src:url('foo') ,url('bar') }" ],
144 // Interesting cases with string values
145 // - Double quotes, single quotes
146 [ 'foo { content: ""; }', 'foo{content:""}' ],
147 [ "foo { content: ''; }", "foo{content:''}" ],
148 [ 'foo { content: "\'"; }', 'foo{content:"\'"}' ],
149 [ "foo { content: '\"'; }", "foo{content:'\"'}" ],
150 // - Whitespace in string values
151 [ 'foo { content: " "; }', 'foo{content:" "}' ],
155 public static function provideIsRemoteUrl() {
157 [ true, 'http://localhost/w/red.gif?123' ],
158 [ true, 'https://example.org/x.png' ],
159 [ true, '//example.org/x.y.z/image.png' ],
160 [ true, '//localhost/styles.css?query=yes' ],
161 [ true, 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=' ],
164 [ false, './x.gif' ],
165 [ false, '../x.gif' ],
170 * @dataProvider provideIsRemoteUrl
171 * @covers CSSMin::isRemoteUrl
173 public function testIsRemoteUrl( $expect, $url ) {
174 $this->assertEquals( CSSMinTestable
::isRemoteUrl( $url ), $expect );
177 public static function provideIsLocalUrls() {
181 [ false, './x.gif' ],
182 [ false, '../x.gif' ],
187 * @dataProvider provideIsLocalUrls
188 * @covers CSSMin::isLocalUrl
190 public function testIsLocalUrl( $expect, $url ) {
191 $this->assertEquals( CSSMinTestable
::isLocalUrl( $url ), $expect );
195 * This test tests funky parameters to CSSMin::remap.
197 * @see testRemapRemapping for testing of the basic functionality
198 * @dataProvider provideRemapCases
199 * @covers CSSMin::remap
200 * @covers CSSMin::remapOne
202 public function testRemap( $message, $params, $expectedOutput ) {
203 $remapped = call_user_func_array( 'CSSMin::remap', $params );
205 $messageAdd = " Case: $message";
209 'CSSMin::remap should return the expected url form.' . $messageAdd
213 public static function provideRemapCases() {
214 // Parameter signature:
215 // CSSMin::remap( $code, $local, $remote, $embedData = true )
219 [ 'foo { prop: url(bar.png); }', false, 'http://example.org', false ],
220 'foo { prop: url(http://example.org/bar.png); }',
223 'Without trailing slash',
224 [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux', false ],
225 'foo { prop: url(http://example.org/bar.png); }',
228 'With trailing slash on remote (T29052)',
229 [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux/', false ],
230 'foo { prop: url(http://example.org/bar.png); }',
233 'Guard against stripping double slashes from query',
234 [ 'foo { prop: url(bar.png?corge=//grault); }', false, 'http://example.org/quux/', false ],
235 'foo { prop: url(http://example.org/quux/bar.png?corge=//grault); }',
238 'Expand absolute paths',
239 [ 'foo { prop: url(/w/skin/images/bar.png); }', false, 'http://example.org/quux', false ],
240 'foo { prop: url(http://doc.example.org/w/skin/images/bar.png); }',
243 "Don't barf at behavior: url(#default#behaviorName) - T162973",
244 [ 'foo { behavior: url(#default#bar); }', false, '/w/', false ],
245 'foo { behavior: url("#default#bar"); }',
251 * This tests the basic functionality of CSSMin::remap.
253 * @see testRemap for testing of funky parameters
254 * @dataProvider provideRemapRemappingCases
257 public function testRemapRemapping( $message, $input, $expectedOutput ) {
258 $localPath = __DIR__
. '/../../data/cssmin';
259 $remotePath = 'http://localhost/w';
261 $realOutput = CSSMin
::remap( $input, $localPath, $remotePath );
262 $this->assertEquals( $expectedOutput, $realOutput, "CSSMin::remap: $message" );
265 public static function provideRemapRemappingCases() {
266 // red.gif and green.gif are one-pixel 35-byte GIFs.
267 // large.png is a 35K PNG that should be non-embeddable.
268 // Full paths start with http://localhost/w/.
269 // Timestamps in output are replaced with 'timestamp'.
271 // data: URIs for red.gif, green.gif, circle.svg
272 $red = 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=';
273 $green = 'data:image/gif;base64,R0lGODlhAQABAIAAAACAADAAACwAAAAAAQABAAACAkQBADs=';
274 $svg = 'data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%228'
275 . '%22 height=%228%22 viewBox=%220 0 8 8%22%3E %3Ccircle cx=%224%22 cy=%224%22 '
276 . 'r=%222%22/%3E %3Ca xmlns:xlink=%22http://www.w3.org/1999/xlink%22 xlink:title='
277 . '%22%3F%3E%22%3Etest%3C/a%3E %3C/svg%3E';
279 // phpcs:disable Generic.Files.LineLength
283 'foo { background: url(red.gif); }',
284 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
287 'Regular file (missing)',
288 'foo { background: url(theColorOfHerHair.gif); }',
289 'foo { background: url(http://localhost/w/theColorOfHerHair.gif); }',
293 'foo { background: url(http://example.org/w/foo.png); }',
294 'foo { background: url(http://example.org/w/foo.png); }',
297 'Protocol-relative remote URL',
298 'foo { background: url(//example.org/w/foo.png); }',
299 'foo { background: url(//example.org/w/foo.png); }',
302 'Remote URL with query',
303 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
304 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
307 'Protocol-relative remote URL with query',
308 'foo { background: url(//example.org/w/foo.png?query=yes); }',
309 'foo { background: url(//example.org/w/foo.png?query=yes); }',
312 'Domain-relative URL',
313 'foo { background: url(/static/foo.png); }',
314 'foo { background: url(http://doc.example.org/static/foo.png); }',
317 'Domain-relative URL with query',
318 'foo { background: url(/static/foo.png?query=yes); }',
319 'foo { background: url(http://doc.example.org/static/foo.png?query=yes); }',
322 'Remote URL (unnecessary quotes not preserved)',
323 'foo { background: url("http://example.org/w/unnecessary-quotes.png"); }',
324 'foo { background: url(http://example.org/w/unnecessary-quotes.png); }',
328 'foo { /* @embed */ background: url(red.gif); }',
329 "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; }",
332 'Embedded file, other comments before the rule',
333 "foo { /* Bar. */ /* @embed */ background: url(red.gif); }",
334 "foo { /* Bar. */ background: url($red); /* Bar. */ background: url(http://localhost/w/red.gif?34ac6)!ie; }",
337 'Can not re-embed data: URIs',
338 "foo { /* @embed */ background: url($red); }",
339 "foo { background: url($red); }",
342 'Can not remap data: URIs',
343 "foo { background: url($red); }",
344 "foo { background: url($red); }",
347 'Can not embed remote URLs',
348 'foo { /* @embed */ background: url(http://example.org/w/foo.png); }',
349 'foo { background: url(http://example.org/w/foo.png); }',
352 'Embedded file (inline @embed)',
353 'foo { background: /* @embed */ url(red.gif); }',
354 "foo { background: url($red); "
355 . "background: url(http://localhost/w/red.gif?34ac6)!ie; }",
358 'Can not embed large files',
359 'foo { /* @embed */ background: url(large.png); }',
360 "foo { background: url(http://localhost/w/large.png?e3d1f); }",
363 'SVG files are embedded without base64 encoding and unnecessary IE 6 and 7 fallback',
364 'foo { /* @embed */ background: url(circle.svg); }',
365 "foo { background: url(\"$svg\"); }",
368 'Two regular files in one rule',
369 'foo { background: url(red.gif), url(green.gif); }',
370 'foo { background: url(http://localhost/w/red.gif?34ac6), '
371 . 'url(http://localhost/w/green.gif?13651); }',
374 'Two embedded files in one rule',
375 'foo { /* @embed */ background: url(red.gif), url(green.gif); }',
376 "foo { background: url($red), url($green); "
377 . "background: url(http://localhost/w/red.gif?34ac6), "
378 . "url(http://localhost/w/green.gif?13651)!ie; }",
381 'Two embedded files in one rule (inline @embed)',
382 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(green.gif); }',
383 "foo { background: url($red), url($green); "
384 . "background: url(http://localhost/w/red.gif?34ac6), "
385 . "url(http://localhost/w/green.gif?13651)!ie; }",
388 'Two embedded files in one rule (inline @embed), one too large',
389 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(large.png); }',
390 "foo { background: url($red), url(http://localhost/w/large.png?e3d1f); "
391 . "background: url(http://localhost/w/red.gif?34ac6), "
392 . "url(http://localhost/w/large.png?e3d1f)!ie; }",
395 'Practical example with some noise',
396 'foo { /* @embed */ background: #f9f9f9 url(red.gif) 0 0 no-repeat; }',
397 "foo { background: #f9f9f9 url($red) 0 0 no-repeat; "
398 . "background: #f9f9f9 url(http://localhost/w/red.gif?34ac6) 0 0 no-repeat!ie; }",
401 'Does not mess with other properties',
402 'foo { color: red; background: url(red.gif); font-size: small; }',
403 'foo { color: red; background: url(http://localhost/w/red.gif?34ac6); font-size: small; }',
406 'Spacing and miscellanea not changed (1)',
407 'foo { background: url(red.gif); }',
408 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
411 'Spacing and miscellanea not changed (2)',
412 'foo {background:url(red.gif)}',
413 'foo {background:url(http://localhost/w/red.gif?34ac6)}',
416 'Spaces within url() parentheses are ignored',
417 'foo { background: url( red.gif ); }',
418 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
421 '@import rule to local file (should we remap this?)',
422 '@import url(/styles.css)',
423 '@import url(http://doc.example.org/styles.css)',
426 '@import rule to local file (should we remap this?)',
427 '@import url(/styles.css)',
428 '@import url(http://doc.example.org/styles.css)',
431 '@import rule to URL',
432 '@import url(//localhost/styles.css?query=val)',
433 '@import url(//localhost/styles.css?query=val)',
436 'Background URL (double quotes)',
437 'foo { background: url("//localhost/styles.css?quoted=double") }',
438 'foo { background: url(//localhost/styles.css?quoted=double) }',
441 'Background URL (single quotes)',
442 'foo { background: url(\'//localhost/styles.css?quoted=single\') }',
443 'foo { background: url(//localhost/styles.css?quoted=single) }',
446 'Background URL (containing parentheses; T60473)',
447 'foo { background: url("//localhost/styles.css?query=(parens)") }',
448 'foo { background: url("//localhost/styles.css?query=(parens)") }',
451 'Background URL (double quoted, containing single quotes; T60473)',
452 'foo { background: url("//localhost/styles.css?quote=\'") }',
453 'foo { background: url("//localhost/styles.css?quote=\'") }',
456 'Background URL (single quoted, containing double quotes; T60473)',
457 'foo { background: url(\'//localhost/styles.css?quote="\') }',
458 'foo { background: url("//localhost/styles.css?quote=\"") }',
461 'Simple case with comments before url',
462 'foo { prop: /* some {funny;} comment */ url(bar.png); }',
463 'foo { prop: /* some {funny;} comment */ url(http://localhost/w/bar.png); }',
466 'Simple case with comments after url',
467 'foo { prop: url(red.gif)/* some {funny;} comment */ ; }',
468 'foo { prop: url(http://localhost/w/red.gif?34ac6)/* some {funny;} comment */ ; }',
471 'Embedded file with comment before url',
472 'foo { /* @embed */ background: /* some {funny;} comment */ url(red.gif); }',
473 "foo { background: /* some {funny;} comment */ url($red); background: /* some {funny;} comment */ url(http://localhost/w/red.gif?34ac6)!ie; }",
476 'Embedded file with comments inside and outside the rule',
477 'foo { /* @embed */ background: url(red.gif) /* some {foo;} comment */; /* some {bar;} comment */ }',
478 "foo { background: url($red) /* some {foo;} comment */; background: url(http://localhost/w/red.gif?34ac6) /* some {foo;} comment */!ie; /* some {bar;} comment */ }",
481 'Embedded file with comment outside the rule',
482 'foo { /* @embed */ background: url(red.gif); /* some {funny;} comment */ }',
483 "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; /* some {funny;} comment */ }",
486 'Rule with two urls, each with comments',
487 '{ background: /*asd*/ url(something.png); background: /*jkl*/ url(something.png); }',
488 '{ background: /*asd*/ url(http://localhost/w/something.png); background: /*jkl*/ url(http://localhost/w/something.png); }',
491 'Sanity check for offending line from jquery.ui.theme.css (T62077)',
492 '.ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default { border: 1px solid #d3d3d3/*{borderColorDefault}*/; background: #e6e6e6/*{bgColorDefault}*/ url(images/ui-bg_glass_75_e6e6e6_1x400.png)/*{bgImgUrlDefault}*/ 50%/*{bgDefaultXPos}*/ 50%/*{bgDefaultYPos}*/ repeat-x/*{bgDefaultRepeat}*/; font-weight: normal/*{fwDefault}*/; color: #555555/*{fcDefault}*/; }',
493 '.ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default { border: 1px solid #d3d3d3/*{borderColorDefault}*/; background: #e6e6e6/*{bgColorDefault}*/ url(http://localhost/w/images/ui-bg_glass_75_e6e6e6_1x400.png)/*{bgImgUrlDefault}*/ 50%/*{bgDefaultXPos}*/ 50%/*{bgDefaultYPos}*/ repeat-x/*{bgDefaultRepeat}*/; font-weight: normal/*{fwDefault}*/; color: #555555/*{fcDefault}*/; }',
500 * This tests basic functionality of CSSMin::buildUrlValue.
502 * @dataProvider provideBuildUrlValueCases
503 * @covers CSSMin::buildUrlValue
505 public function testBuildUrlValue( $message, $input, $expectedOutput ) {
508 CSSMin
::buildUrlValue( $input ),
509 "CSSMin::buildUrlValue: $message"
513 public static function provideBuildUrlValueCases() {
517 'scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s',
518 'url(scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s)',
522 'data:image/png;base64,R0lGODlh/+==',
523 'url(data:image/png;base64,R0lGODlh/+==)',
527 "https://en.wikipedia.org/wiki/Wendy's",
528 "url(\"https://en.wikipedia.org/wiki/Wendy's\")",
531 'URL with parentheses',
532 'https://en.wikipedia.org/wiki/Boston_(band)',
533 'url("https://en.wikipedia.org/wiki/Boston_(band)")',
539 * Seperated because they are currently broken (T37492)
542 * @dataProvider provideStringCases
543 * @covers CSSMin::remap
545 public function testMinifyWithCSSStringValues( $code, $expectedOutput ) {
546 $this->testMinifyOutput( $code, $expectedOutput );
549 public static function provideStringCases() {
551 // String values should be respected
552 // - More than one space in a string value
553 [ 'foo { content: " "; }', 'foo{content:" "}' ],
554 // - Using a tab in a string value (turns into a space)
555 [ "foo { content: '\t'; }", "foo{content:'\t'}" ],
556 // - Using css-like syntax in string values
558 'foo::after { content: "{;}"; position: absolute; }',
559 'foo::after{content:"{;}";position:absolute}'
565 class CSSMinTestable
extends CSSMin
{
566 // Make some protected methods public
567 public static function isRemoteUrl( $maybeUrl ) {
568 return parent
::isRemoteUrl( $maybeUrl );
570 public static function isLocalUrl( $maybeUrl ) {
571 return parent
::isLocalUrl( $maybeUrl );