Merge "Fix FOUC on preferences."
[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 array(
141 '.foo { border-radius: .25em 15px 0pt 0ex; }',
142 '.foo { border-radius: .25em 0ex 0pt 15px; }'
143 ),
144 array(
145 '.foo { x-unknown: a b c d; }'
146 ),
147 array(
148 '.foo barpx 0 2% { opacity: 0; }'
149 ),
150 array(
151 '#settings td p strong'
152 ),
153 array(
154 // Do not mangle 5 or more values
155 '.foo { -x-unknown: 1 2 3 4 5; }'
156 ),
157 array(
158 '.foo { -x-unknown: 1 2 3 4 5 6; }'
159 ),
160
161 // Shorthand / Three notation
162 array(
163 '.foo { margin: 1em 0 .25em; }'
164 ),
165 array(
166 '.foo { margin:-1.5em 0 -.75em; }'
167 ),
168
169 // Shorthand / Two notation
170 array(
171 '.foo { padding: 1px 2px; }'
172 ),
173
174 // Shorthand / One notation
175 array(
176 '.foo { padding: 1px; }'
177 ),
178
179 // Direction
180 // Note: This differs from the Python implementation,
181 // see also CSSJanus::fixDirection for more info.
182 array(
183 '.foo { direction: ltr; }',
184 '.foo { direction: rtl; }'
185 ),
186 array(
187 '.foo { direction: rtl; }',
188 '.foo { direction: ltr; }'
189 ),
190 array(
191 'input { direction: ltr; }',
192 'input { direction: rtl; }'
193 ),
194 array(
195 'input { direction: rtl; }',
196 'input { direction: ltr; }'
197 ),
198 array(
199 'body { direction: ltr; }',
200 'body { direction: rtl; }'
201 ),
202 array(
203 '.foo, body, input { direction: ltr; }',
204 '.foo, body, input { direction: rtl; }'
205 ),
206 array(
207 'body { padding: 10px; direction: ltr; }',
208 'body { padding: 10px; direction: rtl; }'
209 ),
210 array(
211 'body { direction: ltr } .myClass { direction: ltr }',
212 'body { direction: rtl } .myClass { direction: rtl }'
213 ),
214
215 // Left/right values
216 array(
217 '.foo { float: left; }',
218 '.foo { float: right; }'
219 ),
220 array(
221 '.foo { text-align: left; }',
222 '.foo { text-align: right; }'
223 ),
224 array(
225 '.foo { -x-unknown: left; }',
226 '.foo { -x-unknown: right; }'
227 ),
228 // Guard against selectors that look flippable
229 array(
230 '.column-left { width: 0; }'
231 ),
232 array(
233 'a.left { width: 0; }'
234 ),
235 array(
236 'a.leftification { width: 0; }'
237 ),
238 array(
239 'a.ltr { width: 0; }'
240 ),
241 array(
242 # <div class="a-ltr png">
243 '.a-ltr.png { width: 0; }'
244 ),
245 array(
246 # <foo-ltr attr="x">
247 'foo-ltr[attr="x"] { width: 0; }'
248 ),
249 array(
250 'div.left > span.right+span.left { width: 0; }'
251 ),
252 array(
253 '.thisclass .left .myclass { width: 0; }'
254 ),
255 array(
256 '.thisclass .left .myclass #myid { width: 0; }'
257 ),
258
259 // Cursor values (east/west)
260 array(
261 '.foo { cursor: e-resize; }',
262 '.foo { cursor: w-resize; }'
263 ),
264 array(
265 '.foo { cursor: se-resize; }',
266 '.foo { cursor: sw-resize; }'
267 ),
268 array(
269 '.foo { cursor: ne-resize; }',
270 '.foo { cursor: nw-resize; }'
271 ),
272
273 // Background
274 array(
275 '.foo { background-position: top left; }',
276 '.foo { background-position: top right; }'
277 ),
278 array(
279 '.foo { background: url(/foo/bar.png) top left; }',
280 '.foo { background: url(/foo/bar.png) top right; }'
281 ),
282 array(
283 '.foo { background: url(/foo/bar.png) top left no-repeat; }',
284 '.foo { background: url(/foo/bar.png) top right no-repeat; }'
285 ),
286 array(
287 '.foo { background: url(/foo/bar.png) no-repeat top left; }',
288 '.foo { background: url(/foo/bar.png) no-repeat top right; }'
289 ),
290 array(
291 '.foo { background: #fff url(/foo/bar.png) no-repeat top left; }',
292 '.foo { background: #fff url(/foo/bar.png) no-repeat top right; }'
293 ),
294 array(
295 '.foo { background-position: 100% 40%; }',
296 '.foo { background-position: 0% 40%; }'
297 ),
298 array(
299 '.foo { background-position: 23% 0; }',
300 '.foo { background-position: 77% 0; }'
301 ),
302 array(
303 '.foo { background-position: 23% auto; }',
304 '.foo { background-position: 77% auto; }'
305 ),
306 array(
307 '.foo { background-position-x: 23%; }',
308 '.foo { background-position-x: 77%; }'
309 ),
310 array(
311 '.foo { background-position-y: 23%; }',
312 '.foo { background-position-y: 23%; }'
313 ),
314 array(
315 '.foo { background:url(../foo.png) no-repeat 75% 50%; }',
316 '.foo { background:url(../foo.png) no-repeat 25% 50%; }'
317 ),
318 array(
319 '.foo { background: 10% 20% } .bar { background: 40% 30% }',
320 '.foo { background: 90% 20% } .bar { background: 60% 30% }'
321 ),
322
323 // Multiple rules
324 array(
325 'body { direction: rtl; float: right; } .foo { direction: ltr; float: right; }',
326 'body { direction: ltr; float: left; } .foo { direction: rtl; float: left; }',
327 ),
328
329 // Duplicate properties
330 array(
331 '.foo { float: left; float: right; float: left; }',
332 '.foo { float: right; float: left; float: right; }',
333 ),
334
335 // Preserve comments
336 array(
337 '/* left /* right */left: 10px',
338 '/* left /* right */right: 10px'
339 ),
340 array(
341 '/*left*//*left*/left: 10px',
342 '/*left*//*left*/right: 10px'
343 ),
344 array(
345 '/* Going right is cool */ .foo { width: 0 }',
346 ),
347 array(
348 "/* padding-right 1 2 3 4 */\n#test { width: 0}\n/*right*/"
349 ),
350 array(
351 "/** Two line comment\n * left\n \*/\n#test {width: 0}"
352 ),
353
354 // @noflip annotation
355 array(
356 // before selector (single)
357 '/* @noflip */ div { float: left; }'
358 ),
359 array(
360 // before selector (multiple)
361 '/* @noflip */ div, .notme { float: left; }'
362 ),
363 array(
364 // inside selector
365 'div, /* @noflip */ .foo { float: left; }'
366 ),
367 array(
368 // after selector
369 'div, .notme /* @noflip */ { float: left; }'
370 ),
371 array(
372 // before multiple rules
373 '/* @noflip */ div { float: left; } .foo { float: left; }',
374 '/* @noflip */ div { float: left; } .foo { float: right; }'
375 ),
376 array(
377 // after multiple rules
378 '.foo { float: left; } /* @noflip */ div { float: left; }',
379 '.foo { float: right; } /* @noflip */ div { float: left; }'
380 ),
381 array(
382 // before multiple properties
383 'div { /* @noflip */ float: left; text-align: left; }',
384 'div { /* @noflip */ float: left; text-align: right; }'
385 ),
386 array(
387 // after multiple properties
388 'div { float: left; /* @noflip */ text-align: left; }',
389 'div { float: right; /* @noflip */ text-align: left; }'
390 ),
391
392 // Guard against css3 stuff
393 array(
394 'background-image: -moz-linear-gradient(#326cc1, #234e8c);'
395 ),
396 array(
397 'background-image: -webkit-gradient(linear, 100% 0%, 0% 0%, from(#666666), to(#ffffff));'
398 ),
399
400 // CSS syntax / white-space variations
401 // spaces, no spaces, tabs, new lines, omitting semi-colons
402 array(
403 ".foo { left: 0; }",
404 ".foo { right: 0; }"
405 ),
406 array(
407 ".foo{ left: 0; }",
408 ".foo{ right: 0; }"
409 ),
410 array(
411 ".foo{ left: 0 }",
412 ".foo{ right: 0 }"
413 ),
414 array(
415 ".foo{left:0 }",
416 ".foo{right:0 }"
417 ),
418 array(
419 ".foo{left:0}",
420 ".foo{right:0}"
421 ),
422 array(
423 ".foo { left : 0 ; }",
424 ".foo { right : 0 ; }"
425 ),
426 array(
427 ".foo\n { left : 0 ; }",
428 ".foo\n { right : 0 ; }"
429 ),
430 array(
431 ".foo\n { \nleft : 0 ; }",
432 ".foo\n { \nright : 0 ; }"
433 ),
434 array(
435 ".foo\n { \n left : 0 ; }",
436 ".foo\n { \n right : 0 ; }"
437 ),
438 array(
439 ".foo\n { \n left\n : 0; }",
440 ".foo\n { \n right\n : 0; }"
441 ),
442 array(
443 ".foo \n { \n left\n : 0; }",
444 ".foo \n { \n right\n : 0; }"
445 ),
446 array(
447 ".foo\n{\nleft\n:\n0;}",
448 ".foo\n{\nright\n:\n0;}"
449 ),
450 array(
451 ".foo\n.bar {\n\tleft: 0;\n}",
452 ".foo\n.bar {\n\tright: 0;\n}"
453 ),
454 array(
455 ".foo\t{\tleft\t:\t0;}",
456 ".foo\t{\tright\t:\t0;}"
457 ),
458
459 // Guard against partial keys
460 array(
461 '.foo { leftxx: 0; }',
462 '.foo { leftxx: 0; }'
463 ),
464 array(
465 '.foo { rightxx: 0; }',
466 '.foo { rightxx: 0; }'
467 ),
468 );
469 }
470
471 /**
472 * These cases are tested in one way only (format: actual, expected, msg).
473 * If both ways can be tested, either put both versions in here or move
474 * it to provideTransformCases().
475 */
476 public static function provideTransformAdvancedCases() {
477 $bgPairs = array(
478 # [ - _ . ] <-> [ left right ltr rtl ]
479 'foo.jpg' => 'foo.jpg',
480 'left.jpg' => 'right.jpg',
481 'ltr.jpg' => 'rtl.jpg',
482
483 'foo-left.png' => 'foo-right.png',
484 'foo_left.png' => 'foo_right.png',
485 'foo.left.png' => 'foo.right.png',
486
487 'foo-ltr.png' => 'foo-rtl.png',
488 'foo_ltr.png' => 'foo_rtl.png',
489 'foo.ltr.png' => 'foo.rtl.png',
490
491 'left-foo.png' => 'right-foo.png',
492 'left_foo.png' => 'right_foo.png',
493 'left.foo.png' => 'right.foo.png',
494
495 'ltr-foo.png' => 'rtl-foo.png',
496 'ltr_foo.png' => 'rtl_foo.png',
497 'ltr.foo.png' => 'rtl.foo.png',
498
499 'foo-ltr-left.gif' => 'foo-rtl-right.gif',
500 'foo_ltr_left.gif' => 'foo_rtl_right.gif',
501 'foo.ltr.left.gif' => 'foo.rtl.right.gif',
502 'foo-ltr_left.gif' => 'foo-rtl_right.gif',
503 'foo_ltr.left.gif' => 'foo_rtl.right.gif',
504 );
505 $provider = array();
506 foreach ( $bgPairs as $left => $right ) {
507 # By default '-rtl' and '-left' etc. are not touched,
508 # Only when the appropiate parameter is set.
509 $provider[] = array(
510 ".foo { background: url(images/$left); }",
511 ".foo { background: url(images/$left); }"
512 );
513 $provider[] = array(
514 ".foo { background: url(images/$right); }",
515 ".foo { background: url(images/$right); }"
516 );
517 $provider[] = array(
518 ".foo { background: url(images/$left); }",
519 ".foo { background: url(images/$right); }",
520 array(
521 'swapLtrRtlInURL' => true,
522 'swapLeftRightInURL' => true,
523 )
524 );
525 $provider[] = array(
526 ".foo { background: url(images/$right); }",
527 ".foo { background: url(images/$left); }",
528 array(
529 'swapLtrRtlInURL' => true,
530 'swapLeftRightInURL' => true,
531 )
532 );
533 }
534
535 return $provider;
536 }
537
538 /**
539 * Cases that are currently failing, but
540 * should be looked at in the future as enhancements and/or bug fix
541 */
542 public static function provideTransformBrokenCases() {
543 return array(
544 // Guard against selectors that look flippable
545 array(
546 # <foo-left-x attr="x">
547 'foo-left-x[attr="x"] { width: 0; }',
548 'foo-left-x[attr="x"] { width: 0; }'
549 ),
550 array(
551 # <div class="foo" data-left="x">
552 '.foo[data-left="x"] { width: 0; }',
553 '.foo[data-left="x"] { width: 0; }'
554 ),
555 );
556 }
557 }