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; }'
140 // border-radius assigns different meanings to the values
142 '.foo { border-radius: .25em 15px 0pt 0ex; }',
143 '.foo { border-radius: 15px .25em 0ex 0pt; }'
146 '.foo { border-radius: 0px 0px 5px 5px; }',
148 // Ensure the rule doesn't break other stuff
150 '.foo { x-unknown: a b c d; }'
153 '.foo barpx 0 2% { opacity: 0; }'
156 '#settings td p strong'
160 '.foo { border-color: red green blue white }',
161 '.foo { border-color: red white blue green }',
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 }',
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 }',
174 // Do not mangle 5 or more values
175 '.foo { -x-unknown: 1 2 3 4 5; }'
178 '.foo { -x-unknown: 1 2 3 4 5 6; }'
181 // Shorthand / Three notation
183 '.foo { margin: 1em 0 .25em; }'
186 '.foo { margin:-1.5em 0 -.75em; }'
189 // Shorthand / Two notation
191 '.foo { padding: 1px 2px; }'
194 // Shorthand / One notation
196 '.foo { padding: 1px; }'
200 // Note: This differs from the Python implementation,
201 // see also CSSJanus::fixDirection for more info.
203 '.foo { direction: ltr; }',
204 '.foo { direction: rtl; }'
207 '.foo { direction: rtl; }',
208 '.foo { direction: ltr; }'
211 'input { direction: ltr; }',
212 'input { direction: rtl; }'
215 'input { direction: rtl; }',
216 'input { direction: ltr; }'
219 'body { direction: ltr; }',
220 'body { direction: rtl; }'
223 '.foo, body, input { direction: ltr; }',
224 '.foo, body, input { direction: rtl; }'
227 'body { padding: 10px; direction: ltr; }',
228 'body { padding: 10px; direction: rtl; }'
231 'body { direction: ltr } .myClass { direction: ltr }',
232 'body { direction: rtl } .myClass { direction: rtl }'
237 '.foo { float: left; }',
238 '.foo { float: right; }'
241 '.foo { text-align: left; }',
242 '.foo { text-align: right; }'
245 '.foo { -x-unknown: left; }',
246 '.foo { -x-unknown: right; }'
248 // Guard against selectors that look flippable
250 '.column-left { width: 0; }'
253 'a.left { width: 0; }'
256 'a.leftification { width: 0; }'
259 'a.ltr { width: 0; }'
262 # <div class="a-ltr png">
263 '.a-ltr.png { width: 0; }'
267 'foo-ltr[attr="x"] { width: 0; }'
270 'div.left > span.right+span.left { width: 0; }'
273 '.thisclass .left .myclass { width: 0; }'
276 '.thisclass .left .myclass #myid { width: 0; }'
279 // Cursor values (east/west)
281 '.foo { cursor: e-resize; }',
282 '.foo { cursor: w-resize; }'
285 '.foo { cursor: se-resize; }',
286 '.foo { cursor: sw-resize; }'
289 '.foo { cursor: ne-resize; }',
290 '.foo { cursor: nw-resize; }'
295 '.foo { background-position: top left; }',
296 '.foo { background-position: top right; }'
299 '.foo { background: url(/foo/bar.png) top left; }',
300 '.foo { background: url(/foo/bar.png) top right; }'
303 '.foo { background: url(/foo/bar.png) top left no-repeat; }',
304 '.foo { background: url(/foo/bar.png) top right no-repeat; }'
307 '.foo { background: url(/foo/bar.png) no-repeat top left; }',
308 '.foo { background: url(/foo/bar.png) no-repeat top right; }'
311 '.foo { background: #fff url(/foo/bar.png) no-repeat top left; }',
312 '.foo { background: #fff url(/foo/bar.png) no-repeat top right; }'
315 '.foo { background-position: 100% 40%; }',
316 '.foo { background-position: 0% 40%; }'
319 '.foo { background-position: 23% 0; }',
320 '.foo { background-position: 77% 0; }'
323 '.foo { background-position: 23% auto; }',
324 '.foo { background-position: 77% auto; }'
327 '.foo { background-position-x: 23%; }',
328 '.foo { background-position-x: 77%; }'
331 '.foo { background-position-y: 23%; }',
332 '.foo { background-position-y: 23%; }'
335 '.foo { background:url(../foo.png) no-repeat 75% 50%; }',
336 '.foo { background:url(../foo.png) no-repeat 25% 50%; }'
339 '.foo { background: 10% 20% } .bar { background: 40% 30% }',
340 '.foo { background: 90% 20% } .bar { background: 60% 30% }'
345 'body { direction: rtl; float: right; } .foo { direction: ltr; float: right; }',
346 'body { direction: ltr; float: left; } .foo { direction: rtl; float: left; }',
349 // Duplicate properties
351 '.foo { float: left; float: right; float: left; }',
352 '.foo { float: right; float: left; float: right; }',
357 '/* left /* right */left: 10px',
358 '/* left /* right */right: 10px'
361 '/*left*//*left*/left: 10px',
362 '/*left*//*left*/right: 10px'
365 '/* Going right is cool */ .foo { width: 0 }',
368 "/* padding-right 1 2 3 4 */\n#test { width: 0}\n/*right*/"
371 "/** Two line comment\n * left\n \*/\n#test {width: 0}"
374 // @noflip annotation
376 // before selector (single)
377 '/* @noflip */ div { float: left; }'
380 // before selector (multiple)
381 '/* @noflip */ div, .notme { float: left; }'
385 'div, /* @noflip */ .foo { float: left; }'
389 'div, .notme /* @noflip */ { float: left; }'
392 // before multiple rules
393 '/* @noflip */ div { float: left; } .foo { float: left; }',
394 '/* @noflip */ div { float: left; } .foo { float: right; }'
397 // after multiple rules
398 '.foo { float: left; } /* @noflip */ div { float: left; }',
399 '.foo { float: right; } /* @noflip */ div { float: left; }'
402 // before multiple properties
403 'div { /* @noflip */ float: left; text-align: left; }',
404 'div { /* @noflip */ float: left; text-align: right; }'
407 // after multiple properties
408 'div { float: left; /* @noflip */ text-align: left; }',
409 'div { float: right; /* @noflip */ text-align: left; }'
412 // Guard against css3 stuff
414 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
417 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
420 // CSS syntax / white-space variations
421 // spaces, no spaces, tabs, new lines, omitting semi-colons
443 ".foo { left : 0 ; }",
444 ".foo { right : 0 ; }"
447 ".foo\n { left : 0 ; }",
448 ".foo\n { right : 0 ; }"
451 ".foo\n { \nleft : 0 ; }",
452 ".foo\n { \nright : 0 ; }"
455 ".foo\n { \n left : 0 ; }",
456 ".foo\n { \n right : 0 ; }"
459 ".foo\n { \n left\n : 0; }",
460 ".foo\n { \n right\n : 0; }"
463 ".foo \n { \n left\n : 0; }",
464 ".foo \n { \n right\n : 0; }"
467 ".foo\n{\nleft\n:\n0;}",
468 ".foo\n{\nright\n:\n0;}"
471 ".foo\n.bar {\n\tleft: 0;\n}",
472 ".foo\n.bar {\n\tright: 0;\n}"
475 ".foo\t{\tleft\t:\t0;}",
476 ".foo\t{\tright\t:\t0;}"
479 // Guard against partial keys
481 '.foo { leftxx: 0; }',
482 '.foo { leftxx: 0; }'
485 '.foo { rightxx: 0; }',
486 '.foo { rightxx: 0; }'
492 * These cases are tested in one way only (format: actual, expected, msg).
493 * If both ways can be tested, either put both versions in here or move
494 * it to provideTransformCases().
496 public static function provideTransformAdvancedCases() {
498 # [ - _ . ] <-> [ left right ltr rtl ]
499 'foo.jpg' => 'foo.jpg',
500 'left.jpg' => 'right.jpg',
501 'ltr.jpg' => 'rtl.jpg',
503 'foo-left.png' => 'foo-right.png',
504 'foo_left.png' => 'foo_right.png',
505 'foo.left.png' => 'foo.right.png',
507 'foo-ltr.png' => 'foo-rtl.png',
508 'foo_ltr.png' => 'foo_rtl.png',
509 'foo.ltr.png' => 'foo.rtl.png',
511 'left-foo.png' => 'right-foo.png',
512 'left_foo.png' => 'right_foo.png',
513 'left.foo.png' => 'right.foo.png',
515 'ltr-foo.png' => 'rtl-foo.png',
516 'ltr_foo.png' => 'rtl_foo.png',
517 'ltr.foo.png' => 'rtl.foo.png',
519 'foo-ltr-left.gif' => 'foo-rtl-right.gif',
520 'foo_ltr_left.gif' => 'foo_rtl_right.gif',
521 'foo.ltr.left.gif' => 'foo.rtl.right.gif',
522 'foo-ltr_left.gif' => 'foo-rtl_right.gif',
523 'foo_ltr.left.gif' => 'foo_rtl.right.gif',
526 foreach ( $bgPairs as $left => $right ) {
527 # By default '-rtl' and '-left' etc. are not touched,
528 # Only when the appropiate parameter is set.
530 ".foo { background: url(images/$left); }",
531 ".foo { background: url(images/$left); }"
534 ".foo { background: url(images/$right); }",
535 ".foo { background: url(images/$right); }"
538 ".foo { background: url(images/$left); }",
539 ".foo { background: url(images/$right); }",
541 'swapLtrRtlInURL' => true,
542 'swapLeftRightInURL' => true,
546 ".foo { background: url(images/$right); }",
547 ".foo { background: url(images/$left); }",
549 'swapLtrRtlInURL' => true,
550 'swapLeftRightInURL' => true,
559 * Cases that are currently failing, but
560 * should be looked at in the future as enhancements and/or bug fix
562 public static function provideTransformBrokenCases() {
564 // Guard against selectors that look flippable
566 # <foo-left-x attr="x">
567 'foo-left-x[attr="x"] { width: 0; }',
568 'foo-left-x[attr="x"] { width: 0; }'
571 # <div class="foo" data-left="x">
572 '.foo[data-left="x"] { width: 0; }',
573 '.foo[data-left="x"] { width: 0; }'