2 /** tests for includes/Html.php */
4 class HtmlTest
extends MediaWikiTestCase
{
6 protected function setUp() {
10 'wgWellFormedXml' => false,
11 'wgUseMediaWikiUIEverywhere' => false,
14 $langObj = Language
::factory( 'en' );
16 // Hardcode namespaces during test runs,
17 // so that html output based on existing namespaces
18 // can be properly evaluated.
19 $langObj->setNamespaces( [
31 9 => 'MediaWiki_talk',
33 11 => 'Template_talk',
35 15 => 'Category_talk',
39 $this->setUserLang( $langObj );
40 $this->setContentLang( $langObj );
44 * @covers Html::element
46 public function testElementBasics() {
49 Html
::element( 'img', null, '' ),
50 'No close tag for short-tag elements'
54 '<element></element>',
55 Html
::element( 'element', null, null ),
56 'Close tag for empty element (null, null)'
60 '<element></element>',
61 Html
::element( 'element', [], '' ),
62 'Close tag for empty element (array, string)'
65 $this->setMwGlobals( 'wgWellFormedXml', true );
69 Html
::element( 'img', null, '' ),
70 'Self-closing tag for short-tag elements (wgWellFormedXml = true)'
74 public function dataXmlMimeType() {
76 // ( $mimetype, $isXmlMimeType )
77 # HTML is not an XML MimeType
78 [ 'text/html', false ],
79 # XML is an XML MimeType
81 [ 'application/xml', true ],
82 # XHTML is an XML MimeType
83 [ 'application/xhtml+xml', true ],
84 # Make sure other +xml MimeTypes are supported
85 # SVG is another random MimeType even though we don't use it
86 [ 'image/svg+xml', true ],
87 # Complete random other MimeTypes are not XML
88 [ 'text/plain', false ],
93 * @dataProvider dataXmlMimeType
94 * @covers Html::isXmlMimeType
96 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
97 $this->assertEquals( $isXmlMimeType, Html
::isXmlMimeType( $mimetype ) );
101 * @covers Html::expandAttributes
103 public function testExpandAttributesSkipsNullAndFalse() {
107 Html
::expandAttributes( [ 'foo' => null ] ),
108 'skip keys with null value'
111 Html
::expandAttributes( [ 'foo' => false ] ),
112 'skip keys with false value'
116 Html
::expandAttributes( [ 'foo' => '' ] ),
117 'keep keys with an empty string'
122 * @covers Html::expandAttributes
124 public function testExpandAttributesForBooleans() {
127 Html
::expandAttributes( [ 'selected' => false ] ),
128 'Boolean attributes do not generates output when value is false'
132 Html
::expandAttributes( [ 'selected' => null ] ),
133 'Boolean attributes do not generates output when value is null'
138 Html
::expandAttributes( [ 'selected' => true ] ),
139 'Boolean attributes have no value when value is true'
143 Html
::expandAttributes( [ 'selected' ] ),
144 'Boolean attributes have no value when value is true (passed as numerical array)'
147 $this->setMwGlobals( 'wgWellFormedXml', true );
151 Html
::expandAttributes( [ 'selected' => true ] ),
152 'Boolean attributes have empty string value when value is true (wgWellFormedXml)'
157 * @covers Html::expandAttributes
159 public function testExpandAttributesForNumbers() {
162 Html
::expandAttributes( [ 'value' => 1 ] ),
163 'Integer value is cast to a string'
167 Html
::expandAttributes( [ 'value' => 1.1 ] ),
168 'Float value is cast to a string'
173 * @covers Html::expandAttributes
175 public function testExpandAttributesForObjects() {
177 ' value=stringValue',
178 Html
::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
179 'Object value is converted to a string'
184 * Test for Html::expandAttributes()
185 * Please note it output a string prefixed with a space!
186 * @covers Html::expandAttributes
188 public function testExpandAttributesVariousExpansions() {
192 Html
::expandAttributes( [ 'empty_string' => '' ] ),
193 'Empty string is always quoted'
197 Html
::expandAttributes( [ 'key' => 'value' ] ),
198 'Simple string value needs no quotes'
202 Html
::expandAttributes( [ 'one' => 1 ] ),
203 'Number 1 value needs no quotes'
207 Html
::expandAttributes( [ 'zero' => 0 ] ),
208 'Number 0 value needs no quotes'
211 $this->setMwGlobals( 'wgWellFormedXml', true );
215 Html
::expandAttributes( [ 'empty_string' => '' ] ),
216 'Attribute values are always quoted (wgWellFormedXml): Empty string'
220 Html
::expandAttributes( [ 'key' => 'value' ] ),
221 'Attribute values are always quoted (wgWellFormedXml): Simple string'
225 Html
::expandAttributes( [ 'one' => 1 ] ),
226 'Attribute values are always quoted (wgWellFormedXml): Number 1'
230 Html
::expandAttributes( [ 'zero' => 0 ] ),
231 'Attribute values are always quoted (wgWellFormedXml): Number 0'
236 * Html::expandAttributes has special features for HTML
237 * attributes that use space separated lists and also
238 * allows arrays to be used as values.
239 * @covers Html::expandAttributes
241 public function testExpandAttributesListValueAttributes() {
244 ' class="redundant spaces here"',
245 Html
::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
246 'Normalization should strip redundant spaces'
250 Html
::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
251 'Normalization should remove duplicates in string-lists'
253 # ## "EMPTY" ARRAY VALUES
256 Html
::expandAttributes( [ 'class' => [] ] ),
257 'Value with an empty array'
261 Html
::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
262 'Array with null, empty string and spaces'
264 # ## NON-EMPTY ARRAY VALUES
267 Html
::expandAttributes( [ 'class' => [
274 'Normalization should remove duplicates in the array'
278 Html
::expandAttributes( [ 'class' => [
284 'Normalization should remove duplicates in string-lists in the array'
289 * Test feature added by r96188, let pass attributes values as
290 * a PHP array. Restricted to class,rel, accesskey.
291 * @covers Html::expandAttributes
293 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
295 ' class="booltrue one"',
296 Html
::expandAttributes( [ 'class' => [
300 # Method use isset() internally, make sure we do discard
301 # attributes values which have been assigned well known values
303 'boolfalse' => false,
311 * How do we handle duplicate keys in HTML attributes expansion?
312 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
313 * The later will take precedence.
315 * Feature added by r96188
316 * @covers Html::expandAttributes
318 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
321 Html
::expandAttributes( [ 'class' => [
330 * @covers Html::expandAttributes
331 * @expectedException MWException
333 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
334 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
335 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
336 Html
::expandAttributes( [
345 * @covers Html::namespaceSelector
347 public function testNamespaceSelector() {
349 '<select id=namespace name=namespace>' . "\n" .
350 '<option value=0>(Main)</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" .
367 Html
::namespaceSelector(),
368 'Basic namespace selector without custom options'
372 '<label for=mw-test-namespace>Select a namespace:</label> ' .
373 '<select id=mw-test-namespace name=wpNamespace>' . "\n" .
374 '<option value=all>all</option>' . "\n" .
375 '<option value=0>(Main)</option>' . "\n" .
376 '<option value=1>Talk</option>' . "\n" .
377 '<option value=2 selected>User</option>' . "\n" .
378 '<option value=3>User talk</option>' . "\n" .
379 '<option value=4>MyWiki</option>' . "\n" .
380 '<option value=5>MyWiki Talk</option>' . "\n" .
381 '<option value=6>File</option>' . "\n" .
382 '<option value=7>File talk</option>' . "\n" .
383 '<option value=8>MediaWiki</option>' . "\n" .
384 '<option value=9>MediaWiki talk</option>' . "\n" .
385 '<option value=10>Template</option>' . "\n" .
386 '<option value=11>Template talk</option>' . "\n" .
387 '<option value=14>Category</option>' . "\n" .
388 '<option value=15>Category talk</option>' . "\n" .
389 '<option value=100>Custom</option>' . "\n" .
390 '<option value=101>Custom talk</option>' . "\n" .
392 Html
::namespaceSelector(
393 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
394 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
396 'Basic namespace selector with custom values'
400 '<label for=namespace>Select a namespace:</label> ' .
401 '<select id=namespace name=namespace>' . "\n" .
402 '<option value=0>(Main)</option>' . "\n" .
403 '<option value=1>Talk</option>' . "\n" .
404 '<option value=2>User</option>' . "\n" .
405 '<option value=3>User talk</option>' . "\n" .
406 '<option value=4>MyWiki</option>' . "\n" .
407 '<option value=5>MyWiki Talk</option>' . "\n" .
408 '<option value=6>File</option>' . "\n" .
409 '<option value=7>File talk</option>' . "\n" .
410 '<option value=8>MediaWiki</option>' . "\n" .
411 '<option value=9>MediaWiki talk</option>' . "\n" .
412 '<option value=10>Template</option>' . "\n" .
413 '<option value=11>Template talk</option>' . "\n" .
414 '<option value=14>Category</option>' . "\n" .
415 '<option value=15>Category talk</option>' . "\n" .
416 '<option value=100>Custom</option>' . "\n" .
417 '<option value=101>Custom talk</option>' . "\n" .
419 Html
::namespaceSelector(
420 [ 'label' => 'Select a namespace:' ]
422 'Basic namespace selector with a custom label but no id attribtue for the <select>'
426 public function testCanFilterOutNamespaces() {
428 '<select id=namespace name=namespace>' . "\n" .
429 '<option value=2>User</option>' . "\n" .
430 '<option value=4>MyWiki</option>' . "\n" .
431 '<option value=5>MyWiki Talk</option>' . "\n" .
432 '<option value=6>File</option>' . "\n" .
433 '<option value=7>File talk</option>' . "\n" .
434 '<option value=8>MediaWiki</option>' . "\n" .
435 '<option value=9>MediaWiki talk</option>' . "\n" .
436 '<option value=10>Template</option>' . "\n" .
437 '<option value=11>Template talk</option>' . "\n" .
438 '<option value=14>Category</option>' . "\n" .
439 '<option value=15>Category talk</option>' . "\n" .
441 Html
::namespaceSelector(
442 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
444 'Namespace selector namespace filtering.'
448 public function testCanDisableANamespaces() {
450 '<select id=namespace name=namespace>' . "\n" .
451 '<option disabled value=0>(Main)</option>' . "\n" .
452 '<option disabled value=1>Talk</option>' . "\n" .
453 '<option disabled value=2>User</option>' . "\n" .
454 '<option disabled value=3>User talk</option>' . "\n" .
455 '<option disabled value=4>MyWiki</option>' . "\n" .
456 '<option value=5>MyWiki Talk</option>' . "\n" .
457 '<option value=6>File</option>' . "\n" .
458 '<option value=7>File talk</option>' . "\n" .
459 '<option value=8>MediaWiki</option>' . "\n" .
460 '<option value=9>MediaWiki talk</option>' . "\n" .
461 '<option value=10>Template</option>' . "\n" .
462 '<option value=11>Template talk</option>' . "\n" .
463 '<option value=14>Category</option>' . "\n" .
464 '<option value=15>Category talk</option>' . "\n" .
465 '<option value=100>Custom</option>' . "\n" .
466 '<option value=101>Custom talk</option>' . "\n" .
468 Html
::namespaceSelector( [
469 'disable' => [ 0, 1, 2, 3, 4 ]
471 'Namespace selector namespace disabling'
476 * @dataProvider provideHtml5InputTypes
477 * @covers Html::element
479 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
481 '<input type=' . $HTML5InputType . '>',
482 Html
::element( 'input', [ 'type' => $HTML5InputType ] ),
483 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
488 * List of input element types values introduced by HTML5
489 * Full list at http://www.w3.org/TR/html-markup/input.html
491 public static function provideHtml5InputTypes() {
508 foreach ( $types as $type ) {
509 $cases[] = [ $type ];
516 * Test out Html::element drops or enforces default value
517 * @covers Html::dropDefaults
518 * @dataProvider provideElementsWithAttributesHavingDefaultValues
520 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
521 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
524 public static function provideElementsWithAttributesHavingDefaultValues() {
525 # Use cases in a concise format:
526 # <expected>, <element name>, <array of attributes> [, <message>]
527 # Will be mapped to Html::element()
530 # ## Generic cases, match $attribDefault static array
531 $cases[] = [ '<area>',
532 'area', [ 'shape' => 'rect' ]
535 $cases[] = [ '<button type=submit></button>',
536 'button', [ 'formaction' => 'GET' ]
538 $cases[] = [ '<button type=submit></button>',
539 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
542 $cases[] = [ '<canvas></canvas>',
543 'canvas', [ 'height' => '150' ]
545 $cases[] = [ '<canvas></canvas>',
546 'canvas', [ 'width' => '300' ]
548 # Also check with numeric values
549 $cases[] = [ '<canvas></canvas>',
550 'canvas', [ 'height' => 150 ]
552 $cases[] = [ '<canvas></canvas>',
553 'canvas', [ 'width' => 300 ]
556 $cases[] = [ '<command>',
557 'command', [ 'type' => 'command' ]
560 $cases[] = [ '<form></form>',
561 'form', [ 'action' => 'GET' ]
563 $cases[] = [ '<form></form>',
564 'form', [ 'autocomplete' => 'on' ]
566 $cases[] = [ '<form></form>',
567 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
570 $cases[] = [ '<input>',
571 'input', [ 'formaction' => 'GET' ]
573 $cases[] = [ '<input>',
574 'input', [ 'type' => 'text' ]
577 $cases[] = [ '<keygen>',
578 'keygen', [ 'keytype' => 'rsa' ]
581 $cases[] = [ '<link>',
582 'link', [ 'media' => 'all' ]
585 $cases[] = [ '<menu></menu>',
586 'menu', [ 'type' => 'list' ]
589 $cases[] = [ '<script></script>',
590 'script', [ 'type' => 'text/javascript' ]
593 $cases[] = [ '<style></style>',
594 'style', [ 'media' => 'all' ]
596 $cases[] = [ '<style></style>',
597 'style', [ 'type' => 'text/css' ]
600 $cases[] = [ '<textarea></textarea>',
601 'textarea', [ 'wrap' => 'soft' ]
606 # <link type="text/css">
607 $cases[] = [ '<link>',
608 'link', [ 'type' => 'text/css' ]
611 # <input> specific handling
612 $cases[] = [ '<input type=checkbox>',
613 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
614 'Default value "on" is stripped of checkboxes',
616 $cases[] = [ '<input type=radio>',
617 'input', [ 'type' => 'radio', 'value' => 'on' ],
618 'Default value "on" is stripped of radio buttons',
620 $cases[] = [ '<input type=submit value=Submit>',
621 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
622 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
624 $cases[] = [ '<input type=color>',
625 'input', [ 'type' => 'color', 'value' => '' ],
627 $cases[] = [ '<input type=range>',
628 'input', [ 'type' => 'range', 'value' => '' ],
631 # <button> specific handling
632 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
633 $cases[] = [ '<button type=submit></button>',
634 'button', [ 'type' => 'submit' ],
635 'According to standard the default type is "submit". '
636 . 'Depending on compatibility mode IE might use "button", instead.',
639 # <select> specific handling
640 $cases[] = [ '<select multiple></select>',
641 'select', [ 'size' => '4', 'multiple' => true ],
643 # .. with numeric value
644 $cases[] = [ '<select multiple></select>',
645 'select', [ 'size' => 4, 'multiple' => true ],
647 $cases[] = [ '<select></select>',
648 'select', [ 'size' => '1', 'multiple' => false ],
650 # .. with numeric value
651 $cases[] = [ '<select></select>',
652 'select', [ 'size' => 1, 'multiple' => false ],
655 # Passing an array as value
656 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
657 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
658 "dropDefaults accepts values given as an array"
661 # FIXME: doDropDefault should remove defaults given in an array
662 # Expected should be '<a></a>'
663 $cases[] = [ '<a class=""></a>',
664 'a', [ 'class' => [ '', '' ] ],
665 "dropDefaults accepts values given as an array"
668 # Craft the Html elements
670 foreach ( $cases as $case ) {
674 isset( $case[3] ) ?
$case[3] : ''
682 * @covers Html::expandAttributes
684 public function testFormValidationBlacklist() {
686 Html
::expandAttributes( [
693 'Blacklist form validation attributes.'
697 Html
::expandAttributes(
705 'Allow special case "step=any".'
710 public function testWrapperInput() {
712 '<input type=radio value=testval name=testname>',
713 Html
::input( 'testname', 'testval', 'radio' ),
714 'Input wrapper with type and value.'
717 '<input name=testname>',
718 Html
::input( 'testname' ),
719 'Input wrapper with all default values.'
723 public function testWrapperCheck() {
725 '<input type=checkbox value=1 name=testname>',
726 Html
::check( 'testname' ),
727 'Checkbox wrapper unchecked.'
730 '<input checked type=checkbox value=1 name=testname>',
731 Html
::check( 'testname', true ),
732 'Checkbox wrapper checked.'
735 '<input type=checkbox value=testval name=testname>',
736 Html
::check( 'testname', false, [ 'value' => 'testval' ] ),
737 'Checkbox wrapper with a value override.'
741 public function testWrapperRadio() {
743 '<input type=radio value=1 name=testname>',
744 Html
::radio( 'testname' ),
745 'Radio wrapper unchecked.'
748 '<input checked type=radio value=1 name=testname>',
749 Html
::radio( 'testname', true ),
750 'Radio wrapper checked.'
753 '<input type=radio value=testval name=testname>',
754 Html
::radio( 'testname', false, [ 'value' => 'testval' ] ),
755 'Radio wrapper with a value override.'
759 public function testWrapperLabel() {
761 '<label for=testid>testlabel</label>',
762 Html
::label( 'testlabel', 'testid' ),
767 public static function provideSrcSetImages() {
769 [ [], '', 'when there are no images, return empty string' ],
771 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
772 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
773 'pixel depth keys may include a trailing "x"'
776 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
777 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
778 'pixel depth keys may omit a trailing "x"'
784 * @dataProvider provideSrcSetImages
785 * @covers Html::srcSet
787 public function testSrcSet( $images, $expected, $message ) {
788 $this->assertEquals( Html
::srcSet( $images ), $expected, $message );
792 class HtmlTestValue
{
793 function __toString() {
794 return 'stringValue';