3 use Wikimedia\TestingAccessWrapper
;
6 * @group ResourceLoader
9 class CSSMinTest
extends MediaWikiTestCase
{
11 protected function setUp() {
15 $server = 'https://expand.example';
16 $this->setMwGlobals( [
17 'wgServer' => $server,
18 'wgCanonicalServer' => $server,
23 * @dataProvider provideSerializeStringValue
24 * @covers CSSMin::serializeStringValue
26 public function testSerializeStringValue( $input, $expected ) {
27 $output = CSSMin
::serializeStringValue( $input );
31 'Serialized output must be in the expected form.'
35 public static function provideSerializeStringValue() {
37 [ 'Hello World!', '"Hello World!"' ],
38 [ "Null\0Null", "\"Null\\fffd Null\"" ],
42 [ "Tab\tTab", '"Tab\\9 Tab"' ],
43 [ "Space tab \t space", '"Space tab \\9 space"' ],
44 [ "Line\nfeed", '"Line\\a feed"' ],
45 [ "Return\rreturn", '"Return\\d return"' ],
46 [ "Next\xc2\x85line", "\"Next\xc2\x85line\"" ],
47 [ "Del\x7fDel", '"Del\\7f Del"' ],
48 [ "nb\xc2\xa0sp", "\"nb\xc2\xa0sp\"" ],
49 [ "AMP&AMP", "\"AMP&AMP\"" ],
50 [ '!"#$%&\'()*+,-./0123456789:;<=>?', '"!\\"#$%&\'()*+,-./0123456789:;<=>?"' ],
51 [ '@[\\]^_`{|}~', '"@[\\\\]^_`{|}~"' ],
55 [ '𝒞', '"𝒞"' ], // U+1D49E 'MATHEMATICAL SCRIPT CAPITAL C'
60 * @dataProvider provideMimeType
61 * @covers CSSMin::getMimeType
63 public function testGetMimeType( $fileContents, $fileExtension, $expected ) {
64 $fileName = wfTempDir() . DIRECTORY_SEPARATOR
. uniqid( 'MW_PHPUnit_CSSMinTest_' ) . '.'
66 $this->addTmpFiles( $fileName );
67 file_put_contents( $fileName, $fileContents );
68 $this->assertSame( $expected, CSSMin
::getMimeType( $fileName ) );
71 public static function provideMimeType() {
73 'JPEG with short extension' => [
78 'JPEG with long extension' => [
84 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
89 'PNG extension but JPEG content' => [
94 'JPEG extension but PNG content' => [
95 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
99 'PNG extension but SVG content' => [
100 '<?xml version="1.0"?><svg></svg>',
104 'SVG extension but PNG content' => [
105 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
110 'SVG with all headers' => [
111 '<?xml version="1.0"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
112 . '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>',
116 'SVG with XML header only' => [
117 '<?xml version="1.0"?><svg></svg>',
121 'SVG with DOCTYPE only' => [
122 '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
123 . '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>',
127 'SVG without any header' => [
136 * @dataProvider provideMinifyCases
137 * @covers CSSMin::minify
139 public function testMinify( $code, $expectedOutput ) {
140 $minified = CSSMin
::minify( $code );
145 'Minified output should be in the form expected.'
149 public static function provideMinifyCases() {
152 [ "\r\t\f \v\n\r", "" ],
153 [ "foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
157 [ "/*******\n foo\n *******/", "" ],
158 [ "/*!\n foo\n */", "" ],
160 // Inline comments in various different places
161 [ "/* comment */foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
162 [ "foo/* comment */, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
163 [ "foo,/* comment */ bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
164 [ "foo, bar/* comment */ {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
165 [ "foo, bar {\n\t/* comment */prop: value;\n}", "foo,bar{prop:value}" ],
166 [ "foo, bar {\n\tprop: /* comment */value;\n}", "foo,bar{prop:value}" ],
167 [ "foo, bar {\n\tprop: value /* comment */;\n}", "foo,bar{prop:value }" ],
168 [ "foo, bar {\n\tprop: value; /* comment */\n}", "foo,bar{prop:value; }" ],
170 // Keep track of things that aren't as minified as much as they
172 [ 'foo { prop: value ;}', 'foo{prop:value }' ],
173 [ 'foo { prop : value; }', 'foo{prop :value}' ],
174 [ 'foo { prop: value ; }', 'foo{prop:value }' ],
175 [ 'foo { font-family: "foo" , "bar"; }', 'foo{font-family:"foo" ,"bar"}' ],
176 [ "foo { src:\n\turl('foo') ,\n\turl('bar') ; }", "foo{src:url('foo') ,url('bar') }" ],
178 // Interesting cases with string values
179 // - Double quotes, single quotes
180 [ 'foo { content: ""; }', 'foo{content:""}' ],
181 [ "foo { content: ''; }", "foo{content:''}" ],
182 [ 'foo { content: "\'"; }', 'foo{content:"\'"}' ],
183 [ "foo { content: '\"'; }", "foo{content:'\"'}" ],
184 // - Whitespace in string values
185 [ 'foo { content: " "; }', 'foo{content:" "}' ],
187 // Whitespaces after opening and before closing parentheses and brackets
188 [ 'a:not( [ href ] ) { prop: url( foobar.png ); }', 'a:not([href]){prop:url(foobar.png)}' ],
190 // Ensure that the invalid "url (" will not become the valid "url(" by minification
191 [ 'foo { prop: url ( foobar.png ); }', 'foo{prop:url (foobar.png)}' ],
195 public static function provideIsRemoteUrl() {
197 [ true, 'http://localhost/w/red.gif?123' ],
198 [ true, 'https://example.org/x.png' ],
199 [ true, '//example.org/x.y.z/image.png' ],
200 [ true, '//localhost/styles.css?query=yes' ],
201 [ true, 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=' ],
204 [ false, './x.gif' ],
205 [ false, '../x.gif' ],
210 * @dataProvider provideIsRemoteUrl
211 * @covers CSSMin::isRemoteUrl
213 public function testIsRemoteUrl( $expect, $url ) {
214 $class = TestingAccessWrapper
::newFromClass( CSSMin
::class );
215 $this->assertEquals( $class->isRemoteUrl( $url ), $expect );
218 public static function provideIsLocalUrls() {
222 [ false, './x.gif' ],
223 [ false, '../x.gif' ],
228 * @dataProvider provideIsLocalUrls
229 * @covers CSSMin::isLocalUrl
231 public function testIsLocalUrl( $expect, $url ) {
232 $class = TestingAccessWrapper
::newFromClass( CSSMin
::class );
233 $this->assertEquals( $class->isLocalUrl( $url ), $expect );
237 * This test tests funky parameters to CSSMin::remap.
239 * @see testRemapRemapping for testing of the basic functionality
240 * @dataProvider provideRemapCases
241 * @covers CSSMin::remap
242 * @covers CSSMin::remapOne
244 public function testRemap( $message, $params, $expectedOutput ) {
245 $remapped = call_user_func_array( 'CSSMin::remap', $params );
247 $messageAdd = " Case: $message";
251 'CSSMin::remap should return the expected url form.' . $messageAdd
255 public static function provideRemapCases() {
256 // Parameter signature:
257 // CSSMin::remap( $code, $local, $remote, $embedData = true )
261 [ 'foo { prop: url(bar.png); }', false, 'http://example.org', false ],
262 'foo { prop: url(http://example.org/bar.png); }',
265 'Without trailing slash',
266 [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux', false ],
267 'foo { prop: url(http://example.org/bar.png); }',
270 'With trailing slash on remote (T29052)',
271 [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux/', false ],
272 'foo { prop: url(http://example.org/bar.png); }',
275 'Guard against stripping double slashes from query',
276 [ 'foo { prop: url(bar.png?corge=//grault); }', false, 'http://example.org/quux/', false ],
277 'foo { prop: url(http://example.org/quux/bar.png?corge=//grault); }',
280 'Expand absolute paths',
281 [ 'foo { prop: url(/w/skin/images/bar.png); }', false, 'http://example.org/quux', false ],
282 'foo { prop: url(https://expand.example/w/skin/images/bar.png); }',
285 "Don't barf at behavior: url(#default#behaviorName) - T162973",
286 [ 'foo { behavior: url(#default#bar); }', false, '/w/', false ],
287 'foo { behavior: url("#default#bar"); }',
293 * Cases with empty url() for CSSMin::remap.
295 * Regression test for T191237.
297 * @dataProvider provideRemapEmptyUrl
300 public function testRemapEmptyUrl( $params, $expected ) {
301 $remapped = call_user_func_array( 'CSSMin::remap', $params );
302 $this->assertEquals( $expected, $remapped, 'Ignore empty url' );
305 public static function provideRemapEmptyUrl() {
308 [ "background-image: url();", false, '/example', false ],
309 "background-image: url();",
312 [ "background-image: url('');", false, '/example', false ],
313 "background-image: url('');",
316 [ 'background-image: url("");', false, '/example', false ],
317 'background-image: url("");',
319 'Single quote with outer spacing' => [
320 [ "background-image: url( '' );", false, '/example', false ],
321 "background-image: url( '' );",
327 * This tests the basic functionality of CSSMin::remap.
329 * @see testRemap for testing of funky parameters
330 * @dataProvider provideRemapRemappingCases
333 public function testRemapRemapping( $message, $input, $expectedOutput ) {
334 $localPath = __DIR__
. '/../../data/cssmin';
335 $remotePath = 'http://localhost/w';
337 $realOutput = CSSMin
::remap( $input, $localPath, $remotePath );
338 $this->assertEquals( $expectedOutput, $realOutput, "CSSMin::remap: $message" );
341 public static function provideRemapRemappingCases() {
342 // red.gif and green.gif are one-pixel 35-byte GIFs.
343 // large.png is a 35K PNG that should be non-embeddable.
344 // Full paths start with http://localhost/w/.
345 // Timestamps in output are replaced with 'timestamp'.
347 // data: URIs for red.gif, green.gif, circle.svg
348 $red = 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=';
349 $green = 'data:image/gif;base64,R0lGODlhAQABAIAAAACAADAAACwAAAAAAQABAAACAkQBADs=';
350 $svg = 'data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%228'
351 . '%22 height=%228%22 viewBox=%220 0 8 8%22%3E %3Ccircle cx=%224%22 cy=%224%22 '
352 . 'r=%222%22/%3E %3Ca xmlns:xlink=%22http://www.w3.org/1999/xlink%22 xlink:title='
353 . '%22%3F%3E%22%3Etest%3C/a%3E %3C/svg%3E';
355 // phpcs:disable Generic.Files.LineLength
359 'foo { background: url(red.gif); }',
360 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
363 'Regular file (missing)',
364 'foo { background: url(theColorOfHerHair.gif); }',
365 'foo { background: url(http://localhost/w/theColorOfHerHair.gif); }',
369 'foo { background: url(http://example.org/w/foo.png); }',
370 'foo { background: url(http://example.org/w/foo.png); }',
373 'Protocol-relative remote URL',
374 'foo { background: url(//example.org/w/foo.png); }',
375 'foo { background: url(//example.org/w/foo.png); }',
378 'Remote URL with query',
379 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
380 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
383 'Protocol-relative remote URL with query',
384 'foo { background: url(//example.org/w/foo.png?query=yes); }',
385 'foo { background: url(//example.org/w/foo.png?query=yes); }',
388 'Domain-relative URL',
389 'foo { background: url(/static/foo.png); }',
390 'foo { background: url(https://expand.example/static/foo.png); }',
393 'Domain-relative URL with query',
394 'foo { background: url(/static/foo.png?query=yes); }',
395 'foo { background: url(https://expand.example/static/foo.png?query=yes); }',
398 'Path-relative URL with query',
399 "foo { background: url(?query=yes); }",
400 'foo { background: url(http://localhost/w/?query=yes); }',
403 'Remote URL (unnecessary quotes not preserved)',
404 'foo { background: url("http://example.org/w/unnecessary-quotes.png"); }',
405 'foo { background: url(http://example.org/w/unnecessary-quotes.png); }',
409 'foo { /* @embed */ background: url(red.gif); }',
410 "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; }",
413 'Embedded file, other comments before the rule',
414 "foo { /* Bar. */ /* @embed */ background: url(red.gif); }",
415 "foo { /* Bar. */ background: url($red); /* Bar. */ background: url(http://localhost/w/red.gif?34ac6)!ie; }",
418 'Can not re-embed data: URIs',
419 "foo { /* @embed */ background: url($red); }",
420 "foo { background: url($red); }",
423 'Can not remap data: URIs',
424 "foo { background: url($red); }",
425 "foo { background: url($red); }",
428 'Can not embed remote URLs',
429 'foo { /* @embed */ background: url(http://example.org/w/foo.png); }',
430 'foo { background: url(http://example.org/w/foo.png); }',
433 'Embedded file (inline @embed)',
434 'foo { background: /* @embed */ url(red.gif); }',
435 "foo { background: url($red); "
436 . "background: url(http://localhost/w/red.gif?34ac6)!ie; }",
439 'Can not embed large files',
440 'foo { /* @embed */ background: url(large.png); }',
441 "foo { background: url(http://localhost/w/large.png?e3d1f); }",
444 'SVG files are embedded without base64 encoding and unnecessary IE 6 and 7 fallback',
445 'foo { /* @embed */ background: url(circle.svg); }',
446 "foo { background: url(\"$svg\"); }",
449 'Two regular files in one rule',
450 'foo { background: url(red.gif), url(green.gif); }',
451 'foo { background: url(http://localhost/w/red.gif?34ac6), '
452 . 'url(http://localhost/w/green.gif?13651); }',
455 'Two embedded files in one rule',
456 'foo { /* @embed */ background: url(red.gif), url(green.gif); }',
457 "foo { background: url($red), url($green); "
458 . "background: url(http://localhost/w/red.gif?34ac6), "
459 . "url(http://localhost/w/green.gif?13651)!ie; }",
462 'Two embedded files in one rule (inline @embed)',
463 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(green.gif); }',
464 "foo { background: url($red), url($green); "
465 . "background: url(http://localhost/w/red.gif?34ac6), "
466 . "url(http://localhost/w/green.gif?13651)!ie; }",
469 'Two embedded files in one rule (inline @embed), one too large',
470 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(large.png); }',
471 "foo { background: url($red), url(http://localhost/w/large.png?e3d1f); "
472 . "background: url(http://localhost/w/red.gif?34ac6), "
473 . "url(http://localhost/w/large.png?e3d1f)!ie; }",
476 'Practical example with some noise',
477 'foo { /* @embed */ background: #f9f9f9 url(red.gif) 0 0 no-repeat; }',
478 "foo { background: #f9f9f9 url($red) 0 0 no-repeat; "
479 . "background: #f9f9f9 url(http://localhost/w/red.gif?34ac6) 0 0 no-repeat!ie; }",
482 'Does not mess with other properties',
483 'foo { color: red; background: url(red.gif); font-size: small; }',
484 'foo { color: red; background: url(http://localhost/w/red.gif?34ac6); font-size: small; }',
487 'Spacing and miscellanea not changed (1)',
488 'foo { background: url(red.gif); }',
489 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
492 'Spacing and miscellanea not changed (2)',
493 'foo {background:url(red.gif)}',
494 'foo {background:url(http://localhost/w/red.gif?34ac6)}',
497 'Spaces within url() parentheses are ignored',
498 'foo { background: url( red.gif ); }',
499 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
502 '@import rule to local file (should we remap this?)',
503 '@import url(/styles.css)',
504 '@import url(https://expand.example/styles.css)',
507 '@import rule to local file (should we remap this?)',
508 '@import url(/styles.css)',
509 '@import url(https://expand.example/styles.css)',
512 '@import rule to URL',
513 '@import url(//localhost/styles.css?query=val)',
514 '@import url(//localhost/styles.css?query=val)',
517 'Background URL (double quotes)',
518 'foo { background: url("//localhost/styles.css?quoted=double") }',
519 'foo { background: url(//localhost/styles.css?quoted=double) }',
522 'Background URL (single quotes)',
523 'foo { background: url(\'//localhost/styles.css?quoted=single\') }',
524 'foo { background: url(//localhost/styles.css?quoted=single) }',
527 'Background URL (double quoted, containing parentheses; T60473)',
528 'foo { background: url("//localhost/styles.css?query=(parens)") }',
529 'foo { background: url("//localhost/styles.css?query=(parens)") }',
532 'Background URL (double quoted, containing single quotes; T60473)',
533 'foo { background: url("//localhost/styles.css?quote=\'") }',
534 'foo { background: url("//localhost/styles.css?quote=\'") }',
537 'Background URL (single quoted, containing double quotes; T60473)',
538 'foo { background: url(\'//localhost/styles.css?quote="\') }',
539 'foo { background: url("//localhost/styles.css?quote=\"") }',
542 'Background URL (double quoted with outer spacing)',
543 'foo { background: url( "http://localhost/styles.css?quoted=double" ) }',
544 'foo { background: url(http://localhost/styles.css?quoted=double) }',
547 'Background URL (single quoted, containing spaces, with outer spacing)',
548 "foo { background: url( ' red.gif ' ); }",
549 'foo { background: url("http://localhost/w/ red.gif "); }',
552 'Simple case with comments before url',
553 'foo { prop: /* some {funny;} comment */ url(bar.png); }',
554 'foo { prop: /* some {funny;} comment */ url(http://localhost/w/bar.png); }',
557 'Simple case with comments after url',
558 'foo { prop: url(red.gif)/* some {funny;} comment */ ; }',
559 'foo { prop: url(http://localhost/w/red.gif?34ac6)/* some {funny;} comment */ ; }',
562 'Embedded file with comment before url',
563 'foo { /* @embed */ background: /* some {funny;} comment */ url(red.gif); }',
564 "foo { background: /* some {funny;} comment */ url($red); background: /* some {funny;} comment */ url(http://localhost/w/red.gif?34ac6)!ie; }",
567 'Embedded file with comments inside and outside the rule',
568 'foo { /* @embed */ background: url(red.gif) /* some {foo;} comment */; /* some {bar;} comment */ }',
569 "foo { background: url($red) /* some {foo;} comment */; background: url(http://localhost/w/red.gif?34ac6) /* some {foo;} comment */!ie; /* some {bar;} comment */ }",
572 'Embedded file with comment outside the rule',
573 'foo { /* @embed */ background: url(red.gif); /* some {funny;} comment */ }',
574 "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; /* some {funny;} comment */ }",
577 'Rule with two urls, each with comments',
578 '{ background: /*asd*/ url(something.png); background: /*jkl*/ url(something.png); }',
579 '{ background: /*asd*/ url(http://localhost/w/something.png); background: /*jkl*/ url(http://localhost/w/something.png); }',
582 'Sanity check for offending line from jquery.ui.theme.css (T62077)',
583 '.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}*/; }',
584 '.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}*/; }',
591 * This tests basic functionality of CSSMin::buildUrlValue.
593 * @dataProvider provideBuildUrlValueCases
594 * @covers CSSMin::buildUrlValue
596 public function testBuildUrlValue( $message, $input, $expectedOutput ) {
599 CSSMin
::buildUrlValue( $input ),
600 "CSSMin::buildUrlValue: $message"
604 public static function provideBuildUrlValueCases() {
608 'scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s',
609 'url(scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s)',
613 'data:image/png;base64,R0lGODlh/+==',
614 'url(data:image/png;base64,R0lGODlh/+==)',
618 "https://en.wikipedia.org/wiki/Wendy's",
619 "url(\"https://en.wikipedia.org/wiki/Wendy's\")",
622 'URL with parentheses',
623 'https://en.wikipedia.org/wiki/Boston_(band)',
624 'url("https://en.wikipedia.org/wiki/Boston_(band)")',
630 * Seperated because they are currently broken (T37492)
633 * @dataProvider provideStringCases
634 * @covers CSSMin::remap
636 public function testMinifyWithCSSStringValues( $code, $expectedOutput ) {
637 $this->testMinifyOutput( $code, $expectedOutput );
640 public static function provideStringCases() {
642 // String values should be respected
643 // - More than one space in a string value
644 [ 'foo { content: " "; }', 'foo{content:" "}' ],
645 // - Using a tab in a string value (turns into a space)
646 [ "foo { content: '\t'; }", "foo{content:'\t'}" ],
647 // - Using css-like syntax in string values
649 'foo::after { content: "{;}"; position: absolute; }',
650 'foo::after{content:"{;}";position:absolute}'