3 use Wikimedia\TestingAccessWrapper;
11 protected function setUp() {
15 $server =
'https://expand.example';
17 'wgServer' => $server,
18 'wgCanonicalServer' => $server,
26 public function testGetLocalFileReferences(
$input, $expected ) {
31 'getLocalFileReferences() must find the local file properly'
35 public static function providesReferencedFiles() {
38 [
'url("//example.org")', [] ],
39 [
'url("https://example.org")', [] ],
40 [
'url("#default#")', [] ],
41 [
'url("WikiFont-Glyphs.svg#wikiglyph")', [
'/WikiFont-Glyphs.svg' ] ],
42 [
'url("#some-anchor")', [] ],
50 public function testSerializeStringValue(
$input, $expected ) {
55 'Serialized output must be in the expected form.'
59 public static function provideSerializeStringValue() {
61 [
'Hello World!',
'"Hello World!"' ],
62 [
"Null\0Null",
"\"Null\u{FFFD}Null\"" ],
66 [
"Tab\tTab",
'"Tab\\9 Tab"' ],
67 [
"Space tab \t space",
'"Space tab \\9 space"' ],
68 [
"Line\nfeed",
'"Line\\a feed"' ],
69 [
"Return\rreturn",
'"Return\\d return"' ],
70 [
"Next\u{0085}line",
"\"Next\u{0085}line\"" ],
71 [
"Del\x7fDel",
'"Del\\7f Del"' ],
72 [
"nb\u{00A0}sp",
"\"nb\u{00A0}sp\"" ],
73 [
"AMP&AMP",
"\"AMP&AMP\"" ],
74 [
'!"#$%&\'()*+,-./0123456789:;<=>?',
'"!\\"#$%&\'()*+,-./0123456789:;<=>?"' ],
75 [
'@[\\]^_`{|}~',
'"@[\\\\]^_`{|}~"' ],
87 public function testGetMimeType( $fileContents, $fileExtension, $expected ) {
88 $fileName =
wfTempDir() . DIRECTORY_SEPARATOR . uniqid(
'MW_PHPUnit_CSSMinTest_' ) .
'.'
91 file_put_contents( $fileName, $fileContents );
92 $this->assertSame( $expected, CSSMin::getMimeType( $fileName ) );
95 public static function provideMimeType() {
97 'JPEG with short extension' => [
102 'JPEG with long extension' => [
108 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
113 'PNG extension but JPEG content' => [
118 'JPEG extension but PNG content' => [
119 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
123 'PNG extension but SVG content' => [
124 '<?xml version="1.0"?><svg></svg>',
128 'SVG extension but PNG content' => [
129 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
134 'SVG with all headers' => [
135 '<?xml version="1.0"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
136 .
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>',
140 'SVG with XML header only' => [
141 '<?xml version="1.0"?><svg></svg>',
145 'SVG with DOCTYPE only' => [
146 '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
147 .
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg></svg>',
151 'SVG without any header' => [
163 public function testMinify(
$code, $expectedOutput ) {
164 $minified = CSSMin::minify(
$code );
169 'Minified output should be in the form expected.'
173 public static function provideMinifyCases() {
176 [
"\r\t\f \v\n\r",
"" ],
177 [
"foo, bar {\n\tprop: value;\n}",
"foo,bar{prop:value}" ],
181 [
"/*******\n foo\n *******/",
"" ],
182 [
"/*!\n foo\n */",
"" ],
185 [
"/* comment */foo, bar {\n\tprop: value;\n}",
"foo,bar{prop:value}" ],
186 [
"foo/* comment */, bar {\n\tprop: value;\n}",
"foo,bar{prop:value}" ],
187 [
"foo,/* comment */ bar {\n\tprop: value;\n}",
"foo,bar{prop:value}" ],
188 [
"foo, bar/* comment */ {\n\tprop: value;\n}",
"foo,bar{prop:value}" ],
189 [
"foo, bar {\n\t/* comment */prop: value;\n}",
"foo,bar{prop:value}" ],
190 [
"foo, bar {\n\tprop: /* comment */value;\n}",
"foo,bar{prop:value}" ],
191 [
"foo, bar {\n\tprop: value /* comment */;\n}",
"foo,bar{prop:value }" ],
192 [
"foo, bar {\n\tprop: value; /* comment */\n}",
"foo,bar{prop:value; }" ],
196 [
'foo { prop: value ;}',
'foo{prop:value }' ],
197 [
'foo { prop : value; }',
'foo{prop :value}' ],
198 [
'foo { prop: value ; }',
'foo{prop:value }' ],
199 [
'foo { font-family: "foo" , "bar"; }',
'foo{font-family:"foo" ,"bar"}' ],
200 [
"foo { src:\n\turl('foo') ,\n\turl('bar') ; }",
"foo{src:url('foo') ,url('bar') }" ],
204 [
'foo { content: ""; }',
'foo{content:""}' ],
205 [
"foo { content: ''; }",
"foo{content:''}" ],
206 [
'foo { content: "\'"; }',
'foo{content:"\'"}' ],
207 [
"foo { content: '\"'; }",
"foo{content:'\"'}" ],
209 [
'foo { content: " "; }',
'foo{content:" "}' ],
212 [
'a:not( [ href ] ) { prop: url( foobar.png ); }',
'a:not([href]){prop:url(foobar.png)}' ],
215 [
'foo { prop: url ( foobar.png ); }',
'foo{prop:url (foobar.png)}' ],
219 public static function provideIsRemoteUrl() {
221 [
true,
'http://localhost/w/red.gif?123' ],
222 [
true,
'https://example.org/x.png' ],
223 [
true,
'//example.org/x.y.z/image.png' ],
224 [
true,
'//localhost/styles.css?query=yes' ],
225 [
true,
'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=' ],
231 [
false,
'./x.gif' ],
232 [
false,
'../x.gif' ],
240 public function testIsRemoteUrl( $expect, $url ) {
241 $class = TestingAccessWrapper::newFromClass(
CSSMin::class );
242 $this->assertEquals( $class->isRemoteUrl( $url ), $expect );
245 public static function provideIsLocalUrls() {
252 [
false,
'./x.gif' ],
253 [
false,
'../x.gif' ],
261 public function testIsLocalUrl( $expect, $url ) {
262 $class = TestingAccessWrapper::newFromClass(
CSSMin::class );
263 $this->assertEquals( $class->isLocalUrl( $url ), $expect );
274 public function testRemap( $message,
$params, $expectedOutput ) {
275 $remapped = call_user_func_array(
'CSSMin::remap',
$params );
277 $messageAdd =
" Case: $message";
281 'CSSMin::remap should return the expected url form.' . $messageAdd
285 public static function provideRemapCases() {
291 [
'foo { prop: url(bar.png); }',
false,
'http://example.org',
false ],
292 'foo { prop: url(http://example.org/bar.png); }',
295 'Without trailing slash',
296 [
'foo { prop: url(../bar.png); }',
false,
'http://example.org/quux',
false ],
297 'foo { prop: url(http://example.org/bar.png); }',
300 'With trailing slash on remote (T29052)',
301 [
'foo { prop: url(../bar.png); }',
false,
'http://example.org/quux/',
false ],
302 'foo { prop: url(http://example.org/bar.png); }',
305 'Guard against stripping double slashes from query',
306 [
'foo { prop: url(bar.png?corge=//grault); }',
false,
'http://example.org/quux/',
false ],
307 'foo { prop: url(http://example.org/quux/bar.png?corge=//grault); }',
310 'Expand absolute paths',
311 [
'foo { prop: url(/w/skin/images/bar.png); }',
false,
'http://example.org/quux',
false ],
312 'foo { prop: url(https://expand.example/w/skin/images/bar.png); }',
315 "Don't barf at behavior: url(#default#behaviorName) - T162973",
316 [
'foo { behavior: url(#default#bar); }',
false,
'/w/',
false ],
317 'foo { behavior: url("#default#bar"); }',
321 [
'url(#other)',
false,
'/',
false ],
325 'Keeps anchors after a path',
326 [
'url(images/file.svg#id)',
false,
'/',
false ],
327 'url("/images/file.svg#id")'
340 public function testRemapEmptyUrl(
$params, $expected ) {
341 $remapped = call_user_func_array(
'CSSMin::remap',
$params );
342 $this->assertEquals( $expected, $remapped,
'Ignore empty url' );
345 public static function provideRemapEmptyUrl() {
348 [
"background-image: url();",
false,
'/example',
false ],
349 "background-image: url();",
352 [
"background-image: url('');",
false,
'/example',
false ],
353 "background-image: url('');",
356 [
'background-image: url("");',
false,
'/example',
false ],
357 'background-image: url("");',
359 'Single quote with outer spacing' => [
360 [
"background-image: url( '' );",
false,
'/example',
false ],
361 "background-image: url( '' );",
373 public function testRemapRemapping( $message,
$input, $expectedOutput ) {
374 $localPath = __DIR__ .
'/../../data/cssmin';
375 $remotePath =
'http://localhost/w';
377 $realOutput = CSSMin::remap(
$input, $localPath, $remotePath );
378 $this->assertEquals( $expectedOutput, $realOutput,
"CSSMin::remap: $message" );
381 public static function provideRemapRemappingCases() {
388 $red =
'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=';
389 $green =
'data:image/gif;base64,R0lGODlhAQABAIAAAACAADAAACwAAAAAAQABAAACAkQBADs=';
390 $svg =
'data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%228'
391 .
'%22 height=%228%22 viewBox=%220 0 8 8%22%3E %3Ccircle cx=%224%22 cy=%224%22 '
392 .
'r=%222%22/%3E %3Ca xmlns:xlink=%22http://www.w3.org/1999/xlink%22 xlink:title='
393 .
'%22%3F%3E%22%3Etest%3C/a%3E %3C/svg%3E';
399 'foo { background: url(red.gif); }',
400 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
403 'Regular file (missing)',
404 'foo { background: url(theColorOfHerHair.gif); }',
405 'foo { background: url(http://localhost/w/theColorOfHerHair.gif); }',
409 'foo { background: url(http://example.org/w/foo.png); }',
410 'foo { background: url(http://example.org/w/foo.png); }',
413 'Protocol-relative remote URL',
414 'foo { background: url(//example.org/w/foo.png); }',
415 'foo { background: url(//example.org/w/foo.png); }',
418 'Remote URL with query',
419 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
420 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
423 'Protocol-relative remote URL with query',
424 'foo { background: url(//example.org/w/foo.png?query=yes); }',
425 'foo { background: url(//example.org/w/foo.png?query=yes); }',
428 'Domain-relative URL',
429 'foo { background: url(/static/foo.png); }',
430 'foo { background: url(https://expand.example/static/foo.png); }',
433 'Domain-relative URL with query',
434 'foo { background: url(/static/foo.png?query=yes); }',
435 'foo { background: url(https://expand.example/static/foo.png?query=yes); }',
438 'Path-relative URL with query',
439 "foo { background: url(?query=yes); }",
440 'foo { background: url(http://localhost/w/?query=yes); }',
443 'Remote URL (unnecessary quotes not preserved)',
444 'foo { background: url("http://example.org/w/unnecessary-quotes.png"); }',
445 'foo { background: url(http://example.org/w/unnecessary-quotes.png); }',
449 'foo { /* @embed */ background: url(red.gif); }',
450 "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; }",
453 'Embedded file, other comments before the rule',
454 "foo { /* Bar. */ /* @embed */ background: url(red.gif); }",
455 "foo { /* Bar. */ background: url($red); /* Bar. */ background: url(http://localhost/w/red.gif?34ac6)!ie; }",
458 'Can not re-embed data: URIs',
459 "foo { /* @embed */ background: url($red); }",
460 "foo { background: url($red); }",
463 'Can not remap data: URIs',
464 "foo { background: url($red); }",
465 "foo { background: url($red); }",
468 'Can not embed remote URLs',
469 'foo { /* @embed */ background: url(http://example.org/w/foo.png); }',
470 'foo { background: url(http://example.org/w/foo.png); }',
473 'Embedded file (inline @embed)',
474 'foo { background: /* @embed */ url(red.gif); }',
475 "foo { background: url($red); "
476 .
"background: url(http://localhost/w/red.gif?34ac6)!ie; }",
479 'Can not embed large files',
480 'foo { /* @embed */ background: url(large.png); }',
481 "foo { background: url(http://localhost/w/large.png?e3d1f); }",
484 'SVG files are embedded without base64 encoding and unnecessary IE 6 and 7 fallback',
485 'foo { /* @embed */ background: url(circle.svg); }',
486 "foo { background: url(\"$svg\"); }",
489 'Two regular files in one rule',
490 'foo { background: url(red.gif), url(green.gif); }',
491 'foo { background: url(http://localhost/w/red.gif?34ac6), '
492 .
'url(http://localhost/w/green.gif?13651); }',
495 'Two embedded files in one rule',
496 'foo { /* @embed */ background: url(red.gif), url(green.gif); }',
497 "foo { background: url($red), url($green); "
498 .
"background: url(http://localhost/w/red.gif?34ac6), "
499 .
"url(http://localhost/w/green.gif?13651)!ie; }",
502 'Two embedded files in one rule (inline @embed)',
503 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(green.gif); }',
504 "foo { background: url($red), url($green); "
505 .
"background: url(http://localhost/w/red.gif?34ac6), "
506 .
"url(http://localhost/w/green.gif?13651)!ie; }",
509 'Two embedded files in one rule (inline @embed), one too large',
510 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(large.png); }',
511 "foo { background: url($red), url(http://localhost/w/large.png?e3d1f); "
512 .
"background: url(http://localhost/w/red.gif?34ac6), "
513 .
"url(http://localhost/w/large.png?e3d1f)!ie; }",
516 'Practical example with some noise',
517 'foo { /* @embed */ background: #f9f9f9 url(red.gif) 0 0 no-repeat; }',
518 "foo { background: #f9f9f9 url($red) 0 0 no-repeat; "
519 .
"background: #f9f9f9 url(http://localhost/w/red.gif?34ac6) 0 0 no-repeat!ie; }",
522 'Does not mess with other properties',
523 'foo { color: red; background: url(red.gif); font-size: small; }',
524 'foo { color: red; background: url(http://localhost/w/red.gif?34ac6); font-size: small; }',
527 'Spacing and miscellanea not changed (1)',
528 'foo { background: url(red.gif); }',
529 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
532 'Spacing and miscellanea not changed (2)',
533 'foo {background:url(red.gif)}',
534 'foo {background:url(http://localhost/w/red.gif?34ac6)}',
537 'Spaces within url() parentheses are ignored',
538 'foo { background: url( red.gif ); }',
539 'foo { background: url(http://localhost/w/red.gif?34ac6); }',
542 '@import rule to local file (should we remap this?)',
543 '@import url(/styles.css)',
544 '@import url(https://expand.example/styles.css)',
547 '@import rule to local file (should we remap this?)',
548 '@import url(/styles.css)',
549 '@import url(https://expand.example/styles.css)',
552 '@import rule to URL',
553 '@import url(//localhost/styles.css?query=val)',
554 '@import url(//localhost/styles.css?query=val)',
557 'Background URL (double quotes)',
558 'foo { background: url("//localhost/styles.css?quoted=double") }',
559 'foo { background: url(//localhost/styles.css?quoted=double) }',
562 'Background URL (single quotes)',
563 'foo { background: url(\'//localhost/styles.css?quoted=single\') }',
564 'foo { background: url(//localhost/styles.css?quoted=single) }',
567 'Background URL (double quoted, containing parentheses; T60473)',
568 'foo { background: url("//localhost/styles.css?query=(parens)") }',
569 'foo { background: url("//localhost/styles.css?query=(parens)") }',
572 'Background URL (double quoted, containing single quotes; T60473)',
573 'foo { background: url("//localhost/styles.css?quote=\'") }',
574 'foo { background: url("//localhost/styles.css?quote=\'") }',
577 'Background URL (single quoted, containing double quotes; T60473)',
578 'foo { background: url(\'//localhost/styles.css?quote="\') }',
579 'foo { background: url("//localhost/styles.css?quote=\"") }',
582 'Background URL (double quoted with outer spacing)',
583 'foo { background: url( "http://localhost/styles.css?quoted=double" ) }',
584 'foo { background: url(http://localhost/styles.css?quoted=double) }',
587 'Background URL (single quoted, containing spaces, with outer spacing)',
588 "foo { background: url( ' red.gif ' ); }",
589 'foo { background: url("http://localhost/w/ red.gif "); }',
592 'Simple case with comments before url',
593 'foo { prop: /* some {funny;} comment */ url(bar.png); }',
594 'foo { prop: /* some {funny;} comment */ url(http://localhost/w/bar.png); }',
597 'Simple case with comments after url',
598 'foo { prop: url(red.gif)/* some {funny;} comment */ ; }',
599 'foo { prop: url(http://localhost/w/red.gif?34ac6)/* some {funny;} comment */ ; }',
602 'Embedded file with comment before url',
603 'foo { /* @embed */ background: /* some {funny;} comment */ url(red.gif); }',
604 "foo { background: /* some {funny;} comment */ url($red); background: /* some {funny;} comment */ url(http://localhost/w/red.gif?34ac6)!ie; }",
607 'Embedded file with comments inside and outside the rule',
608 'foo { /* @embed */ background: url(red.gif) /* some {foo;} comment */; /* some {bar;} comment */ }',
609 "foo { background: url($red) /* some {foo;} comment */; background: url(http://localhost/w/red.gif?34ac6) /* some {foo;} comment */!ie; /* some {bar;} comment */ }",
612 'Embedded file with comment outside the rule',
613 'foo { /* @embed */ background: url(red.gif); /* some {funny;} comment */ }',
614 "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; /* some {funny;} comment */ }",
617 'Rule with two urls, each with comments',
618 '{ background: /*asd*/ url(something.png); background: /*jkl*/ url(something.png); }',
619 '{ background: /*asd*/ url(http://localhost/w/something.png); background: /*jkl*/ url(http://localhost/w/something.png); }',
622 'Sanity check for offending line from jquery.ui.theme.css (T62077)',
623 '.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}*/; }',
624 '.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}*/; }',
636 public function testBuildUrlValue( $message,
$input, $expectedOutput ) {
639 CSSMin::buildUrlValue(
$input ),
640 "CSSMin::buildUrlValue: $message"
644 public static function provideBuildUrlValueCases() {
648 'scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s',
649 'url(scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s)',
653 'data:image/png;base64,R0lGODlh/+==',
654 'url(data:image/png;base64,R0lGODlh/+==)',
658 "https://en.wikipedia.org/wiki/Wendy's",
659 "url(\"https://en.wikipedia.org/wiki/Wendy's\")",
662 'URL with parentheses',
663 'https://en.wikipedia.org/wiki/Boston_(band)',
664 'url("https://en.wikipedia.org/wiki/Boston_(band)")',
676 public function testMinifyWithCSSStringValues(
$code, $expectedOutput ) {
677 $this->testMinifyOutput(
$code, $expectedOutput );
680 public static function provideStringCases() {
684 [
'foo { content: " "; }',
'foo{content:" "}' ],
686 [
"foo { content: '\t'; }",
"foo{content:'\t'}" ],
689 'foo::after { content: "{;}"; position: absolute; }',
690 'foo::after{content:"{;}";position:absolute}'