3 class HtmlTest
extends MediaWikiTestCase
{
5 protected function setUp() {
9 'wgUseMediaWikiUIEverywhere' => false,
12 $langObj = Language
::factory( 'en' );
14 // Hardcode namespaces during test runs,
15 // so that html output based on existing namespaces
16 // can be properly evaluated.
17 $langObj->setNamespaces( [
29 9 => 'MediaWiki_talk',
31 11 => 'Template_talk',
33 15 => 'Category_talk',
37 $this->setUserLang( $langObj );
38 $this->setContentLang( $langObj );
42 * @covers Html::element
43 * @covers Html::rawElement
44 * @covers Html::openElement
45 * @covers Html::closeElement
47 public function testElementBasics() {
50 Html
::element( 'img', null, '' ),
51 'Self-closing tag for short-tag elements'
55 '<element></element>',
56 Html
::element( 'element', null, null ),
57 'Close tag for empty element (null, null)'
61 '<element></element>',
62 Html
::element( 'element', [], '' ),
63 'Close tag for empty element (array, string)'
67 public function dataXmlMimeType() {
69 // ( $mimetype, $isXmlMimeType )
70 # HTML is not an XML MimeType
71 [ 'text/html', false ],
72 # XML is an XML MimeType
74 [ 'application/xml', true ],
75 # XHTML is an XML MimeType
76 [ 'application/xhtml+xml', true ],
77 # Make sure other +xml MimeTypes are supported
78 # SVG is another random MimeType even though we don't use it
79 [ 'image/svg+xml', true ],
80 # Complete random other MimeTypes are not XML
81 [ 'text/plain', false ],
86 * @dataProvider dataXmlMimeType
87 * @covers Html::isXmlMimeType
89 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
90 $this->assertEquals( $isXmlMimeType, Html
::isXmlMimeType( $mimetype ) );
94 * @covers Html::expandAttributes
96 public function testExpandAttributesSkipsNullAndFalse() {
99 Html
::expandAttributes( [ 'foo' => null ] ),
100 'skip keys with null value'
103 Html
::expandAttributes( [ 'foo' => false ] ),
104 'skip keys with false value'
108 Html
::expandAttributes( [ 'foo' => '' ] ),
109 'keep keys with an empty string'
114 * @covers Html::expandAttributes
116 public function testExpandAttributesForBooleans() {
119 Html
::expandAttributes( [ 'selected' => false ] ),
120 'Boolean attributes do not generates output when value is false'
124 Html
::expandAttributes( [ 'selected' => null ] ),
125 'Boolean attributes do not generates output when value is null'
130 Html
::expandAttributes( [ 'selected' => true ] ),
131 'Boolean attributes have no value when value is true'
135 Html
::expandAttributes( [ 'selected' ] ),
136 'Boolean attributes have no value when value is true (passed as numerical array)'
141 * @covers Html::expandAttributes
143 public function testExpandAttributesForNumbers() {
146 Html
::expandAttributes( [ 'value' => 1 ] ),
147 'Integer value is cast to a string'
151 Html
::expandAttributes( [ 'value' => 1.1 ] ),
152 'Float value is cast to a string'
157 * @covers Html::expandAttributes
159 public function testExpandAttributesForObjects() {
161 ' value="stringValue"',
162 Html
::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
163 'Object value is converted to a string'
168 * Test for Html::expandAttributes()
169 * Please note it output a string prefixed with a space!
170 * @covers Html::expandAttributes
172 public function testExpandAttributesVariousExpansions() {
176 Html
::expandAttributes( [ 'empty_string' => '' ] ),
177 'Empty string is always quoted'
181 Html
::expandAttributes( [ 'key' => 'value' ] ),
182 'Simple string value needs no quotes'
186 Html
::expandAttributes( [ 'one' => 1 ] ),
187 'Number 1 value needs no quotes'
191 Html
::expandAttributes( [ 'zero' => 0 ] ),
192 'Number 0 value needs no quotes'
197 * Html::expandAttributes has special features for HTML
198 * attributes that use space separated lists and also
199 * allows arrays to be used as values.
200 * @covers Html::expandAttributes
202 public function testExpandAttributesListValueAttributes() {
205 ' class="redundant spaces here"',
206 Html
::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
207 'Normalization should strip redundant spaces'
211 Html
::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
212 'Normalization should remove duplicates in string-lists'
214 # ## "EMPTY" ARRAY VALUES
217 Html
::expandAttributes( [ 'class' => [] ] ),
218 'Value with an empty array'
222 Html
::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
223 'Array with null, empty string and spaces'
225 # ## NON-EMPTY ARRAY VALUES
228 Html
::expandAttributes( [ 'class' => [
235 'Normalization should remove duplicates in the array'
239 Html
::expandAttributes( [ 'class' => [
245 'Normalization should remove duplicates in string-lists in the array'
250 * Test feature added by r96188, let pass attributes values as
251 * a PHP array. Restricted to class,rel, accesskey.
252 * @covers Html::expandAttributes
254 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
256 ' class="booltrue one"',
257 Html
::expandAttributes( [ 'class' => [
261 # Method use isset() internally, make sure we do discard
262 # attributes values which have been assigned well known values
264 'boolfalse' => false,
272 * How do we handle duplicate keys in HTML attributes expansion?
273 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
274 * The latter will take precedence.
276 * Feature added by r96188
277 * @covers Html::expandAttributes
279 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
282 Html
::expandAttributes( [ 'class' => [
291 * @covers Html::expandAttributes
292 * @expectedException MWException
294 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
295 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
296 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
297 Html
::expandAttributes( [
306 * @covers Html::namespaceSelector
307 * @covers Html::namespaceSelectorOptions
309 public function testNamespaceSelector() {
311 '<select id="namespace" name="namespace">' . "\n" .
312 '<option value="0">(Main)</option>' . "\n" .
313 '<option value="1">Talk</option>' . "\n" .
314 '<option value="2">User</option>' . "\n" .
315 '<option value="3">User talk</option>' . "\n" .
316 '<option value="4">MyWiki</option>' . "\n" .
317 '<option value="5">MyWiki Talk</option>' . "\n" .
318 '<option value="6">File</option>' . "\n" .
319 '<option value="7">File talk</option>' . "\n" .
320 '<option value="8">MediaWiki</option>' . "\n" .
321 '<option value="9">MediaWiki talk</option>' . "\n" .
322 '<option value="10">Template</option>' . "\n" .
323 '<option value="11">Template talk</option>' . "\n" .
324 '<option value="14">Category</option>' . "\n" .
325 '<option value="15">Category talk</option>' . "\n" .
326 '<option value="100">Custom</option>' . "\n" .
327 '<option value="101">Custom talk</option>' . "\n" .
329 Html
::namespaceSelector(),
330 'Basic namespace selector without custom options'
334 '<label for="mw-test-namespace">Select a namespace:</label> ' .
335 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
336 '<option value="all">all</option>' . "\n" .
337 '<option value="0">(Main)</option>' . "\n" .
338 '<option value="1">Talk</option>' . "\n" .
339 '<option value="2" selected="">User</option>' . "\n" .
340 '<option value="3">User talk</option>' . "\n" .
341 '<option value="4">MyWiki</option>' . "\n" .
342 '<option value="5">MyWiki Talk</option>' . "\n" .
343 '<option value="6">File</option>' . "\n" .
344 '<option value="7">File talk</option>' . "\n" .
345 '<option value="8">MediaWiki</option>' . "\n" .
346 '<option value="9">MediaWiki talk</option>' . "\n" .
347 '<option value="10">Template</option>' . "\n" .
348 '<option value="11">Template talk</option>' . "\n" .
349 '<option value="14">Category</option>' . "\n" .
350 '<option value="15">Category talk</option>' . "\n" .
351 '<option value="100">Custom</option>' . "\n" .
352 '<option value="101">Custom talk</option>' . "\n" .
354 Html
::namespaceSelector(
355 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
356 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
358 'Basic namespace selector with custom values'
362 '<label for="namespace">Select a namespace:</label> ' .
363 '<select id="namespace" name="namespace">' . "\n" .
364 '<option value="0">(Main)</option>' . "\n" .
365 '<option value="1">Talk</option>' . "\n" .
366 '<option value="2">User</option>' . "\n" .
367 '<option value="3">User talk</option>' . "\n" .
368 '<option value="4">MyWiki</option>' . "\n" .
369 '<option value="5">MyWiki Talk</option>' . "\n" .
370 '<option value="6">File</option>' . "\n" .
371 '<option value="7">File talk</option>' . "\n" .
372 '<option value="8">MediaWiki</option>' . "\n" .
373 '<option value="9">MediaWiki talk</option>' . "\n" .
374 '<option value="10">Template</option>' . "\n" .
375 '<option value="11">Template talk</option>' . "\n" .
376 '<option value="14">Category</option>' . "\n" .
377 '<option value="15">Category talk</option>' . "\n" .
378 '<option value="100">Custom</option>' . "\n" .
379 '<option value="101">Custom talk</option>' . "\n" .
381 Html
::namespaceSelector(
382 [ 'label' => 'Select a namespace:' ]
384 'Basic namespace selector with a custom label but no id attribtue for the <select>'
389 * @covers Html::namespaceSelector
391 public function testCanFilterOutNamespaces() {
393 '<select id="namespace" name="namespace">' . "\n" .
394 '<option value="2">User</option>' . "\n" .
395 '<option value="4">MyWiki</option>' . "\n" .
396 '<option value="5">MyWiki Talk</option>' . "\n" .
397 '<option value="6">File</option>' . "\n" .
398 '<option value="7">File talk</option>' . "\n" .
399 '<option value="8">MediaWiki</option>' . "\n" .
400 '<option value="9">MediaWiki talk</option>' . "\n" .
401 '<option value="10">Template</option>' . "\n" .
402 '<option value="11">Template talk</option>' . "\n" .
403 '<option value="14">Category</option>' . "\n" .
404 '<option value="15">Category talk</option>' . "\n" .
406 Html
::namespaceSelector(
407 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
409 'Namespace selector namespace filtering.'
414 * @covers Html::namespaceSelector
416 public function testCanDisableANamespaces() {
418 '<select id="namespace" name="namespace">' . "\n" .
419 '<option disabled="" value="0">(Main)</option>' . "\n" .
420 '<option disabled="" value="1">Talk</option>' . "\n" .
421 '<option disabled="" value="2">User</option>' . "\n" .
422 '<option disabled="" value="3">User talk</option>' . "\n" .
423 '<option disabled="" value="4">MyWiki</option>' . "\n" .
424 '<option value="5">MyWiki Talk</option>' . "\n" .
425 '<option value="6">File</option>' . "\n" .
426 '<option value="7">File talk</option>' . "\n" .
427 '<option value="8">MediaWiki</option>' . "\n" .
428 '<option value="9">MediaWiki talk</option>' . "\n" .
429 '<option value="10">Template</option>' . "\n" .
430 '<option value="11">Template talk</option>' . "\n" .
431 '<option value="14">Category</option>' . "\n" .
432 '<option value="15">Category talk</option>' . "\n" .
433 '<option value="100">Custom</option>' . "\n" .
434 '<option value="101">Custom talk</option>' . "\n" .
436 Html
::namespaceSelector( [
437 'disable' => [ 0, 1, 2, 3, 4 ]
439 'Namespace selector namespace disabling'
444 * @dataProvider provideHtml5InputTypes
445 * @covers Html::element
447 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
449 '<input type="' . $HTML5InputType . '"/>',
450 Html
::element( 'input', [ 'type' => $HTML5InputType ] ),
451 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
456 * @covers Html::warningBox
457 * @covers Html::messageBox
459 public function testWarningBox() {
461 Html
::warningBox( 'warn' ),
462 '<div class="warningbox">warn</div>'
467 * @covers Html::errorBox
468 * @covers Html::messageBox
470 public function testErrorBox() {
472 Html
::errorBox( 'err' ),
473 '<div class="errorbox">err</div>'
476 Html
::errorBox( 'err', 'heading' ),
477 '<div class="errorbox"><h2>heading</h2>err</div>'
482 * @covers Html::successBox
483 * @covers Html::messageBox
485 public function testSuccessBox() {
487 Html
::successBox( 'great' ),
488 '<div class="successbox">great</div>'
491 Html
::successBox( '<script>beware no escaping!</script>' ),
492 '<div class="successbox"><script>beware no escaping!</script></div>'
497 * List of input element types values introduced by HTML5
498 * Full list at https://www.w3.org/TR/html-markup/input.html
500 public static function provideHtml5InputTypes() {
517 foreach ( $types as $type ) {
518 $cases[] = [ $type ];
525 * Test out Html::element drops or enforces default value
526 * @covers Html::dropDefaults
527 * @dataProvider provideElementsWithAttributesHavingDefaultValues
529 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
530 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
533 public static function provideElementsWithAttributesHavingDefaultValues() {
534 # Use cases in a concise format:
535 # <expected>, <element name>, <array of attributes> [, <message>]
536 # Will be mapped to Html::element()
539 # ## Generic cases, match $attribDefault static array
540 $cases[] = [ '<area/>',
541 'area', [ 'shape' => 'rect' ]
544 $cases[] = [ '<button type="submit"></button>',
545 'button', [ 'formaction' => 'GET' ]
547 $cases[] = [ '<button type="submit"></button>',
548 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
551 $cases[] = [ '<canvas></canvas>',
552 'canvas', [ 'height' => '150' ]
554 $cases[] = [ '<canvas></canvas>',
555 'canvas', [ 'width' => '300' ]
557 # Also check with numeric values
558 $cases[] = [ '<canvas></canvas>',
559 'canvas', [ 'height' => 150 ]
561 $cases[] = [ '<canvas></canvas>',
562 'canvas', [ 'width' => 300 ]
565 $cases[] = [ '<form></form>',
566 'form', [ 'action' => 'GET' ]
568 $cases[] = [ '<form></form>',
569 'form', [ 'autocomplete' => 'on' ]
571 $cases[] = [ '<form></form>',
572 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
575 $cases[] = [ '<input/>',
576 'input', [ 'formaction' => 'GET' ]
578 $cases[] = [ '<input/>',
579 'input', [ 'type' => 'text' ]
582 $cases[] = [ '<keygen/>',
583 'keygen', [ 'keytype' => 'rsa' ]
586 $cases[] = [ '<link/>',
587 'link', [ 'media' => 'all' ]
590 $cases[] = [ '<menu></menu>',
591 'menu', [ 'type' => 'list' ]
594 $cases[] = [ '<script></script>',
595 'script', [ 'type' => 'text/javascript' ]
598 $cases[] = [ '<style></style>',
599 'style', [ 'media' => 'all' ]
601 $cases[] = [ '<style></style>',
602 'style', [ 'type' => 'text/css' ]
605 $cases[] = [ '<textarea></textarea>',
606 'textarea', [ 'wrap' => 'soft' ]
611 # <link type="text/css">
612 $cases[] = [ '<link/>',
613 'link', [ 'type' => 'text/css' ]
616 # <input> specific handling
617 $cases[] = [ '<input type="checkbox"/>',
618 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
619 'Default value "on" is stripped of checkboxes',
621 $cases[] = [ '<input type="radio"/>',
622 'input', [ 'type' => 'radio', 'value' => 'on' ],
623 'Default value "on" is stripped of radio buttons',
625 $cases[] = [ '<input type="submit" value="Submit"/>',
626 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
627 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
629 $cases[] = [ '<input type="color"/>',
630 'input', [ 'type' => 'color', 'value' => '' ],
632 $cases[] = [ '<input type="range"/>',
633 'input', [ 'type' => 'range', 'value' => '' ],
636 # <button> specific handling
637 # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
638 $cases[] = [ '<button type="submit"></button>',
639 'button', [ 'type' => 'submit' ],
640 'According to standard the default type is "submit". '
641 . 'Depending on compatibility mode IE might use "button", instead.',
644 # <select> specific handling
645 $cases[] = [ '<select multiple=""></select>',
646 'select', [ 'size' => '4', 'multiple' => true ],
648 # .. with numeric value
649 $cases[] = [ '<select multiple=""></select>',
650 'select', [ 'size' => 4, 'multiple' => true ],
652 $cases[] = [ '<select></select>',
653 'select', [ 'size' => '1', 'multiple' => false ],
655 # .. with numeric value
656 $cases[] = [ '<select></select>',
657 'select', [ 'size' => 1, 'multiple' => false ],
660 # Passing an array as value
661 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
662 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
663 "dropDefaults accepts values given as an array"
666 # FIXME: doDropDefault should remove defaults given in an array
667 # Expected should be '<a></a>'
668 $cases[] = [ '<a class=""></a>',
669 'a', [ 'class' => [ '', '' ] ],
670 "dropDefaults accepts values given as an array"
673 # Craft the Html elements
675 foreach ( $cases as $case ) {
679 isset( $case[3] ) ?
$case[3] : ''
687 * @covers Html::input
689 public function testWrapperInput() {
691 '<input type="radio" value="testval" name="testname"/>',
692 Html
::input( 'testname', 'testval', 'radio' ),
693 'Input wrapper with type and value.'
696 '<input name="testname"/>',
697 Html
::input( 'testname' ),
698 'Input wrapper with all default values.'
703 * @covers Html::check
705 public function testWrapperCheck() {
707 '<input type="checkbox" value="1" name="testname"/>',
708 Html
::check( 'testname' ),
709 'Checkbox wrapper unchecked.'
712 '<input checked="" type="checkbox" value="1" name="testname"/>',
713 Html
::check( 'testname', true ),
714 'Checkbox wrapper checked.'
717 '<input type="checkbox" value="testval" name="testname"/>',
718 Html
::check( 'testname', false, [ 'value' => 'testval' ] ),
719 'Checkbox wrapper with a value override.'
724 * @covers Html::radio
726 public function testWrapperRadio() {
728 '<input type="radio" value="1" name="testname"/>',
729 Html
::radio( 'testname' ),
730 'Radio wrapper unchecked.'
733 '<input checked="" type="radio" value="1" name="testname"/>',
734 Html
::radio( 'testname', true ),
735 'Radio wrapper checked.'
738 '<input type="radio" value="testval" name="testname"/>',
739 Html
::radio( 'testname', false, [ 'value' => 'testval' ] ),
740 'Radio wrapper with a value override.'
745 * @covers Html::label
747 public function testWrapperLabel() {
749 '<label for="testid">testlabel</label>',
750 Html
::label( 'testlabel', 'testid' ),
755 public static function provideSrcSetImages() {
757 [ [], '', 'when there are no images, return empty string' ],
759 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
760 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
761 'pixel depth keys may include a trailing "x"'
764 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
765 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
766 'pixel depth keys may omit a trailing "x"'
769 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
770 'small.png 1x, large.png 1.5x',
771 'omit larger duplicates'
774 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
775 'small.png 1x, large.png 1.5x',
776 'omit larger duplicates in irregular order'
782 * @dataProvider provideSrcSetImages
783 * @covers Html::srcSet
785 public function testSrcSet( $images, $expected, $message ) {
786 $this->assertEquals( Html
::srcSet( $images ), $expected, $message );
790 class HtmlTestValue
{
791 function __toString() {
792 return 'stringValue';