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.
8 class CSSJanusTest
extends MediaWikiTestCase
{
10 * @dataProvider provideTransformCases
12 function testTransform( $cssA, $cssB = null ) {
15 $transformedA = CSSJanus
::transform( $cssA );
16 $this->assertEquals( $transformedA, $cssB, 'Test A-B transformation' );
18 $transformedB = CSSJanus
::transform( $cssB );
19 $this->assertEquals( $transformedB, $cssA, 'Test B-A transformation' );
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' );
29 * @dataProvider provideTransformAdvancedCases
31 function testTransformAdvanced( $code, $expectedOutput, $options = array() ) {
32 $swapLtrRtlInURL = isset( $options['swapLtrRtlInURL'] ) ?
$options['swapLtrRtlInURL'] : false;
33 $swapLeftRightInURL = isset( $options['swapLeftRightInURL'] ) ?
$options['swapLeftRightInURL'] : false;
35 $flipped = CSSJanus
::transform( $code, $swapLtrRtlInURL, $swapLeftRightInURL );
37 $this->assertEquals( $expectedOutput, $flipped,
38 'Test flipping, options: url-ltr-rtl=' . ( $swapLtrRtlInURL ?
'true' : 'false' )
39 . ' url-left-right=' . ( $swapLeftRightInURL ?
'true' : 'false' )
44 * @dataProvider provideTransformBrokenCases
47 function testTransformBroken( $code, $expectedOutput ) {
48 $flipped = CSSJanus
::transform( $code );
50 $this->assertEquals( $expectedOutput, $flipped, 'Test flipping' );
54 * These transform cases are tested *in both directions*
55 * No need to declare a principle twice in both directions here.
57 public static function provideTransformCases() {
64 // Guard against partial keys
65 // (CSS currently doesn't have flippable properties
66 // that contain the direction as part of the key without
69 '.foo { alright: 0; }'
72 '.foo { balleft: 0; }'
75 // Dashed property keys
77 '.foo { padding-left: 0; }',
78 '.foo { padding-right: 0; }'
81 '.foo { margin-left: 0; }',
82 '.foo { margin-right: 0; }'
85 '.foo { border-left: 0; }',
86 '.foo { border-right: 0; }'
89 // Double-dashed property keys
91 '.foo { border-left-color: red; }',
92 '.foo { border-right-color: red; }'
95 // Includes unknown properties?
96 '.foo { x-left-y: 0; }',
97 '.foo { x-right-y: 0; }'
100 // Multi-value properties
102 '.foo { padding: 0; }'
105 '.foo { padding: 0 1px; }'
108 '.foo { padding: 0 1px 2px; }'
111 '.foo { padding: 0 1px 2px 3px; }',
112 '.foo { padding: 0 3px 2px 1px; }'
115 // Shorthand / Four notation
117 '.foo { padding: .25em 15px 0pt 0ex; }',
118 '.foo { padding: .25em 0ex 0pt 15px; }'
121 '.foo { margin: 1px -4px 3px 2px; }',
122 '.foo { margin: 1px 2px 3px -4px; }'
125 '.foo { padding: 0 15px .25em 0; }',
126 '.foo { padding: 0 0 .25em 15px; }'
129 '.foo { padding: 1px 4.1grad 3px 2%; }',
130 '.foo { padding: 1px 2% 3px 4.1grad; }'
133 '.foo { padding: 1px 2px 3px auto; }',
134 '.foo { padding: 1px auto 3px 2px; }'
137 '.foo { padding: 1px inherit 3px auto; }',
138 '.foo { padding: 1px auto 3px inherit; }'
141 '.foo { border-radius: .25em 15px 0pt 0ex; }',
142 '.foo { border-radius: .25em 0ex 0pt 15px; }'
145 '.foo { x-unknown: a b c d; }'
148 '.foo barpx 0 2% { opacity: 0; }'
151 '#settings td p strong'
155 '.foo { border-color: red green blue white }',
156 '.foo { border-color: red white blue green }',
159 // Color name, hexdecimal, RGB & RGBA
160 '.foo { border-color: red #f00 rgb(255, 0, 0) rgba(255, 0, 0, 0.5) }',
161 '.foo { border-color: red rgba(255, 0, 0, 0.5) rgb(255, 0, 0) #f00 }',
164 // Color name, hexdecimal, HSL & HSLA
165 '.foo { border-color: red #f00 hsl(0, 100%, 50%) hsla(0, 100%, 50%, 0.5) }',
166 '.foo { border-color: red hsla(0, 100%, 50%, 0.5) hsl(0, 100%, 50%) #f00 }',
169 // Do not mangle 5 or more values
170 '.foo { -x-unknown: 1 2 3 4 5; }'
173 '.foo { -x-unknown: 1 2 3 4 5 6; }'
176 // Shorthand / Three notation
178 '.foo { margin: 1em 0 .25em; }'
181 '.foo { margin:-1.5em 0 -.75em; }'
184 // Shorthand / Two notation
186 '.foo { padding: 1px 2px; }'
189 // Shorthand / One notation
191 '.foo { padding: 1px; }'
195 // Note: This differs from the Python implementation,
196 // see also CSSJanus::fixDirection for more info.
198 '.foo { direction: ltr; }',
199 '.foo { direction: rtl; }'
202 '.foo { direction: rtl; }',
203 '.foo { direction: ltr; }'
206 'input { direction: ltr; }',
207 'input { direction: rtl; }'
210 'input { direction: rtl; }',
211 'input { direction: ltr; }'
214 'body { direction: ltr; }',
215 'body { direction: rtl; }'
218 '.foo, body, input { direction: ltr; }',
219 '.foo, body, input { direction: rtl; }'
222 'body { padding: 10px; direction: ltr; }',
223 'body { padding: 10px; direction: rtl; }'
226 'body { direction: ltr } .myClass { direction: ltr }',
227 'body { direction: rtl } .myClass { direction: rtl }'
232 '.foo { float: left; }',
233 '.foo { float: right; }'
236 '.foo { text-align: left; }',
237 '.foo { text-align: right; }'
240 '.foo { -x-unknown: left; }',
241 '.foo { -x-unknown: right; }'
243 // Guard against selectors that look flippable
245 '.column-left { width: 0; }'
248 'a.left { width: 0; }'
251 'a.leftification { width: 0; }'
254 'a.ltr { width: 0; }'
257 # <div class="a-ltr png">
258 '.a-ltr.png { width: 0; }'
262 'foo-ltr[attr="x"] { width: 0; }'
265 'div.left > span.right+span.left { width: 0; }'
268 '.thisclass .left .myclass { width: 0; }'
271 '.thisclass .left .myclass #myid { width: 0; }'
274 // Cursor values (east/west)
276 '.foo { cursor: e-resize; }',
277 '.foo { cursor: w-resize; }'
280 '.foo { cursor: se-resize; }',
281 '.foo { cursor: sw-resize; }'
284 '.foo { cursor: ne-resize; }',
285 '.foo { cursor: nw-resize; }'
290 '.foo { background-position: top left; }',
291 '.foo { background-position: top right; }'
294 '.foo { background: url(/foo/bar.png) top left; }',
295 '.foo { background: url(/foo/bar.png) top right; }'
298 '.foo { background: url(/foo/bar.png) top left no-repeat; }',
299 '.foo { background: url(/foo/bar.png) top right no-repeat; }'
302 '.foo { background: url(/foo/bar.png) no-repeat top left; }',
303 '.foo { background: url(/foo/bar.png) no-repeat top right; }'
306 '.foo { background: #fff url(/foo/bar.png) no-repeat top left; }',
307 '.foo { background: #fff url(/foo/bar.png) no-repeat top right; }'
310 '.foo { background-position: 100% 40%; }',
311 '.foo { background-position: 0% 40%; }'
314 '.foo { background-position: 23% 0; }',
315 '.foo { background-position: 77% 0; }'
318 '.foo { background-position: 23% auto; }',
319 '.foo { background-position: 77% auto; }'
322 '.foo { background-position-x: 23%; }',
323 '.foo { background-position-x: 77%; }'
326 '.foo { background-position-y: 23%; }',
327 '.foo { background-position-y: 23%; }'
330 '.foo { background:url(../foo.png) no-repeat 75% 50%; }',
331 '.foo { background:url(../foo.png) no-repeat 25% 50%; }'
334 '.foo { background: 10% 20% } .bar { background: 40% 30% }',
335 '.foo { background: 90% 20% } .bar { background: 60% 30% }'
340 'body { direction: rtl; float: right; } .foo { direction: ltr; float: right; }',
341 'body { direction: ltr; float: left; } .foo { direction: rtl; float: left; }',
344 // Duplicate properties
346 '.foo { float: left; float: right; float: left; }',
347 '.foo { float: right; float: left; float: right; }',
352 '/* left /* right */left: 10px',
353 '/* left /* right */right: 10px'
356 '/*left*//*left*/left: 10px',
357 '/*left*//*left*/right: 10px'
360 '/* Going right is cool */ .foo { width: 0 }',
363 "/* padding-right 1 2 3 4 */\n#test { width: 0}\n/*right*/"
366 "/** Two line comment\n * left\n \*/\n#test {width: 0}"
369 // @noflip annotation
371 // before selector (single)
372 '/* @noflip */ div { float: left; }'
375 // before selector (multiple)
376 '/* @noflip */ div, .notme { float: left; }'
380 'div, /* @noflip */ .foo { float: left; }'
384 'div, .notme /* @noflip */ { float: left; }'
387 // before multiple rules
388 '/* @noflip */ div { float: left; } .foo { float: left; }',
389 '/* @noflip */ div { float: left; } .foo { float: right; }'
392 // after multiple rules
393 '.foo { float: left; } /* @noflip */ div { float: left; }',
394 '.foo { float: right; } /* @noflip */ div { float: left; }'
397 // before multiple properties
398 'div { /* @noflip */ float: left; text-align: left; }',
399 'div { /* @noflip */ float: left; text-align: right; }'
402 // after multiple properties
403 'div { float: left; /* @noflip */ text-align: left; }',
404 'div { float: right; /* @noflip */ text-align: left; }'
407 // Guard against css3 stuff
409 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
412 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
415 // CSS syntax / white-space variations
416 // spaces, no spaces, tabs, new lines, omitting semi-colons
438 ".foo { left : 0 ; }",
439 ".foo { right : 0 ; }"
442 ".foo\n { left : 0 ; }",
443 ".foo\n { right : 0 ; }"
446 ".foo\n { \nleft : 0 ; }",
447 ".foo\n { \nright : 0 ; }"
450 ".foo\n { \n left : 0 ; }",
451 ".foo\n { \n right : 0 ; }"
454 ".foo\n { \n left\n : 0; }",
455 ".foo\n { \n right\n : 0; }"
458 ".foo \n { \n left\n : 0; }",
459 ".foo \n { \n right\n : 0; }"
462 ".foo\n{\nleft\n:\n0;}",
463 ".foo\n{\nright\n:\n0;}"
466 ".foo\n.bar {\n\tleft: 0;\n}",
467 ".foo\n.bar {\n\tright: 0;\n}"
470 ".foo\t{\tleft\t:\t0;}",
471 ".foo\t{\tright\t:\t0;}"
474 // Guard against partial keys
476 '.foo { leftxx: 0; }',
477 '.foo { leftxx: 0; }'
480 '.foo { rightxx: 0; }',
481 '.foo { rightxx: 0; }'
487 * These cases are tested in one way only (format: actual, expected, msg).
488 * If both ways can be tested, either put both versions in here or move
489 * it to provideTransformCases().
491 public static function provideTransformAdvancedCases() {
493 # [ - _ . ] <-> [ left right ltr rtl ]
494 'foo.jpg' => 'foo.jpg',
495 'left.jpg' => 'right.jpg',
496 'ltr.jpg' => 'rtl.jpg',
498 'foo-left.png' => 'foo-right.png',
499 'foo_left.png' => 'foo_right.png',
500 'foo.left.png' => 'foo.right.png',
502 'foo-ltr.png' => 'foo-rtl.png',
503 'foo_ltr.png' => 'foo_rtl.png',
504 'foo.ltr.png' => 'foo.rtl.png',
506 'left-foo.png' => 'right-foo.png',
507 'left_foo.png' => 'right_foo.png',
508 'left.foo.png' => 'right.foo.png',
510 'ltr-foo.png' => 'rtl-foo.png',
511 'ltr_foo.png' => 'rtl_foo.png',
512 'ltr.foo.png' => 'rtl.foo.png',
514 'foo-ltr-left.gif' => 'foo-rtl-right.gif',
515 'foo_ltr_left.gif' => 'foo_rtl_right.gif',
516 'foo.ltr.left.gif' => 'foo.rtl.right.gif',
517 'foo-ltr_left.gif' => 'foo-rtl_right.gif',
518 'foo_ltr.left.gif' => 'foo_rtl.right.gif',
521 foreach ( $bgPairs as $left => $right ) {
522 # By default '-rtl' and '-left' etc. are not touched,
523 # Only when the appropiate parameter is set.
525 ".foo { background: url(images/$left); }",
526 ".foo { background: url(images/$left); }"
529 ".foo { background: url(images/$right); }",
530 ".foo { background: url(images/$right); }"
533 ".foo { background: url(images/$left); }",
534 ".foo { background: url(images/$right); }",
536 'swapLtrRtlInURL' => true,
537 'swapLeftRightInURL' => true,
541 ".foo { background: url(images/$right); }",
542 ".foo { background: url(images/$left); }",
544 'swapLtrRtlInURL' => true,
545 'swapLeftRightInURL' => true,
554 * Cases that are currently failing, but
555 * should be looked at in the future as enhancements and/or bug fix
557 public static function provideTransformBrokenCases() {
559 // Guard against selectors that look flippable
561 # <foo-left-x attr="x">
562 'foo-left-x[attr="x"] { width: 0; }',
563 'foo-left-x[attr="x"] { width: 0; }'
566 # <div class="foo" data-left="x">
567 '.foo[data-left="x"] { width: 0; }',
568 '.foo[data-left="x"] { width: 0; }'