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>' . "\u{00A0}" .
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>' . "\u{00A0}" .
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>'
480 Html
::errorBox( 'err', '0' ),
481 '<div class="errorbox"><h2>0</h2>err</div>'
486 * @covers Html::successBox
487 * @covers Html::messageBox
489 public function testSuccessBox() {
491 Html
::successBox( 'great' ),
492 '<div class="successbox">great</div>'
495 Html
::successBox( '<script>beware no escaping!</script>' ),
496 '<div class="successbox"><script>beware no escaping!</script></div>'
501 * List of input element types values introduced by HTML5
502 * Full list at https://www.w3.org/TR/html-markup/input.html
504 public static function provideHtml5InputTypes() {
521 foreach ( $types as $type ) {
522 $cases[] = [ $type ];
529 * Test out Html::element drops or enforces default value
530 * @covers Html::dropDefaults
531 * @dataProvider provideElementsWithAttributesHavingDefaultValues
533 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
534 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
537 public static function provideElementsWithAttributesHavingDefaultValues() {
538 # Use cases in a concise format:
539 # <expected>, <element name>, <array of attributes> [, <message>]
540 # Will be mapped to Html::element()
543 # ## Generic cases, match $attribDefault static array
544 $cases[] = [ '<area/>',
545 'area', [ 'shape' => 'rect' ]
548 $cases[] = [ '<button type="submit"></button>',
549 'button', [ 'formaction' => 'GET' ]
551 $cases[] = [ '<button type="submit"></button>',
552 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
555 $cases[] = [ '<canvas></canvas>',
556 'canvas', [ 'height' => '150' ]
558 $cases[] = [ '<canvas></canvas>',
559 'canvas', [ 'width' => '300' ]
561 # Also check with numeric values
562 $cases[] = [ '<canvas></canvas>',
563 'canvas', [ 'height' => 150 ]
565 $cases[] = [ '<canvas></canvas>',
566 'canvas', [ 'width' => 300 ]
569 $cases[] = [ '<form></form>',
570 'form', [ 'action' => 'GET' ]
572 $cases[] = [ '<form></form>',
573 'form', [ 'autocomplete' => 'on' ]
575 $cases[] = [ '<form></form>',
576 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
579 $cases[] = [ '<input/>',
580 'input', [ 'formaction' => 'GET' ]
582 $cases[] = [ '<input/>',
583 'input', [ 'type' => 'text' ]
586 $cases[] = [ '<keygen/>',
587 'keygen', [ 'keytype' => 'rsa' ]
590 $cases[] = [ '<link/>',
591 'link', [ 'media' => 'all' ]
594 $cases[] = [ '<menu></menu>',
595 'menu', [ 'type' => 'list' ]
598 $cases[] = [ '<script></script>',
599 'script', [ 'type' => 'text/javascript' ]
602 $cases[] = [ '<style></style>',
603 'style', [ 'media' => 'all' ]
605 $cases[] = [ '<style></style>',
606 'style', [ 'type' => 'text/css' ]
609 $cases[] = [ '<textarea></textarea>',
610 'textarea', [ 'wrap' => 'soft' ]
615 # <link type="text/css">
616 $cases[] = [ '<link/>',
617 'link', [ 'type' => 'text/css' ]
620 # <input> specific handling
621 $cases[] = [ '<input type="checkbox"/>',
622 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
623 'Default value "on" is stripped of checkboxes',
625 $cases[] = [ '<input type="radio"/>',
626 'input', [ 'type' => 'radio', 'value' => 'on' ],
627 'Default value "on" is stripped of radio buttons',
629 $cases[] = [ '<input type="submit" value="Submit"/>',
630 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
631 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
633 $cases[] = [ '<input type="color"/>',
634 'input', [ 'type' => 'color', 'value' => '' ],
636 $cases[] = [ '<input type="range"/>',
637 'input', [ 'type' => 'range', 'value' => '' ],
640 # <button> specific handling
641 # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
642 $cases[] = [ '<button type="submit"></button>',
643 'button', [ 'type' => 'submit' ],
644 'According to standard the default type is "submit". '
645 . 'Depending on compatibility mode IE might use "button", instead.',
648 # <select> specific handling
649 $cases[] = [ '<select multiple=""></select>',
650 'select', [ 'size' => '4', 'multiple' => true ],
652 # .. with numeric value
653 $cases[] = [ '<select multiple=""></select>',
654 'select', [ 'size' => 4, 'multiple' => true ],
656 $cases[] = [ '<select></select>',
657 'select', [ 'size' => '1', 'multiple' => false ],
659 # .. with numeric value
660 $cases[] = [ '<select></select>',
661 'select', [ 'size' => 1, 'multiple' => false ],
664 # Passing an array as value
665 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
666 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
667 "dropDefaults accepts values given as an array"
670 # FIXME: doDropDefault should remove defaults given in an array
671 # Expected should be '<a></a>'
672 $cases[] = [ '<a class=""></a>',
673 'a', [ 'class' => [ '', '' ] ],
674 "dropDefaults accepts values given as an array"
677 # Craft the Html elements
679 foreach ( $cases as $case ) {
691 * @covers Html::input
693 public function testWrapperInput() {
695 '<input type="radio" value="testval" name="testname"/>',
696 Html
::input( 'testname', 'testval', 'radio' ),
697 'Input wrapper with type and value.'
700 '<input name="testname"/>',
701 Html
::input( 'testname' ),
702 'Input wrapper with all default values.'
707 * @covers Html::check
709 public function testWrapperCheck() {
711 '<input type="checkbox" value="1" name="testname"/>',
712 Html
::check( 'testname' ),
713 'Checkbox wrapper unchecked.'
716 '<input checked="" type="checkbox" value="1" name="testname"/>',
717 Html
::check( 'testname', true ),
718 'Checkbox wrapper checked.'
721 '<input type="checkbox" value="testval" name="testname"/>',
722 Html
::check( 'testname', false, [ 'value' => 'testval' ] ),
723 'Checkbox wrapper with a value override.'
728 * @covers Html::radio
730 public function testWrapperRadio() {
732 '<input type="radio" value="1" name="testname"/>',
733 Html
::radio( 'testname' ),
734 'Radio wrapper unchecked.'
737 '<input checked="" type="radio" value="1" name="testname"/>',
738 Html
::radio( 'testname', true ),
739 'Radio wrapper checked.'
742 '<input type="radio" value="testval" name="testname"/>',
743 Html
::radio( 'testname', false, [ 'value' => 'testval' ] ),
744 'Radio wrapper with a value override.'
749 * @covers Html::label
751 public function testWrapperLabel() {
753 '<label for="testid">testlabel</label>',
754 Html
::label( 'testlabel', 'testid' ),
759 public static function provideSrcSetImages() {
761 [ [], '', 'when there are no images, return empty string' ],
763 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
764 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
765 'pixel depth keys may include a trailing "x"'
768 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
769 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
770 'pixel depth keys may omit a trailing "x"'
773 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
774 'small.png 1x, large.png 1.5x',
775 'omit larger duplicates'
778 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
779 'small.png 1x, large.png 1.5x',
780 'omit larger duplicates in irregular order'
786 * @dataProvider provideSrcSetImages
787 * @covers Html::srcSet
789 public function testSrcSet( $images, $expected, $message ) {
790 $this->assertEquals( Html
::srcSet( $images ), $expected, $message );
793 public static function provideInlineScript() {
800 'EXAMPLE.label("foo");',
801 '<script>EXAMPLE.label("foo");</script>'
804 'EXAMPLE.is(a && b);',
805 '<script>/*<![CDATA[*/EXAMPLE.is(a && b);/*]]>*/</script>'
808 'EXAMPLE.label("<a>");',
809 '<script>/*<![CDATA[*/EXAMPLE.label("<a>");/*]]>*/</script>'
811 'Script closing string' => [
812 'EXAMPLE.label("</script>");',
813 // Broken: First </script> ends the script in HTML
814 '<script>/*<![CDATA[*/EXAMPLE.label("</script>");/*]]>*/</script>'
817 'EXAMPLE.label("&> CDATA ]]>");',
818 // Broken: Works in HTML, but is invalid XML.
819 '<script>/*<![CDATA[*/EXAMPLE.label("&> CDATA ]]>");/*]]>*/</script>'
825 * @dataProvider provideInlineScript
826 * @covers Html::inlineScript
828 public function testInlineScript( $code, $expected ) {
829 $this->assertSame( Html
::inlineScript( $code ), $expected );
833 class HtmlTestValue
{
834 function __toString() {
835 return 'stringValue';