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 );
20 str_replace( '/* @noflip */ ', '', $cssB ),
21 'Test A-B transformation'
24 $transformedB = CSSJanus
::transform( $cssB );
27 str_replace( '/* @noflip */ ', '', $cssA ),
28 'Test B-A transformation'
31 // If no B version is provided, it means
32 // the output should equal the input (modulo @noflip annotations).
33 $transformedA = CSSJanus
::transform( $cssA );
36 str_replace( '/* @noflip */ ', '', $cssA ),
43 * @dataProvider provideTransformAdvancedCases
45 public function testTransformAdvanced( $code, $expectedOutput, $options = array() ) {
46 $swapLtrRtlInURL = isset( $options['swapLtrRtlInURL'] ) ?
47 $options['swapLtrRtlInURL'] : false;
48 $swapLeftRightInURL = isset( $options['swapLeftRightInURL'] ) ?
49 $options['swapLeftRightInURL'] : false;
51 $flipped = CSSJanus
::transform( $code, $swapLtrRtlInURL, $swapLeftRightInURL );
53 $this->assertEquals( $expectedOutput, $flipped,
54 'Test flipping, options: url-ltr-rtl=' . ( $swapLtrRtlInURL ?
'true' : 'false' )
55 . ' url-left-right=' . ( $swapLeftRightInURL ?
'true' : 'false' )
60 * @dataProvider provideTransformBrokenCases
63 public function testTransformBroken( $code, $expectedOutput ) {
64 $flipped = CSSJanus
::transform( $code );
66 $this->assertEquals( $expectedOutput, $flipped, 'Test flipping' );
70 * These transform cases are tested *in both directions*
71 * No need to declare a principle twice in both directions here.
73 public static function provideTransformCases() {
80 // Guard against partial keys
81 // (CSS currently doesn't have flippable properties
82 // that contain the direction as part of the key without
85 '.foo { alright: 0; }'
88 '.foo { balleft: 0; }'
91 // Dashed property keys
93 '.foo { padding-left: 0; }',
94 '.foo { padding-right: 0; }'
97 '.foo { margin-left: 0; }',
98 '.foo { margin-right: 0; }'
101 '.foo { border-left: 0; }',
102 '.foo { border-right: 0; }'
105 // Double-dashed property keys
107 '.foo { border-left-color: red; }',
108 '.foo { border-right-color: red; }'
111 // Includes unknown properties?
112 '.foo { x-left-y: 0; }',
113 '.foo { x-right-y: 0; }'
116 // Multi-value properties
118 '.foo { padding: 0; }'
121 '.foo { padding: 0 1px; }'
124 '.foo { padding: 0 1px 2px; }'
127 '.foo { padding: 0 1px 2px 3px; }',
128 '.foo { padding: 0 3px 2px 1px; }'
131 // Shorthand / Four notation
133 '.foo { padding: .25em 15px 0pt 0ex; }',
134 '.foo { padding: .25em 0ex 0pt 15px; }'
137 '.foo { margin: 1px -4px 3px 2px; }',
138 '.foo { margin: 1px 2px 3px -4px; }'
141 '.foo { padding: 0 15px .25em 0; }',
142 '.foo { padding: 0 0 .25em 15px; }'
145 '.foo { padding: 1px 4.1grad 3px 2%; }',
146 '.foo { padding: 1px 2% 3px 4.1grad; }'
149 '.foo { padding: 1px 2px 3px auto; }',
150 '.foo { padding: 1px auto 3px 2px; }'
153 '.foo { padding: 1px inherit 3px auto; }',
154 '.foo { padding: 1px auto 3px inherit; }'
156 // border-radius assigns different meanings to the values
158 '.foo { border-radius: .25em 15px 0pt 0ex; }',
159 '.foo { border-radius: 15px .25em 0ex 0pt; }'
162 '.foo { border-radius: 0px 0px 5px 5px; }',
164 // Ensure the rule doesn't break other stuff
166 '.foo { x-unknown: a b c d; }'
169 '.foo barpx 0 2% { opacity: 0; }'
172 '#settings td p strong'
176 '.foo { border-color: red green blue white }',
177 '.foo { border-color: red white blue green }',
180 // Color name, hexdecimal, RGB & RGBA
181 '.foo { border-color: red #f00 rgb(255, 0, 0) rgba(255, 0, 0, 0.5) }',
182 '.foo { border-color: red rgba(255, 0, 0, 0.5) rgb(255, 0, 0) #f00 }',
185 // Color name, hexdecimal, HSL & HSLA
186 '.foo { border-color: red #f00 hsl(0, 100%, 50%) hsla(0, 100%, 50%, 0.5) }',
187 '.foo { border-color: red hsla(0, 100%, 50%, 0.5) hsl(0, 100%, 50%) #f00 }',
190 // Do not mangle 5 or more values
191 '.foo { -x-unknown: 1 2 3 4 5; }'
194 '.foo { -x-unknown: 1 2 3 4 5 6; }'
197 // Shorthand / Three notation
199 '.foo { margin: 1em 0 .25em; }'
202 '.foo { margin:-1.5em 0 -.75em; }'
205 // Shorthand / Two notation
207 '.foo { padding: 1px 2px; }'
210 // Shorthand / One notation
212 '.foo { padding: 1px; }'
215 // text-shadow and box-shadow
217 '.foo { box-shadow: -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
218 '.foo { box-shadow: 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
221 '.foo { box-shadow: inset -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
222 '.foo { box-shadow: inset 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
225 '.foo { text-shadow: orange 2px 0; }',
226 '.foo { text-shadow: orange -2px 0; }',
229 '.foo { text-shadow: 2px 0 orange; }',
230 '.foo { text-shadow: -2px 0 orange; }',
233 // Don't mangle zeroes
234 '.foo { text-shadow: orange 0 2px; }'
237 // Make sure floats are not considered zero
238 '.foo { box-shadow: inset .5em 0 0 white; }',
239 '.foo { box-shadow: inset -.5em 0 0 white; }',
243 // Note: This differs from the Python implementation,
244 // see also CSSJanus::fixDirection for more info.
246 '.foo { direction: ltr; }',
247 '.foo { direction: rtl; }'
250 '.foo { direction: rtl; }',
251 '.foo { direction: ltr; }'
254 'input { direction: ltr; }',
255 'input { direction: rtl; }'
258 'input { direction: rtl; }',
259 'input { direction: ltr; }'
262 'body { direction: ltr; }',
263 'body { direction: rtl; }'
266 '.foo, body, input { direction: ltr; }',
267 '.foo, body, input { direction: rtl; }'
270 'body { padding: 10px; direction: ltr; }',
271 'body { padding: 10px; direction: rtl; }'
274 'body { direction: ltr } .myClass { direction: ltr }',
275 'body { direction: rtl } .myClass { direction: rtl }'
280 '.foo { float: left; }',
281 '.foo { float: right; }'
284 '.foo { text-align: left; }',
285 '.foo { text-align: right; }'
288 '.foo { -x-unknown: left; }',
289 '.foo { -x-unknown: right; }'
291 // Guard against selectors that look flippable
293 '.column-left { width: 0; }'
296 'a.left { width: 0; }'
299 'a.leftification { width: 0; }'
302 'a.ltr { width: 0; }'
305 # <div class="a-ltr png">
306 '.a-ltr.png { width: 0; }'
310 'foo-ltr[attr="x"] { width: 0; }'
313 'div.left > span.right+span.left { width: 0; }'
316 '.thisclass .left .myclass { width: 0; }'
319 '.thisclass .left .myclass #myid { width: 0; }'
322 // Cursor values (east/west)
324 '.foo { cursor: e-resize; }',
325 '.foo { cursor: w-resize; }'
328 '.foo { cursor: se-resize; }',
329 '.foo { cursor: sw-resize; }'
332 '.foo { cursor: ne-resize; }',
333 '.foo { cursor: nw-resize; }'
338 '.foo { background-position: top left; }',
339 '.foo { background-position: top right; }'
342 '.foo { background: url(/foo/bar.png) top left; }',
343 '.foo { background: url(/foo/bar.png) top right; }'
346 '.foo { background: url(/foo/bar.png) top left no-repeat; }',
347 '.foo { background: url(/foo/bar.png) top right no-repeat; }'
350 '.foo { background: url(/foo/bar.png) no-repeat top left; }',
351 '.foo { background: url(/foo/bar.png) no-repeat top right; }'
354 '.foo { background: #fff url(/foo/bar.png) no-repeat top left; }',
355 '.foo { background: #fff url(/foo/bar.png) no-repeat top right; }'
358 '.foo { background-position: 100% 40%; }',
359 '.foo { background-position: 0% 40%; }'
362 '.foo { background-position: 23% 0; }',
363 '.foo { background-position: 77% 0; }'
366 '.foo { background-position: 23% auto; }',
367 '.foo { background-position: 77% auto; }'
370 '.foo { background-position-x: 23%; }',
371 '.foo { background-position-x: 77%; }'
374 '.foo { background-position-y: 23%; }',
375 '.foo { background-position-y: 23%; }'
378 '.foo { background:url(../foo.png) no-repeat 75% 50%; }',
379 '.foo { background:url(../foo.png) no-repeat 25% 50%; }'
382 '.foo { background: 10% 20% } .bar { background: 40% 30% }',
383 '.foo { background: 90% 20% } .bar { background: 60% 30% }'
388 'body { direction: rtl; float: right; } .foo { direction: ltr; float: right; }',
389 'body { direction: ltr; float: left; } .foo { direction: rtl; float: left; }',
392 // Duplicate properties
394 '.foo { float: left; float: right; float: left; }',
395 '.foo { float: right; float: left; float: right; }',
400 '/* left /* right */left: 10px',
401 '/* left /* right */right: 10px'
404 '/*left*//*left*/left: 10px',
405 '/*left*//*left*/right: 10px'
408 '/* Going right is cool */ .foo { width: 0 }',
411 "/* padding-right 1 2 3 4 */\n#test { width: 0}\n/*right*/"
414 "/** Two line comment\n * left\n \*/\n#test {width: 0}"
417 // @noflip annotation
419 // before selector (single)
420 '/* @noflip */ div { float: left; }'
423 // before selector (multiple)
424 '/* @noflip */ div, .notme { float: left; }'
428 'div, /* @noflip */ .foo { float: left; }'
432 'div, .notme /* @noflip */ { float: left; }'
435 // before multiple rules
436 '/* @noflip */ div { float: left; } .foo { float: left; }',
437 '/* @noflip */ div { float: left; } .foo { float: right; }'
440 // support parentheses in selector
441 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }',
442 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }'
445 // after multiple rules
446 '.foo { float: left; } /* @noflip */ div { float: left; }',
447 '.foo { float: right; } /* @noflip */ div { float: left; }'
450 // before multiple properties
451 'div { /* @noflip */ float: left; text-align: left; }',
452 'div { /* @noflip */ float: left; text-align: right; }'
455 // after multiple properties
456 'div { float: left; /* @noflip */ text-align: left; }',
457 'div { float: right; /* @noflip */ text-align: left; }'
460 // before a *= attribute selector with multiple properties
461 '/* @noflip */ div.foo[bar*=baz] { float:left; clear: left; }'
464 // before a ^= attribute selector with multiple properties
465 '/* @noflip */ div.foo[bar^=baz] { float:left; clear: left; }'
468 // before a ~= attribute selector with multiple properties
469 '/* @noflip */ div.foo[bar~=baz] { float:left; clear: left; }'
472 // before a = attribute selector with multiple properties
473 '/* @noflip */ div.foo[bar=baz] { float:left; clear: left; }'
476 // before a quoted attribute selector with multiple properties
477 '/* @noflip */ div.foo[bar=\'baz{quux\'] { float:left; clear: left; }'
480 // Guard against css3 stuff
482 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
485 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
488 // CSS syntax / white-space variations
489 // spaces, no spaces, tabs, new lines, omitting semi-colons
511 ".foo { left : 0 ; }",
512 ".foo { right : 0 ; }"
515 ".foo\n { left : 0 ; }",
516 ".foo\n { right : 0 ; }"
519 ".foo\n { \nleft : 0 ; }",
520 ".foo\n { \nright : 0 ; }"
523 ".foo\n { \n left : 0 ; }",
524 ".foo\n { \n right : 0 ; }"
527 ".foo\n { \n left\n : 0; }",
528 ".foo\n { \n right\n : 0; }"
531 ".foo \n { \n left\n : 0; }",
532 ".foo \n { \n right\n : 0; }"
535 ".foo\n{\nleft\n:\n0;}",
536 ".foo\n{\nright\n:\n0;}"
539 ".foo\n.bar {\n\tleft: 0;\n}",
540 ".foo\n.bar {\n\tright: 0;\n}"
543 ".foo\t{\tleft\t:\t0;}",
544 ".foo\t{\tright\t:\t0;}"
547 // Guard against partial keys
549 '.foo { leftxx: 0; }',
550 '.foo { leftxx: 0; }'
553 '.foo { rightxx: 0; }',
554 '.foo { rightxx: 0; }'
560 * These cases are tested in one way only (format: actual, expected, msg).
561 * If both ways can be tested, either put both versions in here or move
562 * it to provideTransformCases().
564 public static function provideTransformAdvancedCases() {
566 # [ - _ . ] <-> [ left right ltr rtl ]
567 'foo.jpg' => 'foo.jpg',
568 'left.jpg' => 'right.jpg',
569 'ltr.jpg' => 'rtl.jpg',
571 'foo-left.png' => 'foo-right.png',
572 'foo_left.png' => 'foo_right.png',
573 'foo.left.png' => 'foo.right.png',
575 'foo-ltr.png' => 'foo-rtl.png',
576 'foo_ltr.png' => 'foo_rtl.png',
577 'foo.ltr.png' => 'foo.rtl.png',
579 'left-foo.png' => 'right-foo.png',
580 'left_foo.png' => 'right_foo.png',
581 'left.foo.png' => 'right.foo.png',
583 'ltr-foo.png' => 'rtl-foo.png',
584 'ltr_foo.png' => 'rtl_foo.png',
585 'ltr.foo.png' => 'rtl.foo.png',
587 'foo-ltr-left.gif' => 'foo-rtl-right.gif',
588 'foo_ltr_left.gif' => 'foo_rtl_right.gif',
589 'foo.ltr.left.gif' => 'foo.rtl.right.gif',
590 'foo-ltr_left.gif' => 'foo-rtl_right.gif',
591 'foo_ltr.left.gif' => 'foo_rtl.right.gif',
594 foreach ( $bgPairs as $left => $right ) {
595 # By default '-rtl' and '-left' etc. are not touched,
596 # Only when the appropiate parameter is set.
598 ".foo { background: url(images/$left); }",
599 ".foo { background: url(images/$left); }"
602 ".foo { background: url(images/$right); }",
603 ".foo { background: url(images/$right); }"
606 ".foo { background: url(images/$left); }",
607 ".foo { background: url(images/$right); }",
609 'swapLtrRtlInURL' => true,
610 'swapLeftRightInURL' => true,
614 ".foo { background: url(images/$right); }",
615 ".foo { background: url(images/$left); }",
617 'swapLtrRtlInURL' => true,
618 'swapLeftRightInURL' => true,
627 * Cases that are currently failing, but
628 * should be looked at in the future as enhancements and/or bug fix
630 public static function provideTransformBrokenCases() {
632 // Guard against selectors that look flippable
634 # <foo-left-x attr="x">
635 'foo-left-x[attr="x"] { width: 0; }',
636 'foo-left-x[attr="x"] { width: 0; }'
639 # <div class="foo" data-left="x">
640 '.foo[data-left="x"] { width: 0; }',
641 '.foo[data-left="x"] { width: 0; }'