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'] ) ?
35 $options['swapLtrRtlInURL'] : false;
36 $swapLeftRightInURL = isset( $options['swapLeftRightInURL'] ) ?
37 $options['swapLeftRightInURL'] : false;
39 $flipped = CSSJanus
::transform( $code, $swapLtrRtlInURL, $swapLeftRightInURL );
41 $this->assertEquals( $expectedOutput, $flipped,
42 'Test flipping, options: url-ltr-rtl=' . ( $swapLtrRtlInURL ?
'true' : 'false' )
43 . ' url-left-right=' . ( $swapLeftRightInURL ?
'true' : 'false' )
48 * @dataProvider provideTransformBrokenCases
51 public function testTransformBroken( $code, $expectedOutput ) {
52 $flipped = CSSJanus
::transform( $code );
54 $this->assertEquals( $expectedOutput, $flipped, 'Test flipping' );
58 * These transform cases are tested *in both directions*
59 * No need to declare a principle twice in both directions here.
61 public static function provideTransformCases() {
68 // Guard against partial keys
69 // (CSS currently doesn't have flippable properties
70 // that contain the direction as part of the key without
73 '.foo { alright: 0; }'
76 '.foo { balleft: 0; }'
79 // Dashed property keys
81 '.foo { padding-left: 0; }',
82 '.foo { padding-right: 0; }'
85 '.foo { margin-left: 0; }',
86 '.foo { margin-right: 0; }'
89 '.foo { border-left: 0; }',
90 '.foo { border-right: 0; }'
93 // Double-dashed property keys
95 '.foo { border-left-color: red; }',
96 '.foo { border-right-color: red; }'
99 // Includes unknown properties?
100 '.foo { x-left-y: 0; }',
101 '.foo { x-right-y: 0; }'
104 // Multi-value properties
106 '.foo { padding: 0; }'
109 '.foo { padding: 0 1px; }'
112 '.foo { padding: 0 1px 2px; }'
115 '.foo { padding: 0 1px 2px 3px; }',
116 '.foo { padding: 0 3px 2px 1px; }'
119 // Shorthand / Four notation
121 '.foo { padding: .25em 15px 0pt 0ex; }',
122 '.foo { padding: .25em 0ex 0pt 15px; }'
125 '.foo { margin: 1px -4px 3px 2px; }',
126 '.foo { margin: 1px 2px 3px -4px; }'
129 '.foo { padding: 0 15px .25em 0; }',
130 '.foo { padding: 0 0 .25em 15px; }'
133 '.foo { padding: 1px 4.1grad 3px 2%; }',
134 '.foo { padding: 1px 2% 3px 4.1grad; }'
137 '.foo { padding: 1px 2px 3px auto; }',
138 '.foo { padding: 1px auto 3px 2px; }'
141 '.foo { padding: 1px inherit 3px auto; }',
142 '.foo { padding: 1px auto 3px inherit; }'
144 // border-radius assigns different meanings to the values
146 '.foo { border-radius: .25em 15px 0pt 0ex; }',
147 '.foo { border-radius: 15px .25em 0ex 0pt; }'
150 '.foo { border-radius: 0px 0px 5px 5px; }',
152 // Ensure the rule doesn't break other stuff
154 '.foo { x-unknown: a b c d; }'
157 '.foo barpx 0 2% { opacity: 0; }'
160 '#settings td p strong'
164 '.foo { border-color: red green blue white }',
165 '.foo { border-color: red white blue green }',
168 // Color name, hexdecimal, RGB & RGBA
169 '.foo { border-color: red #f00 rgb(255, 0, 0) rgba(255, 0, 0, 0.5) }',
170 '.foo { border-color: red rgba(255, 0, 0, 0.5) rgb(255, 0, 0) #f00 }',
173 // Color name, hexdecimal, HSL & HSLA
174 '.foo { border-color: red #f00 hsl(0, 100%, 50%) hsla(0, 100%, 50%, 0.5) }',
175 '.foo { border-color: red hsla(0, 100%, 50%, 0.5) hsl(0, 100%, 50%) #f00 }',
178 // Do not mangle 5 or more values
179 '.foo { -x-unknown: 1 2 3 4 5; }'
182 '.foo { -x-unknown: 1 2 3 4 5 6; }'
185 // Shorthand / Three notation
187 '.foo { margin: 1em 0 .25em; }'
190 '.foo { margin:-1.5em 0 -.75em; }'
193 // Shorthand / Two notation
195 '.foo { padding: 1px 2px; }'
198 // Shorthand / One notation
200 '.foo { padding: 1px; }'
203 // text-shadow and box-shadow
205 '.foo { box-shadow: -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
206 '.foo { box-shadow: 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
209 '.foo { box-shadow: inset -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
210 '.foo { box-shadow: inset 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
213 '.foo { text-shadow: orange 2px 0; }',
214 '.foo { text-shadow: orange -2px 0; }',
217 '.foo { text-shadow: 2px 0 orange; }',
218 '.foo { text-shadow: -2px 0 orange; }',
221 // Don't mangle zeroes
222 '.foo { text-shadow: orange 0 2px; }'
226 // Note: This differs from the Python implementation,
227 // see also CSSJanus::fixDirection for more info.
229 '.foo { direction: ltr; }',
230 '.foo { direction: rtl; }'
233 '.foo { direction: rtl; }',
234 '.foo { direction: ltr; }'
237 'input { direction: ltr; }',
238 'input { direction: rtl; }'
241 'input { direction: rtl; }',
242 'input { direction: ltr; }'
245 'body { direction: ltr; }',
246 'body { direction: rtl; }'
249 '.foo, body, input { direction: ltr; }',
250 '.foo, body, input { direction: rtl; }'
253 'body { padding: 10px; direction: ltr; }',
254 'body { padding: 10px; direction: rtl; }'
257 'body { direction: ltr } .myClass { direction: ltr }',
258 'body { direction: rtl } .myClass { direction: rtl }'
263 '.foo { float: left; }',
264 '.foo { float: right; }'
267 '.foo { text-align: left; }',
268 '.foo { text-align: right; }'
271 '.foo { -x-unknown: left; }',
272 '.foo { -x-unknown: right; }'
274 // Guard against selectors that look flippable
276 '.column-left { width: 0; }'
279 'a.left { width: 0; }'
282 'a.leftification { width: 0; }'
285 'a.ltr { width: 0; }'
288 # <div class="a-ltr png">
289 '.a-ltr.png { width: 0; }'
293 'foo-ltr[attr="x"] { width: 0; }'
296 'div.left > span.right+span.left { width: 0; }'
299 '.thisclass .left .myclass { width: 0; }'
302 '.thisclass .left .myclass #myid { width: 0; }'
305 // Cursor values (east/west)
307 '.foo { cursor: e-resize; }',
308 '.foo { cursor: w-resize; }'
311 '.foo { cursor: se-resize; }',
312 '.foo { cursor: sw-resize; }'
315 '.foo { cursor: ne-resize; }',
316 '.foo { cursor: nw-resize; }'
321 '.foo { background-position: top left; }',
322 '.foo { background-position: top right; }'
325 '.foo { background: url(/foo/bar.png) top left; }',
326 '.foo { background: url(/foo/bar.png) top right; }'
329 '.foo { background: url(/foo/bar.png) top left no-repeat; }',
330 '.foo { background: url(/foo/bar.png) top right no-repeat; }'
333 '.foo { background: url(/foo/bar.png) no-repeat top left; }',
334 '.foo { background: url(/foo/bar.png) no-repeat top right; }'
337 '.foo { background: #fff url(/foo/bar.png) no-repeat top left; }',
338 '.foo { background: #fff url(/foo/bar.png) no-repeat top right; }'
341 '.foo { background-position: 100% 40%; }',
342 '.foo { background-position: 0% 40%; }'
345 '.foo { background-position: 23% 0; }',
346 '.foo { background-position: 77% 0; }'
349 '.foo { background-position: 23% auto; }',
350 '.foo { background-position: 77% auto; }'
353 '.foo { background-position-x: 23%; }',
354 '.foo { background-position-x: 77%; }'
357 '.foo { background-position-y: 23%; }',
358 '.foo { background-position-y: 23%; }'
361 '.foo { background:url(../foo.png) no-repeat 75% 50%; }',
362 '.foo { background:url(../foo.png) no-repeat 25% 50%; }'
365 '.foo { background: 10% 20% } .bar { background: 40% 30% }',
366 '.foo { background: 90% 20% } .bar { background: 60% 30% }'
371 'body { direction: rtl; float: right; } .foo { direction: ltr; float: right; }',
372 'body { direction: ltr; float: left; } .foo { direction: rtl; float: left; }',
375 // Duplicate properties
377 '.foo { float: left; float: right; float: left; }',
378 '.foo { float: right; float: left; float: right; }',
383 '/* left /* right */left: 10px',
384 '/* left /* right */right: 10px'
387 '/*left*//*left*/left: 10px',
388 '/*left*//*left*/right: 10px'
391 '/* Going right is cool */ .foo { width: 0 }',
394 "/* padding-right 1 2 3 4 */\n#test { width: 0}\n/*right*/"
397 "/** Two line comment\n * left\n \*/\n#test {width: 0}"
400 // @noflip annotation
402 // before selector (single)
403 '/* @noflip */ div { float: left; }'
406 // before selector (multiple)
407 '/* @noflip */ div, .notme { float: left; }'
411 'div, /* @noflip */ .foo { float: left; }'
415 'div, .notme /* @noflip */ { float: left; }'
418 // before multiple rules
419 '/* @noflip */ div { float: left; } .foo { float: left; }',
420 '/* @noflip */ div { float: left; } .foo { float: right; }'
423 // support parentheses in selector
424 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }',
425 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }'
428 // after multiple rules
429 '.foo { float: left; } /* @noflip */ div { float: left; }',
430 '.foo { float: right; } /* @noflip */ div { float: left; }'
433 // before multiple properties
434 'div { /* @noflip */ float: left; text-align: left; }',
435 'div { /* @noflip */ float: left; text-align: right; }'
438 // after multiple properties
439 'div { float: left; /* @noflip */ text-align: left; }',
440 'div { float: right; /* @noflip */ text-align: left; }'
443 // before a *= attribute selector with multiple properties
444 '/* @noflip */ div.foo[bar*=baz] { float:left; clear: left; }'
447 // before a ^= attribute selector with multiple properties
448 '/* @noflip */ div.foo[bar^=baz] { float:left; clear: left; }'
451 // before a ~= attribute selector with multiple properties
452 '/* @noflip */ div.foo[bar~=baz] { float:left; clear: left; }'
455 // before a = attribute selector with multiple properties
456 '/* @noflip */ div.foo[bar=baz] { float:left; clear: left; }'
459 // before a quoted attribute selector with multiple properties
460 '/* @noflip */ div.foo[bar=\'baz{quux\'] { float:left; clear: left; }'
463 // Guard against css3 stuff
465 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
468 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
471 // CSS syntax / white-space variations
472 // spaces, no spaces, tabs, new lines, omitting semi-colons
494 ".foo { left : 0 ; }",
495 ".foo { right : 0 ; }"
498 ".foo\n { left : 0 ; }",
499 ".foo\n { right : 0 ; }"
502 ".foo\n { \nleft : 0 ; }",
503 ".foo\n { \nright : 0 ; }"
506 ".foo\n { \n left : 0 ; }",
507 ".foo\n { \n right : 0 ; }"
510 ".foo\n { \n left\n : 0; }",
511 ".foo\n { \n right\n : 0; }"
514 ".foo \n { \n left\n : 0; }",
515 ".foo \n { \n right\n : 0; }"
518 ".foo\n{\nleft\n:\n0;}",
519 ".foo\n{\nright\n:\n0;}"
522 ".foo\n.bar {\n\tleft: 0;\n}",
523 ".foo\n.bar {\n\tright: 0;\n}"
526 ".foo\t{\tleft\t:\t0;}",
527 ".foo\t{\tright\t:\t0;}"
530 // Guard against partial keys
532 '.foo { leftxx: 0; }',
533 '.foo { leftxx: 0; }'
536 '.foo { rightxx: 0; }',
537 '.foo { rightxx: 0; }'
543 * These cases are tested in one way only (format: actual, expected, msg).
544 * If both ways can be tested, either put both versions in here or move
545 * it to provideTransformCases().
547 public static function provideTransformAdvancedCases() {
549 # [ - _ . ] <-> [ left right ltr rtl ]
550 'foo.jpg' => 'foo.jpg',
551 'left.jpg' => 'right.jpg',
552 'ltr.jpg' => 'rtl.jpg',
554 'foo-left.png' => 'foo-right.png',
555 'foo_left.png' => 'foo_right.png',
556 'foo.left.png' => 'foo.right.png',
558 'foo-ltr.png' => 'foo-rtl.png',
559 'foo_ltr.png' => 'foo_rtl.png',
560 'foo.ltr.png' => 'foo.rtl.png',
562 'left-foo.png' => 'right-foo.png',
563 'left_foo.png' => 'right_foo.png',
564 'left.foo.png' => 'right.foo.png',
566 'ltr-foo.png' => 'rtl-foo.png',
567 'ltr_foo.png' => 'rtl_foo.png',
568 'ltr.foo.png' => 'rtl.foo.png',
570 'foo-ltr-left.gif' => 'foo-rtl-right.gif',
571 'foo_ltr_left.gif' => 'foo_rtl_right.gif',
572 'foo.ltr.left.gif' => 'foo.rtl.right.gif',
573 'foo-ltr_left.gif' => 'foo-rtl_right.gif',
574 'foo_ltr.left.gif' => 'foo_rtl.right.gif',
577 foreach ( $bgPairs as $left => $right ) {
578 # By default '-rtl' and '-left' etc. are not touched,
579 # Only when the appropiate parameter is set.
581 ".foo { background: url(images/$left); }",
582 ".foo { background: url(images/$left); }"
585 ".foo { background: url(images/$right); }",
586 ".foo { background: url(images/$right); }"
589 ".foo { background: url(images/$left); }",
590 ".foo { background: url(images/$right); }",
592 'swapLtrRtlInURL' => true,
593 'swapLeftRightInURL' => true,
597 ".foo { background: url(images/$right); }",
598 ".foo { background: url(images/$left); }",
600 'swapLtrRtlInURL' => true,
601 'swapLeftRightInURL' => true,
610 * Cases that are currently failing, but
611 * should be looked at in the future as enhancements and/or bug fix
613 public static function provideTransformBrokenCases() {
615 // Guard against selectors that look flippable
617 # <foo-left-x attr="x">
618 'foo-left-x[attr="x"] { width: 0; }',
619 'foo-left-x[attr="x"] { width: 0; }'
622 # <div class="foo" data-left="x">
623 '.foo[data-left="x"] { width: 0; }',
624 '.foo[data-left="x"] { width: 0; }'