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