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 // support parentheses in selector
398 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }',
399 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }'
402 // after multiple rules
403 '.foo { float: left; } /* @noflip */ div { float: left; }',
404 '.foo { float: right; } /* @noflip */ div { float: left; }'
407 // before multiple properties
408 'div { /* @noflip */ float: left; text-align: left; }',
409 'div { /* @noflip */ float: left; text-align: right; }'
412 // after multiple properties
413 'div { float: left; /* @noflip */ text-align: left; }',
414 'div { float: right; /* @noflip */ text-align: left; }'
417 // Guard against css3 stuff
419 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
422 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
425 // CSS syntax / white-space variations
426 // spaces, no spaces, tabs, new lines, omitting semi-colons
448 ".foo { left : 0 ; }",
449 ".foo { right : 0 ; }"
452 ".foo\n { left : 0 ; }",
453 ".foo\n { right : 0 ; }"
456 ".foo\n { \nleft : 0 ; }",
457 ".foo\n { \nright : 0 ; }"
460 ".foo\n { \n left : 0 ; }",
461 ".foo\n { \n right : 0 ; }"
464 ".foo\n { \n left\n : 0; }",
465 ".foo\n { \n right\n : 0; }"
468 ".foo \n { \n left\n : 0; }",
469 ".foo \n { \n right\n : 0; }"
472 ".foo\n{\nleft\n:\n0;}",
473 ".foo\n{\nright\n:\n0;}"
476 ".foo\n.bar {\n\tleft: 0;\n}",
477 ".foo\n.bar {\n\tright: 0;\n}"
480 ".foo\t{\tleft\t:\t0;}",
481 ".foo\t{\tright\t:\t0;}"
484 // Guard against partial keys
486 '.foo { leftxx: 0; }',
487 '.foo { leftxx: 0; }'
490 '.foo { rightxx: 0; }',
491 '.foo { rightxx: 0; }'
497 * These cases are tested in one way only (format: actual, expected, msg).
498 * If both ways can be tested, either put both versions in here or move
499 * it to provideTransformCases().
501 public static function provideTransformAdvancedCases() {
503 # [ - _ . ] <-> [ left right ltr rtl ]
504 'foo.jpg' => 'foo.jpg',
505 'left.jpg' => 'right.jpg',
506 'ltr.jpg' => 'rtl.jpg',
508 'foo-left.png' => 'foo-right.png',
509 'foo_left.png' => 'foo_right.png',
510 'foo.left.png' => 'foo.right.png',
512 'foo-ltr.png' => 'foo-rtl.png',
513 'foo_ltr.png' => 'foo_rtl.png',
514 'foo.ltr.png' => 'foo.rtl.png',
516 'left-foo.png' => 'right-foo.png',
517 'left_foo.png' => 'right_foo.png',
518 'left.foo.png' => 'right.foo.png',
520 'ltr-foo.png' => 'rtl-foo.png',
521 'ltr_foo.png' => 'rtl_foo.png',
522 'ltr.foo.png' => 'rtl.foo.png',
524 'foo-ltr-left.gif' => 'foo-rtl-right.gif',
525 'foo_ltr_left.gif' => 'foo_rtl_right.gif',
526 'foo.ltr.left.gif' => 'foo.rtl.right.gif',
527 'foo-ltr_left.gif' => 'foo-rtl_right.gif',
528 'foo_ltr.left.gif' => 'foo_rtl.right.gif',
531 foreach ( $bgPairs as $left => $right ) {
532 # By default '-rtl' and '-left' etc. are not touched,
533 # Only when the appropiate parameter is set.
535 ".foo { background: url(images/$left); }",
536 ".foo { background: url(images/$left); }"
539 ".foo { background: url(images/$right); }",
540 ".foo { background: url(images/$right); }"
543 ".foo { background: url(images/$left); }",
544 ".foo { background: url(images/$right); }",
546 'swapLtrRtlInURL' => true,
547 'swapLeftRightInURL' => true,
551 ".foo { background: url(images/$right); }",
552 ".foo { background: url(images/$left); }",
554 'swapLtrRtlInURL' => true,
555 'swapLeftRightInURL' => true,
564 * Cases that are currently failing, but
565 * should be looked at in the future as enhancements and/or bug fix
567 public static function provideTransformBrokenCases() {
569 // Guard against selectors that look flippable
571 # <foo-left-x attr="x">
572 'foo-left-x[attr="x"] { width: 0; }',
573 'foo-left-x[attr="x"] { width: 0; }'
576 # <div class="foo" data-left="x">
577 '.foo[data-left="x"] { width: 0; }',
578 '.foo[data-left="x"] { width: 0; }'