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