Merge "Force $wgCategoryCollation to be uppercase in tests"
[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 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 array(
141 '.foo { border-radius: .25em 15px 0pt 0ex; }',
142 '.foo { border-radius: .25em 0ex 0pt 15px; }'
143 ),
144 array(
145 '.foo { x-unknown: a b c d; }'
146 ),
147 array(
148 '.foo barpx 0 2% { opacity: 0; }'
149 ),
150 array(
151 '#settings td p strong'
152 ),
153 array(
154 // Test the colors regex
155 '.foo { border-color: red green blue white }',
156 '.foo { border-color: red white blue green }',
157 ),
158 array(
159 // Test the colors regex harder
160 '.foo { border-color: red #f00 rgb(255, 0, 0%) rgba(100%, 0, 0, 0) }',
161 '.foo { border-color: red rgba(100%, 0, 0, 0) rgb(255, 0, 0%) #f00 }',
162 ),
163 array(
164 // Do not bork 4+ values
165 '.foo { x-unknown: 1 2 3 4 5; }'
166 ),
167 array(
168 '.foo { x-unknown: 1 2 3 4 5 6; }'
169 ),
170
171 // Shorthand / Three notation
172 array(
173 '.foo { margin: 1em 0 .25em; }'
174 ),
175 array(
176 '.foo { margin:-1.5em 0 -.75em; }'
177 ),
178
179 // Shorthand / Two notation
180 array(
181 '.foo { padding: 1px 2px; }'
182 ),
183
184 // Shorthand / One notation
185 array(
186 '.foo { padding: 1px; }'
187 ),
188
189 // text-shadow and box-shadow
190 array(
191 '.foo { box-shadow: -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
192 '.foo { box-shadow: 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
193 ),
194 array(
195 '.foo { box-shadow: inset -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
196 '.foo { box-shadow: inset 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
197 ),
198 array(
199 '.foo { text-shadow: orange 2px 0; }',
200 '.foo { text-shadow: orange -2px 0; }',
201 ),
202 array(
203 '.foo { text-shadow: 2px 0 orange; }',
204 '.foo { text-shadow: -2px 0 orange; }',
205 ),
206 array(
207 // Don't mangle zeroes
208 '.foo { text-shadow: orange 0 2px; }'
209 ),
210
211 // Direction
212 // Note: This differs from the Python implementation,
213 // see also CSSJanus::fixDirection for more info.
214 array(
215 '.foo { direction: ltr; }',
216 '.foo { direction: rtl; }'
217 ),
218 array(
219 '.foo { direction: rtl; }',
220 '.foo { direction: ltr; }'
221 ),
222 array(
223 'input { direction: ltr; }',
224 'input { direction: rtl; }'
225 ),
226 array(
227 'input { direction: rtl; }',
228 'input { direction: ltr; }'
229 ),
230 array(
231 'body { direction: ltr; }',
232 'body { direction: rtl; }'
233 ),
234 array(
235 '.foo, body, input { direction: ltr; }',
236 '.foo, body, input { direction: rtl; }'
237 ),
238 array(
239 'body { padding: 10px; direction: ltr; }',
240 'body { padding: 10px; direction: rtl; }'
241 ),
242 array(
243 'body { direction: ltr } .myClass { direction: ltr }',
244 'body { direction: rtl } .myClass { direction: rtl }'
245 ),
246
247 // Left/right values
248 array(
249 '.foo { float: left; }',
250 '.foo { float: right; }'
251 ),
252 array(
253 '.foo { text-align: left; }',
254 '.foo { text-align: right; }'
255 ),
256 array(
257 '.foo { -x-unknown: left; }',
258 '.foo { -x-unknown: right; }'
259 ),
260 // Guard against selectors that look flippable
261 array(
262 '.column-left { width: 0; }'
263 ),
264 array(
265 'a.left { width: 0; }'
266 ),
267 array(
268 'a.leftification { width: 0; }'
269 ),
270 array(
271 'a.ltr { width: 0; }'
272 ),
273 array(
274 # <div class="a-ltr png">
275 '.a-ltr.png { width: 0; }'
276 ),
277 array(
278 # <foo-ltr attr="x">
279 'foo-ltr[attr="x"] { width: 0; }'
280 ),
281 array(
282 'div.left > span.right+span.left { width: 0; }'
283 ),
284 array(
285 '.thisclass .left .myclass { width: 0; }'
286 ),
287 array(
288 '.thisclass .left .myclass #myid { width: 0; }'
289 ),
290
291 // Cursor values (east/west)
292 array(
293 '.foo { cursor: e-resize; }',
294 '.foo { cursor: w-resize; }'
295 ),
296 array(
297 '.foo { cursor: se-resize; }',
298 '.foo { cursor: sw-resize; }'
299 ),
300 array(
301 '.foo { cursor: ne-resize; }',
302 '.foo { cursor: nw-resize; }'
303 ),
304
305 // Background
306 array(
307 '.foo { background-position: top left; }',
308 '.foo { background-position: top right; }'
309 ),
310 array(
311 '.foo { background: url(/foo/bar.png) top left; }',
312 '.foo { background: url(/foo/bar.png) top right; }'
313 ),
314 array(
315 '.foo { background: url(/foo/bar.png) top left no-repeat; }',
316 '.foo { background: url(/foo/bar.png) top right no-repeat; }'
317 ),
318 array(
319 '.foo { background: url(/foo/bar.png) no-repeat top left; }',
320 '.foo { background: url(/foo/bar.png) no-repeat top right; }'
321 ),
322 array(
323 '.foo { background: #fff url(/foo/bar.png) no-repeat top left; }',
324 '.foo { background: #fff url(/foo/bar.png) no-repeat top right; }'
325 ),
326 array(
327 '.foo { background-position: 100% 40%; }',
328 '.foo { background-position: 0% 40%; }'
329 ),
330 array(
331 '.foo { background-position: 23% 0; }',
332 '.foo { background-position: 77% 0; }'
333 ),
334 array(
335 '.foo { background-position: 23% auto; }',
336 '.foo { background-position: 77% auto; }'
337 ),
338 array(
339 '.foo { background-position-x: 23%; }',
340 '.foo { background-position-x: 77%; }'
341 ),
342 array(
343 '.foo { background-position-y: 23%; }',
344 '.foo { background-position-y: 23%; }'
345 ),
346 array(
347 '.foo { background:url(../foo.png) no-repeat 75% 50%; }',
348 '.foo { background:url(../foo.png) no-repeat 25% 50%; }'
349 ),
350 array(
351 '.foo { background: 10% 20% } .bar { background: 40% 30% }',
352 '.foo { background: 90% 20% } .bar { background: 60% 30% }'
353 ),
354
355 // Multiple rules
356 array(
357 'body { direction: rtl; float: right; } .foo { direction: ltr; float: right; }',
358 'body { direction: ltr; float: left; } .foo { direction: rtl; float: left; }',
359 ),
360
361 // Duplicate properties
362 array(
363 '.foo { float: left; float: right; float: left; }',
364 '.foo { float: right; float: left; float: right; }',
365 ),
366
367 // Preserve comments
368 array(
369 '/* left /* right */left: 10px',
370 '/* left /* right */right: 10px'
371 ),
372 array(
373 '/*left*//*left*/left: 10px',
374 '/*left*//*left*/right: 10px'
375 ),
376 array(
377 '/* Going right is cool */ .foo { width: 0 }',
378 ),
379 array(
380 "/* padding-right 1 2 3 4 */\n#test { width: 0}\n/*right*/"
381 ),
382 array(
383 "/** Two line comment\n * left\n \*/\n#test {width: 0}"
384 ),
385
386 // @noflip annotation
387 array(
388 // before selector (single)
389 '/* @noflip */ div { float: left; }'
390 ),
391 array(
392 // before selector (multiple)
393 '/* @noflip */ div, .notme { float: left; }'
394 ),
395 array(
396 // inside selector
397 'div, /* @noflip */ .foo { float: left; }'
398 ),
399 array(
400 // after selector
401 'div, .notme /* @noflip */ { float: left; }'
402 ),
403 array(
404 // before multiple rules
405 '/* @noflip */ div { float: left; } .foo { float: left; }',
406 '/* @noflip */ div { float: left; } .foo { float: right; }'
407 ),
408 array(
409 // after multiple rules
410 '.foo { float: left; } /* @noflip */ div { float: left; }',
411 '.foo { float: right; } /* @noflip */ div { float: left; }'
412 ),
413 array(
414 // before multiple properties
415 'div { /* @noflip */ float: left; text-align: left; }',
416 'div { /* @noflip */ float: left; text-align: right; }'
417 ),
418 array(
419 // after multiple properties
420 'div { float: left; /* @noflip */ text-align: left; }',
421 'div { float: right; /* @noflip */ text-align: left; }'
422 ),
423
424 // Guard against css3 stuff
425 array(
426 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
427 ),
428 array(
429 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
430 ),
431
432 // CSS syntax / white-space variations
433 // spaces, no spaces, tabs, new lines, omitting semi-colons
434 array(
435 ".foo { left: 0; }",
436 ".foo { right: 0; }"
437 ),
438 array(
439 ".foo{ left: 0; }",
440 ".foo{ right: 0; }"
441 ),
442 array(
443 ".foo{ left: 0 }",
444 ".foo{ right: 0 }"
445 ),
446 array(
447 ".foo{left:0 }",
448 ".foo{right:0 }"
449 ),
450 array(
451 ".foo{left:0}",
452 ".foo{right:0}"
453 ),
454 array(
455 ".foo { left : 0 ; }",
456 ".foo { right : 0 ; }"
457 ),
458 array(
459 ".foo\n { left : 0 ; }",
460 ".foo\n { right : 0 ; }"
461 ),
462 array(
463 ".foo\n { \nleft : 0 ; }",
464 ".foo\n { \nright : 0 ; }"
465 ),
466 array(
467 ".foo\n { \n left : 0 ; }",
468 ".foo\n { \n right : 0 ; }"
469 ),
470 array(
471 ".foo\n { \n left\n : 0; }",
472 ".foo\n { \n right\n : 0; }"
473 ),
474 array(
475 ".foo \n { \n left\n : 0; }",
476 ".foo \n { \n right\n : 0; }"
477 ),
478 array(
479 ".foo\n{\nleft\n:\n0;}",
480 ".foo\n{\nright\n:\n0;}"
481 ),
482 array(
483 ".foo\n.bar {\n\tleft: 0;\n}",
484 ".foo\n.bar {\n\tright: 0;\n}"
485 ),
486 array(
487 ".foo\t{\tleft\t:\t0;}",
488 ".foo\t{\tright\t:\t0;}"
489 ),
490
491 // Guard against partial keys
492 array(
493 '.foo { leftxx: 0; }',
494 '.foo { leftxx: 0; }'
495 ),
496 array(
497 '.foo { rightxx: 0; }',
498 '.foo { rightxx: 0; }'
499 ),
500 );
501 }
502
503 /**
504 * These cases are tested in one way only (format: actual, expected, msg).
505 * If both ways can be tested, either put both versions in here or move
506 * it to provideTransformCases().
507 */
508 function provideTransformAdvancedCases() {
509 $bgPairs = array(
510 # [ - _ . ] <-> [ left right ltr rtl ]
511 'foo.jpg' => 'foo.jpg',
512 'left.jpg' => 'right.jpg',
513 'ltr.jpg' => 'rtl.jpg',
514
515 'foo-left.png' => 'foo-right.png',
516 'foo_left.png' => 'foo_right.png',
517 'foo.left.png' => 'foo.right.png',
518
519 'foo-ltr.png' => 'foo-rtl.png',
520 'foo_ltr.png' => 'foo_rtl.png',
521 'foo.ltr.png' => 'foo.rtl.png',
522
523 'left-foo.png' => 'right-foo.png',
524 'left_foo.png' => 'right_foo.png',
525 'left.foo.png' => 'right.foo.png',
526
527 'ltr-foo.png' => 'rtl-foo.png',
528 'ltr_foo.png' => 'rtl_foo.png',
529 'ltr.foo.png' => 'rtl.foo.png',
530
531 'foo-ltr-left.gif' => 'foo-rtl-right.gif',
532 'foo_ltr_left.gif' => 'foo_rtl_right.gif',
533 'foo.ltr.left.gif' => 'foo.rtl.right.gif',
534 'foo-ltr_left.gif' => 'foo-rtl_right.gif',
535 'foo_ltr.left.gif' => 'foo_rtl.right.gif',
536 );
537 $provider = array();
538 foreach ( $bgPairs as $left => $right ) {
539 # By default '-rtl' and '-left' etc. are not touched,
540 # Only when the appropiate parameter is set.
541 $provider[] = array(
542 ".foo { background: url(images/$left); }",
543 ".foo { background: url(images/$left); }"
544 );
545 $provider[] = array(
546 ".foo { background: url(images/$right); }",
547 ".foo { background: url(images/$right); }"
548 );
549 $provider[] = array(
550 ".foo { background: url(images/$left); }",
551 ".foo { background: url(images/$right); }",
552 array(
553 'swapLtrRtlInURL' => true,
554 'swapLeftRightInURL' => true,
555 )
556 );
557 $provider[] = array(
558 ".foo { background: url(images/$right); }",
559 ".foo { background: url(images/$left); }",
560 array(
561 'swapLtrRtlInURL' => true,
562 'swapLeftRightInURL' => true,
563 )
564 );
565 }
566
567 return $provider;
568 }
569
570 /**
571 * Cases that are currently failing, but
572 * should be looked at in the future as enhancements and/or bug fix
573 */
574 function provideTransformBrokenCases() {
575 return array(
576 // Guard against selectors that look flippable
577 array(
578 # <foo-left-x attr="x">
579 'foo-left-x[attr="x"] { width: 0; }',
580 'foo-left-x[attr="x"] { width: 0; }'
581 ),
582 array(
583 # <div class="foo" data-left="x">
584 '.foo[data-left="x"] { width: 0; }',
585 '.foo[data-left="x"] { width: 0; }'
586 ),
587 );
588 }
589 }