3 * This file test the CSSMin library shipped with Mediawiki.
8 class CSSMinTest
extends MediaWikiTestCase
{
10 protected function setUp() {
13 $server = 'http://doc.example.org';
15 $this->setMwGlobals( [
16 'wgServer' => $server,
17 'wgCanonicalServer' => $server,
22 * @dataProvider mimeTypeProvider
24 public function testGetMimeType( $fileContents, $fileExtension, $expected ) {
25 $fileName = wfTempDir() . DIRECTORY_SEPARATOR
. uniqid( 'MW_PHPUnit_CSSMinTest_' ) . '.'
27 $this->addTmpFiles( $fileName );
28 file_put_contents( $fileName, $fileContents );
29 $this->assertSame( $expected, CSSMin
::getMimeType( $fileName ) );
32 public function mimeTypeProvider() {
34 'JPEG with short extension' => [
39 'JPEG with long extension' => [
45 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
50 'PNG extension but JPEG content' => [
55 'JPEG extension but PNG content' => [
56 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
60 'PNG extension but SVG content' => [
61 '<?xml version="1.0"?><svg></svg>',
65 'SVG extension but PNG content' => [
66 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
71 'SVG with all headers' => [
72 '<?xml version="1.0"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
73 . '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>',
77 'SVG with XML header only' => [
78 '<?xml version="1.0"?><svg></svg>',
82 'SVG with DOCTYPE only' => [
83 '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
84 . '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>',
88 'SVG without any header' => [
97 * @dataProvider provideMinifyCases
98 * @covers CSSMin::minify
100 public function testMinify( $code, $expectedOutput ) {
101 $minified = CSSMin
::minify( $code );
106 'Minified output should be in the form expected.'
110 public static function provideMinifyCases() {
113 [ "\r\t\f \v\n\r", "" ],
114 [ "foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
118 [ "/*******\n foo\n *******/", "" ],
119 [ "/*!\n foo\n */", "" ],
121 // Inline comments in various different places
122 [ "/* comment */foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
123 [ "foo/* comment */, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
124 [ "foo,/* comment */ bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
125 [ "foo, bar/* comment */ {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
126 [ "foo, bar {\n\t/* comment */prop: value;\n}", "foo,bar{prop:value}" ],
127 [ "foo, bar {\n\tprop: /* comment */value;\n}", "foo,bar{prop:value}" ],
128 [ "foo, bar {\n\tprop: value /* comment */;\n}", "foo,bar{prop:value }" ],
129 [ "foo, bar {\n\tprop: value; /* comment */\n}", "foo,bar{prop:value; }" ],
131 // Keep track of things that aren't as minified as much as they
133 [ 'foo { prop: value ;}', 'foo{prop:value }' ],
134 [ 'foo { prop : value; }', 'foo{prop :value}' ],
135 [ 'foo { prop: value ; }', 'foo{prop:value }' ],
136 [ 'foo { font-family: "foo" , "bar"; }', 'foo{font-family:"foo" ,"bar"}' ],
137 [ "foo { src:\n\turl('foo') ,\n\turl('bar') ; }", "foo{src:url('foo') ,url('bar') }" ],
139 // Interesting cases with string values
140 // - Double quotes, single quotes
141 [ 'foo { content: ""; }', 'foo{content:""}' ],
142 [ "foo { content: ''; }", "foo{content:''}" ],
143 [ 'foo { content: "\'"; }', 'foo{content:"\'"}' ],
144 [ "foo { content: '\"'; }", "foo{content:'\"'}" ],
145 // - Whitespace in string values
146 [ 'foo { content: " "; }', 'foo{content:" "}' ],
150 public static function provideIsRemoteUrl() {
152 [ true, 'http://localhost/w/red.gif?123' ],
153 [ true, 'https://example.org/x.png' ],
154 [ true, '//example.org/x.y.z/image.png' ],
155 [ true, '//localhost/styles.css?query=yes' ],
156 [ true, 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=' ],
159 [ false, './x.gif' ],
160 [ false, '../x.gif' ],
165 * @dataProvider provideIsRemoteUrl
166 * @cover CSSMin::isRemoteUrl
168 public function testIsRemoteUrl( $expect, $url ) {
169 $this->assertEquals( CSSMinTestable
::isRemoteUrl( $url ), $expect );
172 public static function provideIsLocalUrls() {
176 [ false, './x.gif' ],
177 [ false, '../x.gif' ],
182 * @dataProvider provideIsLocalUrls
183 * @cover CSSMin::isLocalUrl
185 public function testIsLocalUrl( $expect, $url ) {
186 $this->assertEquals( CSSMinTestable
::isLocalUrl( $url ), $expect );
190 * This tests funky parameters to CSSMin::remap. testRemapRemapping tests
191 * the basic functionality.
193 * @dataProvider provideRemapCases
194 * @covers CSSMin::remap
196 public function testRemap( $message, $params, $expectedOutput ) {
197 $remapped = call_user_func_array( 'CSSMin::remap', $params );
199 $messageAdd = " Case: $message";
203 'CSSMin::remap should return the expected url form.' . $messageAdd
207 public static function provideRemapCases() {
208 // Parameter signature:
209 // CSSMin::remap( $code, $local, $remote, $embedData = true )
213 [ 'foo { prop: url(bar.png); }', false, 'http://example.org', false ],
214 'foo { prop: url(http://example.org/bar.png); }',
217 'Without trailing slash',
218 [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux', false ],
219 'foo { prop: url(http://example.org/bar.png); }',
222 'With trailing slash on remote (T29052)',
223 [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux/', false ],
224 'foo { prop: url(http://example.org/bar.png); }',
227 'Guard against stripping double slashes from query',
228 [ 'foo { prop: url(bar.png?corge=//grault); }', false, 'http://example.org/quux/', false ],
229 'foo { prop: url(http://example.org/quux/bar.png?corge=//grault); }',
232 'Expand absolute paths',
233 [ 'foo { prop: url(/w/skin/images/bar.png); }', false, 'http://example.org/quux', false ],
234 'foo { prop: url(http://doc.example.org/w/skin/images/bar.png); }',
240 * This tests basic functionality of CSSMin::remap. testRemapRemapping tests funky parameters.
242 * @dataProvider provideRemapRemappingCases
243 * @covers CSSMin::remap
245 public function testRemapRemapping( $message, $input, $expectedOutput ) {
246 $localPath = __DIR__
. '/../../data/cssmin';
247 $remotePath = 'http://localhost/w';
249 $realOutput = CSSMin
::remap( $input, $localPath, $remotePath );
250 $this->assertEquals( $expectedOutput, $realOutput, "CSSMin::remap: $message" );
253 public static function provideRemapRemappingCases() {
254 // red.gif and green.gif are one-pixel 35-byte GIFs.
255 // large.png is a 35K PNG that should be non-embeddable.
256 // Full paths start with http://localhost/w/.
257 // Timestamps in output are replaced with 'timestamp'.
259 // data: URIs for red.gif, green.gif, circle.svg
260 $red = 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=';
261 $green = 'data:image/gif;base64,R0lGODlhAQABAIAAAACAADAAACwAAAAAAQABAAACAkQBADs=';
262 $svg = 'data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A'
263 . '%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%228%22%20height%3D'
264 . '%228%22%3E%0A%3Ccircle%20cx%3D%224%22%20cy%3D%224%22%20r%3D%222%22%2F%3E%0A%3C%2Fsvg%3E%0A';
266 // @codingStandardsIgnoreStart Generic.Files.LineLength
270 'foo { background: url(red.gif); }',
271 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
274 'Regular file (missing)',
275 'foo { background: url(theColorOfHerHair.gif); }',
276 'foo { background: url(http://localhost/w/theColorOfHerHair.gif); }',
280 'foo { background: url(http://example.org/w/foo.png); }',
281 'foo { background: url(http://example.org/w/foo.png); }',
284 'Protocol-relative remote URL',
285 'foo { background: url(//example.org/w/foo.png); }',
286 'foo { background: url(//example.org/w/foo.png); }',
289 'Remote URL with query',
290 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
291 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
294 'Protocol-relative remote URL with query',
295 'foo { background: url(//example.org/w/foo.png?query=yes); }',
296 'foo { background: url(//example.org/w/foo.png?query=yes); }',
299 'Domain-relative URL',
300 'foo { background: url(/static/foo.png); }',
301 'foo { background: url(http://doc.example.org/static/foo.png); }',
304 'Domain-relative URL with query',
305 'foo { background: url(/static/foo.png?query=yes); }',
306 'foo { background: url(http://doc.example.org/static/foo.png?query=yes); }',
309 'Remote URL (unnecessary quotes not preserved)',
310 'foo { background: url("http://example.org/w/unnecessary-quotes.png"); }',
311 'foo { background: url(http://example.org/w/unnecessary-quotes.png); }',
315 'foo { /* @embed */ background: url(red.gif); }',
316 "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; }",
319 'Embedded file, other comments before the rule',
320 "foo { /* Bar. */ /* @embed */ background: url(red.gif); }",
321 "foo { /* Bar. */ background: url($red); /* Bar. */ background: url(http://localhost/w/red.gif?34ac6)!ie; }",
324 'Can not re-embed data: URIs',
325 "foo { /* @embed */ background: url($red); }",
326 "foo { background: url($red); }",
329 'Can not remap data: URIs',
330 "foo { background: url($red); }",
331 "foo { background: url($red); }",
334 'Can not embed remote URLs',
335 'foo { /* @embed */ background: url(http://example.org/w/foo.png); }',
336 'foo { background: url(http://example.org/w/foo.png); }',
339 'Embedded file (inline @embed)',
340 'foo { background: /* @embed */ url(red.gif); }',
341 "foo { background: url($red); "
342 . "background: url(http://localhost/w/red.gif?34ac6)!ie; }",
345 'Can not embed large files',
346 'foo { /* @embed */ background: url(large.png); }',
347 "foo { background: url(http://localhost/w/large.png?e3d1f); }",
350 'SVG files are embedded without base64 encoding and unnecessary IE 6 and 7 fallback',
351 'foo { /* @embed */ background: url(circle.svg); }',
352 "foo { background: url($svg); }",
355 'Two regular files in one rule',
356 'foo { background: url(red.gif), url(green.gif); }',
357 'foo { background: url(http://localhost/w/red.gif?34ac6), '
358 . 'url(http://localhost/w/green.gif?13651); }',
361 'Two embedded files in one rule',
362 'foo { /* @embed */ background: url(red.gif), url(green.gif); }',
363 "foo { background: url($red), url($green); "
364 . "background: url(http://localhost/w/red.gif?34ac6), "
365 . "url(http://localhost/w/green.gif?13651)!ie; }",
368 'Two embedded files in one rule (inline @embed)',
369 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(green.gif); }',
370 "foo { background: url($red), url($green); "
371 . "background: url(http://localhost/w/red.gif?34ac6), "
372 . "url(http://localhost/w/green.gif?13651)!ie; }",
375 'Two embedded files in one rule (inline @embed), one too large',
376 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(large.png); }',
377 "foo { background: url($red), url(http://localhost/w/large.png?e3d1f); "
378 . "background: url(http://localhost/w/red.gif?34ac6), "
379 . "url(http://localhost/w/large.png?e3d1f)!ie; }",
382 'Practical example with some noise',
383 'foo { /* @embed */ background: #f9f9f9 url(red.gif) 0 0 no-repeat; }',
384 "foo { background: #f9f9f9 url($red) 0 0 no-repeat; "
385 . "background: #f9f9f9 url(http://localhost/w/red.gif?34ac6) 0 0 no-repeat!ie; }",
388 'Does not mess with other properties',
389 'foo { color: red; background: url(red.gif); font-size: small; }',
390 'foo { color: red; background: url(http://localhost/w/red.gif?34ac6); font-size: small; }',
393 'Spacing and miscellanea not changed (1)',
394 'foo { background: url(red.gif); }',
395 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
398 'Spacing and miscellanea not changed (2)',
399 'foo {background:url(red.gif)}',
400 'foo {background:url(http://localhost/w/red.gif?34ac6)}',
403 'Spaces within url() parentheses are ignored',
404 'foo { background: url( red.gif ); }',
405 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
408 '@import rule to local file (should we remap this?)',
409 '@import url(/styles.css)',
410 '@import url(http://doc.example.org/styles.css)',
413 '@import rule to local file (should we remap this?)',
414 '@import url(/styles.css)',
415 '@import url(http://doc.example.org/styles.css)',
418 '@import rule to URL',
419 '@import url(//localhost/styles.css?query=val)',
420 '@import url(//localhost/styles.css?query=val)',
423 'Background URL (double quotes)',
424 'foo { background: url("//localhost/styles.css?quoted=double") }',
425 'foo { background: url(//localhost/styles.css?quoted=double) }',
428 'Background URL (single quotes)',
429 'foo { background: url(\'//localhost/styles.css?quoted=single\') }',
430 'foo { background: url(//localhost/styles.css?quoted=single) }',
433 'Background URL (containing parentheses; T60473)',
434 'foo { background: url("//localhost/styles.css?query=(parens)") }',
435 'foo { background: url("//localhost/styles.css?query=(parens)") }',
438 'Background URL (double quoted, containing single quotes; T60473)',
439 'foo { background: url("//localhost/styles.css?quote=\'") }',
440 'foo { background: url("//localhost/styles.css?quote=\'") }',
443 'Background URL (single quoted, containing double quotes; T60473)',
444 'foo { background: url(\'//localhost/styles.css?quote="\') }',
445 'foo { background: url("//localhost/styles.css?quote=\"") }',
448 'Simple case with comments before url',
449 'foo { prop: /* some {funny;} comment */ url(bar.png); }',
450 'foo { prop: /* some {funny;} comment */ url(http://localhost/w/bar.png); }',
453 'Simple case with comments after url',
454 'foo { prop: url(red.gif)/* some {funny;} comment */ ; }',
455 'foo { prop: url(http://localhost/w/red.gif?34ac6)/* some {funny;} comment */ ; }',
458 'Embedded file with comment before url',
459 'foo { /* @embed */ background: /* some {funny;} comment */ url(red.gif); }',
460 "foo { background: /* some {funny;} comment */ url($red); background: /* some {funny;} comment */ url(http://localhost/w/red.gif?34ac6)!ie; }",
463 'Embedded file with comments inside and outside the rule',
464 'foo { /* @embed */ background: url(red.gif) /* some {foo;} comment */; /* some {bar;} comment */ }',
465 "foo { background: url($red) /* some {foo;} comment */; background: url(http://localhost/w/red.gif?34ac6) /* some {foo;} comment */!ie; /* some {bar;} comment */ }",
468 'Embedded file with comment outside the rule',
469 'foo { /* @embed */ background: url(red.gif); /* some {funny;} comment */ }',
470 "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; /* some {funny;} comment */ }",
473 'Rule with two urls, each with comments',
474 '{ background: /*asd*/ url(something.png); background: /*jkl*/ url(something.png); }',
475 '{ background: /*asd*/ url(http://localhost/w/something.png); background: /*jkl*/ url(http://localhost/w/something.png); }',
478 'Sanity check for offending line from jquery.ui.theme.css (T62077)',
479 '.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}*/; }',
480 '.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}*/; }',
483 // @codingStandardsIgnoreEnd
487 * This tests basic functionality of CSSMin::buildUrlValue.
489 * @dataProvider provideBuildUrlValueCases
490 * @covers CSSMin::buildUrlValue
492 public function testBuildUrlValue( $message, $input, $expectedOutput ) {
495 CSSMin
::buildUrlValue( $input ),
496 "CSSMin::buildUrlValue: $message"
500 public static function provideBuildUrlValueCases() {
504 'scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s',
505 'url(scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s)',
509 'data:image/png;base64,R0lGODlh/+==',
510 'url(data:image/png;base64,R0lGODlh/+==)',
514 "https://en.wikipedia.org/wiki/Wendy's",
515 "url(\"https://en.wikipedia.org/wiki/Wendy's\")",
518 'URL with parentheses',
519 'https://en.wikipedia.org/wiki/Boston_(band)',
520 'url("https://en.wikipedia.org/wiki/Boston_(band)")',
526 * Seperated because they are currently broken (T37492)
529 * @dataProvider provideStringCases
530 * @covers CSSMin::remap
532 public function testMinifyWithCSSStringValues( $code, $expectedOutput ) {
533 $this->testMinifyOutput( $code, $expectedOutput );
536 public static function provideStringCases() {
538 // String values should be respected
539 // - More than one space in a string value
540 [ 'foo { content: " "; }', 'foo{content:" "}' ],
541 // - Using a tab in a string value (turns into a space)
542 [ "foo { content: '\t'; }", "foo{content:'\t'}" ],
543 // - Using css-like syntax in string values
545 'foo::after { content: "{;}"; position: absolute; }',
546 'foo::after{content:"{;}";position:absolute}'
552 class CSSMinTestable
extends CSSMin
{
553 // Make some protected methods public
554 public static function isRemoteUrl( $maybeUrl ) {
555 return parent
::isRemoteUrl( $maybeUrl );
557 public static function isLocalUrl( $maybeUrl ) {
558 return parent
::isLocalUrl( $maybeUrl );