1c898f021c9a95e4bd52e02cf062391c6e94225d
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / CSSJanusTest.php
1 <?php
2 /**
3 * Based on the test suite of the original Python
4 * CSSJanus libary:
5 * http://code.google.com/p/cssjanus/source/browse/trunk/cssjanus_test.py
6 * Ported to PHP for ResourceLoader and has been extended since.
7 */
8 class CSSJanusTest extends MediaWikiTestCase {
9 /**
10 * @dataProvider provideTransformCases
11 */
12 function testTransform( $cssA, $cssB = null ) {
13
14 if ( $cssB ) {
15 $transformedA = CSSJanus::transform( $cssA );
16 $this->assertEquals( $transformedA, $cssB, 'Test A-B transformation' );
17
18 $transformedB = CSSJanus::transform( $cssB );
19 $this->assertEquals( $transformedB, $cssA, 'Test B-A transformation' );
20 } else {
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' );
25 }
26 }
27
28 /**
29 * @dataProvider provideTransformAdvancedCases
30 */
31 function testTransformAdvanced( $code, $expectedOutput, $options = array() ) {
32 $swapLtrRtlInURL = isset( $options['swapLtrRtlInURL'] ) ? $options['swapLtrRtlInURL'] : false;
33 $swapLeftRightInURL = isset( $options['swapLeftRightInURL'] ) ? $options['swapLeftRightInURL'] : false;
34
35 $flipped = CSSJanus::transform( $code, $swapLtrRtlInURL, $swapLeftRightInURL );
36
37 $this->assertEquals( $expectedOutput, $flipped,
38 'Test flipping, options: url-ltr-rtl=' . ( $swapLtrRtlInURL ? 'true' : 'false' )
39 . ' url-left-right=' . ( $swapLeftRightInURL ? 'true' : 'false' )
40 );
41 }
42
43 /**
44 * @dataProvider provideTransformBrokenCases
45 * @group Broken
46 */
47 function testTransformBroken( $code, $expectedOutput ) {
48 $flipped = CSSJanus::transform( $code );
49
50 $this->assertEquals( $expectedOutput, $flipped, 'Test flipping' );
51 }
52
53 /**
54 * These transform cases are tested *in both directions*
55 * No need to declare a principle twice in both directions here.
56 */
57 public static function provideTransformCases() {
58 return array(
59 // Property keys
60 array(
61 '.foo { left: 0; }',
62 '.foo { right: 0; }'
63 ),
64 // Guard against partial keys
65 // (CSS currently doesn't have flippable properties
66 // that contain the direction as part of the key without
67 // dash separation)
68 array(
69 '.foo { alright: 0; }'
70 ),
71 array(
72 '.foo { balleft: 0; }'
73 ),
74
75 // Dashed property keys
76 array(
77 '.foo { padding-left: 0; }',
78 '.foo { padding-right: 0; }'
79 ),
80 array(
81 '.foo { margin-left: 0; }',
82 '.foo { margin-right: 0; }'
83 ),
84 array(
85 '.foo { border-left: 0; }',
86 '.foo { border-right: 0; }'
87 ),
88
89 // Double-dashed property keys
90 array(
91 '.foo { border-left-color: red; }',
92 '.foo { border-right-color: red; }'
93 ),
94 array(
95 // Includes unknown properties?
96 '.foo { x-left-y: 0; }',
97 '.foo { x-right-y: 0; }'
98 ),
99
100 // Multi-value properties
101 array(
102 '.foo { padding: 0; }'
103 ),
104 array(
105 '.foo { padding: 0 1px; }'
106 ),
107 array(
108 '.foo { padding: 0 1px 2px; }'
109 ),
110 array(
111 '.foo { padding: 0 1px 2px 3px; }',
112 '.foo { padding: 0 3px 2px 1px; }'
113 ),
114
115 // Shorthand / Four notation
116 array(
117 '.foo { padding: .25em 15px 0pt 0ex; }',
118 '.foo { padding: .25em 0ex 0pt 15px; }'
119 ),
120 array(
121 '.foo { margin: 1px -4px 3px 2px; }',
122 '.foo { margin: 1px 2px 3px -4px; }'
123 ),
124 array(
125 '.foo { padding: 0 15px .25em 0; }',
126 '.foo { padding: 0 0 .25em 15px; }'
127 ),
128 array(
129 '.foo { padding: 1px 4.1grad 3px 2%; }',
130 '.foo { padding: 1px 2% 3px 4.1grad; }'
131 ),
132 array(
133 '.foo { padding: 1px 2px 3px auto; }',
134 '.foo { padding: 1px auto 3px 2px; }'
135 ),
136 array(
137 '.foo { padding: 1px inherit 3px auto; }',
138 '.foo { padding: 1px auto 3px inherit; }'
139 ),
140 // border-radius assigns different meanings to the values
141 array(
142 '.foo { border-radius: .25em 15px 0pt 0ex; }',
143 '.foo { border-radius: 15px .25em 0ex 0pt; }'
144 ),
145 array(
146 '.foo { border-radius: 0px 0px 5px 5px; }',
147 ),
148 // Ensure the rule doesn't break other stuff
149 array(
150 '.foo { x-unknown: a b c d; }'
151 ),
152 array(
153 '.foo barpx 0 2% { opacity: 0; }'
154 ),
155 array(
156 '#settings td p strong'
157 ),
158 array(
159 // Color names
160 '.foo { border-color: red green blue white }',
161 '.foo { border-color: red white blue green }',
162 ),
163 array(
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 }',
167 ),
168 array(
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 }',
172 ),
173 array(
174 // Do not mangle 5 or more values
175 '.foo { -x-unknown: 1 2 3 4 5; }'
176 ),
177 array(
178 '.foo { -x-unknown: 1 2 3 4 5 6; }'
179 ),
180
181 // Shorthand / Three notation
182 array(
183 '.foo { margin: 1em 0 .25em; }'
184 ),
185 array(
186 '.foo { margin:-1.5em 0 -.75em; }'
187 ),
188
189 // Shorthand / Two notation
190 array(
191 '.foo { padding: 1px 2px; }'
192 ),
193
194 // Shorthand / One notation
195 array(
196 '.foo { padding: 1px; }'
197 ),
198
199 // text-shadow and box-shadow
200 array(
201 '.foo { box-shadow: -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
202 '.foo { box-shadow: 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
203 ),
204 array(
205 '.foo { box-shadow: inset -6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
206 '.foo { box-shadow: inset 6px 3px 8px 5px rgba(0, 0, 0, 0.25); }',
207 ),
208 array(
209 '.foo { text-shadow: orange 2px 0; }',
210 '.foo { text-shadow: orange -2px 0; }',
211 ),
212 array(
213 '.foo { text-shadow: 2px 0 orange; }',
214 '.foo { text-shadow: -2px 0 orange; }',
215 ),
216 array(
217 // Don't mangle zeroes
218 '.foo { text-shadow: orange 0 2px; }'
219 ),
220
221 // Direction
222 // Note: This differs from the Python implementation,
223 // see also CSSJanus::fixDirection for more info.
224 array(
225 '.foo { direction: ltr; }',
226 '.foo { direction: rtl; }'
227 ),
228 array(
229 '.foo { direction: rtl; }',
230 '.foo { direction: ltr; }'
231 ),
232 array(
233 'input { direction: ltr; }',
234 'input { direction: rtl; }'
235 ),
236 array(
237 'input { direction: rtl; }',
238 'input { direction: ltr; }'
239 ),
240 array(
241 'body { direction: ltr; }',
242 'body { direction: rtl; }'
243 ),
244 array(
245 '.foo, body, input { direction: ltr; }',
246 '.foo, body, input { direction: rtl; }'
247 ),
248 array(
249 'body { padding: 10px; direction: ltr; }',
250 'body { padding: 10px; direction: rtl; }'
251 ),
252 array(
253 'body { direction: ltr } .myClass { direction: ltr }',
254 'body { direction: rtl } .myClass { direction: rtl }'
255 ),
256
257 // Left/right values
258 array(
259 '.foo { float: left; }',
260 '.foo { float: right; }'
261 ),
262 array(
263 '.foo { text-align: left; }',
264 '.foo { text-align: right; }'
265 ),
266 array(
267 '.foo { -x-unknown: left; }',
268 '.foo { -x-unknown: right; }'
269 ),
270 // Guard against selectors that look flippable
271 array(
272 '.column-left { width: 0; }'
273 ),
274 array(
275 'a.left { width: 0; }'
276 ),
277 array(
278 'a.leftification { width: 0; }'
279 ),
280 array(
281 'a.ltr { width: 0; }'
282 ),
283 array(
284 # <div class="a-ltr png">
285 '.a-ltr.png { width: 0; }'
286 ),
287 array(
288 # <foo-ltr attr="x">
289 'foo-ltr[attr="x"] { width: 0; }'
290 ),
291 array(
292 'div.left > span.right+span.left { width: 0; }'
293 ),
294 array(
295 '.thisclass .left .myclass { width: 0; }'
296 ),
297 array(
298 '.thisclass .left .myclass #myid { width: 0; }'
299 ),
300
301 // Cursor values (east/west)
302 array(
303 '.foo { cursor: e-resize; }',
304 '.foo { cursor: w-resize; }'
305 ),
306 array(
307 '.foo { cursor: se-resize; }',
308 '.foo { cursor: sw-resize; }'
309 ),
310 array(
311 '.foo { cursor: ne-resize; }',
312 '.foo { cursor: nw-resize; }'
313 ),
314
315 // Background
316 array(
317 '.foo { background-position: top left; }',
318 '.foo { background-position: top right; }'
319 ),
320 array(
321 '.foo { background: url(/foo/bar.png) top left; }',
322 '.foo { background: url(/foo/bar.png) top right; }'
323 ),
324 array(
325 '.foo { background: url(/foo/bar.png) top left no-repeat; }',
326 '.foo { background: url(/foo/bar.png) top right no-repeat; }'
327 ),
328 array(
329 '.foo { background: url(/foo/bar.png) no-repeat top left; }',
330 '.foo { background: url(/foo/bar.png) no-repeat top right; }'
331 ),
332 array(
333 '.foo { background: #fff url(/foo/bar.png) no-repeat top left; }',
334 '.foo { background: #fff url(/foo/bar.png) no-repeat top right; }'
335 ),
336 array(
337 '.foo { background-position: 100% 40%; }',
338 '.foo { background-position: 0% 40%; }'
339 ),
340 array(
341 '.foo { background-position: 23% 0; }',
342 '.foo { background-position: 77% 0; }'
343 ),
344 array(
345 '.foo { background-position: 23% auto; }',
346 '.foo { background-position: 77% auto; }'
347 ),
348 array(
349 '.foo { background-position-x: 23%; }',
350 '.foo { background-position-x: 77%; }'
351 ),
352 array(
353 '.foo { background-position-y: 23%; }',
354 '.foo { background-position-y: 23%; }'
355 ),
356 array(
357 '.foo { background:url(../foo.png) no-repeat 75% 50%; }',
358 '.foo { background:url(../foo.png) no-repeat 25% 50%; }'
359 ),
360 array(
361 '.foo { background: 10% 20% } .bar { background: 40% 30% }',
362 '.foo { background: 90% 20% } .bar { background: 60% 30% }'
363 ),
364
365 // Multiple rules
366 array(
367 'body { direction: rtl; float: right; } .foo { direction: ltr; float: right; }',
368 'body { direction: ltr; float: left; } .foo { direction: rtl; float: left; }',
369 ),
370
371 // Duplicate properties
372 array(
373 '.foo { float: left; float: right; float: left; }',
374 '.foo { float: right; float: left; float: right; }',
375 ),
376
377 // Preserve comments
378 array(
379 '/* left /* right */left: 10px',
380 '/* left /* right */right: 10px'
381 ),
382 array(
383 '/*left*//*left*/left: 10px',
384 '/*left*//*left*/right: 10px'
385 ),
386 array(
387 '/* Going right is cool */ .foo { width: 0 }',
388 ),
389 array(
390 "/* padding-right 1 2 3 4 */\n#test { width: 0}\n/*right*/"
391 ),
392 array(
393 "/** Two line comment\n * left\n \*/\n#test {width: 0}"
394 ),
395
396 // @noflip annotation
397 array(
398 // before selector (single)
399 '/* @noflip */ div { float: left; }'
400 ),
401 array(
402 // before selector (multiple)
403 '/* @noflip */ div, .notme { float: left; }'
404 ),
405 array(
406 // inside selector
407 'div, /* @noflip */ .foo { float: left; }'
408 ),
409 array(
410 // after selector
411 'div, .notme /* @noflip */ { float: left; }'
412 ),
413 array(
414 // before multiple rules
415 '/* @noflip */ div { float: left; } .foo { float: left; }',
416 '/* @noflip */ div { float: left; } .foo { float: right; }'
417 ),
418 array(
419 // support parentheses in selector
420 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }',
421 '/* @noflip */ .test:not(:first) { margin-right: -0.25em; margin-left: 0.25em; }'
422 ),
423 array(
424 // after multiple rules
425 '.foo { float: left; } /* @noflip */ div { float: left; }',
426 '.foo { float: right; } /* @noflip */ div { float: left; }'
427 ),
428 array(
429 // before multiple properties
430 'div { /* @noflip */ float: left; text-align: left; }',
431 'div { /* @noflip */ float: left; text-align: right; }'
432 ),
433 array(
434 // after multiple properties
435 'div { float: left; /* @noflip */ text-align: left; }',
436 'div { float: right; /* @noflip */ text-align: left; }'
437 ),
438
439 // Guard against css3 stuff
440 array(
441 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
442 ),
443 array(
444 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
445 ),
446
447 // CSS syntax / white-space variations
448 // spaces, no spaces, tabs, new lines, omitting semi-colons
449 array(
450 ".foo { left: 0; }",
451 ".foo { right: 0; }"
452 ),
453 array(
454 ".foo{ left: 0; }",
455 ".foo{ right: 0; }"
456 ),
457 array(
458 ".foo{ left: 0 }",
459 ".foo{ right: 0 }"
460 ),
461 array(
462 ".foo{left:0 }",
463 ".foo{right:0 }"
464 ),
465 array(
466 ".foo{left:0}",
467 ".foo{right:0}"
468 ),
469 array(
470 ".foo { left : 0 ; }",
471 ".foo { right : 0 ; }"
472 ),
473 array(
474 ".foo\n { left : 0 ; }",
475 ".foo\n { right : 0 ; }"
476 ),
477 array(
478 ".foo\n { \nleft : 0 ; }",
479 ".foo\n { \nright : 0 ; }"
480 ),
481 array(
482 ".foo\n { \n left : 0 ; }",
483 ".foo\n { \n right : 0 ; }"
484 ),
485 array(
486 ".foo\n { \n left\n : 0; }",
487 ".foo\n { \n right\n : 0; }"
488 ),
489 array(
490 ".foo \n { \n left\n : 0; }",
491 ".foo \n { \n right\n : 0; }"
492 ),
493 array(
494 ".foo\n{\nleft\n:\n0;}",
495 ".foo\n{\nright\n:\n0;}"
496 ),
497 array(
498 ".foo\n.bar {\n\tleft: 0;\n}",
499 ".foo\n.bar {\n\tright: 0;\n}"
500 ),
501 array(
502 ".foo\t{\tleft\t:\t0;}",
503 ".foo\t{\tright\t:\t0;}"
504 ),
505
506 // Guard against partial keys
507 array(
508 '.foo { leftxx: 0; }',
509 '.foo { leftxx: 0; }'
510 ),
511 array(
512 '.foo { rightxx: 0; }',
513 '.foo { rightxx: 0; }'
514 ),
515 );
516 }
517
518 /**
519 * These cases are tested in one way only (format: actual, expected, msg).
520 * If both ways can be tested, either put both versions in here or move
521 * it to provideTransformCases().
522 */
523 public static function provideTransformAdvancedCases() {
524 $bgPairs = array(
525 # [ - _ . ] <-> [ left right ltr rtl ]
526 'foo.jpg' => 'foo.jpg',
527 'left.jpg' => 'right.jpg',
528 'ltr.jpg' => 'rtl.jpg',
529
530 'foo-left.png' => 'foo-right.png',
531 'foo_left.png' => 'foo_right.png',
532 'foo.left.png' => 'foo.right.png',
533
534 'foo-ltr.png' => 'foo-rtl.png',
535 'foo_ltr.png' => 'foo_rtl.png',
536 'foo.ltr.png' => 'foo.rtl.png',
537
538 'left-foo.png' => 'right-foo.png',
539 'left_foo.png' => 'right_foo.png',
540 'left.foo.png' => 'right.foo.png',
541
542 'ltr-foo.png' => 'rtl-foo.png',
543 'ltr_foo.png' => 'rtl_foo.png',
544 'ltr.foo.png' => 'rtl.foo.png',
545
546 'foo-ltr-left.gif' => 'foo-rtl-right.gif',
547 'foo_ltr_left.gif' => 'foo_rtl_right.gif',
548 'foo.ltr.left.gif' => 'foo.rtl.right.gif',
549 'foo-ltr_left.gif' => 'foo-rtl_right.gif',
550 'foo_ltr.left.gif' => 'foo_rtl.right.gif',
551 );
552 $provider = array();
553 foreach ( $bgPairs as $left => $right ) {
554 # By default '-rtl' and '-left' etc. are not touched,
555 # Only when the appropiate parameter is set.
556 $provider[] = array(
557 ".foo { background: url(images/$left); }",
558 ".foo { background: url(images/$left); }"
559 );
560 $provider[] = array(
561 ".foo { background: url(images/$right); }",
562 ".foo { background: url(images/$right); }"
563 );
564 $provider[] = array(
565 ".foo { background: url(images/$left); }",
566 ".foo { background: url(images/$right); }",
567 array(
568 'swapLtrRtlInURL' => true,
569 'swapLeftRightInURL' => true,
570 )
571 );
572 $provider[] = array(
573 ".foo { background: url(images/$right); }",
574 ".foo { background: url(images/$left); }",
575 array(
576 'swapLtrRtlInURL' => true,
577 'swapLeftRightInURL' => true,
578 )
579 );
580 }
581
582 return $provider;
583 }
584
585 /**
586 * Cases that are currently failing, but
587 * should be looked at in the future as enhancements and/or bug fix
588 */
589 public static function provideTransformBrokenCases() {
590 return array(
591 // Guard against selectors that look flippable
592 array(
593 # <foo-left-x attr="x">
594 'foo-left-x[attr="x"] { width: 0; }',
595 'foo-left-x[attr="x"] { width: 0; }'
596 ),
597 array(
598 # <div class="foo" data-left="x">
599 '.foo[data-left="x"] { width: 0; }',
600 '.foo[data-left="x"] { width: 0; }'
601 ),
602 );
603 }
604 }