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 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'
154 // Test the colors regex
155 '.foo { border-color: red green blue white }',
156 '.foo { border-color: red white blue green }',
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 }',
164 // Do not bork 4+ values
165 '.foo { x-unknown: 1 2 3 4 5; }'
168 '.foo { x-unknown: 1 2 3 4 5 6; }'
171 // Shorthand / Three notation
173 '.foo { margin: 1em 0 .25em; }'
176 '.foo { margin:-1.5em 0 -.75em; }'
179 // Shorthand / Two notation
181 '.foo { padding: 1px 2px; }'
184 // Shorthand / One notation
186 '.foo { padding: 1px; }'
189 // text-shadow and box-shadow
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); }',
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); }',
199 '.foo { text-shadow: orange 2px 0; }',
200 '.foo { text-shadow: orange -2px 0; }',
203 '.foo { text-shadow: 2px 0 orange; }',
204 '.foo { text-shadow: -2px 0 orange; }',
207 // Don't mangle zeroes
208 '.foo { text-shadow: orange 0 2px; }'
212 // Note: This differs from the Python implementation,
213 // see also CSSJanus::fixDirection for more info.
215 '.foo { direction: ltr; }',
216 '.foo { direction: rtl; }'
219 '.foo { direction: rtl; }',
220 '.foo { direction: ltr; }'
223 'input { direction: ltr; }',
224 'input { direction: rtl; }'
227 'input { direction: rtl; }',
228 'input { direction: ltr; }'
231 'body { direction: ltr; }',
232 'body { direction: rtl; }'
235 '.foo, body, input { direction: ltr; }',
236 '.foo, body, input { direction: rtl; }'
239 'body { padding: 10px; direction: ltr; }',
240 'body { padding: 10px; direction: rtl; }'
243 'body { direction: ltr } .myClass { direction: ltr }',
244 'body { direction: rtl } .myClass { direction: rtl }'
249 '.foo { float: left; }',
250 '.foo { float: right; }'
253 '.foo { text-align: left; }',
254 '.foo { text-align: right; }'
257 '.foo { -x-unknown: left; }',
258 '.foo { -x-unknown: right; }'
260 // Guard against selectors that look flippable
262 '.column-left { width: 0; }'
265 'a.left { width: 0; }'
268 'a.leftification { width: 0; }'
271 'a.ltr { width: 0; }'
274 # <div class="a-ltr png">
275 '.a-ltr.png { width: 0; }'
279 'foo-ltr[attr="x"] { width: 0; }'
282 'div.left > span.right+span.left { width: 0; }'
285 '.thisclass .left .myclass { width: 0; }'
288 '.thisclass .left .myclass #myid { width: 0; }'
291 // Cursor values (east/west)
293 '.foo { cursor: e-resize; }',
294 '.foo { cursor: w-resize; }'
297 '.foo { cursor: se-resize; }',
298 '.foo { cursor: sw-resize; }'
301 '.foo { cursor: ne-resize; }',
302 '.foo { cursor: nw-resize; }'
307 '.foo { background-position: top left; }',
308 '.foo { background-position: top right; }'
311 '.foo { background: url(/foo/bar.png) top left; }',
312 '.foo { background: url(/foo/bar.png) top right; }'
315 '.foo { background: url(/foo/bar.png) top left no-repeat; }',
316 '.foo { background: url(/foo/bar.png) top right no-repeat; }'
319 '.foo { background: url(/foo/bar.png) no-repeat top left; }',
320 '.foo { background: url(/foo/bar.png) no-repeat top right; }'
323 '.foo { background: #fff url(/foo/bar.png) no-repeat top left; }',
324 '.foo { background: #fff url(/foo/bar.png) no-repeat top right; }'
327 '.foo { background-position: 100% 40%; }',
328 '.foo { background-position: 0% 40%; }'
331 '.foo { background-position: 23% 0; }',
332 '.foo { background-position: 77% 0; }'
335 '.foo { background-position: 23% auto; }',
336 '.foo { background-position: 77% auto; }'
339 '.foo { background-position-x: 23%; }',
340 '.foo { background-position-x: 77%; }'
343 '.foo { background-position-y: 23%; }',
344 '.foo { background-position-y: 23%; }'
347 '.foo { background:url(../foo.png) no-repeat 75% 50%; }',
348 '.foo { background:url(../foo.png) no-repeat 25% 50%; }'
351 '.foo { background: 10% 20% } .bar { background: 40% 30% }',
352 '.foo { background: 90% 20% } .bar { background: 60% 30% }'
357 'body { direction: rtl; float: right; } .foo { direction: ltr; float: right; }',
358 'body { direction: ltr; float: left; } .foo { direction: rtl; float: left; }',
361 // Duplicate properties
363 '.foo { float: left; float: right; float: left; }',
364 '.foo { float: right; float: left; float: right; }',
369 '/* left /* right */left: 10px',
370 '/* left /* right */right: 10px'
373 '/*left*//*left*/left: 10px',
374 '/*left*//*left*/right: 10px'
377 '/* Going right is cool */ .foo { width: 0 }',
380 "/* padding-right 1 2 3 4 */\n#test { width: 0}\n/*right*/"
383 "/** Two line comment\n * left\n \*/\n#test {width: 0}"
386 // @noflip annotation
388 // before selector (single)
389 '/* @noflip */ div { float: left; }'
392 // before selector (multiple)
393 '/* @noflip */ div, .notme { float: left; }'
397 'div, /* @noflip */ .foo { float: left; }'
401 'div, .notme /* @noflip */ { float: left; }'
404 // before multiple rules
405 '/* @noflip */ div { float: left; } .foo { float: left; }',
406 '/* @noflip */ div { float: left; } .foo { float: right; }'
409 // after multiple rules
410 '.foo { float: left; } /* @noflip */ div { float: left; }',
411 '.foo { float: right; } /* @noflip */ div { float: left; }'
414 // before multiple properties
415 'div { /* @noflip */ float: left; text-align: left; }',
416 'div { /* @noflip */ float: left; text-align: right; }'
419 // after multiple properties
420 'div { float: left; /* @noflip */ text-align: left; }',
421 'div { float: right; /* @noflip */ text-align: left; }'
424 // Guard against css3 stuff
426 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
429 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
432 // CSS syntax / white-space variations
433 // spaces, no spaces, tabs, new lines, omitting semi-colons
455 ".foo { left : 0 ; }",
456 ".foo { right : 0 ; }"
459 ".foo\n { left : 0 ; }",
460 ".foo\n { right : 0 ; }"
463 ".foo\n { \nleft : 0 ; }",
464 ".foo\n { \nright : 0 ; }"
467 ".foo\n { \n left : 0 ; }",
468 ".foo\n { \n right : 0 ; }"
471 ".foo\n { \n left\n : 0; }",
472 ".foo\n { \n right\n : 0; }"
475 ".foo \n { \n left\n : 0; }",
476 ".foo \n { \n right\n : 0; }"
479 ".foo\n{\nleft\n:\n0;}",
480 ".foo\n{\nright\n:\n0;}"
483 ".foo\n.bar {\n\tleft: 0;\n}",
484 ".foo\n.bar {\n\tright: 0;\n}"
487 ".foo\t{\tleft\t:\t0;}",
488 ".foo\t{\tright\t:\t0;}"
491 // Guard against partial keys
493 '.foo { leftxx: 0; }',
494 '.foo { leftxx: 0; }'
497 '.foo { rightxx: 0; }',
498 '.foo { rightxx: 0; }'
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().
508 function provideTransformAdvancedCases() {
510 # [ - _ . ] <-> [ left right ltr rtl ]
511 'foo.jpg' => 'foo.jpg',
512 'left.jpg' => 'right.jpg',
513 'ltr.jpg' => 'rtl.jpg',
515 'foo-left.png' => 'foo-right.png',
516 'foo_left.png' => 'foo_right.png',
517 'foo.left.png' => 'foo.right.png',
519 'foo-ltr.png' => 'foo-rtl.png',
520 'foo_ltr.png' => 'foo_rtl.png',
521 'foo.ltr.png' => 'foo.rtl.png',
523 'left-foo.png' => 'right-foo.png',
524 'left_foo.png' => 'right_foo.png',
525 'left.foo.png' => 'right.foo.png',
527 'ltr-foo.png' => 'rtl-foo.png',
528 'ltr_foo.png' => 'rtl_foo.png',
529 'ltr.foo.png' => 'rtl.foo.png',
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',
538 foreach ( $bgPairs as $left => $right ) {
539 # By default '-rtl' and '-left' etc. are not touched,
540 # Only when the appropiate parameter is set.
542 ".foo { background: url(images/$left); }",
543 ".foo { background: url(images/$left); }"
546 ".foo { background: url(images/$right); }",
547 ".foo { background: url(images/$right); }"
550 ".foo { background: url(images/$left); }",
551 ".foo { background: url(images/$right); }",
553 'swapLtrRtlInURL' => true,
554 'swapLeftRightInURL' => true,
558 ".foo { background: url(images/$right); }",
559 ".foo { background: url(images/$left); }",
561 'swapLtrRtlInURL' => true,
562 'swapLeftRightInURL' => true,
571 * Cases that are currently failing, but
572 * should be looked at in the future as enhancements and/or bug fix
574 function provideTransformBrokenCases() {
576 // Guard against selectors that look flippable
578 # <foo-left-x attr="x">
579 'foo-left-x[attr="x"] { width: 0; }',
580 'foo-left-x[attr="x"] { width: 0; }'
583 # <div class="foo" data-left="x">
584 '.foo[data-left="x"] { width: 0; }',
585 '.foo[data-left="x"] { width: 0; }'