Merge "thumb_handler.php doesn't seem to extract path_info correctly"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / CSSJanusTest.php
1 <?php
2 /**
3 * Based on the test suite of the original Python
4 * CSSJanus libary:
5 * http://code.google.com/p/cssjanus/source/browse/trunk/cssjanus_test.py
6 * Ported to PHP for ResourceLoader and has been extended since.
7 */
8 class CSSJanusTest extends MediaWikiTestCase {
9 /**
10 * @dataProvider provideTransformCases
11 */
12 function testTransform( $cssA, $cssB = null ) {
13
14 if ( $cssB ) {
15 $transformedA = CSSJanus::transform( $cssA );
16 $this->assertEquals( $transformedA, $cssB, 'Test A-B transformation' );
17
18 $transformedB = CSSJanus::transform( $cssB );
19 $this->assertEquals( $transformedB, $cssA, 'Test B-A transformation' );
20 } else {
21 // If no B version is provided, it means
22 // the output should equal the input.
23 $transformedA = CSSJanus::transform( $cssA );
24 $this->assertEquals( $transformedA, $cssA, 'Nothing was flipped' );
25 }
26 }
27
28 /**
29 * @dataProvider provideTransformAdvancedCases
30 */
31 function testTransformAdvanced( $code, $expectedOutput, $options = array() ) {
32 $swapLtrRtlInURL = isset( $options['swapLtrRtlInURL'] ) ? $options['swapLtrRtlInURL'] : false;
33 $swapLeftRightInURL = isset( $options['swapLeftRightInURL'] ) ? $options['swapLeftRightInURL'] : false;
34
35 $flipped = CSSJanus::transform( $code, $swapLtrRtlInURL, $swapLeftRightInURL );
36
37 $this->assertEquals( $expectedOutput, $flipped,
38 'Test flipping, options: url-ltr-rtl=' . ( $swapLtrRtlInURL ? 'true' : 'false' )
39 . ' url-left-right=' . ( $swapLeftRightInURL ? 'true' : 'false' )
40 );
41 }
42
43 /**
44 * @dataProvider provideTransformBrokenCases
45 * @group Broken
46 */
47 function testTransformBroken( $code, $expectedOutput ) {
48 $flipped = CSSJanus::transform( $code );
49
50 $this->assertEquals( $expectedOutput, $flipped, 'Test flipping' );
51 }
52
53 /**
54 * These transform cases are tested *in both directions*
55 * No need to declare a principle twice in both directions here.
56 */
57 public static function provideTransformCases() {
58 return array(
59 // Property keys
60 array(
61 '.foo { left: 0; }',
62 '.foo { right: 0; }'
63 ),
64 // Guard against partial keys
65 // (CSS currently doesn't have flippable properties
66 // that contain the direction as part of the key without
67 // dash separation)
68 array(
69 '.foo { alright: 0; }'
70 ),
71 array(
72 '.foo { balleft: 0; }'
73 ),
74
75 // Dashed property keys
76 array(
77 '.foo { padding-left: 0; }',
78 '.foo { padding-right: 0; }'
79 ),
80 array(
81 '.foo { margin-left: 0; }',
82 '.foo { margin-right: 0; }'
83 ),
84 array(
85 '.foo { border-left: 0; }',
86 '.foo { border-right: 0; }'
87 ),
88
89 // Double-dashed property keys
90 array(
91 '.foo { border-left-color: red; }',
92 '.foo { border-right-color: red; }'
93 ),
94 array(
95 // Includes unknown properties?
96 '.foo { x-left-y: 0; }',
97 '.foo { x-right-y: 0; }'
98 ),
99
100 // Multi-value properties
101 array(
102 '.foo { padding: 0; }'
103 ),
104 array(
105 '.foo { padding: 0 1px; }'
106 ),
107 array(
108 '.foo { padding: 0 1px 2px; }'
109 ),
110 array(
111 '.foo { padding: 0 1px 2px 3px; }',
112 '.foo { padding: 0 3px 2px 1px; }'
113 ),
114
115 // Shorthand / Four notation
116 array(
117 '.foo { padding: .25em 15px 0pt 0ex; }',
118 '.foo { padding: .25em 0ex 0pt 15px; }'
119 ),
120 array(
121 '.foo { margin: 1px -4px 3px 2px; }',
122 '.foo { margin: 1px 2px 3px -4px; }'
123 ),
124 array(
125 '.foo { padding: 0 15px .25em 0; }',
126 '.foo { padding: 0 0 .25em 15px; }'
127 ),
128 array(
129 '.foo { padding: 1px 4.1grad 3px 2%; }',
130 '.foo { padding: 1px 2% 3px 4.1grad; }'
131 ),
132 array(
133 '.foo { padding: 1px 2px 3px auto; }',
134 '.foo { padding: 1px auto 3px 2px; }'
135 ),
136 array(
137 '.foo { padding: 1px inherit 3px auto; }',
138 '.foo { padding: 1px auto 3px inherit; }'
139 ),
140 // border-radius assigns different meanings to the values
141 array(
142 '.foo { border-radius: .25em 15px 0pt 0ex; }',
143 '.foo { border-radius: 15px .25em 0ex 0pt; }'
144 ),
145 array(
146 '.foo { border-radius: 0px 0px 5px 5px; }',
147 ),
148 // Ensure the rule doesn't break other stuff
149 array(
150 '.foo { x-unknown: a b c d; }'
151 ),
152 array(
153 '.foo barpx 0 2% { opacity: 0; }'
154 ),
155 array(
156 '#settings td p strong'
157 ),
158 array(
159 // Color names
160 '.foo { border-color: red green blue white }',
161 '.foo { border-color: red white blue green }',
162 ),
163 array(
164 // Color name, hexdecimal, RGB & RGBA
165 '.foo { border-color: red #f00 rgb(255, 0, 0) rgba(255, 0, 0, 0.5) }',
166 '.foo { border-color: red rgba(255, 0, 0, 0.5) rgb(255, 0, 0) #f00 }',
167 ),
168 array(
169 // Color name, hexdecimal, HSL & HSLA
170 '.foo { border-color: red #f00 hsl(0, 100%, 50%) hsla(0, 100%, 50%, 0.5) }',
171 '.foo { border-color: red hsla(0, 100%, 50%, 0.5) hsl(0, 100%, 50%) #f00 }',
172 ),
173 array(
174 // Do not mangle 5 or more values
175 '.foo { -x-unknown: 1 2 3 4 5; }'
176 ),
177 array(
178 '.foo { -x-unknown: 1 2 3 4 5 6; }'
179 ),
180
181 // Shorthand / Three notation
182 array(
183 '.foo { margin: 1em 0 .25em; }'
184 ),
185 array(
186 '.foo { margin:-1.5em 0 -.75em; }'
187 ),
188
189 // Shorthand / Two notation
190 array(
191 '.foo { padding: 1px 2px; }'
192 ),
193
194 // Shorthand / One notation
195 array(
196 '.foo { padding: 1px; }'
197 ),
198
199 // Direction
200 // Note: This differs from the Python implementation,
201 // see also CSSJanus::fixDirection for more info.
202 array(
203 '.foo { direction: ltr; }',
204 '.foo { direction: rtl; }'
205 ),
206 array(
207 '.foo { direction: rtl; }',
208 '.foo { direction: ltr; }'
209 ),
210 array(
211 'input { direction: ltr; }',
212 'input { direction: rtl; }'
213 ),
214 array(
215 'input { direction: rtl; }',
216 'input { direction: ltr; }'
217 ),
218 array(
219 'body { direction: ltr; }',
220 'body { direction: rtl; }'
221 ),
222 array(
223 '.foo, body, input { direction: ltr; }',
224 '.foo, body, input { direction: rtl; }'
225 ),
226 array(
227 'body { padding: 10px; direction: ltr; }',
228 'body { padding: 10px; direction: rtl; }'
229 ),
230 array(
231 'body { direction: ltr } .myClass { direction: ltr }',
232 'body { direction: rtl } .myClass { direction: rtl }'
233 ),
234
235 // Left/right values
236 array(
237 '.foo { float: left; }',
238 '.foo { float: right; }'
239 ),
240 array(
241 '.foo { text-align: left; }',
242 '.foo { text-align: right; }'
243 ),
244 array(
245 '.foo { -x-unknown: left; }',
246 '.foo { -x-unknown: right; }'
247 ),
248 // Guard against selectors that look flippable
249 array(
250 '.column-left { width: 0; }'
251 ),
252 array(
253 'a.left { width: 0; }'
254 ),
255 array(
256 'a.leftification { width: 0; }'
257 ),
258 array(
259 'a.ltr { width: 0; }'
260 ),
261 array(
262 # <div class="a-ltr png">
263 '.a-ltr.png { width: 0; }'
264 ),
265 array(
266 # <foo-ltr attr="x">
267 'foo-ltr[attr="x"] { width: 0; }'
268 ),
269 array(
270 'div.left > span.right+span.left { width: 0; }'
271 ),
272 array(
273 '.thisclass .left .myclass { width: 0; }'
274 ),
275 array(
276 '.thisclass .left .myclass #myid { width: 0; }'
277 ),
278
279 // Cursor values (east/west)
280 array(
281 '.foo { cursor: e-resize; }',
282 '.foo { cursor: w-resize; }'
283 ),
284 array(
285 '.foo { cursor: se-resize; }',
286 '.foo { cursor: sw-resize; }'
287 ),
288 array(
289 '.foo { cursor: ne-resize; }',
290 '.foo { cursor: nw-resize; }'
291 ),
292
293 // Background
294 array(
295 '.foo { background-position: top left; }',
296 '.foo { background-position: top right; }'
297 ),
298 array(
299 '.foo { background: url(/foo/bar.png) top left; }',
300 '.foo { background: url(/foo/bar.png) top right; }'
301 ),
302 array(
303 '.foo { background: url(/foo/bar.png) top left no-repeat; }',
304 '.foo { background: url(/foo/bar.png) top right no-repeat; }'
305 ),
306 array(
307 '.foo { background: url(/foo/bar.png) no-repeat top left; }',
308 '.foo { background: url(/foo/bar.png) no-repeat top right; }'
309 ),
310 array(
311 '.foo { background: #fff url(/foo/bar.png) no-repeat top left; }',
312 '.foo { background: #fff url(/foo/bar.png) no-repeat top right; }'
313 ),
314 array(
315 '.foo { background-position: 100% 40%; }',
316 '.foo { background-position: 0% 40%; }'
317 ),
318 array(
319 '.foo { background-position: 23% 0; }',
320 '.foo { background-position: 77% 0; }'
321 ),
322 array(
323 '.foo { background-position: 23% auto; }',
324 '.foo { background-position: 77% auto; }'
325 ),
326 array(
327 '.foo { background-position-x: 23%; }',
328 '.foo { background-position-x: 77%; }'
329 ),
330 array(
331 '.foo { background-position-y: 23%; }',
332 '.foo { background-position-y: 23%; }'
333 ),
334 array(
335 '.foo { background:url(../foo.png) no-repeat 75% 50%; }',
336 '.foo { background:url(../foo.png) no-repeat 25% 50%; }'
337 ),
338 array(
339 '.foo { background: 10% 20% } .bar { background: 40% 30% }',
340 '.foo { background: 90% 20% } .bar { background: 60% 30% }'
341 ),
342
343 // Multiple rules
344 array(
345 'body { direction: rtl; float: right; } .foo { direction: ltr; float: right; }',
346 'body { direction: ltr; float: left; } .foo { direction: rtl; float: left; }',
347 ),
348
349 // Duplicate properties
350 array(
351 '.foo { float: left; float: right; float: left; }',
352 '.foo { float: right; float: left; float: right; }',
353 ),
354
355 // Preserve comments
356 array(
357 '/* left /* right */left: 10px',
358 '/* left /* right */right: 10px'
359 ),
360 array(
361 '/*left*//*left*/left: 10px',
362 '/*left*//*left*/right: 10px'
363 ),
364 array(
365 '/* Going right is cool */ .foo { width: 0 }',
366 ),
367 array(
368 "/* padding-right 1 2 3 4 */\n#test { width: 0}\n/*right*/"
369 ),
370 array(
371 "/** Two line comment\n * left\n \*/\n#test {width: 0}"
372 ),
373
374 // @noflip annotation
375 array(
376 // before selector (single)
377 '/* @noflip */ div { float: left; }'
378 ),
379 array(
380 // before selector (multiple)
381 '/* @noflip */ div, .notme { float: left; }'
382 ),
383 array(
384 // inside selector
385 'div, /* @noflip */ .foo { float: left; }'
386 ),
387 array(
388 // after selector
389 'div, .notme /* @noflip */ { float: left; }'
390 ),
391 array(
392 // before multiple rules
393 '/* @noflip */ div { float: left; } .foo { float: left; }',
394 '/* @noflip */ div { float: left; } .foo { float: right; }'
395 ),
396 array(
397 // support parentheses in selector
398 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }',
399 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }'
400 ),
401 array(
402 // after multiple rules
403 '.foo { float: left; } /* @noflip */ div { float: left; }',
404 '.foo { float: right; } /* @noflip */ div { float: left; }'
405 ),
406 array(
407 // before multiple properties
408 'div { /* @noflip */ float: left; text-align: left; }',
409 'div { /* @noflip */ float: left; text-align: right; }'
410 ),
411 array(
412 // after multiple properties
413 'div { float: left; /* @noflip */ text-align: left; }',
414 'div { float: right; /* @noflip */ text-align: left; }'
415 ),
416
417 // Guard against css3 stuff
418 array(
419 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
420 ),
421 array(
422 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
423 ),
424
425 // CSS syntax / white-space variations
426 // spaces, no spaces, tabs, new lines, omitting semi-colons
427 array(
428 ".foo { left: 0; }",
429 ".foo { right: 0; }"
430 ),
431 array(
432 ".foo{ left: 0; }",
433 ".foo{ right: 0; }"
434 ),
435 array(
436 ".foo{ left: 0 }",
437 ".foo{ right: 0 }"
438 ),
439 array(
440 ".foo{left:0 }",
441 ".foo{right:0 }"
442 ),
443 array(
444 ".foo{left:0}",
445 ".foo{right:0}"
446 ),
447 array(
448 ".foo { left : 0 ; }",
449 ".foo { right : 0 ; }"
450 ),
451 array(
452 ".foo\n { left : 0 ; }",
453 ".foo\n { right : 0 ; }"
454 ),
455 array(
456 ".foo\n { \nleft : 0 ; }",
457 ".foo\n { \nright : 0 ; }"
458 ),
459 array(
460 ".foo\n { \n left : 0 ; }",
461 ".foo\n { \n right : 0 ; }"
462 ),
463 array(
464 ".foo\n { \n left\n : 0; }",
465 ".foo\n { \n right\n : 0; }"
466 ),
467 array(
468 ".foo \n { \n left\n : 0; }",
469 ".foo \n { \n right\n : 0; }"
470 ),
471 array(
472 ".foo\n{\nleft\n:\n0;}",
473 ".foo\n{\nright\n:\n0;}"
474 ),
475 array(
476 ".foo\n.bar {\n\tleft: 0;\n}",
477 ".foo\n.bar {\n\tright: 0;\n}"
478 ),
479 array(
480 ".foo\t{\tleft\t:\t0;}",
481 ".foo\t{\tright\t:\t0;}"
482 ),
483
484 // Guard against partial keys
485 array(
486 '.foo { leftxx: 0; }',
487 '.foo { leftxx: 0; }'
488 ),
489 array(
490 '.foo { rightxx: 0; }',
491 '.foo { rightxx: 0; }'
492 ),
493 );
494 }
495
496 /**
497 * These cases are tested in one way only (format: actual, expected, msg).
498 * If both ways can be tested, either put both versions in here or move
499 * it to provideTransformCases().
500 */
501 public static function provideTransformAdvancedCases() {
502 $bgPairs = array(
503 # [ - _ . ] <-> [ left right ltr rtl ]
504 'foo.jpg' => 'foo.jpg',
505 'left.jpg' => 'right.jpg',
506 'ltr.jpg' => 'rtl.jpg',
507
508 'foo-left.png' => 'foo-right.png',
509 'foo_left.png' => 'foo_right.png',
510 'foo.left.png' => 'foo.right.png',
511
512 'foo-ltr.png' => 'foo-rtl.png',
513 'foo_ltr.png' => 'foo_rtl.png',
514 'foo.ltr.png' => 'foo.rtl.png',
515
516 'left-foo.png' => 'right-foo.png',
517 'left_foo.png' => 'right_foo.png',
518 'left.foo.png' => 'right.foo.png',
519
520 'ltr-foo.png' => 'rtl-foo.png',
521 'ltr_foo.png' => 'rtl_foo.png',
522 'ltr.foo.png' => 'rtl.foo.png',
523
524 'foo-ltr-left.gif' => 'foo-rtl-right.gif',
525 'foo_ltr_left.gif' => 'foo_rtl_right.gif',
526 'foo.ltr.left.gif' => 'foo.rtl.right.gif',
527 'foo-ltr_left.gif' => 'foo-rtl_right.gif',
528 'foo_ltr.left.gif' => 'foo_rtl.right.gif',
529 );
530 $provider = array();
531 foreach ( $bgPairs as $left => $right ) {
532 # By default '-rtl' and '-left' etc. are not touched,
533 # Only when the appropiate parameter is set.
534 $provider[] = array(
535 ".foo { background: url(images/$left); }",
536 ".foo { background: url(images/$left); }"
537 );
538 $provider[] = array(
539 ".foo { background: url(images/$right); }",
540 ".foo { background: url(images/$right); }"
541 );
542 $provider[] = array(
543 ".foo { background: url(images/$left); }",
544 ".foo { background: url(images/$right); }",
545 array(
546 'swapLtrRtlInURL' => true,
547 'swapLeftRightInURL' => true,
548 )
549 );
550 $provider[] = array(
551 ".foo { background: url(images/$right); }",
552 ".foo { background: url(images/$left); }",
553 array(
554 'swapLtrRtlInURL' => true,
555 'swapLeftRightInURL' => true,
556 )
557 );
558 }
559
560 return $provider;
561 }
562
563 /**
564 * Cases that are currently failing, but
565 * should be looked at in the future as enhancements and/or bug fix
566 */
567 public static function provideTransformBrokenCases() {
568 return array(
569 // Guard against selectors that look flippable
570 array(
571 # <foo-left-x attr="x">
572 'foo-left-x[attr="x"] { width: 0; }',
573 'foo-left-x[attr="x"] { width: 0; }'
574 ),
575 array(
576 # <div class="foo" data-left="x">
577 '.foo[data-left="x"] { width: 0; }',
578 '.foo[data-left="x"] { width: 0; }'
579 ),
580 );
581 }
582 }