Debian Codegraph @

tests/phpunit/includes/Html/HtmlTest.php

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