MediaWiki  1.29.1
CSSMinTest.php
Go to the documentation of this file.
1 <?php
8 class CSSMinTest extends MediaWikiTestCase {
9 
10  protected function setUp() {
11  parent::setUp();
12 
13  $server = 'http://doc.example.org';
14 
15  $this->setMwGlobals( [
16  'wgServer' => $server,
17  'wgCanonicalServer' => $server,
18  ] );
19  }
20 
24  public function testGetMimeType( $fileContents, $fileExtension, $expected ) {
25  $fileName = wfTempDir() . DIRECTORY_SEPARATOR . uniqid( 'MW_PHPUnit_CSSMinTest_' ) . '.'
26  . $fileExtension;
27  $this->addTmpFiles( $fileName );
28  file_put_contents( $fileName, $fileContents );
29  $this->assertSame( $expected, CSSMin::getMimeType( $fileName ) );
30  }
31 
32  public function mimeTypeProvider() {
33  return [
34  'JPEG with short extension' => [
35  "\xFF\xD8\xFF",
36  'jpg',
37  'image/jpeg'
38  ],
39  'JPEG with long extension' => [
40  "\xFF\xD8\xFF",
41  'jpeg',
42  'image/jpeg'
43  ],
44  'PNG' => [
45  "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
46  'png',
47  'image/png'
48  ],
49 
50  'PNG extension but JPEG content' => [
51  "\xFF\xD8\xFF",
52  'png',
53  'image/png'
54  ],
55  'JPEG extension but PNG content' => [
56  "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
57  'jpg',
58  'image/jpeg'
59  ],
60  'PNG extension but SVG content' => [
61  '<?xml version="1.0"?><svg></svg>',
62  'png',
63  'image/png'
64  ],
65  'SVG extension but PNG content' => [
66  "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A",
67  'svg',
68  'image/svg+xml'
69  ],
70 
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>',
74  'svg',
75  'image/svg+xml'
76  ],
77  'SVG with XML header only' => [
78  '<?xml version="1.0"?><svg></svg>',
79  'svg',
80  'image/svg+xml'
81  ],
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>',
85  'svg',
86  'image/svg+xml'
87  ],
88  'SVG without any header' => [
89  '<svg></svg>',
90  'svg',
91  'image/svg+xml'
92  ],
93  ];
94  }
95 
100  public function testMinify( $code, $expectedOutput ) {
101  $minified = CSSMin::minify( $code );
102 
103  $this->assertEquals(
104  $expectedOutput,
105  $minified,
106  'Minified output should be in the form expected.'
107  );
108  }
109 
110  public static function provideMinifyCases() {
111  return [
112  // Whitespace
113  [ "\r\t\f \v\n\r", "" ],
114  [ "foo, bar {\n\tprop: value;\n}", "foo,bar{prop:value}" ],
115 
116  // Loose comments
117  [ "/* foo */", "" ],
118  [ "/*******\n foo\n *******/", "" ],
119  [ "/*!\n foo\n */", "" ],
120 
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; }" ],
130 
131  // Keep track of things that aren't as minified as much as they
132  // could be (T37493)
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') }" ],
138 
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:" "}' ],
147  ];
148  }
149 
157  public function testRemap( $message, $params, $expectedOutput ) {
158  $remapped = call_user_func_array( 'CSSMin::remap', $params );
159 
160  $messageAdd = " Case: $message";
161  $this->assertEquals(
162  $expectedOutput,
163  $remapped,
164  'CSSMin::remap should return the expected url form.' . $messageAdd
165  );
166  }
167 
168  public static function provideRemapCases() {
169  // Parameter signature:
170  // CSSMin::remap( $code, $local, $remote, $embedData = true )
171  return [
172  [
173  'Simple case',
174  [ 'foo { prop: url(bar.png); }', false, 'http://example.org', false ],
175  'foo { prop: url(http://example.org/bar.png); }',
176  ],
177  [
178  'Without trailing slash',
179  [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux', false ],
180  'foo { prop: url(http://example.org/bar.png); }',
181  ],
182  [
183  'With trailing slash on remote (T29052)',
184  [ 'foo { prop: url(../bar.png); }', false, 'http://example.org/quux/', false ],
185  'foo { prop: url(http://example.org/bar.png); }',
186  ],
187  [
188  'Guard against stripping double slashes from query',
189  [ 'foo { prop: url(bar.png?corge=//grault); }', false, 'http://example.org/quux/', false ],
190  'foo { prop: url(http://example.org/quux/bar.png?corge=//grault); }',
191  ],
192  [
193  'Expand absolute paths',
194  [ 'foo { prop: url(/w/skin/images/bar.png); }', false, 'http://example.org/quux', false ],
195  'foo { prop: url(http://doc.example.org/w/skin/images/bar.png); }',
196  ],
197  ];
198  }
199 
206  public function testRemapRemapping( $message, $input, $expectedOutput ) {
207  $localPath = __DIR__ . '/../../data/cssmin';
208  $remotePath = 'http://localhost/w';
209 
210  $realOutput = CSSMin::remap( $input, $localPath, $remotePath );
211  $this->assertEquals( $expectedOutput, $realOutput, "CSSMin::remap: $message" );
212  }
213 
214  public static function provideIsRemoteUrl() {
215  return [
216  [ true, 'http://localhost/w/red.gif?123' ],
217  [ true, 'https://example.org/x.png' ],
218  [ true, '//example.org/x.y.z/image.png' ],
219  [ true, '//localhost/styles.css?query=yes' ],
220  [ true, 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=' ],
221  [ false, 'x.gif' ],
222  [ false, '/x.gif' ],
223  [ false, './x.gif' ],
224  [ false, '../x.gif' ],
225  ];
226  }
227 
232  public function testIsRemoteUrl( $expect, $url ) {
233  $this->assertEquals( CSSMinTestable::isRemoteUrl( $url ), $expect );
234  }
235 
236  public static function provideIsLocalUrls() {
237  return [
238  [ false, 'x.gif' ],
239  [ true, '/x.gif' ],
240  [ false, './x.gif' ],
241  [ false, '../x.gif' ],
242  ];
243  }
244 
249  public function testIsLocalUrl( $expect, $url ) {
250  $this->assertEquals( CSSMinTestable::isLocalUrl( $url ), $expect );
251  }
252 
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'.
258 
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';
265 
266  // @codingStandardsIgnoreStart Generic.Files.LineLength
267  return [
268  [
269  'Regular file',
270  'foo { background: url(red.gif); }',
271  'foo { background: url(http://localhost/w/red.gif?34ac6); }',
272  ],
273  [
274  'Regular file (missing)',
275  'foo { background: url(theColorOfHerHair.gif); }',
276  'foo { background: url(http://localhost/w/theColorOfHerHair.gif); }',
277  ],
278  [
279  'Remote URL',
280  'foo { background: url(http://example.org/w/foo.png); }',
281  'foo { background: url(http://example.org/w/foo.png); }',
282  ],
283  [
284  'Protocol-relative remote URL',
285  'foo { background: url(//example.org/w/foo.png); }',
286  'foo { background: url(//example.org/w/foo.png); }',
287  ],
288  [
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); }',
292  ],
293  [
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); }',
297  ],
298  [
299  'Domain-relative URL',
300  'foo { background: url(/static/foo.png); }',
301  'foo { background: url(http://doc.example.org/static/foo.png); }',
302  ],
303  [
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); }',
307  ],
308  [
309  'Remote URL (unnecessary quotes not preserved)',
310  'foo { background: url("http://example.org/w/foo.png"); }',
311  'foo { background: url(http://example.org/w/foo.png); }',
312  ],
313  [
314  'Embedded file',
315  'foo { /* @embed */ background: url(red.gif); }',
316  "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; }",
317  ],
318  [
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; }",
322  ],
323  [
324  'Can not re-embed data: URIs',
325  "foo { /* @embed */ background: url($red); }",
326  "foo { background: url($red); }",
327  ],
328  [
329  'Can not remap data: URIs',
330  "foo { background: url($red); }",
331  "foo { background: url($red); }",
332  ],
333  [
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); }',
337  ],
338  [
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; }",
343  ],
344  [
345  'Can not embed large files',
346  'foo { /* @embed */ background: url(large.png); }',
347  "foo { background: url(http://localhost/w/large.png?e3d1f); }",
348  ],
349  [
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); }",
353  ],
354  [
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); }',
359  ],
360  [
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; }",
366  ],
367  [
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; }",
373  ],
374  [
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; }",
380  ],
381  [
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; }",
386  ],
387  [
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; }',
391  ],
392  [
393  'Spacing and miscellanea not changed (1)',
394  'foo { background: url(red.gif); }',
395  'foo { background: url(http://localhost/w/red.gif?34ac6); }',
396  ],
397  [
398  'Spacing and miscellanea not changed (2)',
399  'foo {background:url(red.gif)}',
400  'foo {background:url(http://localhost/w/red.gif?34ac6)}',
401  ],
402  [
403  'Spaces within url() parentheses are ignored',
404  'foo { background: url( red.gif ); }',
405  'foo { background: url(http://localhost/w/red.gif?34ac6); }',
406  ],
407  [
408  '@import rule to local file (should we remap this?)',
409  '@import url(/styles.css)',
410  '@import url(http://doc.example.org/styles.css)',
411  ],
412  [
413  '@import rule to URL (should we remap this?)',
414  '@import url(//localhost/styles.css?query=yes)',
415  '@import url(//localhost/styles.css?query=yes)',
416  ],
417  [
418  'Simple case with comments before url',
419  'foo { prop: /* some {funny;} comment */ url(bar.png); }',
420  'foo { prop: /* some {funny;} comment */ url(http://localhost/w/bar.png); }',
421  ],
422  [
423  'Simple case with comments after url',
424  'foo { prop: url(red.gif)/* some {funny;} comment */ ; }',
425  'foo { prop: url(http://localhost/w/red.gif?34ac6)/* some {funny;} comment */ ; }',
426  ],
427  [
428  'Embedded file with comment before url',
429  'foo { /* @embed */ background: /* some {funny;} comment */ url(red.gif); }',
430  "foo { background: /* some {funny;} comment */ url($red); background: /* some {funny;} comment */ url(http://localhost/w/red.gif?34ac6)!ie; }",
431  ],
432  [
433  'Embedded file with comments inside and outside the rule',
434  'foo { /* @embed */ background: url(red.gif) /* some {foo;} comment */; /* some {bar;} comment */ }',
435  "foo { background: url($red) /* some {foo;} comment */; background: url(http://localhost/w/red.gif?34ac6) /* some {foo;} comment */!ie; /* some {bar;} comment */ }",
436  ],
437  [
438  'Embedded file with comment outside the rule',
439  'foo { /* @embed */ background: url(red.gif); /* some {funny;} comment */ }',
440  "foo { background: url($red); background: url(http://localhost/w/red.gif?34ac6)!ie; /* some {funny;} comment */ }",
441  ],
442  [
443  'Rule with two urls, each with comments',
444  '{ background: /*asd*/ url(something.png); background: /*jkl*/ url(something.png); }',
445  '{ background: /*asd*/ url(http://localhost/w/something.png); background: /*jkl*/ url(http://localhost/w/something.png); }',
446  ],
447  [
448  'Sanity check for offending line from jquery.ui.theme.css (T62077)',
449  '.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}*/; }',
450  '.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}*/; }',
451  ],
452  ];
453  // @codingStandardsIgnoreEnd
454  }
455 
462  public function testBuildUrlValue( $message, $input, $expectedOutput ) {
463  $this->assertEquals(
464  $expectedOutput,
466  "CSSMin::buildUrlValue: $message"
467  );
468  }
469 
470  public static function provideBuildUrlValueCases() {
471  return [
472  [
473  'Full URL',
474  'scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s',
475  'url(scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s)',
476  ],
477  [
478  'data: URI',
479  'data:image/png;base64,R0lGODlh/+==',
480  'url(data:image/png;base64,R0lGODlh/+==)',
481  ],
482  [
483  'URL with quotes',
484  "https://en.wikipedia.org/wiki/Wendy's",
485  "url(\"https://en.wikipedia.org/wiki/Wendy's\")",
486  ],
487  [
488  'URL with parentheses',
489  'https://en.wikipedia.org/wiki/Boston_(band)',
490  'url("https://en.wikipedia.org/wiki/Boston_(band)")',
491  ],
492  ];
493  }
494 
502  public function testMinifyWithCSSStringValues( $code, $expectedOutput ) {
503  $this->testMinifyOutput( $code, $expectedOutput );
504  }
505 
506  public static function provideStringCases() {
507  return [
508  // String values should be respected
509  // - More than one space in a string value
510  [ 'foo { content: " "; }', 'foo{content:" "}' ],
511  // - Using a tab in a string value (turns into a space)
512  [ "foo { content: '\t'; }", "foo{content:'\t'}" ],
513  // - Using css-like syntax in string values
514  [
515  'foo::after { content: "{;}"; position: absolute; }',
516  'foo::after{content:"{;}";position:absolute}'
517  ],
518  ];
519  }
520 }
521 
522 class CSSMinTestable extends CSSMin {
523  // Make some protected methods public
524  public static function isRemoteUrl( $maybeUrl ) {
525  return parent::isRemoteUrl( $maybeUrl );
526  }
527  public static function isLocalUrl( $maybeUrl ) {
528  return parent::isLocalUrl( $maybeUrl );
529  }
530 }
CSSMin\isRemoteUrl
static isRemoteUrl( $maybeUrl)
Is this CSS rule referencing a remote URL?
Definition: CSSMin.php:368
CSSMin\buildUrlValue
static buildUrlValue( $url)
Build a CSS 'url()' value for the given URL, quoting parentheses (and other funny characters) and esc...
Definition: CSSMin.php:207
CSSMin\minify
static minify( $css)
Removes whitespace from CSS data.
Definition: CSSMin.php:452
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
CSSMin\remap
static remap( $source, $local, $remote, $embedData=true)
Remaps CSS URL paths and automatically embeds data URIs for CSS rules or url() values preceded by an ...
Definition: CSSMin.php:229
$params
$params
Definition: styleTest.css.php:40
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$input
if(is_array( $mode)) switch( $mode) $input
Definition: postprocess-phan.php:141
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:658
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
CSSMin\isLocalUrl
static isLocalUrl( $maybeUrl)
Is this CSS rule referencing a local URL?
Definition: CSSMin.php:381
MediaWikiTestCase\addTmpFiles
addTmpFiles( $files)
Definition: MediaWikiTestCase.php:510
MediaWikiTestCase\setUp
setUp()
Definition: MediaWikiTestCase.php:473
wfTempDir
wfTempDir()
Tries to get the system directory for temporary files.
Definition: GlobalFunctions.php:2061
$code
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:783
CSSMin\getMimeType
static getMimeType( $file)
Definition: CSSMin.php:178
CSSMin
Transforms CSS data.
Definition: CSSMin.php:30