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 Language
::factory( 'en' )->resetNamespaces();
46 if ( $this->restoreWarnings
) {
47 $this->restoreWarnings
= false;
48 Wikimedia\restoreWarnings
();
55 * @covers Html::element
56 * @covers Html::rawElement
57 * @covers Html::openElement
58 * @covers Html::closeElement
60 public function testElementBasics() {
63 Html
::element( 'img', null, '' ),
64 'Self-closing tag for short-tag elements'
68 '<element></element>',
69 Html
::element( 'element', null, null ),
70 'Close tag for empty element (null, null)'
74 '<element></element>',
75 Html
::element( 'element', [], '' ),
76 'Close tag for empty element (array, string)'
80 public function dataXmlMimeType() {
82 // ( $mimetype, $isXmlMimeType )
83 # HTML is not an XML MimeType
84 [ 'text/html', false ],
85 # XML is an XML MimeType
87 [ 'application/xml', true ],
88 # XHTML is an XML MimeType
89 [ 'application/xhtml+xml', true ],
90 # Make sure other +xml MimeTypes are supported
91 # SVG is another random MimeType even though we don't use it
92 [ 'image/svg+xml', true ],
93 # Complete random other MimeTypes are not XML
94 [ 'text/plain', false ],
99 * @dataProvider dataXmlMimeType
100 * @covers Html::isXmlMimeType
102 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
103 $this->assertEquals( $isXmlMimeType, Html
::isXmlMimeType( $mimetype ) );
107 * @covers Html::expandAttributes
109 public function testExpandAttributesSkipsNullAndFalse() {
112 Html
::expandAttributes( [ 'foo' => null ] ),
113 'skip keys with null value'
116 Html
::expandAttributes( [ 'foo' => false ] ),
117 'skip keys with false value'
121 Html
::expandAttributes( [ 'foo' => '' ] ),
122 'keep keys with an empty string'
127 * @covers Html::expandAttributes
129 public function testExpandAttributesForBooleans() {
132 Html
::expandAttributes( [ 'selected' => false ] ),
133 'Boolean attributes do not generates output when value is false'
137 Html
::expandAttributes( [ 'selected' => null ] ),
138 'Boolean attributes do not generates output when value is null'
143 Html
::expandAttributes( [ 'selected' => true ] ),
144 'Boolean attributes have no value when value is true'
148 Html
::expandAttributes( [ 'selected' ] ),
149 'Boolean attributes have no value when value is true (passed as numerical array)'
154 * @covers Html::expandAttributes
156 public function testExpandAttributesForNumbers() {
159 Html
::expandAttributes( [ 'value' => 1 ] ),
160 'Integer value is cast to a string'
164 Html
::expandAttributes( [ 'value' => 1.1 ] ),
165 'Float value is cast to a string'
170 * @covers Html::expandAttributes
172 public function testExpandAttributesForObjects() {
174 ' value="stringValue"',
175 Html
::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
176 'Object value is converted to a string'
181 * Test for Html::expandAttributes()
182 * Please note it output a string prefixed with a space!
183 * @covers Html::expandAttributes
185 public function testExpandAttributesVariousExpansions() {
189 Html
::expandAttributes( [ 'empty_string' => '' ] ),
190 'Empty string is always quoted'
194 Html
::expandAttributes( [ 'key' => 'value' ] ),
195 'Simple string value needs no quotes'
199 Html
::expandAttributes( [ 'one' => 1 ] ),
200 'Number 1 value needs no quotes'
204 Html
::expandAttributes( [ 'zero' => 0 ] ),
205 'Number 0 value needs no quotes'
210 * Html::expandAttributes has special features for HTML
211 * attributes that use space separated lists and also
212 * allows arrays to be used as values.
213 * @covers Html::expandAttributes
215 public function testExpandAttributesListValueAttributes() {
218 ' class="redundant spaces here"',
219 Html
::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
220 'Normalization should strip redundant spaces'
224 Html
::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
225 'Normalization should remove duplicates in string-lists'
227 # ## "EMPTY" ARRAY VALUES
230 Html
::expandAttributes( [ 'class' => [] ] ),
231 'Value with an empty array'
235 Html
::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
236 'Array with null, empty string and spaces'
238 # ## NON-EMPTY ARRAY VALUES
241 Html
::expandAttributes( [ 'class' => [
248 'Normalization should remove duplicates in the array'
252 Html
::expandAttributes( [ 'class' => [
258 'Normalization should remove duplicates in string-lists in the array'
263 * Test feature added by r96188, let pass attributes values as
264 * a PHP array. Restricted to class,rel, accesskey.
265 * @covers Html::expandAttributes
267 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
269 ' class="booltrue one"',
270 Html
::expandAttributes( [ 'class' => [
274 # Method use isset() internally, make sure we do discard
275 # attributes values which have been assigned well known values
277 'boolfalse' => false,
285 * How do we handle duplicate keys in HTML attributes expansion?
286 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
287 * The latter will take precedence.
289 * Feature added by r96188
290 * @covers Html::expandAttributes
292 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
295 Html
::expandAttributes( [ 'class' => [
304 * @covers Html::expandAttributes
305 * @expectedException MWException
307 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
308 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
309 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
310 Html
::expandAttributes( [
319 * @covers Html::namespaceSelector
320 * @covers Html::namespaceSelectorOptions
322 public function testNamespaceSelector() {
324 '<select id="namespace" name="namespace">' . "\n" .
325 '<option value="0">(Main)</option>' . "\n" .
326 '<option value="1">Talk</option>' . "\n" .
327 '<option value="2">User</option>' . "\n" .
328 '<option value="3">User talk</option>' . "\n" .
329 '<option value="4">MyWiki</option>' . "\n" .
330 '<option value="5">MyWiki Talk</option>' . "\n" .
331 '<option value="6">File</option>' . "\n" .
332 '<option value="7">File talk</option>' . "\n" .
333 '<option value="8">MediaWiki</option>' . "\n" .
334 '<option value="9">MediaWiki talk</option>' . "\n" .
335 '<option value="10">Template</option>' . "\n" .
336 '<option value="11">Template talk</option>' . "\n" .
337 '<option value="14">Category</option>' . "\n" .
338 '<option value="15">Category talk</option>' . "\n" .
339 '<option value="100">Custom</option>' . "\n" .
340 '<option value="101">Custom talk</option>' . "\n" .
342 Html
::namespaceSelector(),
343 'Basic namespace selector without custom options'
347 '<label for="mw-test-namespace">Select a namespace:</label>' . "\u{00A0}" .
348 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
349 '<option value="all">all</option>' . "\n" .
350 '<option value="0">(Main)</option>' . "\n" .
351 '<option value="1">Talk</option>' . "\n" .
352 '<option value="2" selected="">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 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
369 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
371 'Basic namespace selector with custom values'
375 '<label for="namespace">Select a namespace:</label>' . "\u{00A0}" .
376 '<select id="namespace" name="namespace">' . "\n" .
377 '<option value="0">(Main)</option>' . "\n" .
378 '<option value="1">Talk</option>' . "\n" .
379 '<option value="2">User</option>' . "\n" .
380 '<option value="3">User talk</option>' . "\n" .
381 '<option value="4">MyWiki</option>' . "\n" .
382 '<option value="5">MyWiki Talk</option>' . "\n" .
383 '<option value="6">File</option>' . "\n" .
384 '<option value="7">File talk</option>' . "\n" .
385 '<option value="8">MediaWiki</option>' . "\n" .
386 '<option value="9">MediaWiki talk</option>' . "\n" .
387 '<option value="10">Template</option>' . "\n" .
388 '<option value="11">Template talk</option>' . "\n" .
389 '<option value="14">Category</option>' . "\n" .
390 '<option value="15">Category talk</option>' . "\n" .
391 '<option value="100">Custom</option>' . "\n" .
392 '<option value="101">Custom talk</option>' . "\n" .
394 Html
::namespaceSelector(
395 [ 'label' => 'Select a namespace:' ]
397 'Basic namespace selector with a custom label but no id attribtue for the <select>'
402 * @covers Html::namespaceSelector
404 public function testCanFilterOutNamespaces() {
406 '<select id="namespace" name="namespace">' . "\n" .
407 '<option value="2">User</option>' . "\n" .
408 '<option value="4">MyWiki</option>' . "\n" .
409 '<option value="5">MyWiki Talk</option>' . "\n" .
410 '<option value="6">File</option>' . "\n" .
411 '<option value="7">File talk</option>' . "\n" .
412 '<option value="8">MediaWiki</option>' . "\n" .
413 '<option value="9">MediaWiki talk</option>' . "\n" .
414 '<option value="10">Template</option>' . "\n" .
415 '<option value="11">Template talk</option>' . "\n" .
416 '<option value="14">Category</option>' . "\n" .
417 '<option value="15">Category talk</option>' . "\n" .
419 Html
::namespaceSelector(
420 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
422 'Namespace selector namespace filtering.'
427 * @covers Html::namespaceSelector
429 public function testCanDisableANamespaces() {
431 '<select id="namespace" name="namespace">' . "\n" .
432 '<option disabled="" value="0">(Main)</option>' . "\n" .
433 '<option disabled="" value="1">Talk</option>' . "\n" .
434 '<option disabled="" value="2">User</option>' . "\n" .
435 '<option disabled="" value="3">User talk</option>' . "\n" .
436 '<option disabled="" value="4">MyWiki</option>' . "\n" .
437 '<option value="5">MyWiki Talk</option>' . "\n" .
438 '<option value="6">File</option>' . "\n" .
439 '<option value="7">File talk</option>' . "\n" .
440 '<option value="8">MediaWiki</option>' . "\n" .
441 '<option value="9">MediaWiki talk</option>' . "\n" .
442 '<option value="10">Template</option>' . "\n" .
443 '<option value="11">Template talk</option>' . "\n" .
444 '<option value="14">Category</option>' . "\n" .
445 '<option value="15">Category talk</option>' . "\n" .
446 '<option value="100">Custom</option>' . "\n" .
447 '<option value="101">Custom talk</option>' . "\n" .
449 Html
::namespaceSelector( [
450 'disable' => [ 0, 1, 2, 3, 4 ]
452 'Namespace selector namespace disabling'
457 * @dataProvider provideHtml5InputTypes
458 * @covers Html::element
460 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
462 '<input type="' . $HTML5InputType . '"/>',
463 Html
::element( 'input', [ 'type' => $HTML5InputType ] ),
464 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
469 * @covers Html::warningBox
470 * @covers Html::messageBox
472 public function testWarningBox() {
474 Html
::warningBox( 'warn' ),
475 '<div class="warningbox">warn</div>'
480 * @covers Html::errorBox
481 * @covers Html::messageBox
483 public function testErrorBox() {
485 Html
::errorBox( 'err' ),
486 '<div class="errorbox">err</div>'
489 Html
::errorBox( 'err', 'heading' ),
490 '<div class="errorbox"><h2>heading</h2>err</div>'
493 Html
::errorBox( 'err', '0' ),
494 '<div class="errorbox"><h2>0</h2>err</div>'
499 * @covers Html::successBox
500 * @covers Html::messageBox
502 public function testSuccessBox() {
504 Html
::successBox( 'great' ),
505 '<div class="successbox">great</div>'
508 Html
::successBox( '<script>beware no escaping!</script>' ),
509 '<div class="successbox"><script>beware no escaping!</script></div>'
514 * List of input element types values introduced by HTML5
515 * Full list at https://www.w3.org/TR/html-markup/input.html
517 public static function provideHtml5InputTypes() {
534 foreach ( $types as $type ) {
535 $cases[] = [ $type ];
542 * Test out Html::element drops or enforces default value
543 * @covers Html::dropDefaults
544 * @dataProvider provideElementsWithAttributesHavingDefaultValues
546 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
547 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
550 public static function provideElementsWithAttributesHavingDefaultValues() {
551 # Use cases in a concise format:
552 # <expected>, <element name>, <array of attributes> [, <message>]
553 # Will be mapped to Html::element()
556 # ## Generic cases, match $attribDefault static array
557 $cases[] = [ '<area/>',
558 'area', [ 'shape' => 'rect' ]
561 $cases[] = [ '<button type="submit"></button>',
562 'button', [ 'formaction' => 'GET' ]
564 $cases[] = [ '<button type="submit"></button>',
565 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
568 $cases[] = [ '<canvas></canvas>',
569 'canvas', [ 'height' => '150' ]
571 $cases[] = [ '<canvas></canvas>',
572 'canvas', [ 'width' => '300' ]
574 # Also check with numeric values
575 $cases[] = [ '<canvas></canvas>',
576 'canvas', [ 'height' => 150 ]
578 $cases[] = [ '<canvas></canvas>',
579 'canvas', [ 'width' => 300 ]
582 $cases[] = [ '<form></form>',
583 'form', [ 'action' => 'GET' ]
585 $cases[] = [ '<form></form>',
586 'form', [ 'autocomplete' => 'on' ]
588 $cases[] = [ '<form></form>',
589 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
592 $cases[] = [ '<input/>',
593 'input', [ 'formaction' => 'GET' ]
595 $cases[] = [ '<input/>',
596 'input', [ 'type' => 'text' ]
599 $cases[] = [ '<keygen/>',
600 'keygen', [ 'keytype' => 'rsa' ]
603 $cases[] = [ '<link/>',
604 'link', [ 'media' => 'all' ]
607 $cases[] = [ '<menu></menu>',
608 'menu', [ 'type' => 'list' ]
611 $cases[] = [ '<script></script>',
612 'script', [ 'type' => 'text/javascript' ]
615 $cases[] = [ '<style></style>',
616 'style', [ 'media' => 'all' ]
618 $cases[] = [ '<style></style>',
619 'style', [ 'type' => 'text/css' ]
622 $cases[] = [ '<textarea></textarea>',
623 'textarea', [ 'wrap' => 'soft' ]
628 # <link type="text/css">
629 $cases[] = [ '<link/>',
630 'link', [ 'type' => 'text/css' ]
633 # <input> specific handling
634 $cases[] = [ '<input type="checkbox"/>',
635 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
636 'Default value "on" is stripped of checkboxes',
638 $cases[] = [ '<input type="radio"/>',
639 'input', [ 'type' => 'radio', 'value' => 'on' ],
640 'Default value "on" is stripped of radio buttons',
642 $cases[] = [ '<input type="submit" value="Submit"/>',
643 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
644 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
646 $cases[] = [ '<input type="color"/>',
647 'input', [ 'type' => 'color', 'value' => '' ],
649 $cases[] = [ '<input type="range"/>',
650 'input', [ 'type' => 'range', 'value' => '' ],
653 # <button> specific handling
654 # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
655 $cases[] = [ '<button type="submit"></button>',
656 'button', [ 'type' => 'submit' ],
657 'According to standard the default type is "submit". '
658 . 'Depending on compatibility mode IE might use "button", instead.',
661 # <select> specific handling
662 $cases[] = [ '<select multiple=""></select>',
663 'select', [ 'size' => '4', 'multiple' => true ],
665 # .. with numeric value
666 $cases[] = [ '<select multiple=""></select>',
667 'select', [ 'size' => 4, 'multiple' => true ],
669 $cases[] = [ '<select></select>',
670 'select', [ 'size' => '1', 'multiple' => false ],
672 # .. with numeric value
673 $cases[] = [ '<select></select>',
674 'select', [ 'size' => 1, 'multiple' => false ],
677 # Passing an array as value
678 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
679 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
680 "dropDefaults accepts values given as an array"
683 # FIXME: doDropDefault should remove defaults given in an array
684 # Expected should be '<a></a>'
685 $cases[] = [ '<a class=""></a>',
686 'a', [ 'class' => [ '', '' ] ],
687 "dropDefaults accepts values given as an array"
690 # Craft the Html elements
692 foreach ( $cases as $case ) {
704 * @covers Html::input
706 public function testWrapperInput() {
708 '<input type="radio" value="testval" name="testname"/>',
709 Html
::input( 'testname', 'testval', 'radio' ),
710 'Input wrapper with type and value.'
713 '<input name="testname"/>',
714 Html
::input( 'testname' ),
715 'Input wrapper with all default values.'
720 * @covers Html::check
722 public function testWrapperCheck() {
724 '<input type="checkbox" value="1" name="testname"/>',
725 Html
::check( 'testname' ),
726 'Checkbox wrapper unchecked.'
729 '<input checked="" type="checkbox" value="1" name="testname"/>',
730 Html
::check( 'testname', true ),
731 'Checkbox wrapper checked.'
734 '<input type="checkbox" value="testval" name="testname"/>',
735 Html
::check( 'testname', false, [ 'value' => 'testval' ] ),
736 'Checkbox wrapper with a value override.'
741 * @covers Html::radio
743 public function testWrapperRadio() {
745 '<input type="radio" value="1" name="testname"/>',
746 Html
::radio( 'testname' ),
747 'Radio wrapper unchecked.'
750 '<input checked="" type="radio" value="1" name="testname"/>',
751 Html
::radio( 'testname', true ),
752 'Radio wrapper checked.'
755 '<input type="radio" value="testval" name="testname"/>',
756 Html
::radio( 'testname', false, [ 'value' => 'testval' ] ),
757 'Radio wrapper with a value override.'
762 * @covers Html::label
764 public function testWrapperLabel() {
766 '<label for="testid">testlabel</label>',
767 Html
::label( 'testlabel', 'testid' ),
772 public static function provideSrcSetImages() {
774 [ [], '', 'when there are no images, return empty string' ],
776 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
777 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
778 'pixel depth keys may include a trailing "x"'
781 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
782 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
783 'pixel depth keys may omit a trailing "x"'
786 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
787 'small.png 1x, large.png 1.5x',
788 'omit larger duplicates'
791 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
792 'small.png 1x, large.png 1.5x',
793 'omit larger duplicates in irregular order'
799 * @dataProvider provideSrcSetImages
800 * @covers Html::srcSet
802 public function testSrcSet( $images, $expected, $message ) {
803 $this->assertEquals( Html
::srcSet( $images ), $expected, $message );
806 public static function provideInlineScript() {
813 'EXAMPLE.label("foo");',
814 '<script>EXAMPLE.label("foo");</script>'
817 'EXAMPLE.is(a && b);',
818 '<script>EXAMPLE.is(a && b);</script>'
821 'EXAMPLE.label("<a>");',
822 '<script>EXAMPLE.label("<a>");</script>'
824 'Script closing string (lower)' => [
825 'EXAMPLE.label("</script>");',
826 '<script>/* ERROR: Invalid script */</script>',
829 'Script closing with non-standard attributes (mixed)' => [
830 'EXAMPLE.label("</SCriPT and STyLE>");',
831 '<script>/* ERROR: Invalid script */</script>',
834 'HTML-comment-open and script-open' => [
835 // In HTML, <script> contents aren't just plain CDATA until </script>,
836 // there are levels of escaping modes, and the below sequence puts an
837 // HTML parser in a state where </script> would *not* close the script.
838 // https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escape-end-state
839 'var a = "<!--<script>";',
840 '<script>/* ERROR: Invalid script */</script>',
847 * @dataProvider provideInlineScript
848 * @covers Html::inlineScript
850 public function testInlineScript( $code, $expected, $error = false ) {
852 Wikimedia\
suppressWarnings();
853 $this->restoreWarnings
= true;
855 $this->assertSame( Html
::inlineScript( $code ), $expected );
859 class HtmlTestValue
{
860 function __toString() {
861 return 'stringValue';