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( array(
16 'wgServer' => $server,
17 'wgCanonicalServer' => $server,
22 * @dataProvider provideMinifyCases
23 * @covers CSSMin::minify
25 public function testMinify( $code, $expectedOutput ) {
26 $minified = CSSMin
::minify( $code );
31 'Minified output should be in the form expected.'
35 public static function provideMinifyCases() {
38 array( "\r\t\f \v\n\r", "" ),
39 array( "foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
42 array( "/* foo */", "" ),
43 array( "/*******\n foo\n *******/", "" ),
44 array( "/*!\n foo\n */", "" ),
46 // Inline comments in various different places
47 array( "/* comment */foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
48 array( "foo/* comment */, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
49 array( "foo,/* comment */ bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
50 array( "foo, bar/* comment */ {\n\tprop: value;\n}", "foo,bar{prop:value}" ),
51 array( "foo, bar {\n\t/* comment */prop: value;\n}", "foo,bar{prop:value}" ),
52 array( "foo, bar {\n\tprop: /* comment */value;\n}", "foo,bar{prop:value}" ),
53 array( "foo, bar {\n\tprop: value /* comment */;\n}", "foo,bar{prop:value }" ),
54 array( "foo, bar {\n\tprop: value; /* comment */\n}", "foo,bar{prop:value; }" ),
56 // Keep track of things that aren't as minified as much as they
57 // could be (bug 35493)
58 array( 'foo { prop: value ;}', 'foo{prop:value }' ),
59 array( 'foo { prop : value; }', 'foo{prop :value}' ),
60 array( 'foo { prop: value ; }', 'foo{prop:value }' ),
61 array( 'foo { font-family: "foo" , "bar"; }', 'foo{font-family:"foo" ,"bar"}' ),
62 array( "foo { src:\n\turl('foo') ,\n\turl('bar') ; }", "foo{src:url('foo') ,url('bar') }" ),
64 // Interesting cases with string values
65 // - Double quotes, single quotes
66 array( 'foo { content: ""; }', 'foo{content:""}' ),
67 array( "foo { content: ''; }", "foo{content:''}" ),
68 array( 'foo { content: "\'"; }', 'foo{content:"\'"}' ),
69 array( "foo { content: '\"'; }", "foo{content:'\"'}" ),
70 // - Whitespace in string values
71 array( 'foo { content: " "; }', 'foo{content:" "}' ),
76 * This tests funky parameters to CSSMin::remap. testRemapRemapping tests
77 * the basic functionality.
79 * @dataProvider provideRemapCases
80 * @covers CSSMin::remap
82 public function testRemap( $message, $params, $expectedOutput ) {
83 $remapped = call_user_func_array( 'CSSMin::remap', $params );
85 $messageAdd = " Case: $message";
89 'CSSMin::remap should return the expected url form.' . $messageAdd
93 public static function provideRemapCases() {
94 // Parameter signature:
95 // CSSMin::remap( $code, $local, $remote, $embedData = true )
99 array( 'foo { prop: url(bar.png); }', false, 'http://example.org', false ),
100 'foo { prop: url(http://example.org/bar.png); }',
103 'Without trailing slash',
104 array( 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux', false ),
105 'foo { prop: url(http://example.org/quux/../bar.png); }',
108 'With trailing slash on remote (bug 27052)',
109 array( 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux/', false ),
110 'foo { prop: url(http://example.org/quux/../bar.png); }',
113 'Guard against stripping double slashes from query',
114 array( 'foo { prop: url(bar.png?corge=//grault); }', false, 'http://example.org/quux/', false ),
115 'foo { prop: url(http://example.org/quux/bar.png?corge=//grault); }',
118 'Expand absolute paths',
119 array( 'foo { prop: url(/w/skin/images/bar.png); }', false, 'http://example.org/quux', false ),
120 'foo { prop: url(http://doc.example.org/w/skin/images/bar.png); }',
126 * This tests basic functionality of CSSMin::remap. testRemapRemapping tests funky parameters.
128 * @dataProvider provideRemapRemappingCases
129 * @covers CSSMin::remap
131 public function testRemapRemapping( $message, $input, $expectedOutput ) {
132 $localPath = __DIR__
. '/../../data/cssmin/';
133 $remotePath = 'http://localhost/w/';
135 $realOutput = CSSMin
::remap( $input, $localPath, $remotePath );
139 preg_replace( '/\d+-\d+-\d+T\d+:\d+:\d+Z/', 'timestamp', $realOutput ),
140 "CSSMin::remap: $message"
144 public static function provideIsRemoteUrl() {
146 array( true, 'http://localhost/w/red.gif?123' ),
147 array( true, 'https://example.org/x.png' ),
148 array( true, '//example.org/x.y.z/image.png' ),
149 array( true, '//localhost/styles.css?query=yes' ),
150 array( true, 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=' ),
151 array( false, 'x.gif' ),
152 array( false, '/x.gif' ),
153 array( false, './x.gif' ),
154 array( false, '../x.gif' ),
159 * @dataProvider provideIsRemoteUrl
160 * @cover CSSMin::isRemoteUrl
162 public function testIsRemoteUrl( $expect, $url ) {
163 $this->assertEquals( CSSMin
::isRemoteUrl( $url ), $expect );
166 public static function provideIsLocalUrls() {
168 array( false, 'x.gif' ),
169 array( true, '/x.gif' ),
170 array( false, './x.gif' ),
171 array( false, '../x.gif' ),
176 * @dataProvider provideIsLocalUrls
177 * @cover CSSMin::isLocalUrl
179 public function testIsLocalUrl( $expect, $url ) {
180 $this->assertEquals( CSSMin
::isLocalUrl( $url ), $expect );
183 public static function provideRemapRemappingCases() {
184 // red.gif and green.gif are one-pixel 35-byte GIFs.
185 // large.png is a 35K PNG that should be non-embeddable.
186 // Full paths start with http://localhost/w/.
187 // Timestamps in output are replaced with 'timestamp'.
189 // data: URIs for red.gif, green.gif, circle.svg
190 $red = 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=';
191 $green = 'data:image/gif;base64,R0lGODlhAQABAIAAAACAADAAACwAAAAAAQABAAACAkQBADs=';
192 $svg = 'data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A'
193 . '%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%228%22%20height%3D'
194 . '%228%22%3E%0A%3Ccircle%20cx%3D%224%22%20cy%3D%224%22%20r%3D%222%22%2F%3E%0A%3C%2Fsvg%3E%0A';
199 'foo { background: url(red.gif); }',
200 'foo { background: url(http://localhost/w/red.gif?timestamp); }',
203 'Regular file (missing)',
204 'foo { background: url(theColorOfHerHair.gif); }',
205 'foo { background: url(http://localhost/w/theColorOfHerHair.gif); }',
209 'foo { background: url(http://example.org/w/foo.png); }',
210 'foo { background: url(http://example.org/w/foo.png); }',
213 'Protocol-relative remote URL',
214 'foo { background: url(//example.org/w/foo.png); }',
215 'foo { background: url(//example.org/w/foo.png); }',
218 'Remote URL with query',
219 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
220 'foo { background: url(http://example.org/w/foo.png?query=yes); }',
223 'Protocol-relative remote URL with query',
224 'foo { background: url(//example.org/w/foo.png?query=yes); }',
225 'foo { background: url(//example.org/w/foo.png?query=yes); }',
228 'Domain-relative URL',
229 'foo { background: url(/static/foo.png); }',
230 'foo { background: url(http://doc.example.org/static/foo.png); }',
233 'Domain-relative URL with query',
234 'foo { background: url(/static/foo.png?query=yes); }',
235 'foo { background: url(http://doc.example.org/static/foo.png?query=yes); }',
238 'Remote URL (unnecessary quotes not preserved)',
239 'foo { background: url("http://example.org/w/foo.png"); }',
240 'foo { background: url(http://example.org/w/foo.png); }',
244 'foo { /* @embed */ background: url(red.gif); }',
245 "foo { background: url($red); background: url(http://localhost/w/red.gif?timestamp)!ie; }",
248 'Embedded file, other comments before the rule',
249 "foo { /* Bar. */ /* @embed */ background: url(red.gif); }",
250 "foo { /* Bar. */ background: url($red); /* Bar. */ background: url(http://localhost/w/red.gif?timestamp)!ie; }",
253 'Can not re-embed data: URIs',
254 "foo { /* @embed */ background: url($red); }",
255 "foo { background: url($red); }",
258 'Can not remap data: URIs',
259 "foo { background: url($red); }",
260 "foo { background: url($red); }",
263 'Can not embed remote URLs',
264 'foo { /* @embed */ background: url(http://example.org/w/foo.png); }',
265 'foo { background: url(http://example.org/w/foo.png); }',
268 'Embedded file (inline @embed)',
269 'foo { background: /* @embed */ url(red.gif); }',
270 "foo { background: url($red); "
271 . "background: url(http://localhost/w/red.gif?timestamp)!ie; }",
274 'Can not embed large files',
275 'foo { /* @embed */ background: url(large.png); }',
276 "foo { background: url(http://localhost/w/large.png?timestamp); }",
279 'SVG files are embedded without base64 encoding and unnecessary IE 6 and 7 fallback',
280 'foo { /* @embed */ background: url(circle.svg); }',
281 "foo { background: url($svg); }",
284 'Two regular files in one rule',
285 'foo { background: url(red.gif), url(green.gif); }',
286 'foo { background: url(http://localhost/w/red.gif?timestamp), '
287 . 'url(http://localhost/w/green.gif?timestamp); }',
290 'Two embedded files in one rule',
291 'foo { /* @embed */ background: url(red.gif), url(green.gif); }',
292 "foo { background: url($red), url($green); "
293 . "background: url(http://localhost/w/red.gif?timestamp), "
294 . "url(http://localhost/w/green.gif?timestamp)!ie; }",
297 'Two embedded files in one rule (inline @embed)',
298 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(green.gif); }',
299 "foo { background: url($red), url($green); "
300 . "background: url(http://localhost/w/red.gif?timestamp), "
301 . "url(http://localhost/w/green.gif?timestamp)!ie; }",
304 'Two embedded files in one rule (inline @embed), one too large',
305 'foo { background: /* @embed */ url(red.gif), /* @embed */ url(large.png); }',
306 "foo { background: url($red), url(http://localhost/w/large.png?timestamp); "
307 . "background: url(http://localhost/w/red.gif?timestamp), "
308 . "url(http://localhost/w/large.png?timestamp)!ie; }",
311 'Practical example with some noise',
312 'foo { /* @embed */ background: #f9f9f9 url(red.gif) 0 0 no-repeat; }',
313 "foo { background: #f9f9f9 url($red) 0 0 no-repeat; "
314 . "background: #f9f9f9 url(http://localhost/w/red.gif?timestamp) 0 0 no-repeat!ie; }",
317 'Does not mess with other properties',
318 'foo { color: red; background: url(red.gif); font-size: small; }',
319 'foo { color: red; background: url(http://localhost/w/red.gif?timestamp); font-size: small; }',
322 'Spacing and miscellanea not changed (1)',
323 'foo { background: url(red.gif); }',
324 'foo { background: url(http://localhost/w/red.gif?timestamp); }',
327 'Spacing and miscellanea not changed (2)',
328 'foo {background:url(red.gif)}',
329 'foo {background:url(http://localhost/w/red.gif?timestamp)}',
332 'Spaces within url() parentheses are ignored',
333 'foo { background: url( red.gif ); }',
334 'foo { background: url(http://localhost/w/red.gif?timestamp); }',
337 '@import rule to local file (should we remap this?)',
338 '@import url(/styles.css)',
339 '@import url(http://doc.example.org/styles.css)',
342 '@import rule to URL (should we remap this?)',
343 '@import url(//localhost/styles.css?query=yes)',
344 '@import url(//localhost/styles.css?query=yes)',
347 'Simple case with comments before url',
348 'foo { prop: /* some {funny;} comment */ url(bar.png); }',
349 'foo { prop: /* some {funny;} comment */ url(http://localhost/w/bar.png); }',
352 'Simple case with comments after url',
353 'foo { prop: url(red.gif)/* some {funny;} comment */ ; }',
354 'foo { prop: url(http://localhost/w/red.gif?timestamp)/* some {funny;} comment */ ; }',
357 'Embedded file with comment before url',
358 'foo { /* @embed */ background: /* some {funny;} comment */ url(red.gif); }',
359 "foo { background: /* some {funny;} comment */ url($red); background: /* some {funny;} comment */ url(http://localhost/w/red.gif?timestamp)!ie; }",
362 'Embedded file with comments inside and outside the rule',
363 'foo { /* @embed */ background: url(red.gif) /* some {foo;} comment */; /* some {bar;} comment */ }',
364 "foo { background: url($red) /* some {foo;} comment */; background: url(http://localhost/w/red.gif?timestamp) /* some {foo;} comment */!ie; /* some {bar;} comment */ }",
367 'Embedded file with comment outside the rule',
368 'foo { /* @embed */ background: url(red.gif); /* some {funny;} comment */ }',
369 "foo { background: url($red); background: url(http://localhost/w/red.gif?timestamp)!ie; /* some {funny;} comment */ }",
372 'Rule with two urls, each with comments',
373 '{ background: /*asd*/ url(something.png); background: /*jkl*/ url(something.png); }',
374 '{ background: /*asd*/ url(http://localhost/w/something.png); background: /*jkl*/ url(http://localhost/w/something.png); }',
377 'Sanity check for offending line from jquery.ui.theme.css (bug 60077)',
378 '.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}*/; }',
379 '.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}*/; }',
385 * This tests basic functionality of CSSMin::buildUrlValue.
387 * @dataProvider provideBuildUrlValueCases
388 * @covers CSSMin::buildUrlValue
390 public function testBuildUrlValue( $message, $input, $expectedOutput ) {
393 CSSMin
::buildUrlValue( $input ),
394 "CSSMin::buildUrlValue: $message"
398 public static function provideBuildUrlValueCases() {
402 'scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s',
403 'url(scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s)',
407 'data:image/png;base64,R0lGODlh/+==',
408 'url(data:image/png;base64,R0lGODlh/+==)',
412 "https://en.wikipedia.org/wiki/Wendy's",
413 "url(\"https://en.wikipedia.org/wiki/Wendy's\")",
416 'URL with parentheses',
417 'https://en.wikipedia.org/wiki/Boston_(band)',
418 'url("https://en.wikipedia.org/wiki/Boston_(band)")',
424 * Seperated because they are currently broken (bug 35492)
427 * @dataProvider provideStringCases
428 * @covers CSSMin::remap
430 public function testMinifyWithCSSStringValues( $code, $expectedOutput ) {
431 $this->testMinifyOutput( $code, $expectedOutput );
434 public static function provideStringCases() {
436 // String values should be respected
437 // - More than one space in a string value
438 array( 'foo { content: " "; }', 'foo{content:" "}' ),
439 // - Using a tab in a string value (turns into a space)
440 array( "foo { content: '\t'; }", "foo{content:'\t'}" ),
441 // - Using css-like syntax in string values
443 'foo::after { content: "{;}"; position: absolute; }',
444 'foo::after{content:"{;}";position:absolute}'