SECURITY: blacklist CSS var()
[lhc/web/wiklou.git] / tests / phpunit / includes / HtmlTest.php
1 <?php
2
3 class HtmlTest extends MediaWikiTestCase {
4 private $restoreWarnings;
5
6 protected function setUp() {
7 parent::setUp();
8
9 $this->setMwGlobals( [
10 'wgUseMediaWikiUIEverywhere' => false,
11 ] );
12
13 $contLangObj = Language::factory( 'en' );
14
15 // Hardcode namespaces during test runs,
16 // so that html output based on existing namespaces
17 // can be properly evaluated.
18 $contLangObj->setNamespaces( [
19 -2 => 'Media',
20 -1 => 'Special',
21 0 => '',
22 1 => 'Talk',
23 2 => 'User',
24 3 => 'User_talk',
25 4 => 'MyWiki',
26 5 => 'MyWiki_Talk',
27 6 => 'File',
28 7 => 'File_talk',
29 8 => 'MediaWiki',
30 9 => 'MediaWiki_talk',
31 10 => 'Template',
32 11 => 'Template_talk',
33 14 => 'Category',
34 15 => 'Category_talk',
35 100 => 'Custom',
36 101 => 'Custom_talk',
37 ] );
38 $this->setContentLang( $contLangObj );
39
40 $userLangObj = Language::factory( 'es' );
41 $userLangObj->setNamespaces( [
42 -2 => "Medio",
43 -1 => "Especial",
44 0 => "",
45 1 => "Discusión",
46 2 => "Usuario",
47 3 => "Usuario discusión",
48 4 => "Wiki",
49 5 => "Wiki discusión",
50 6 => "Archivo",
51 7 => "Archivo discusión",
52 8 => "MediaWiki",
53 9 => "MediaWiki discusión",
54 10 => "Plantilla",
55 11 => "Plantilla discusión",
56 12 => "Ayuda",
57 13 => "Ayuda discusión",
58 14 => "Categoría",
59 15 => "Categoría discusión",
60 100 => "Personalizado",
61 101 => "Personalizado discusión",
62 ] );
63 $this->setUserLang( $userLangObj );
64
65 $this->restoreWarnings = false;
66 }
67
68 protected function tearDown() {
69 Language::factory( 'en' )->resetNamespaces();
70
71 if ( $this->restoreWarnings ) {
72 $this->restoreWarnings = false;
73 Wikimedia\restoreWarnings();
74 }
75
76 parent::tearDown();
77 }
78
79 /**
80 * @expectedException PHPUnit_Framework_Error_Notice
81 * @expectedExceptionMessage given element name with space
82 * @covers Html::openElement
83 */
84 public function testOpenElement() {
85 Html::openElement( 'span id="x"' );
86 }
87
88 /**
89 * @covers Html::element
90 * @covers Html::rawElement
91 * @covers Html::openElement
92 * @covers Html::closeElement
93 */
94 public function testElementBasics() {
95 $this->assertEquals(
96 '<img/>',
97 Html::element( 'img', null, '' ),
98 'Self-closing tag for short-tag elements'
99 );
100
101 $this->assertEquals(
102 '<element></element>',
103 Html::element( 'element', null, null ),
104 'Close tag for empty element (null, null)'
105 );
106
107 $this->assertEquals(
108 '<element></element>',
109 Html::element( 'element', [], '' ),
110 'Close tag for empty element (array, string)'
111 );
112 }
113
114 public function dataXmlMimeType() {
115 return [
116 // ( $mimetype, $isXmlMimeType )
117 # HTML is not an XML MimeType
118 [ 'text/html', false ],
119 # XML is an XML MimeType
120 [ 'text/xml', true ],
121 [ 'application/xml', true ],
122 # XHTML is an XML MimeType
123 [ 'application/xhtml+xml', true ],
124 # Make sure other +xml MimeTypes are supported
125 # SVG is another random MimeType even though we don't use it
126 [ 'image/svg+xml', true ],
127 # Complete random other MimeTypes are not XML
128 [ 'text/plain', false ],
129 ];
130 }
131
132 /**
133 * @dataProvider dataXmlMimeType
134 * @covers Html::isXmlMimeType
135 */
136 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
137 $this->assertEquals( $isXmlMimeType, Html::isXmlMimeType( $mimetype ) );
138 }
139
140 /**
141 * @covers Html::expandAttributes
142 */
143 public function testExpandAttributesSkipsNullAndFalse() {
144 # ## EMPTY ########
145 $this->assertEmpty(
146 Html::expandAttributes( [ 'foo' => null ] ),
147 'skip keys with null value'
148 );
149 $this->assertEmpty(
150 Html::expandAttributes( [ 'foo' => false ] ),
151 'skip keys with false value'
152 );
153 $this->assertEquals(
154 ' foo=""',
155 Html::expandAttributes( [ 'foo' => '' ] ),
156 'keep keys with an empty string'
157 );
158 }
159
160 /**
161 * @covers Html::expandAttributes
162 */
163 public function testExpandAttributesForBooleans() {
164 $this->assertEquals(
165 '',
166 Html::expandAttributes( [ 'selected' => false ] ),
167 'Boolean attributes do not generates output when value is false'
168 );
169 $this->assertEquals(
170 '',
171 Html::expandAttributes( [ 'selected' => null ] ),
172 'Boolean attributes do not generates output when value is null'
173 );
174
175 $this->assertEquals(
176 ' selected=""',
177 Html::expandAttributes( [ 'selected' => true ] ),
178 'Boolean attributes have no value when value is true'
179 );
180 $this->assertEquals(
181 ' selected=""',
182 Html::expandAttributes( [ 'selected' ] ),
183 'Boolean attributes have no value when value is true (passed as numerical array)'
184 );
185 }
186
187 /**
188 * @covers Html::expandAttributes
189 */
190 public function testExpandAttributesForNumbers() {
191 $this->assertEquals(
192 ' value="1"',
193 Html::expandAttributes( [ 'value' => 1 ] ),
194 'Integer value is cast to a string'
195 );
196 $this->assertEquals(
197 ' value="1.1"',
198 Html::expandAttributes( [ 'value' => 1.1 ] ),
199 'Float value is cast to a string'
200 );
201 }
202
203 /**
204 * @covers Html::expandAttributes
205 */
206 public function testExpandAttributesForObjects() {
207 $this->assertEquals(
208 ' value="stringValue"',
209 Html::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
210 'Object value is converted to a string'
211 );
212 }
213
214 /**
215 * Please note it output a string prefixed with a space!
216 * @covers Html::expandAttributes
217 */
218 public function testExpandAttributesVariousExpansions() {
219 # ## NOT EMPTY ####
220 $this->assertEquals(
221 ' empty_string=""',
222 Html::expandAttributes( [ 'empty_string' => '' ] ),
223 'Empty string is always quoted'
224 );
225 $this->assertEquals(
226 ' key="value"',
227 Html::expandAttributes( [ 'key' => 'value' ] ),
228 'Simple string value needs no quotes'
229 );
230 $this->assertEquals(
231 ' one="1"',
232 Html::expandAttributes( [ 'one' => 1 ] ),
233 'Number 1 value needs no quotes'
234 );
235 $this->assertEquals(
236 ' zero="0"',
237 Html::expandAttributes( [ 'zero' => 0 ] ),
238 'Number 0 value needs no quotes'
239 );
240 }
241
242 /**
243 * Html::expandAttributes has special features for HTML
244 * attributes that use space separated lists and also
245 * allows arrays to be used as values.
246 * @covers Html::expandAttributes
247 */
248 public function testExpandAttributesListValueAttributes() {
249 # ## STRING VALUES
250 $this->assertEquals(
251 ' class="redundant spaces here"',
252 Html::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
253 'Normalization should strip redundant spaces'
254 );
255 $this->assertEquals(
256 ' class="foo bar"',
257 Html::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
258 'Normalization should remove duplicates in string-lists'
259 );
260 # ## "EMPTY" ARRAY VALUES
261 $this->assertEquals(
262 ' class=""',
263 Html::expandAttributes( [ 'class' => [] ] ),
264 'Value with an empty array'
265 );
266 $this->assertEquals(
267 ' class=""',
268 Html::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
269 'Array with null, empty string and spaces'
270 );
271 # ## NON-EMPTY ARRAY VALUES
272 $this->assertEquals(
273 ' class="foo bar"',
274 Html::expandAttributes( [ 'class' => [
275 'foo',
276 'bar',
277 'foo',
278 'bar',
279 'bar',
280 ] ] ),
281 'Normalization should remove duplicates in the array'
282 );
283 $this->assertEquals(
284 ' class="foo bar"',
285 Html::expandAttributes( [ 'class' => [
286 'foo bar',
287 'bar foo',
288 'foo',
289 'bar bar',
290 ] ] ),
291 'Normalization should remove duplicates in string-lists in the array'
292 );
293 }
294
295 /**
296 * Test feature added by r96188, let pass attributes values as
297 * a PHP array. Restricted to class,rel, accesskey.
298 * @covers Html::expandAttributes
299 */
300 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
301 $this->assertEquals(
302 ' class="booltrue one"',
303 Html::expandAttributes( [ 'class' => [
304 'booltrue' => true,
305 'one' => 1,
306
307 # Method use isset() internally, make sure we do discard
308 # attributes values which have been assigned well known values
309 'emptystring' => '',
310 'boolfalse' => false,
311 'zero' => 0,
312 'null' => null,
313 ] ] )
314 );
315 }
316
317 /**
318 * How do we handle duplicate keys in HTML attributes expansion?
319 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
320 * The latter will take precedence.
321 *
322 * Feature added by r96188
323 * @covers Html::expandAttributes
324 */
325 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
326 $this->assertEquals(
327 ' class=""',
328 Html::expandAttributes( [ 'class' => [
329 'GREEN',
330 'GREEN' => false,
331 'GREEN',
332 ] ] )
333 );
334 }
335
336 /**
337 * @covers Html::expandAttributes
338 * @expectedException MWException
339 */
340 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
341 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
342 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
343 Html::expandAttributes( [
344 'src' => [
345 'ltr' => 'ltr.svg',
346 'rtl' => 'rtl.svg'
347 ]
348 ] );
349 }
350
351 /**
352 * @covers Html::namespaceSelector
353 * @covers Html::namespaceSelectorOptions
354 */
355 public function testNamespaceSelector() {
356 $this->assertEquals(
357 '<select id="namespace" name="namespace">' . "\n" .
358 '<option value="0">(Principal)</option>' . "\n" .
359 '<option value="1">Talk</option>' . "\n" .
360 '<option value="2">User</option>' . "\n" .
361 '<option value="3">User talk</option>' . "\n" .
362 '<option value="4">MyWiki</option>' . "\n" .
363 '<option value="5">MyWiki Talk</option>' . "\n" .
364 '<option value="6">File</option>' . "\n" .
365 '<option value="7">File talk</option>' . "\n" .
366 '<option value="8">MediaWiki</option>' . "\n" .
367 '<option value="9">MediaWiki talk</option>' . "\n" .
368 '<option value="10">Template</option>' . "\n" .
369 '<option value="11">Template talk</option>' . "\n" .
370 '<option value="14">Category</option>' . "\n" .
371 '<option value="15">Category talk</option>' . "\n" .
372 '<option value="100">Custom</option>' . "\n" .
373 '<option value="101">Custom talk</option>' . "\n" .
374 '</select>',
375 Html::namespaceSelector(),
376 'Basic namespace selector without custom options'
377 );
378
379 $this->assertEquals(
380 '<label for="mw-test-namespace">Select a namespace:</label>' . "\u{00A0}" .
381 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
382 '<option value="all">todos</option>' . "\n" .
383 '<option value="0">(Principal)</option>' . "\n" .
384 '<option value="1">Talk</option>' . "\n" .
385 '<option value="2" selected="">User</option>' . "\n" .
386 '<option value="3">User talk</option>' . "\n" .
387 '<option value="4">MyWiki</option>' . "\n" .
388 '<option value="5">MyWiki Talk</option>' . "\n" .
389 '<option value="6">File</option>' . "\n" .
390 '<option value="7">File talk</option>' . "\n" .
391 '<option value="8">MediaWiki</option>' . "\n" .
392 '<option value="9">MediaWiki talk</option>' . "\n" .
393 '<option value="10">Template</option>' . "\n" .
394 '<option value="11">Template talk</option>' . "\n" .
395 '<option value="14">Category</option>' . "\n" .
396 '<option value="15">Category talk</option>' . "\n" .
397 '<option value="100">Custom</option>' . "\n" .
398 '<option value="101">Custom talk</option>' . "\n" .
399 '</select>',
400 Html::namespaceSelector(
401 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
402 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
403 ),
404 'Basic namespace selector with custom values'
405 );
406
407 $this->assertEquals(
408 '<label for="namespace">Select a namespace:</label>' . "\u{00A0}" .
409 '<select id="namespace" name="namespace">' . "\n" .
410 '<option value="0">(Principal)</option>' . "\n" .
411 '<option value="1">Talk</option>' . "\n" .
412 '<option value="2">User</option>' . "\n" .
413 '<option value="3">User talk</option>' . "\n" .
414 '<option value="4">MyWiki</option>' . "\n" .
415 '<option value="5">MyWiki Talk</option>' . "\n" .
416 '<option value="6">File</option>' . "\n" .
417 '<option value="7">File talk</option>' . "\n" .
418 '<option value="8">MediaWiki</option>' . "\n" .
419 '<option value="9">MediaWiki talk</option>' . "\n" .
420 '<option value="10">Template</option>' . "\n" .
421 '<option value="11">Template talk</option>' . "\n" .
422 '<option value="14">Category</option>' . "\n" .
423 '<option value="15">Category talk</option>' . "\n" .
424 '<option value="100">Custom</option>' . "\n" .
425 '<option value="101">Custom talk</option>' . "\n" .
426 '</select>',
427 Html::namespaceSelector(
428 [ 'label' => 'Select a namespace:' ]
429 ),
430 'Basic namespace selector with a custom label but no id attribtue for the <select>'
431 );
432
433 $this->assertEquals(
434 '<select id="namespace" name="namespace">' . "\n" .
435 '<option value="0">(Principal)</option>' . "\n" .
436 '<option value="1">Discusión</option>' . "\n" .
437 '<option value="2">Usuario</option>' . "\n" .
438 '<option value="3">Usuario discusión</option>' . "\n" .
439 '<option value="4">Wiki</option>' . "\n" .
440 '<option value="5">Wiki discusión</option>' . "\n" .
441 '<option value="6">Archivo</option>' . "\n" .
442 '<option value="7">Archivo discusión</option>' . "\n" .
443 '<option value="8">MediaWiki</option>' . "\n" .
444 '<option value="9">MediaWiki discusión</option>' . "\n" .
445 '<option value="10">Plantilla</option>' . "\n" .
446 '<option value="11">Plantilla discusión</option>' . "\n" .
447 '<option value="12">Ayuda</option>' . "\n" .
448 '<option value="13">Ayuda discusión</option>' . "\n" .
449 '<option value="14">Categoría</option>' . "\n" .
450 '<option value="15">Categoría discusión</option>' . "\n" .
451 '<option value="100">Personalizado</option>' . "\n" .
452 '<option value="101">Personalizado discusión</option>' . "\n" .
453 '</select>',
454 Html::namespaceSelector(
455 [ 'in-user-lang' => true ]
456 ),
457 'Basic namespace selector in user language'
458 );
459 }
460
461 /**
462 * @covers Html::namespaceSelector
463 */
464 public function testCanFilterOutNamespaces() {
465 $this->assertEquals(
466 '<select id="namespace" name="namespace">' . "\n" .
467 '<option value="2">User</option>' . "\n" .
468 '<option value="4">MyWiki</option>' . "\n" .
469 '<option value="5">MyWiki Talk</option>' . "\n" .
470 '<option value="6">File</option>' . "\n" .
471 '<option value="7">File talk</option>' . "\n" .
472 '<option value="8">MediaWiki</option>' . "\n" .
473 '<option value="9">MediaWiki talk</option>' . "\n" .
474 '<option value="10">Template</option>' . "\n" .
475 '<option value="11">Template talk</option>' . "\n" .
476 '<option value="14">Category</option>' . "\n" .
477 '<option value="15">Category talk</option>' . "\n" .
478 '</select>',
479 Html::namespaceSelector(
480 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
481 ),
482 'Namespace selector namespace filtering.'
483 );
484 }
485
486 /**
487 * @covers Html::namespaceSelector
488 */
489 public function testCanDisableANamespaces() {
490 $this->assertEquals(
491 '<select id="namespace" name="namespace">' . "\n" .
492 '<option disabled="" value="0">(Principal)</option>' . "\n" .
493 '<option disabled="" value="1">Talk</option>' . "\n" .
494 '<option disabled="" value="2">User</option>' . "\n" .
495 '<option disabled="" value="3">User talk</option>' . "\n" .
496 '<option disabled="" value="4">MyWiki</option>' . "\n" .
497 '<option value="5">MyWiki Talk</option>' . "\n" .
498 '<option value="6">File</option>' . "\n" .
499 '<option value="7">File talk</option>' . "\n" .
500 '<option value="8">MediaWiki</option>' . "\n" .
501 '<option value="9">MediaWiki talk</option>' . "\n" .
502 '<option value="10">Template</option>' . "\n" .
503 '<option value="11">Template talk</option>' . "\n" .
504 '<option value="14">Category</option>' . "\n" .
505 '<option value="15">Category talk</option>' . "\n" .
506 '<option value="100">Custom</option>' . "\n" .
507 '<option value="101">Custom talk</option>' . "\n" .
508 '</select>',
509 Html::namespaceSelector( [
510 'disable' => [ 0, 1, 2, 3, 4 ]
511 ] ),
512 'Namespace selector namespace disabling'
513 );
514 }
515
516 /**
517 * @dataProvider provideHtml5InputTypes
518 * @covers Html::element
519 */
520 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
521 $this->assertEquals(
522 '<input type="' . $HTML5InputType . '"/>',
523 Html::element( 'input', [ 'type' => $HTML5InputType ] ),
524 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
525 );
526 }
527
528 /**
529 * @covers Html::warningBox
530 * @covers Html::messageBox
531 */
532 public function testWarningBox() {
533 $this->assertEquals(
534 Html::warningBox( 'warn' ),
535 '<div class="warningbox">warn</div>'
536 );
537 }
538
539 /**
540 * @covers Html::errorBox
541 * @covers Html::messageBox
542 */
543 public function testErrorBox() {
544 $this->assertEquals(
545 Html::errorBox( 'err' ),
546 '<div class="errorbox">err</div>'
547 );
548 $this->assertEquals(
549 Html::errorBox( 'err', 'heading' ),
550 '<div class="errorbox"><h2>heading</h2>err</div>'
551 );
552 $this->assertEquals(
553 Html::errorBox( 'err', '0' ),
554 '<div class="errorbox"><h2>0</h2>err</div>'
555 );
556 }
557
558 /**
559 * @covers Html::successBox
560 * @covers Html::messageBox
561 */
562 public function testSuccessBox() {
563 $this->assertEquals(
564 Html::successBox( 'great' ),
565 '<div class="successbox">great</div>'
566 );
567 $this->assertEquals(
568 Html::successBox( '<script>beware no escaping!</script>' ),
569 '<div class="successbox"><script>beware no escaping!</script></div>'
570 );
571 }
572
573 /**
574 * List of input element types values introduced by HTML5
575 * Full list at https://www.w3.org/TR/html-markup/input.html
576 */
577 public static function provideHtml5InputTypes() {
578 $types = [
579 'datetime',
580 'datetime-local',
581 'date',
582 'month',
583 'time',
584 'week',
585 'number',
586 'range',
587 'email',
588 'url',
589 'search',
590 'tel',
591 'color',
592 ];
593 $cases = [];
594 foreach ( $types as $type ) {
595 $cases[] = [ $type ];
596 }
597
598 return $cases;
599 }
600
601 /**
602 * Test out Html::element drops or enforces default value
603 * @covers Html::dropDefaults
604 * @dataProvider provideElementsWithAttributesHavingDefaultValues
605 */
606 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
607 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
608 }
609
610 public static function provideElementsWithAttributesHavingDefaultValues() {
611 # Use cases in a concise format:
612 # <expected>, <element name>, <array of attributes> [, <message>]
613 # Will be mapped to Html::element()
614 $cases = [];
615
616 # ## Generic cases, match $attribDefault static array
617 $cases[] = [ '<area/>',
618 'area', [ 'shape' => 'rect' ]
619 ];
620
621 $cases[] = [ '<button type="submit"></button>',
622 'button', [ 'formaction' => 'GET' ]
623 ];
624 $cases[] = [ '<button type="submit"></button>',
625 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
626 ];
627
628 $cases[] = [ '<canvas></canvas>',
629 'canvas', [ 'height' => '150' ]
630 ];
631 $cases[] = [ '<canvas></canvas>',
632 'canvas', [ 'width' => '300' ]
633 ];
634 # Also check with numeric values
635 $cases[] = [ '<canvas></canvas>',
636 'canvas', [ 'height' => 150 ]
637 ];
638 $cases[] = [ '<canvas></canvas>',
639 'canvas', [ 'width' => 300 ]
640 ];
641
642 $cases[] = [ '<form></form>',
643 'form', [ 'action' => 'GET' ]
644 ];
645 $cases[] = [ '<form></form>',
646 'form', [ 'autocomplete' => 'on' ]
647 ];
648 $cases[] = [ '<form></form>',
649 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
650 ];
651
652 $cases[] = [ '<input/>',
653 'input', [ 'formaction' => 'GET' ]
654 ];
655 $cases[] = [ '<input/>',
656 'input', [ 'type' => 'text' ]
657 ];
658
659 $cases[] = [ '<keygen/>',
660 'keygen', [ 'keytype' => 'rsa' ]
661 ];
662
663 $cases[] = [ '<link/>',
664 'link', [ 'media' => 'all' ]
665 ];
666
667 $cases[] = [ '<menu></menu>',
668 'menu', [ 'type' => 'list' ]
669 ];
670
671 $cases[] = [ '<script></script>',
672 'script', [ 'type' => 'text/javascript' ]
673 ];
674
675 $cases[] = [ '<style></style>',
676 'style', [ 'media' => 'all' ]
677 ];
678 $cases[] = [ '<style></style>',
679 'style', [ 'type' => 'text/css' ]
680 ];
681
682 $cases[] = [ '<textarea></textarea>',
683 'textarea', [ 'wrap' => 'soft' ]
684 ];
685
686 # ## SPECIFIC CASES
687
688 # <link type="text/css">
689 $cases[] = [ '<link/>',
690 'link', [ 'type' => 'text/css' ]
691 ];
692
693 # <input> specific handling
694 $cases[] = [ '<input type="checkbox"/>',
695 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
696 'Default value "on" is stripped of checkboxes',
697 ];
698 $cases[] = [ '<input type="radio"/>',
699 'input', [ 'type' => 'radio', 'value' => 'on' ],
700 'Default value "on" is stripped of radio buttons',
701 ];
702 $cases[] = [ '<input type="submit" value="Submit"/>',
703 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
704 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
705 ];
706 $cases[] = [ '<input type="color"/>',
707 'input', [ 'type' => 'color', 'value' => '' ],
708 ];
709 $cases[] = [ '<input type="range"/>',
710 'input', [ 'type' => 'range', 'value' => '' ],
711 ];
712
713 # <button> specific handling
714 # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
715 $cases[] = [ '<button type="submit"></button>',
716 'button', [ 'type' => 'submit' ],
717 'According to standard the default type is "submit". '
718 . 'Depending on compatibility mode IE might use "button", instead.',
719 ];
720
721 # <select> specific handling
722 $cases[] = [ '<select multiple=""></select>',
723 'select', [ 'size' => '4', 'multiple' => true ],
724 ];
725 # .. with numeric value
726 $cases[] = [ '<select multiple=""></select>',
727 'select', [ 'size' => 4, 'multiple' => true ],
728 ];
729 $cases[] = [ '<select></select>',
730 'select', [ 'size' => '1', 'multiple' => false ],
731 ];
732 # .. with numeric value
733 $cases[] = [ '<select></select>',
734 'select', [ 'size' => 1, 'multiple' => false ],
735 ];
736
737 # Passing an array as value
738 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
739 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
740 "dropDefaults accepts values given as an array"
741 ];
742
743 # FIXME: doDropDefault should remove defaults given in an array
744 # Expected should be '<a></a>'
745 $cases[] = [ '<a class=""></a>',
746 'a', [ 'class' => [ '', '' ] ],
747 "dropDefaults accepts values given as an array"
748 ];
749
750 # Craft the Html elements
751 $ret = [];
752 foreach ( $cases as $case ) {
753 $ret[] = [
754 $case[0],
755 $case[1], $case[2],
756 $case[3] ?? ''
757 ];
758 }
759
760 return $ret;
761 }
762
763 /**
764 * @covers Html::input
765 */
766 public function testWrapperInput() {
767 $this->assertEquals(
768 '<input type="radio" value="testval" name="testname"/>',
769 Html::input( 'testname', 'testval', 'radio' ),
770 'Input wrapper with type and value.'
771 );
772 $this->assertEquals(
773 '<input name="testname"/>',
774 Html::input( 'testname' ),
775 'Input wrapper with all default values.'
776 );
777 }
778
779 /**
780 * @covers Html::check
781 */
782 public function testWrapperCheck() {
783 $this->assertEquals(
784 '<input type="checkbox" value="1" name="testname"/>',
785 Html::check( 'testname' ),
786 'Checkbox wrapper unchecked.'
787 );
788 $this->assertEquals(
789 '<input checked="" type="checkbox" value="1" name="testname"/>',
790 Html::check( 'testname', true ),
791 'Checkbox wrapper checked.'
792 );
793 $this->assertEquals(
794 '<input type="checkbox" value="testval" name="testname"/>',
795 Html::check( 'testname', false, [ 'value' => 'testval' ] ),
796 'Checkbox wrapper with a value override.'
797 );
798 }
799
800 /**
801 * @covers Html::radio
802 */
803 public function testWrapperRadio() {
804 $this->assertEquals(
805 '<input type="radio" value="1" name="testname"/>',
806 Html::radio( 'testname' ),
807 'Radio wrapper unchecked.'
808 );
809 $this->assertEquals(
810 '<input checked="" type="radio" value="1" name="testname"/>',
811 Html::radio( 'testname', true ),
812 'Radio wrapper checked.'
813 );
814 $this->assertEquals(
815 '<input type="radio" value="testval" name="testname"/>',
816 Html::radio( 'testname', false, [ 'value' => 'testval' ] ),
817 'Radio wrapper with a value override.'
818 );
819 }
820
821 /**
822 * @covers Html::label
823 */
824 public function testWrapperLabel() {
825 $this->assertEquals(
826 '<label for="testid">testlabel</label>',
827 Html::label( 'testlabel', 'testid' ),
828 'Label wrapper'
829 );
830 }
831
832 public static function provideSrcSetImages() {
833 return [
834 [ [], '', 'when there are no images, return empty string' ],
835 [
836 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
837 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
838 'pixel depth keys may include a trailing "x"'
839 ],
840 [
841 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
842 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
843 'pixel depth keys may omit a trailing "x"'
844 ],
845 [
846 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
847 'small.png 1x, large.png 1.5x',
848 'omit larger duplicates'
849 ],
850 [
851 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
852 'small.png 1x, large.png 1.5x',
853 'omit larger duplicates in irregular order'
854 ],
855 ];
856 }
857
858 /**
859 * @dataProvider provideSrcSetImages
860 * @covers Html::srcSet
861 */
862 public function testSrcSet( $images, $expected, $message ) {
863 $this->assertEquals( Html::srcSet( $images ), $expected, $message );
864 }
865
866 public static function provideInlineScript() {
867 return [
868 'Empty' => [
869 '',
870 '<script></script>'
871 ],
872 'Simple' => [
873 'EXAMPLE.label("foo");',
874 '<script>EXAMPLE.label("foo");</script>'
875 ],
876 'Ampersand' => [
877 'EXAMPLE.is(a && b);',
878 '<script>EXAMPLE.is(a && b);</script>'
879 ],
880 'HTML' => [
881 'EXAMPLE.label("<a>");',
882 '<script>EXAMPLE.label("<a>");</script>'
883 ],
884 'Script closing string (lower)' => [
885 'EXAMPLE.label("</script>");',
886 '<script>/* ERROR: Invalid script */</script>',
887 true,
888 ],
889 'Script closing with non-standard attributes (mixed)' => [
890 'EXAMPLE.label("</SCriPT and STyLE>");',
891 '<script>/* ERROR: Invalid script */</script>',
892 true,
893 ],
894 'HTML-comment-open and script-open' => [
895 // In HTML, <script> contents aren't just plain CDATA until </script>,
896 // there are levels of escaping modes, and the below sequence puts an
897 // HTML parser in a state where </script> would *not* close the script.
898 // https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escape-end-state
899 'var a = "<!--<script>";',
900 '<script>/* ERROR: Invalid script */</script>',
901 true,
902 ],
903 ];
904 }
905
906 /**
907 * @dataProvider provideInlineScript
908 * @covers Html::inlineScript
909 */
910 public function testInlineScript( $code, $expected, $error = false ) {
911 if ( $error ) {
912 Wikimedia\suppressWarnings();
913 $this->restoreWarnings = true;
914 }
915 $this->assertSame( Html::inlineScript( $code ), $expected );
916 }
917 }
918
919 class HtmlTestValue {
920 function __toString() {
921 return 'stringValue';
922 }
923 }