3 class HtmlTest
extends MediaWikiTestCase
{
4 private $restoreWarnings;
6 protected function setUp() {
10 'wgUseMediaWikiUIEverywhere' => false,
13 $langObj = Language
::factory( 'en' );
15 // Hardcode namespaces during test runs,
16 // so that html output based on existing namespaces
17 // can be properly evaluated.
18 $langObj->setNamespaces( [
30 9 => 'MediaWiki_talk',
32 11 => 'Template_talk',
34 15 => 'Category_talk',
38 $this->setUserLang( $langObj );
39 $this->setContentLang( $langObj );
40 $this->restoreWarnings
= false;
43 protected function tearDown() {
44 if ( $this->restoreWarnings
) {
45 $this->restoreWarnings
= false;
46 Wikimedia\restoreWarnings
();
52 * @covers Html::element
53 * @covers Html::rawElement
54 * @covers Html::openElement
55 * @covers Html::closeElement
57 public function testElementBasics() {
60 Html
::element( 'img', null, '' ),
61 'Self-closing tag for short-tag elements'
65 '<element></element>',
66 Html
::element( 'element', null, null ),
67 'Close tag for empty element (null, null)'
71 '<element></element>',
72 Html
::element( 'element', [], '' ),
73 'Close tag for empty element (array, string)'
77 public function dataXmlMimeType() {
79 // ( $mimetype, $isXmlMimeType )
80 # HTML is not an XML MimeType
81 [ 'text/html', false ],
82 # XML is an XML MimeType
84 [ 'application/xml', true ],
85 # XHTML is an XML MimeType
86 [ 'application/xhtml+xml', true ],
87 # Make sure other +xml MimeTypes are supported
88 # SVG is another random MimeType even though we don't use it
89 [ 'image/svg+xml', true ],
90 # Complete random other MimeTypes are not XML
91 [ 'text/plain', false ],
96 * @dataProvider dataXmlMimeType
97 * @covers Html::isXmlMimeType
99 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
100 $this->assertEquals( $isXmlMimeType, Html
::isXmlMimeType( $mimetype ) );
104 * @covers Html::expandAttributes
106 public function testExpandAttributesSkipsNullAndFalse() {
109 Html
::expandAttributes( [ 'foo' => null ] ),
110 'skip keys with null value'
113 Html
::expandAttributes( [ 'foo' => false ] ),
114 'skip keys with false value'
118 Html
::expandAttributes( [ 'foo' => '' ] ),
119 'keep keys with an empty string'
124 * @covers Html::expandAttributes
126 public function testExpandAttributesForBooleans() {
129 Html
::expandAttributes( [ 'selected' => false ] ),
130 'Boolean attributes do not generates output when value is false'
134 Html
::expandAttributes( [ 'selected' => null ] ),
135 'Boolean attributes do not generates output when value is null'
140 Html
::expandAttributes( [ 'selected' => true ] ),
141 'Boolean attributes have no value when value is true'
145 Html
::expandAttributes( [ 'selected' ] ),
146 'Boolean attributes have no value when value is true (passed as numerical array)'
151 * @covers Html::expandAttributes
153 public function testExpandAttributesForNumbers() {
156 Html
::expandAttributes( [ 'value' => 1 ] ),
157 'Integer value is cast to a string'
161 Html
::expandAttributes( [ 'value' => 1.1 ] ),
162 'Float value is cast to a string'
167 * @covers Html::expandAttributes
169 public function testExpandAttributesForObjects() {
171 ' value="stringValue"',
172 Html
::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
173 'Object value is converted to a string'
178 * Test for Html::expandAttributes()
179 * Please note it output a string prefixed with a space!
180 * @covers Html::expandAttributes
182 public function testExpandAttributesVariousExpansions() {
186 Html
::expandAttributes( [ 'empty_string' => '' ] ),
187 'Empty string is always quoted'
191 Html
::expandAttributes( [ 'key' => 'value' ] ),
192 'Simple string value needs no quotes'
196 Html
::expandAttributes( [ 'one' => 1 ] ),
197 'Number 1 value needs no quotes'
201 Html
::expandAttributes( [ 'zero' => 0 ] ),
202 'Number 0 value needs no quotes'
207 * Html::expandAttributes has special features for HTML
208 * attributes that use space separated lists and also
209 * allows arrays to be used as values.
210 * @covers Html::expandAttributes
212 public function testExpandAttributesListValueAttributes() {
215 ' class="redundant spaces here"',
216 Html
::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
217 'Normalization should strip redundant spaces'
221 Html
::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
222 'Normalization should remove duplicates in string-lists'
224 # ## "EMPTY" ARRAY VALUES
227 Html
::expandAttributes( [ 'class' => [] ] ),
228 'Value with an empty array'
232 Html
::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
233 'Array with null, empty string and spaces'
235 # ## NON-EMPTY ARRAY VALUES
238 Html
::expandAttributes( [ 'class' => [
245 'Normalization should remove duplicates in the array'
249 Html
::expandAttributes( [ 'class' => [
255 'Normalization should remove duplicates in string-lists in the array'
260 * Test feature added by r96188, let pass attributes values as
261 * a PHP array. Restricted to class,rel, accesskey.
262 * @covers Html::expandAttributes
264 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
266 ' class="booltrue one"',
267 Html
::expandAttributes( [ 'class' => [
271 # Method use isset() internally, make sure we do discard
272 # attributes values which have been assigned well known values
274 'boolfalse' => false,
282 * How do we handle duplicate keys in HTML attributes expansion?
283 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
284 * The latter will take precedence.
286 * Feature added by r96188
287 * @covers Html::expandAttributes
289 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
292 Html
::expandAttributes( [ 'class' => [
301 * @covers Html::expandAttributes
302 * @expectedException MWException
304 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
305 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
306 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
307 Html
::expandAttributes( [
316 * @covers Html::namespaceSelector
317 * @covers Html::namespaceSelectorOptions
319 public function testNamespaceSelector() {
321 '<select id="namespace" name="namespace">' . "\n" .
322 '<option value="0">(Main)</option>' . "\n" .
323 '<option value="1">Talk</option>' . "\n" .
324 '<option value="2">User</option>' . "\n" .
325 '<option value="3">User talk</option>' . "\n" .
326 '<option value="4">MyWiki</option>' . "\n" .
327 '<option value="5">MyWiki Talk</option>' . "\n" .
328 '<option value="6">File</option>' . "\n" .
329 '<option value="7">File talk</option>' . "\n" .
330 '<option value="8">MediaWiki</option>' . "\n" .
331 '<option value="9">MediaWiki talk</option>' . "\n" .
332 '<option value="10">Template</option>' . "\n" .
333 '<option value="11">Template talk</option>' . "\n" .
334 '<option value="14">Category</option>' . "\n" .
335 '<option value="15">Category talk</option>' . "\n" .
336 '<option value="100">Custom</option>' . "\n" .
337 '<option value="101">Custom talk</option>' . "\n" .
339 Html
::namespaceSelector(),
340 'Basic namespace selector without custom options'
344 '<label for="mw-test-namespace">Select a namespace:</label>' . "\u{00A0}" .
345 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
346 '<option value="all">all</option>' . "\n" .
347 '<option value="0">(Main)</option>' . "\n" .
348 '<option value="1">Talk</option>' . "\n" .
349 '<option value="2" selected="">User</option>' . "\n" .
350 '<option value="3">User talk</option>' . "\n" .
351 '<option value="4">MyWiki</option>' . "\n" .
352 '<option value="5">MyWiki Talk</option>' . "\n" .
353 '<option value="6">File</option>' . "\n" .
354 '<option value="7">File talk</option>' . "\n" .
355 '<option value="8">MediaWiki</option>' . "\n" .
356 '<option value="9">MediaWiki talk</option>' . "\n" .
357 '<option value="10">Template</option>' . "\n" .
358 '<option value="11">Template talk</option>' . "\n" .
359 '<option value="14">Category</option>' . "\n" .
360 '<option value="15">Category talk</option>' . "\n" .
361 '<option value="100">Custom</option>' . "\n" .
362 '<option value="101">Custom talk</option>' . "\n" .
364 Html
::namespaceSelector(
365 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
366 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
368 'Basic namespace selector with custom values'
372 '<label for="namespace">Select a namespace:</label>' . "\u{00A0}" .
373 '<select id="namespace" name="namespace">' . "\n" .
374 '<option value="0">(Main)</option>' . "\n" .
375 '<option value="1">Talk</option>' . "\n" .
376 '<option value="2">User</option>' . "\n" .
377 '<option value="3">User talk</option>' . "\n" .
378 '<option value="4">MyWiki</option>' . "\n" .
379 '<option value="5">MyWiki Talk</option>' . "\n" .
380 '<option value="6">File</option>' . "\n" .
381 '<option value="7">File talk</option>' . "\n" .
382 '<option value="8">MediaWiki</option>' . "\n" .
383 '<option value="9">MediaWiki talk</option>' . "\n" .
384 '<option value="10">Template</option>' . "\n" .
385 '<option value="11">Template talk</option>' . "\n" .
386 '<option value="14">Category</option>' . "\n" .
387 '<option value="15">Category talk</option>' . "\n" .
388 '<option value="100">Custom</option>' . "\n" .
389 '<option value="101">Custom talk</option>' . "\n" .
391 Html
::namespaceSelector(
392 [ 'label' => 'Select a namespace:' ]
394 'Basic namespace selector with a custom label but no id attribtue for the <select>'
399 * @covers Html::namespaceSelector
401 public function testCanFilterOutNamespaces() {
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" .
416 Html
::namespaceSelector(
417 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
419 'Namespace selector namespace filtering.'
424 * @covers Html::namespaceSelector
426 public function testCanDisableANamespaces() {
428 '<select id="namespace" name="namespace">' . "\n" .
429 '<option disabled="" value="0">(Main)</option>' . "\n" .
430 '<option disabled="" value="1">Talk</option>' . "\n" .
431 '<option disabled="" value="2">User</option>' . "\n" .
432 '<option disabled="" value="3">User talk</option>' . "\n" .
433 '<option disabled="" value="4">MyWiki</option>' . "\n" .
434 '<option value="5">MyWiki Talk</option>' . "\n" .
435 '<option value="6">File</option>' . "\n" .
436 '<option value="7">File talk</option>' . "\n" .
437 '<option value="8">MediaWiki</option>' . "\n" .
438 '<option value="9">MediaWiki talk</option>' . "\n" .
439 '<option value="10">Template</option>' . "\n" .
440 '<option value="11">Template talk</option>' . "\n" .
441 '<option value="14">Category</option>' . "\n" .
442 '<option value="15">Category talk</option>' . "\n" .
443 '<option value="100">Custom</option>' . "\n" .
444 '<option value="101">Custom talk</option>' . "\n" .
446 Html
::namespaceSelector( [
447 'disable' => [ 0, 1, 2, 3, 4 ]
449 'Namespace selector namespace disabling'
454 * @dataProvider provideHtml5InputTypes
455 * @covers Html::element
457 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
459 '<input type="' . $HTML5InputType . '"/>',
460 Html
::element( 'input', [ 'type' => $HTML5InputType ] ),
461 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
466 * @covers Html::warningBox
467 * @covers Html::messageBox
469 public function testWarningBox() {
471 Html
::warningBox( 'warn' ),
472 '<div class="warningbox">warn</div>'
477 * @covers Html::errorBox
478 * @covers Html::messageBox
480 public function testErrorBox() {
482 Html
::errorBox( 'err' ),
483 '<div class="errorbox">err</div>'
486 Html
::errorBox( 'err', 'heading' ),
487 '<div class="errorbox"><h2>heading</h2>err</div>'
490 Html
::errorBox( 'err', '0' ),
491 '<div class="errorbox"><h2>0</h2>err</div>'
496 * @covers Html::successBox
497 * @covers Html::messageBox
499 public function testSuccessBox() {
501 Html
::successBox( 'great' ),
502 '<div class="successbox">great</div>'
505 Html
::successBox( '<script>beware no escaping!</script>' ),
506 '<div class="successbox"><script>beware no escaping!</script></div>'
511 * List of input element types values introduced by HTML5
512 * Full list at https://www.w3.org/TR/html-markup/input.html
514 public static function provideHtml5InputTypes() {
531 foreach ( $types as $type ) {
532 $cases[] = [ $type ];
539 * Test out Html::element drops or enforces default value
540 * @covers Html::dropDefaults
541 * @dataProvider provideElementsWithAttributesHavingDefaultValues
543 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
544 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
547 public static function provideElementsWithAttributesHavingDefaultValues() {
548 # Use cases in a concise format:
549 # <expected>, <element name>, <array of attributes> [, <message>]
550 # Will be mapped to Html::element()
553 # ## Generic cases, match $attribDefault static array
554 $cases[] = [ '<area/>',
555 'area', [ 'shape' => 'rect' ]
558 $cases[] = [ '<button type="submit"></button>',
559 'button', [ 'formaction' => 'GET' ]
561 $cases[] = [ '<button type="submit"></button>',
562 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
565 $cases[] = [ '<canvas></canvas>',
566 'canvas', [ 'height' => '150' ]
568 $cases[] = [ '<canvas></canvas>',
569 'canvas', [ 'width' => '300' ]
571 # Also check with numeric values
572 $cases[] = [ '<canvas></canvas>',
573 'canvas', [ 'height' => 150 ]
575 $cases[] = [ '<canvas></canvas>',
576 'canvas', [ 'width' => 300 ]
579 $cases[] = [ '<form></form>',
580 'form', [ 'action' => 'GET' ]
582 $cases[] = [ '<form></form>',
583 'form', [ 'autocomplete' => 'on' ]
585 $cases[] = [ '<form></form>',
586 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
589 $cases[] = [ '<input/>',
590 'input', [ 'formaction' => 'GET' ]
592 $cases[] = [ '<input/>',
593 'input', [ 'type' => 'text' ]
596 $cases[] = [ '<keygen/>',
597 'keygen', [ 'keytype' => 'rsa' ]
600 $cases[] = [ '<link/>',
601 'link', [ 'media' => 'all' ]
604 $cases[] = [ '<menu></menu>',
605 'menu', [ 'type' => 'list' ]
608 $cases[] = [ '<script></script>',
609 'script', [ 'type' => 'text/javascript' ]
612 $cases[] = [ '<style></style>',
613 'style', [ 'media' => 'all' ]
615 $cases[] = [ '<style></style>',
616 'style', [ 'type' => 'text/css' ]
619 $cases[] = [ '<textarea></textarea>',
620 'textarea', [ 'wrap' => 'soft' ]
625 # <link type="text/css">
626 $cases[] = [ '<link/>',
627 'link', [ 'type' => 'text/css' ]
630 # <input> specific handling
631 $cases[] = [ '<input type="checkbox"/>',
632 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
633 'Default value "on" is stripped of checkboxes',
635 $cases[] = [ '<input type="radio"/>',
636 'input', [ 'type' => 'radio', 'value' => 'on' ],
637 'Default value "on" is stripped of radio buttons',
639 $cases[] = [ '<input type="submit" value="Submit"/>',
640 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
641 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
643 $cases[] = [ '<input type="color"/>',
644 'input', [ 'type' => 'color', 'value' => '' ],
646 $cases[] = [ '<input type="range"/>',
647 'input', [ 'type' => 'range', 'value' => '' ],
650 # <button> specific handling
651 # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
652 $cases[] = [ '<button type="submit"></button>',
653 'button', [ 'type' => 'submit' ],
654 'According to standard the default type is "submit". '
655 . 'Depending on compatibility mode IE might use "button", instead.',
658 # <select> specific handling
659 $cases[] = [ '<select multiple=""></select>',
660 'select', [ 'size' => '4', 'multiple' => true ],
662 # .. with numeric value
663 $cases[] = [ '<select multiple=""></select>',
664 'select', [ 'size' => 4, 'multiple' => true ],
666 $cases[] = [ '<select></select>',
667 'select', [ 'size' => '1', 'multiple' => false ],
669 # .. with numeric value
670 $cases[] = [ '<select></select>',
671 'select', [ 'size' => 1, 'multiple' => false ],
674 # Passing an array as value
675 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
676 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
677 "dropDefaults accepts values given as an array"
680 # FIXME: doDropDefault should remove defaults given in an array
681 # Expected should be '<a></a>'
682 $cases[] = [ '<a class=""></a>',
683 'a', [ 'class' => [ '', '' ] ],
684 "dropDefaults accepts values given as an array"
687 # Craft the Html elements
689 foreach ( $cases as $case ) {
701 * @covers Html::input
703 public function testWrapperInput() {
705 '<input type="radio" value="testval" name="testname"/>',
706 Html
::input( 'testname', 'testval', 'radio' ),
707 'Input wrapper with type and value.'
710 '<input name="testname"/>',
711 Html
::input( 'testname' ),
712 'Input wrapper with all default values.'
717 * @covers Html::check
719 public function testWrapperCheck() {
721 '<input type="checkbox" value="1" name="testname"/>',
722 Html
::check( 'testname' ),
723 'Checkbox wrapper unchecked.'
726 '<input checked="" type="checkbox" value="1" name="testname"/>',
727 Html
::check( 'testname', true ),
728 'Checkbox wrapper checked.'
731 '<input type="checkbox" value="testval" name="testname"/>',
732 Html
::check( 'testname', false, [ 'value' => 'testval' ] ),
733 'Checkbox wrapper with a value override.'
738 * @covers Html::radio
740 public function testWrapperRadio() {
742 '<input type="radio" value="1" name="testname"/>',
743 Html
::radio( 'testname' ),
744 'Radio wrapper unchecked.'
747 '<input checked="" type="radio" value="1" name="testname"/>',
748 Html
::radio( 'testname', true ),
749 'Radio wrapper checked.'
752 '<input type="radio" value="testval" name="testname"/>',
753 Html
::radio( 'testname', false, [ 'value' => 'testval' ] ),
754 'Radio wrapper with a value override.'
759 * @covers Html::label
761 public function testWrapperLabel() {
763 '<label for="testid">testlabel</label>',
764 Html
::label( 'testlabel', 'testid' ),
769 public static function provideSrcSetImages() {
771 [ [], '', 'when there are no images, return empty string' ],
773 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
774 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
775 'pixel depth keys may include a trailing "x"'
778 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
779 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
780 'pixel depth keys may omit a trailing "x"'
783 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
784 'small.png 1x, large.png 1.5x',
785 'omit larger duplicates'
788 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
789 'small.png 1x, large.png 1.5x',
790 'omit larger duplicates in irregular order'
796 * @dataProvider provideSrcSetImages
797 * @covers Html::srcSet
799 public function testSrcSet( $images, $expected, $message ) {
800 $this->assertEquals( Html
::srcSet( $images ), $expected, $message );
803 public static function provideInlineScript() {
810 'EXAMPLE.label("foo");',
811 '<script>EXAMPLE.label("foo");</script>'
814 'EXAMPLE.is(a && b);',
815 '<script>EXAMPLE.is(a && b);</script>'
818 'EXAMPLE.label("<a>");',
819 '<script>EXAMPLE.label("<a>");</script>'
821 'Script closing string (lower)' => [
822 'EXAMPLE.label("</script>");',
823 '<script>/* ERROR: Invalid script */</script>',
826 'Script closing with non-standard attributes (mixed)' => [
827 'EXAMPLE.label("</SCriPT and STyLE>");',
828 '<script>/* ERROR: Invalid script */</script>',
831 'HTML-comment-open and script-open' => [
832 // In HTML, <script> contents aren't just plain CDATA until </script>,
833 // there are levels of escaping modes, and the below sequence puts an
834 // HTML parser in a state where </script> would *not* close the script.
835 // https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escape-end-state
836 'var a = "<!--<script>";',
837 '<script>/* ERROR: Invalid script */</script>',
844 * @dataProvider provideInlineScript
845 * @covers Html::inlineScript
847 public function testInlineScript( $code, $expected, $error = false ) {
849 Wikimedia\
suppressWarnings();
850 $this->restoreWarnings
= true;
852 $this->assertSame( Html
::inlineScript( $code ), $expected );
856 class HtmlTestValue
{
857 function __toString() {
858 return 'stringValue';