Update Codex from v1.20.0 to v1.20.1
[mediawiki.git] / tests / phpunit / includes / Html / HtmlTest.php
blobbac571674144e9d97152b5ab998453ff787c0d28
1 <?php
3 use MediaWiki\Html\Html;
4 use MediaWiki\Html\HtmlJsCode;
5 use MediaWiki\MainConfigNames;
7 /**
8 * @covers \MediaWiki\Html\Html
9 */
10 class HtmlTest extends MediaWikiIntegrationTestCase {
12 protected function setUp(): void {
13 parent::setUp();
15 $this->overrideConfigValue( MainConfigNames::UsePigLatinVariant, false );
17 $langFactory = $this->getServiceContainer()->getLanguageFactory();
18 $contLangObj = $langFactory->getLanguage( 'en' );
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 );
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 );
71 public function testOpenElement() {
72 $this->expectPHPError(
73 E_USER_NOTICE,
74 static function () {
75 Html::openElement( 'span id="x"' );
77 'given element name with space'
81 public function testElementBasics() {
82 $this->assertEquals(
83 '<img>',
84 Html::element( 'img', null, '' ),
85 'Self-closing tag for short-tag elements'
88 $this->assertEquals(
89 '<element></element>',
90 Html::element( 'element', null, null ),
91 'Close tag for empty element (null, null)'
94 $this->assertEquals(
95 '<element></element>',
96 Html::element( 'element', [], '' ),
97 'Close tag for empty element (array, string)'
101 public function dataXmlMimeType() {
102 return [
103 // ( $mimetype, $isXmlMimeType )
104 # HTML is not an XML MimeType
105 [ 'text/html', false ],
106 # XML is an XML MimeType
107 [ 'text/xml', true ],
108 [ 'application/xml', true ],
109 # XHTML is an XML MimeType
110 [ 'application/xhtml+xml', true ],
111 # Make sure other +xml MimeTypes are supported
112 # SVG is another random MimeType even though we don't use it
113 [ 'image/svg+xml', true ],
114 # Complete random other MimeTypes are not XML
115 [ 'text/plain', false ],
120 * @dataProvider dataXmlMimeType
122 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
123 $this->assertEquals( $isXmlMimeType, Html::isXmlMimeType( $mimetype ) );
126 public static function provideExpandAttributes() {
127 // $expect, $attributes
128 yield 'keep keys with an empty string' => [
129 ' foo=""',
130 [ 'foo' => '' ]
132 yield 'False bool attribs have no output' => [
134 [ 'selected' => false ]
136 yield 'Null bool attribs have no output' => [
138 [ 'selected' => null ]
140 yield 'True bool attribs have output' => [
141 ' selected=""',
142 [ 'selected' => true ]
144 yield 'True bool attribs have output (passed as numerical array)' => [
145 ' selected=""',
146 [ 'selected' ]
148 yield 'integer value is cast to string' => [
149 ' value="1"',
150 [ 'value' => 1 ]
152 yield 'float value is cast to string' => [
153 ' value="1.1"',
154 [ 'value' => 1.1 ]
156 yield 'object value is converted to string' => [
157 ' value="stringValue"',
158 [ 'value' => new HtmlTestValue() ]
160 yield 'Empty string is always quoted' => [
161 ' empty_string=""',
162 [ 'empty_string' => '' ]
164 yield 'Simple string value needs no quotes' => [
165 ' key="value"',
166 [ 'key' => 'value' ]
168 yield 'Number 1 value needs no quotes' => [
169 ' one="1"',
170 [ 'one' => 1 ]
172 yield 'Number 0 value needs no quotes' => [
173 ' zero="0"',
174 [ 'zero' => 0 ]
179 * @dataProvider provideExpandAttributes
181 public function testExpandAttributes( string $expect, array $attribs ) {
182 $this->assertEquals( $expect, Html::expandAttributes( $attribs ) );
185 public static function provideExpandAttributesEmpty() {
186 // $attributes
187 yield 'skip keys with null value' => [ [ 'foo' => null ] ];
188 yield 'skip keys with false value' => [ [ 'foo' => false ] ];
192 * @dataProvider provideExpandAttributesEmpty
194 public function testExpandAttributesEmpty( array $attribs ) {
195 $this->assertSame( '', Html::expandAttributes( $attribs ) );
198 public static function provideExpandAttributesClass() {
199 // $expect, $classes
200 // string values
201 yield 'Normalization should strip redundant spaces' => [
202 ' class="redundant spaces here"',
203 ' redundant spaces here '
205 yield 'Normalization should remove duplicates in string-lists' => [
206 ' class="foo bar"',
207 'foo bar foo bar bar'
209 // array values
210 yield 'Value with an empty array' => [
211 ' class=""',
214 yield 'Array with null, empty string and spaces' => [
215 ' class=""',
216 [ null, '', ' ', ' ' ]
218 yield 'Normalization should remove duplicates in the array' => [
219 ' class="foo bar"',
220 [ 'foo', 'bar', 'foo', 'bar', 'bar' ]
222 yield 'Normalization should remove duplicates in string-lists in the array' => [
223 ' class="foo bar"',
224 [ 'foo bar', 'bar foo', 'foo', 'bar bar' ]
227 // Feature added in r96188 - pass attributes values as a PHP array
228 // only applies to class, rel, and accesskey
229 yield 'Associative array' => [
230 ' class="booltrue one"',
232 'booltrue' => true,
233 'one' => 1,
235 # Method use isset() internally, make sure we do discard
236 # attributes values which have been assigned well known values
237 'emptystring' => '',
238 'boolfalse' => false,
239 'zero' => 0,
240 'null' => null,
244 // How do we handle duplicate keys in HTML attributes expansion?
245 // We could pass a "class" the values: 'GREEN' and [ 'GREEN' => false ]
246 // The latter will take precedence
247 yield 'Duplicate keys' => [
248 ' class=""',
250 'GREEN',
251 'GREEN' => false,
252 'GREEN',
258 * Html::expandAttributes has special features for HTML
259 * attributes that use space separated lists and also
260 * allows arrays to be used as values.
262 * @dataProvider provideExpandAttributesClass
264 public function testExpandAttributesClass( string $expect, $classes ) {
265 $this->assertEquals(
266 $expect,
267 Html::expandAttributes( [ 'class' => $classes ] )
271 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
272 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
273 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
274 $this->expectException( UnexpectedValueException::class );
275 Html::expandAttributes( [
276 'src' => [
277 'ltr' => 'ltr.svg',
278 'rtl' => 'rtl.svg'
280 ] );
283 public function testNamespaceSelector() {
284 $this->assertEquals(
285 '<select id="namespace" name="namespace">' . "\n" .
286 '<option value="0">(Principal)</option>' . "\n" .
287 '<option value="1">Talk</option>' . "\n" .
288 '<option value="2">User</option>' . "\n" .
289 '<option value="3">User talk</option>' . "\n" .
290 '<option value="4">MyWiki</option>' . "\n" .
291 '<option value="5">MyWiki Talk</option>' . "\n" .
292 '<option value="6">File</option>' . "\n" .
293 '<option value="7">File talk</option>' . "\n" .
294 '<option value="8">MediaWiki</option>' . "\n" .
295 '<option value="9">MediaWiki talk</option>' . "\n" .
296 '<option value="10">Template</option>' . "\n" .
297 '<option value="11">Template talk</option>' . "\n" .
298 '<option value="14">Category</option>' . "\n" .
299 '<option value="15">Category talk</option>' . "\n" .
300 '<option value="100">Custom</option>' . "\n" .
301 '<option value="101">Custom talk</option>' . "\n" .
302 '</select>',
303 Html::namespaceSelector(),
304 'Basic namespace selector without custom options'
307 $this->assertEquals(
308 '<label for="mw-test-namespace">Select a namespace:</label>' . "\u{00A0}" .
309 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
310 '<option value="all">todos</option>' . "\n" .
311 '<option value="0">(Principal)</option>' . "\n" .
312 '<option value="1">Talk</option>' . "\n" .
313 '<option value="2" selected="">User</option>' . "\n" .
314 '<option value="3">User talk</option>' . "\n" .
315 '<option value="4">MyWiki</option>' . "\n" .
316 '<option value="5">MyWiki Talk</option>' . "\n" .
317 '<option value="6">File</option>' . "\n" .
318 '<option value="7">File talk</option>' . "\n" .
319 '<option value="8">MediaWiki</option>' . "\n" .
320 '<option value="9">MediaWiki talk</option>' . "\n" .
321 '<option value="10">Template</option>' . "\n" .
322 '<option value="11">Template talk</option>' . "\n" .
323 '<option value="14">Category</option>' . "\n" .
324 '<option value="15">Category talk</option>' . "\n" .
325 '<option value="100">Custom</option>' . "\n" .
326 '<option value="101">Custom talk</option>' . "\n" .
327 '</select>',
328 Html::namespaceSelector(
329 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
330 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
332 'Basic namespace selector with custom values'
335 $this->assertEquals(
336 '<label for="namespace">Select a namespace:</label>' . "\u{00A0}" .
337 '<select id="namespace" name="namespace">' . "\n" .
338 '<option value="0">(Principal)</option>' . "\n" .
339 '<option value="1">Talk</option>' . "\n" .
340 '<option value="2">User</option>' . "\n" .
341 '<option value="3">User talk</option>' . "\n" .
342 '<option value="4">MyWiki</option>' . "\n" .
343 '<option value="5">MyWiki Talk</option>' . "\n" .
344 '<option value="6">File</option>' . "\n" .
345 '<option value="7">File talk</option>' . "\n" .
346 '<option value="8">MediaWiki</option>' . "\n" .
347 '<option value="9">MediaWiki talk</option>' . "\n" .
348 '<option value="10">Template</option>' . "\n" .
349 '<option value="11">Template talk</option>' . "\n" .
350 '<option value="14">Category</option>' . "\n" .
351 '<option value="15">Category talk</option>' . "\n" .
352 '<option value="100">Custom</option>' . "\n" .
353 '<option value="101">Custom talk</option>' . "\n" .
354 '</select>',
355 Html::namespaceSelector(
356 [ 'label' => 'Select a namespace:' ]
358 'Basic namespace selector with a custom label but no id attribtue for the <select>'
361 $this->assertEquals(
362 '<select id="namespace" name="namespace">' . "\n" .
363 '<option value="0">(Principal)</option>' . "\n" .
364 '<option value="1">Discusión</option>' . "\n" .
365 '<option value="2">Usuario</option>' . "\n" .
366 '<option value="3">Usuario discusión</option>' . "\n" .
367 '<option value="4">Wiki</option>' . "\n" .
368 '<option value="5">Wiki discusión</option>' . "\n" .
369 '<option value="6">Archivo</option>' . "\n" .
370 '<option value="7">Archivo discusión</option>' . "\n" .
371 '<option value="8">MediaWiki</option>' . "\n" .
372 '<option value="9">MediaWiki discusión</option>' . "\n" .
373 '<option value="10">Plantilla</option>' . "\n" .
374 '<option value="11">Plantilla discusión</option>' . "\n" .
375 '<option value="12">Ayuda</option>' . "\n" .
376 '<option value="13">Ayuda discusión</option>' . "\n" .
377 '<option value="14">Categoría</option>' . "\n" .
378 '<option value="15">Categoría discusión</option>' . "\n" .
379 '<option value="100">Personalizado</option>' . "\n" .
380 '<option value="101">Personalizado discusión</option>' . "\n" .
381 '</select>',
382 Html::namespaceSelector(
383 [ 'in-user-lang' => true ]
385 'Basic namespace selector in user language'
389 public function testCanFilterOutNamespaces() {
390 $this->assertEquals(
391 '<select id="namespace" name="namespace">' . "\n" .
392 '<option value="2">User</option>' . "\n" .
393 '<option value="4">MyWiki</option>' . "\n" .
394 '<option value="5">MyWiki Talk</option>' . "\n" .
395 '<option value="6">File</option>' . "\n" .
396 '<option value="7">File talk</option>' . "\n" .
397 '<option value="8">MediaWiki</option>' . "\n" .
398 '<option value="9">MediaWiki talk</option>' . "\n" .
399 '<option value="10">Template</option>' . "\n" .
400 '<option value="11">Template talk</option>' . "\n" .
401 '<option value="14">Category</option>' . "\n" .
402 '<option value="15">Category talk</option>' . "\n" .
403 '</select>',
404 Html::namespaceSelector(
405 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
407 'Namespace selector namespace filtering.'
409 $this->assertEquals(
410 '<select id="namespace" name="namespace">' . "\n" .
411 '<option value="" selected="">todos</option>' . "\n" .
412 '<option value="2">User</option>' . "\n" .
413 '<option value="4">MyWiki</option>' . "\n" .
414 '<option value="5">MyWiki Talk</option>' . "\n" .
415 '<option value="6">File</option>' . "\n" .
416 '<option value="7">File talk</option>' . "\n" .
417 '<option value="8">MediaWiki</option>' . "\n" .
418 '<option value="9">MediaWiki talk</option>' . "\n" .
419 '<option value="10">Template</option>' . "\n" .
420 '<option value="11">Template talk</option>' . "\n" .
421 '<option value="14">Category</option>' . "\n" .
422 '<option value="15">Category talk</option>' . "\n" .
423 '</select>',
424 Html::namespaceSelector(
425 [ 'exclude' => [ 0, 1, 3, 100, 101 ], 'all' => '' ]
427 'Namespace selector namespace filtering with empty custom "all" option.'
431 public function testCanDisableANamespaces() {
432 $this->assertEquals(
433 '<select id="namespace" name="namespace">' . "\n" .
434 '<option disabled="" value="0">(Principal)</option>' . "\n" .
435 '<option disabled="" value="1">Talk</option>' . "\n" .
436 '<option disabled="" value="2">User</option>' . "\n" .
437 '<option disabled="" value="3">User talk</option>' . "\n" .
438 '<option disabled="" value="4">MyWiki</option>' . "\n" .
439 '<option value="5">MyWiki Talk</option>' . "\n" .
440 '<option value="6">File</option>' . "\n" .
441 '<option value="7">File talk</option>' . "\n" .
442 '<option value="8">MediaWiki</option>' . "\n" .
443 '<option value="9">MediaWiki talk</option>' . "\n" .
444 '<option value="10">Template</option>' . "\n" .
445 '<option value="11">Template talk</option>' . "\n" .
446 '<option value="14">Category</option>' . "\n" .
447 '<option value="15">Category talk</option>' . "\n" .
448 '<option value="100">Custom</option>' . "\n" .
449 '<option value="101">Custom talk</option>' . "\n" .
450 '</select>',
451 Html::namespaceSelector( [
452 'disable' => [ 0, 1, 2, 3, 4 ]
453 ] ),
454 'Namespace selector namespace disabling'
459 * @dataProvider provideHtml5InputTypes
461 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
462 $this->assertEquals(
463 '<input type="' . $HTML5InputType . '">',
464 Html::element( 'input', [ 'type' => $HTML5InputType ] ),
465 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
469 public function testWarningBox() {
470 $this->assertEquals(
471 '<div class="cdx-message cdx-message--block cdx-message--warning">'
472 . '<span class="cdx-message__icon"></span>'
473 . '<div class="cdx-message__content">warn</div></div>',
474 Html::warningBox( 'warn' )
478 public function testErrorBox() {
479 $this->assertEquals(
480 '<div class="cdx-message cdx-message--block cdx-message--error">'
481 . '<span class="cdx-message__icon"></span>'
482 . '<div class="cdx-message__content">err</div></div>',
483 Html::errorBox( 'err' )
485 $this->assertEquals(
486 '<div class="cdx-message cdx-message--block cdx-message--error errorbox-custom-class">'
487 . '<span class="cdx-message__icon"></span>'
488 . '<div class="cdx-message__content">'
489 . '<h2>heading</h2>err'
490 . '</div></div>',
491 Html::errorBox( 'err', 'heading', 'errorbox-custom-class' )
493 $this->assertEquals(
494 '<div class="cdx-message cdx-message--block cdx-message--error">'
495 . '<span class="cdx-message__icon"></span>'
496 . '<div class="cdx-message__content">'
497 . '<h2>0</h2>err'
498 . '</div></div>',
499 Html::errorBox( 'err', '0', '' )
503 public function testSuccessBox() {
504 $this->assertEquals(
505 '<div class="cdx-message cdx-message--block cdx-message--success">'
506 . '<span class="cdx-message__icon"></span>'
507 . '<div class="cdx-message__content">great</div></div>',
508 Html::successBox( 'great' )
510 $this->assertEquals(
511 '<div class="cdx-message cdx-message--block cdx-message--success">'
512 . '<span class="cdx-message__icon"></span>'
513 . '<div class="cdx-message__content">'
514 . '<script>beware no escaping!</script>'
515 . '</div></div>',
516 Html::successBox( '<script>beware no escaping!</script>' )
521 * List of input element types values introduced by HTML5
522 * Full list at https://www.w3.org/TR/html-markup/input.html
524 public static function provideHtml5InputTypes() {
525 $types = [
526 'datetime',
527 'datetime-local',
528 'date',
529 'month',
530 'time',
531 'week',
532 'number',
533 'range',
534 'email',
535 'url',
536 'search',
537 'tel',
538 'color',
541 foreach ( $types as $type ) {
542 yield [ $type ];
547 * Test out Html::element drops or enforces default value
548 * @dataProvider provideElementsWithAttributesHavingDefaultValues
550 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
551 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
554 public static function provideElementsWithAttributesHavingDefaultValues() {
555 # Use cases in a concise format:
556 # <expected>, <element name>, <array of attributes> [, <message>]
557 # Will be mapped to Html::element()
558 $cases = [];
560 # ## Generic cases, match $attribDefault static array
561 $cases[] = [ '<area>',
562 'area', [ 'shape' => 'rect' ]
565 $cases[] = [ '<button type="submit"></button>',
566 'button', [ 'formaction' => 'GET' ]
568 $cases[] = [ '<button type="submit"></button>',
569 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
572 $cases[] = [ '<canvas></canvas>',
573 'canvas', [ 'height' => '150' ]
575 $cases[] = [ '<canvas></canvas>',
576 'canvas', [ 'width' => '300' ]
578 # Also check with numeric values
579 $cases[] = [ '<canvas></canvas>',
580 'canvas', [ 'height' => 150 ]
582 $cases[] = [ '<canvas></canvas>',
583 'canvas', [ 'width' => 300 ]
586 $cases[] = [ '<form></form>',
587 'form', [ 'action' => 'GET' ]
589 $cases[] = [ '<form></form>',
590 'form', [ 'autocomplete' => 'on' ]
592 $cases[] = [ '<form></form>',
593 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
596 $cases[] = [ '<input>',
597 'input', [ 'formaction' => 'GET' ]
599 $cases[] = [ '<input>',
600 'input', [ 'type' => 'text' ]
603 $cases[] = [ '<keygen>',
604 'keygen', [ 'keytype' => 'rsa' ]
607 $cases[] = [ '<link>',
608 'link', [ 'media' => 'all' ]
611 $cases[] = [ '<menu></menu>',
612 'menu', [ 'type' => 'list' ]
615 $cases[] = [ '<script></script>',
616 'script', [ 'type' => 'text/javascript' ]
619 $cases[] = [ '<style></style>',
620 'style', [ 'media' => 'all' ]
622 $cases[] = [ '<style></style>',
623 'style', [ 'type' => 'text/css' ]
626 $cases[] = [ '<textarea></textarea>',
627 'textarea', [ 'wrap' => 'soft' ]
630 # ## SPECIFIC CASES
632 # <link type="text/css">
633 $cases[] = [ '<link>',
634 'link', [ 'type' => 'text/css' ]
637 # <input> specific handling
638 $cases[] = [ '<input type="checkbox">',
639 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
640 'Default value "on" is stripped of checkboxes',
642 $cases[] = [ '<input type="radio">',
643 'input', [ 'type' => 'radio', 'value' => 'on' ],
644 'Default value "on" is stripped of radio buttons',
646 $cases[] = [ '<input type="submit" value="Submit">',
647 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
648 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
650 $cases[] = [ '<input type="color">',
651 'input', [ 'type' => 'color', 'value' => '' ],
653 $cases[] = [ '<input type="range">',
654 'input', [ 'type' => 'range', 'value' => '' ],
657 # <button> specific handling
658 # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
659 $cases[] = [ '<button type="submit"></button>',
660 'button', [ 'type' => 'submit' ],
661 'According to standard the default type is "submit". '
662 . 'Depending on compatibility mode IE might use "button", instead.',
665 # <select> specific handling
666 $cases[] = [ '<select multiple=""></select>',
667 'select', [ 'size' => '4', 'multiple' => true ],
669 # .. with numeric value
670 $cases[] = [ '<select multiple=""></select>',
671 'select', [ 'size' => 4, 'multiple' => true ],
673 $cases[] = [ '<select></select>',
674 'select', [ 'size' => '1', 'multiple' => false ],
676 # .. with numeric value
677 $cases[] = [ '<select></select>',
678 'select', [ 'size' => 1, 'multiple' => false ],
681 # Passing an array as value
682 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
683 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
684 "dropDefaults accepts values given as an array"
687 # FIXME: doDropDefault should remove defaults given in an array
688 # Expected should be '<a></a>'
689 $cases[] = [ '<a class=""></a>',
690 'a', [ 'class' => [ '', '' ] ],
691 "dropDefaults accepts values given as an array"
694 return $cases;
697 public function testWrapperInput() {
698 $this->assertEquals(
699 '<input type="radio" value="testval" name="testname">',
700 Html::input( 'testname', 'testval', 'radio' ),
701 'Input wrapper with type and value.'
703 $this->assertEquals(
704 '<input name="testname">',
705 Html::input( 'testname' ),
706 'Input wrapper with all default values.'
710 public function testWrapperCheck() {
711 $this->assertEquals(
712 '<input type="checkbox" value="1" name="testname">',
713 Html::check( 'testname' ),
714 'Checkbox wrapper unchecked.'
716 $this->assertEquals(
717 '<input checked="" type="checkbox" value="1" name="testname">',
718 Html::check( 'testname', true ),
719 'Checkbox wrapper checked.'
721 $this->assertEquals(
722 '<input type="checkbox" value="testval" name="testname">',
723 Html::check( 'testname', false, [ 'value' => 'testval' ] ),
724 'Checkbox wrapper with a value override.'
728 public function testWrapperRadio() {
729 $this->assertEquals(
730 '<input type="radio" value="1" name="testname">',
731 Html::radio( 'testname' ),
732 'Radio wrapper unchecked.'
734 $this->assertEquals(
735 '<input checked="" type="radio" value="1" name="testname">',
736 Html::radio( 'testname', true ),
737 'Radio wrapper checked.'
739 $this->assertEquals(
740 '<input type="radio" value="testval" name="testname">',
741 Html::radio( 'testname', false, [ 'value' => 'testval' ] ),
742 'Radio wrapper with a value override.'
746 public function testWrapperLabel() {
747 $this->assertEquals(
748 '<label for="testid">testlabel</label>',
749 Html::label( 'testlabel', 'testid' ),
750 'Label wrapper'
754 public static function provideSrcSetImages() {
755 return [
756 [ [], '', 'when there are no images, return empty string' ],
758 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
759 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
760 'pixel depth keys may include a trailing "x"'
763 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
764 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
765 'pixel depth keys may omit a trailing "x"'
768 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
769 'small.png 1x, large.png 1.5x',
770 'omit larger duplicates'
773 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
774 'small.png 1x, large.png 1.5x',
775 'omit larger duplicates in irregular order'
781 * @dataProvider provideSrcSetImages
783 public function testSrcSet( $images, $expected, $message ) {
784 $this->assertEquals( $expected, Html::srcSet( $images ), $message );
787 public static function provideInlineScript() {
788 return [
789 'Empty' => [
791 '<script></script>'
793 'Simple' => [
794 'EXAMPLE.label("foo");',
795 '<script>EXAMPLE.label("foo");</script>'
797 'Ampersand' => [
798 'EXAMPLE.is(a && b);',
799 '<script>EXAMPLE.is(a && b);</script>'
801 'HTML' => [
802 'EXAMPLE.label("<a>");',
803 '<script>EXAMPLE.label("<a>");</script>'
805 'Script closing string (lower)' => [
806 'EXAMPLE.label("</script>");',
807 '<script>/* ERROR: Invalid script */</script>',
808 true,
810 'Script closing with non-standard attributes (mixed)' => [
811 'EXAMPLE.label("</SCriPT and STyLE>");',
812 '<script>/* ERROR: Invalid script */</script>',
813 true,
815 'HTML-comment-open and script-open' => [
816 // In HTML, <script> contents aren't just plain CDATA until </script>,
817 // there are levels of escaping modes, and the below sequence puts an
818 // HTML parser in a state where </script> would *not* close the script.
819 // https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escape-end-state
820 'var a = "<!--<script>";',
821 '<script>/* ERROR: Invalid script */</script>',
822 true,
828 * @dataProvider provideInlineScript
830 public function testInlineScript( $code, $expected, $error = false ) {
831 if ( $error ) {
832 $html = @Html::inlineScript( $code );
833 } else {
834 $html = Html::inlineScript( $code );
836 $this->assertSame( $expected, $html );
839 public static function provideEncodeJsVar() {
840 // $expected, $input
841 yield 'boolean' => [ 'true', true ];
842 yield 'null' => [ 'null', null ];
843 yield 'array' => [ '["a",1]', [ 'a', 1 ] ];
844 yield 'associative arary' => [ '{"a":"a","b":1}', [ 'a' => 'a', 'b' => 1 ] ];
845 yield 'object' => [ '{"a":"a","b":1}', (object)[ 'a' => 'a', 'b' => 1 ] ];
846 yield 'int' => [ '123456', 123456 ];
847 yield 'float' => [ '1.5', 1.5 ];
848 yield 'int-like string' => [ '"123456"', '123456' ];
850 $code = 'function () { foo( 42 ); }';
851 yield 'code' => [ $code, new HtmlJsCode( $code ) ];
855 * @covers \MediaWiki\Html\Html
856 * @covers \MediaWiki\Html\HtmlJsCode
857 * @dataProvider provideEncodeJsVar
859 public function testEncodeJsVar( string $expect, $input ) {
860 $this->assertEquals(
861 $expect,
862 Html::encodeJsVar( $input )
867 * @covers \MediaWiki\Html\Html
868 * @covers \MediaWiki\Html\HtmlJsCode
870 public function testEncodeObject() {
871 $codeA = 'function () { foo( 42 ); }';
872 $codeB = 'function ( jQuery ) { bar( 142857 ); }';
873 $obj = HtmlJsCode::encodeObject( [
874 'a' => new HtmlJsCode( $codeA ),
875 'b' => new HtmlJsCode( $codeB )
876 ] );
877 $this->assertEquals(
878 "{\"a\":$codeA,\"b\":$codeB}",
879 Html::encodeJsVar( $obj )
883 public function testListDropdownOptions() {
884 $this->assertEquals(
886 'other reasons' => 'other',
887 'Empty group item' => 'Empty group item',
888 'Foo' => [
889 'Foo 1' => 'Foo 1',
890 'Example' => 'Example',
892 'Bar' => [
893 'Bar 1' => 'Bar 1',
896 Html::listDropdownOptions(
897 "*\n** Empty group item\n* Foo\n** Foo 1\n** Example\n* Bar\n** Bar 1",
898 [ 'other' => 'other reasons' ]
903 public function testListDropdownOptionsOthers() {
904 // Do not use the value for 'other' as option group - T251351
905 $this->assertEquals(
907 'other reasons' => 'other',
908 'Foo 1' => 'Foo 1',
909 'Example' => 'Example',
910 'Bar' => [
911 'Bar 1' => 'Bar 1',
914 Html::listDropdownOptions(
915 "* other reasons\n** Foo 1\n** Example\n* Bar\n** Bar 1",
916 [ 'other' => 'other reasons' ]
921 public function testListDropdownOptionsOoui() {
922 $this->assertEquals(
924 [ 'data' => 'other', 'label' => 'other reasons' ],
925 [ 'optgroup' => 'Foo' ],
926 [ 'data' => 'Foo 1', 'label' => 'Foo 1' ],
927 [ 'data' => 'Example', 'label' => 'Example' ],
928 [ 'optgroup' => 'Bar' ],
929 [ 'data' => 'Bar 1', 'label' => 'Bar 1' ],
931 Html::listDropdownOptionsOoui( [
932 'other reasons' => 'other',
933 'Foo' => [
934 'Foo 1' => 'Foo 1',
935 'Example' => 'Example',
937 'Bar' => [
938 'Bar 1' => 'Bar 1',
944 public function testListDropdownOptionsCodex(): void {
945 $this->assertEquals(
947 [ 'label' => 'other reasons', 'value' => 'other' ],
948 [ 'label' => 'Foo', 'value' => '', 'disabled' => true ],
949 [ 'label' => 'Foo 1', 'value' => 'Foo 1' ],
950 [ 'label' => 'Example', 'value' => 'Example' ],
951 [ 'label' => 'Bar', 'value' => '', 'disabled' => true ],
952 [ 'label' => 'Bar 1', 'value' => 'Bar 1' ],
954 Html::listDropdownOptionsCodex( [
955 'other reasons' => 'other',
956 'Foo' => [
957 'Foo 1' => 'Foo 1',
958 'Example' => 'Example',
960 'Bar' => [
961 'Bar 1' => 'Bar 1',
968 class HtmlTestValue implements Stringable {
969 public function __toString() {
970 return 'stringValue';