3 * Based on the test suite of the original Python
5 * http://code.google.com/p/cssjanus/source/browse/trunk/cssjanus_test.py
6 * Ported to PHP for ResourceLoader and has been extended since.
10 class CSSJanusTest
extends MediaWikiTestCase
{
12 * @dataProvider provideTransformCases
14 public function testTransform( $cssA, $cssB = null ) {
17 $transformedA = CSSJanus
::transform( $cssA );
18 $this->assertEquals( $transformedA, $cssB, 'Test A-B transformation' );
20 $transformedB = CSSJanus
::transform( $cssB );
21 $this->assertEquals( $transformedB, $cssA, 'Test B-A transformation' );
23 // If no B version is provided, it means
24 // the output should equal the input.
25 $transformedA = CSSJanus
::transform( $cssA );
26 $this->assertEquals( $transformedA, $cssA, 'Nothing was flipped' );
31 * @dataProvider provideTransformAdvancedCases
33 public function testTransformAdvanced( $code, $expectedOutput, $options = array() ) {
34 $swapLtrRtlInURL = isset( $options['swapLtrRtlInURL'] ) ?
$options['swapLtrRtlInURL'] : false;
35 $swapLeftRightInURL = isset( $options['swapLeftRightInURL'] ) ?
$options['swapLeftRightInURL'] : false;
37 $flipped = CSSJanus
::transform( $code, $swapLtrRtlInURL, $swapLeftRightInURL );
39 $this->assertEquals( $expectedOutput, $flipped,
40 'Test flipping, options: url-ltr-rtl=' . ( $swapLtrRtlInURL ?
'true' : 'false' )
41 . ' url-left-right=' . ( $swapLeftRightInURL ?
'true' : 'false' )
46 * @dataProvider provideTransformBrokenCases
49 public function testTransformBroken( $code, $expectedOutput ) {
50 $flipped = CSSJanus
::transform( $code );
52 $this->assertEquals( $expectedOutput, $flipped, 'Test flipping' );
56 * These transform cases are tested *in both directions*
57 * No need to declare a principle twice in both directions here.
59 public static function provideTransformCases() {
66 // Guard against partial keys
67 // (CSS currently doesn't have flippable properties
68 // that contain the direction as part of the key without
71 '.foo { alright: 0; }'
74 '.foo { balleft: 0; }'
77 // Dashed property keys
79 '.foo { padding-left: 0; }',
80 '.foo { padding-right: 0; }'
83 '.foo { margin-left: 0; }',
84 '.foo { margin-right: 0; }'
87 '.foo { border-left: 0; }',
88 '.foo { border-right: 0; }'
91 // Double-dashed property keys
93 '.foo { border-left-color: red; }',
94 '.foo { border-right-color: red; }'
97 // Includes unknown properties?
98 '.foo { x-left-y: 0; }',
99 '.foo { x-right-y: 0; }'
102 // Multi-value properties
104 '.foo { padding: 0; }'
107 '.foo { padding: 0 1px; }'
110 '.foo { padding: 0 1px 2px; }'
113 '.foo { padding: 0 1px 2px 3px; }',
114 '.foo { padding: 0 3px 2px 1px; }'
117 // Shorthand / Four notation
119 '.foo { padding: .25em 15px 0pt 0ex; }',
120 '.foo { padding: .25em 0ex 0pt 15px; }'
123 '.foo { margin: 1px -4px 3px 2px; }',
124 '.foo { margin: 1px 2px 3px -4px; }'
127 '.foo { padding: 0 15px .25em 0; }',
128 '.foo { padding: 0 0 .25em 15px; }'
131 '.foo { padding: 1px 4.1grad 3px 2%; }',
132 '.foo { padding: 1px 2% 3px 4.1grad; }'
135 '.foo { padding: 1px 2px 3px auto; }',
136 '.foo { padding: 1px auto 3px 2px; }'
139 '.foo { padding: 1px inherit 3px auto; }',
140 '.foo { padding: 1px auto 3px inherit; }'
142 // border-radius assigns different meanings to the values
144 '.foo { border-radius: .25em 15px 0pt 0ex; }',
145 '.foo { border-radius: 15px .25em 0ex 0pt; }'
148 '.foo { border-radius: 0px 0px 5px 5px; }',
150 // Ensure the rule doesn't break other stuff
152 '.foo { x-unknown: a b c d; }'
155 '.foo barpx 0 2% { opacity: 0; }'
158 '#settings td p strong'
162 '.foo { border-color: red green blue white }',
163 '.foo { border-color: red white blue green }',
166 // Color name, hexdecimal, RGB & RGBA
167 '.foo { border-color: red #f00 rgb(255, 0, 0) rgba(255, 0, 0, 0.5) }',
168 '.foo { border-color: red rgba(255, 0, 0, 0.5) rgb(255, 0, 0) #f00 }',
171 // Color name, hexdecimal, HSL & HSLA
172 '.foo { border-color: red #f00 hsl(0, 100%, 50%) hsla(0, 100%, 50%, 0.5) }',
173 '.foo { border-color: red hsla(0, 100%, 50%, 0.5) hsl(0, 100%, 50%) #f00 }',
176 // Do not mangle 5 or more values
177 '.foo { -x-unknown: 1 2 3 4 5; }'
180 '.foo { -x-unknown: 1 2 3 4 5 6; }'
183 // Shorthand / Three notation
185 '.foo { margin: 1em 0 .25em; }'
188 '.foo { margin:-1.5em 0 -.75em; }'
191 // Shorthand / Two notation
193 '.foo { padding: 1px 2px; }'
196 // Shorthand / One notation
198 '.foo { padding: 1px; }'
201 // text-shadow and box-shadow
203 '.foo { box-shadow: -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
204 '.foo { box-shadow: 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
207 '.foo { box-shadow: inset -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
208 '.foo { box-shadow: inset 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
211 '.foo { text-shadow: orange 2px 0; }',
212 '.foo { text-shadow: orange -2px 0; }',
215 '.foo { text-shadow: 2px 0 orange; }',
216 '.foo { text-shadow: -2px 0 orange; }',
219 // Don't mangle zeroes
220 '.foo { text-shadow: orange 0 2px; }'
224 // Note: This differs from the Python implementation,
225 // see also CSSJanus::fixDirection for more info.
227 '.foo { direction: ltr; }',
228 '.foo { direction: rtl; }'
231 '.foo { direction: rtl; }',
232 '.foo { direction: ltr; }'
235 'input { direction: ltr; }',
236 'input { direction: rtl; }'
239 'input { direction: rtl; }',
240 'input { direction: ltr; }'
243 'body { direction: ltr; }',
244 'body { direction: rtl; }'
247 '.foo, body, input { direction: ltr; }',
248 '.foo, body, input { direction: rtl; }'
251 'body { padding: 10px; direction: ltr; }',
252 'body { padding: 10px; direction: rtl; }'
255 'body { direction: ltr } .myClass { direction: ltr }',
256 'body { direction: rtl } .myClass { direction: rtl }'
261 '.foo { float: left; }',
262 '.foo { float: right; }'
265 '.foo { text-align: left; }',
266 '.foo { text-align: right; }'
269 '.foo { -x-unknown: left; }',
270 '.foo { -x-unknown: right; }'
272 // Guard against selectors that look flippable
274 '.column-left { width: 0; }'
277 'a.left { width: 0; }'
280 'a.leftification { width: 0; }'
283 'a.ltr { width: 0; }'
286 # <div class="a-ltr png">
287 '.a-ltr.png { width: 0; }'
291 'foo-ltr[attr="x"] { width: 0; }'
294 'div.left > span.right+span.left { width: 0; }'
297 '.thisclass .left .myclass { width: 0; }'
300 '.thisclass .left .myclass #myid { width: 0; }'
303 // Cursor values (east/west)
305 '.foo { cursor: e-resize; }',
306 '.foo { cursor: w-resize; }'
309 '.foo { cursor: se-resize; }',
310 '.foo { cursor: sw-resize; }'
313 '.foo { cursor: ne-resize; }',
314 '.foo { cursor: nw-resize; }'
319 '.foo { background-position: top left; }',
320 '.foo { background-position: top right; }'
323 '.foo { background: url(/foo/bar.png) top left; }',
324 '.foo { background: url(/foo/bar.png) top right; }'
327 '.foo { background: url(/foo/bar.png) top left no-repeat; }',
328 '.foo { background: url(/foo/bar.png) top right no-repeat; }'
331 '.foo { background: url(/foo/bar.png) no-repeat top left; }',
332 '.foo { background: url(/foo/bar.png) no-repeat top right; }'
335 '.foo { background: #fff url(/foo/bar.png) no-repeat top left; }',
336 '.foo { background: #fff url(/foo/bar.png) no-repeat top right; }'
339 '.foo { background-position: 100% 40%; }',
340 '.foo { background-position: 0% 40%; }'
343 '.foo { background-position: 23% 0; }',
344 '.foo { background-position: 77% 0; }'
347 '.foo { background-position: 23% auto; }',
348 '.foo { background-position: 77% auto; }'
351 '.foo { background-position-x: 23%; }',
352 '.foo { background-position-x: 77%; }'
355 '.foo { background-position-y: 23%; }',
356 '.foo { background-position-y: 23%; }'
359 '.foo { background:url(../foo.png) no-repeat 75% 50%; }',
360 '.foo { background:url(../foo.png) no-repeat 25% 50%; }'
363 '.foo { background: 10% 20% } .bar { background: 40% 30% }',
364 '.foo { background: 90% 20% } .bar { background: 60% 30% }'
369 'body { direction: rtl; float: right; } .foo { direction: ltr; float: right; }',
370 'body { direction: ltr; float: left; } .foo { direction: rtl; float: left; }',
373 // Duplicate properties
375 '.foo { float: left; float: right; float: left; }',
376 '.foo { float: right; float: left; float: right; }',
381 '/* left /* right */left: 10px',
382 '/* left /* right */right: 10px'
385 '/*left*//*left*/left: 10px',
386 '/*left*//*left*/right: 10px'
389 '/* Going right is cool */ .foo { width: 0 }',
392 "/* padding-right 1 2 3 4 */\n#test { width: 0}\n/*right*/"
395 "/** Two line comment\n * left\n \*/\n#test {width: 0}"
398 // @noflip annotation
400 // before selector (single)
401 '/* @noflip */ div { float: left; }'
404 // before selector (multiple)
405 '/* @noflip */ div, .notme { float: left; }'
409 'div, /* @noflip */ .foo { float: left; }'
413 'div, .notme /* @noflip */ { float: left; }'
416 // before multiple rules
417 '/* @noflip */ div { float: left; } .foo { float: left; }',
418 '/* @noflip */ div { float: left; } .foo { float: right; }'
421 // support parentheses in selector
422 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }',
423 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }'
426 // after multiple rules
427 '.foo { float: left; } /* @noflip */ div { float: left; }',
428 '.foo { float: right; } /* @noflip */ div { float: left; }'
431 // before multiple properties
432 'div { /* @noflip */ float: left; text-align: left; }',
433 'div { /* @noflip */ float: left; text-align: right; }'
436 // after multiple properties
437 'div { float: left; /* @noflip */ text-align: left; }',
438 'div { float: right; /* @noflip */ text-align: left; }'
441 // Guard against css3 stuff
443 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
446 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
449 // CSS syntax / white-space variations
450 // spaces, no spaces, tabs, new lines, omitting semi-colons
472 ".foo { left : 0 ; }",
473 ".foo { right : 0 ; }"
476 ".foo\n { left : 0 ; }",
477 ".foo\n { right : 0 ; }"
480 ".foo\n { \nleft : 0 ; }",
481 ".foo\n { \nright : 0 ; }"
484 ".foo\n { \n left : 0 ; }",
485 ".foo\n { \n right : 0 ; }"
488 ".foo\n { \n left\n : 0; }",
489 ".foo\n { \n right\n : 0; }"
492 ".foo \n { \n left\n : 0; }",
493 ".foo \n { \n right\n : 0; }"
496 ".foo\n{\nleft\n:\n0;}",
497 ".foo\n{\nright\n:\n0;}"
500 ".foo\n.bar {\n\tleft: 0;\n}",
501 ".foo\n.bar {\n\tright: 0;\n}"
504 ".foo\t{\tleft\t:\t0;}",
505 ".foo\t{\tright\t:\t0;}"
508 // Guard against partial keys
510 '.foo { leftxx: 0; }',
511 '.foo { leftxx: 0; }'
514 '.foo { rightxx: 0; }',
515 '.foo { rightxx: 0; }'
521 * These cases are tested in one way only (format: actual, expected, msg).
522 * If both ways can be tested, either put both versions in here or move
523 * it to provideTransformCases().
525 public static function provideTransformAdvancedCases() {
527 # [ - _ . ] <-> [ left right ltr rtl ]
528 'foo.jpg' => 'foo.jpg',
529 'left.jpg' => 'right.jpg',
530 'ltr.jpg' => 'rtl.jpg',
532 'foo-left.png' => 'foo-right.png',
533 'foo_left.png' => 'foo_right.png',
534 'foo.left.png' => 'foo.right.png',
536 'foo-ltr.png' => 'foo-rtl.png',
537 'foo_ltr.png' => 'foo_rtl.png',
538 'foo.ltr.png' => 'foo.rtl.png',
540 'left-foo.png' => 'right-foo.png',
541 'left_foo.png' => 'right_foo.png',
542 'left.foo.png' => 'right.foo.png',
544 'ltr-foo.png' => 'rtl-foo.png',
545 'ltr_foo.png' => 'rtl_foo.png',
546 'ltr.foo.png' => 'rtl.foo.png',
548 'foo-ltr-left.gif' => 'foo-rtl-right.gif',
549 'foo_ltr_left.gif' => 'foo_rtl_right.gif',
550 'foo.ltr.left.gif' => 'foo.rtl.right.gif',
551 'foo-ltr_left.gif' => 'foo-rtl_right.gif',
552 'foo_ltr.left.gif' => 'foo_rtl.right.gif',
555 foreach ( $bgPairs as $left => $right ) {
556 # By default '-rtl' and '-left' etc. are not touched,
557 # Only when the appropiate parameter is set.
559 ".foo { background: url(images/$left); }",
560 ".foo { background: url(images/$left); }"
563 ".foo { background: url(images/$right); }",
564 ".foo { background: url(images/$right); }"
567 ".foo { background: url(images/$left); }",
568 ".foo { background: url(images/$right); }",
570 'swapLtrRtlInURL' => true,
571 'swapLeftRightInURL' => true,
575 ".foo { background: url(images/$right); }",
576 ".foo { background: url(images/$left); }",
578 'swapLtrRtlInURL' => true,
579 'swapLeftRightInURL' => true,
588 * Cases that are currently failing, but
589 * should be looked at in the future as enhancements and/or bug fix
591 public static function provideTransformBrokenCases() {
593 // Guard against selectors that look flippable
595 # <foo-left-x attr="x">
596 'foo-left-x[attr="x"] { width: 0; }',
597 'foo-left-x[attr="x"] { width: 0; }'
600 # <div class="foo" data-left="x">
601 '.foo[data-left="x"] { width: 0; }',
602 '.foo[data-left="x"] { width: 0; }'