2 /** tests for includes/Html.php */
4 class HtmlTest
extends MediaWikiTestCase
{
6 protected function setUp() {
10 $langObj = Language
::factory( $langCode );
12 // Hardcode namespaces during test runs,
13 // so that html output based on existing namespaces
14 // can be properly evaluated.
15 $langObj->setNamespaces( array(
27 9 => 'MediaWiki_talk',
29 11 => 'Template_talk',
31 15 => 'Category_talk',
36 $this->setMwGlobals( array(
37 'wgLanguageCode' => $langCode,
38 'wgContLang' => $langObj,
41 'wgWellFormedXml' => false,
45 public function testElementBasics() {
48 Html
::element( 'img', null, '' ),
49 'No close tag for short-tag elements'
53 '<element></element>',
54 Html
::element( 'element', null, null ),
55 'Close tag for empty element (null, null)'
59 '<element></element>',
60 Html
::element( 'element', array(), '' ),
61 'Close tag for empty element (array, string)'
64 $this->setMwGlobals( 'wgWellFormedXml', true );
68 Html
::element( 'img', null, '' ),
69 'Self-closing tag for short-tag elements (wgWellFormedXml = true)'
73 public function testExpandAttributesSkipsNullAndFalse() {
77 Html
::expandAttributes( array( 'foo' => null ) ),
78 'skip keys with null value'
81 Html
::expandAttributes( array( 'foo' => false ) ),
82 'skip keys with false value'
84 $this->assertNotEmpty(
85 Html
::expandAttributes( array( 'foo' => '' ) ),
86 'keep keys with an empty string'
90 public function testExpandAttributesForBooleans() {
93 Html
::expandAttributes( array( 'selected' => false ) ),
94 'Boolean attributes do not generates output when value is false'
98 Html
::expandAttributes( array( 'selected' => null ) ),
99 'Boolean attributes do not generates output when value is null'
104 Html
::expandAttributes( array( 'selected' => true ) ),
105 'Boolean attributes have no value when value is true'
109 Html
::expandAttributes( array( 'selected' ) ),
110 'Boolean attributes have no value when value is true (passed as numerical array)'
113 $this->setMwGlobals( 'wgWellFormedXml', true );
117 Html
::expandAttributes( array( 'selected' => true ) ),
118 'Boolean attributes have empty string value when value is true (wgWellFormedXml)'
121 $this->setMwGlobals( 'wgHtml5', false );
124 ' selected="selected"',
125 Html
::expandAttributes( array( 'selected' => true ) ),
126 'Boolean attributes have their key as value when value is true (wgWellFormedXml, wgHTML5 = false)'
131 * Test for Html::expandAttributes()
132 * Please note it output a string prefixed with a space!
134 public function testExpandAttributesVariousExpansions() {
138 Html
::expandAttributes( array( 'empty_string' => '' ) ),
139 'Empty string is always quoted'
143 Html
::expandAttributes( array( 'key' => 'value' ) ),
144 'Simple string value needs no quotes'
148 Html
::expandAttributes( array( 'one' => 1 ) ),
149 'Number 1 value needs no quotes'
153 Html
::expandAttributes( array( 'zero' => 0 ) ),
154 'Number 0 value needs no quotes'
157 $this->setMwGlobals( 'wgWellFormedXml', true );
161 Html
::expandAttributes( array( 'empty_string' => '' ) ),
162 'Attribute values are always quoted (wgWellFormedXml): Empty string'
166 Html
::expandAttributes( array( 'key' => 'value' ) ),
167 'Attribute values are always quoted (wgWellFormedXml): Simple string'
171 Html
::expandAttributes( array( 'one' => 1 ) ),
172 'Attribute values are always quoted (wgWellFormedXml): Number 1'
176 Html
::expandAttributes( array( 'zero' => 0 ) ),
177 'Attribute values are always quoted (wgWellFormedXml): Number 0'
182 * Html::expandAttributes has special features for HTML
183 * attributes that use space separated lists and also
184 * allows arrays to be used as values.
186 public function testExpandAttributesListValueAttributes() {
189 ' class="redundant spaces here"',
190 Html
::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
191 'Normalization should strip redundant spaces'
195 Html
::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
196 'Normalization should remove duplicates in string-lists'
198 ### "EMPTY" ARRAY VALUES
201 Html
::expandAttributes( array( 'class' => array() ) ),
202 'Value with an empty array'
206 Html
::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
207 'Array with null, empty string and spaces'
209 ### NON-EMPTY ARRAY VALUES
212 Html
::expandAttributes( array( 'class' => array(
219 'Normalization should remove duplicates in the array'
223 Html
::expandAttributes( array( 'class' => array(
229 'Normalization should remove duplicates in string-lists in the array'
234 * Test feature added by r96188, let pass attributes values as
235 * a PHP array. Restricted to class,rel, accesskey.
237 function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
239 ' class="booltrue one"',
240 Html
::expandAttributes( array( 'class' => array(
244 # Method use isset() internally, make sure we do discard
245 # attributes values which have been assigned well known values
247 'boolfalse' => false,
255 * How do we handle duplicate keys in HTML attributes expansion?
256 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
257 * The later will take precedence.
259 * Feature added by r96188
261 function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
264 Html
::expandAttributes( array( 'class' => array(
272 function testNamespaceSelector() {
274 '<select id=namespace name=namespace>' . "\n" .
275 '<option value=0>(Main)</option>' . "\n" .
276 '<option value=1>Talk</option>' . "\n" .
277 '<option value=2>User</option>' . "\n" .
278 '<option value=3>User talk</option>' . "\n" .
279 '<option value=4>MyWiki</option>' . "\n" .
280 '<option value=5>MyWiki Talk</option>' . "\n" .
281 '<option value=6>File</option>' . "\n" .
282 '<option value=7>File talk</option>' . "\n" .
283 '<option value=8>MediaWiki</option>' . "\n" .
284 '<option value=9>MediaWiki talk</option>' . "\n" .
285 '<option value=10>Template</option>' . "\n" .
286 '<option value=11>Template talk</option>' . "\n" .
287 '<option value=14>Category</option>' . "\n" .
288 '<option value=15>Category talk</option>' . "\n" .
289 '<option value=100>Custom</option>' . "\n" .
290 '<option value=101>Custom talk</option>' . "\n" .
292 Html
::namespaceSelector(),
293 'Basic namespace selector without custom options'
297 '<label for=mw-test-namespace>Select a namespace:</label> ' .
298 '<select id=mw-test-namespace name=wpNamespace>' . "\n" .
299 '<option value=all>all</option>' . "\n" .
300 '<option value=0>(Main)</option>' . "\n" .
301 '<option value=1>Talk</option>' . "\n" .
302 '<option value=2 selected>User</option>' . "\n" .
303 '<option value=3>User talk</option>' . "\n" .
304 '<option value=4>MyWiki</option>' . "\n" .
305 '<option value=5>MyWiki Talk</option>' . "\n" .
306 '<option value=6>File</option>' . "\n" .
307 '<option value=7>File talk</option>' . "\n" .
308 '<option value=8>MediaWiki</option>' . "\n" .
309 '<option value=9>MediaWiki talk</option>' . "\n" .
310 '<option value=10>Template</option>' . "\n" .
311 '<option value=11>Template talk</option>' . "\n" .
312 '<option value=14>Category</option>' . "\n" .
313 '<option value=15>Category talk</option>' . "\n" .
314 '<option value=100>Custom</option>' . "\n" .
315 '<option value=101>Custom talk</option>' . "\n" .
317 Html
::namespaceSelector(
318 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
319 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
321 'Basic namespace selector with custom values'
325 '<label for=namespace>Select a namespace:</label> ' .
326 '<select id=namespace name=namespace>' . "\n" .
327 '<option value=0>(Main)</option>' . "\n" .
328 '<option value=1>Talk</option>' . "\n" .
329 '<option value=2>User</option>' . "\n" .
330 '<option value=3>User talk</option>' . "\n" .
331 '<option value=4>MyWiki</option>' . "\n" .
332 '<option value=5>MyWiki Talk</option>' . "\n" .
333 '<option value=6>File</option>' . "\n" .
334 '<option value=7>File talk</option>' . "\n" .
335 '<option value=8>MediaWiki</option>' . "\n" .
336 '<option value=9>MediaWiki talk</option>' . "\n" .
337 '<option value=10>Template</option>' . "\n" .
338 '<option value=11>Template talk</option>' . "\n" .
339 '<option value=14>Category</option>' . "\n" .
340 '<option value=15>Category talk</option>' . "\n" .
341 '<option value=100>Custom</option>' . "\n" .
342 '<option value=101>Custom talk</option>' . "\n" .
344 Html
::namespaceSelector(
345 array( 'label' => 'Select a namespace:' )
347 'Basic namespace selector with a custom label but no id attribtue for the <select>'
351 function testCanFilterOutNamespaces() {
353 '<select id=namespace name=namespace>' . "\n" .
354 '<option value=2>User</option>' . "\n" .
355 '<option value=4>MyWiki</option>' . "\n" .
356 '<option value=5>MyWiki Talk</option>' . "\n" .
357 '<option value=6>File</option>' . "\n" .
358 '<option value=7>File talk</option>' . "\n" .
359 '<option value=8>MediaWiki</option>' . "\n" .
360 '<option value=9>MediaWiki talk</option>' . "\n" .
361 '<option value=10>Template</option>' . "\n" .
362 '<option value=11>Template talk</option>' . "\n" .
363 '<option value=14>Category</option>' . "\n" .
364 '<option value=15>Category talk</option>' . "\n" .
366 Html
::namespaceSelector(
367 array( 'exclude' => array( 0, 1, 3, 100, 101 ) )
369 'Namespace selector namespace filtering.'
373 function testCanDisableANamespaces() {
375 '<select id=namespace name=namespace>' . "\n" .
376 '<option disabled value=0>(Main)</option>' . "\n" .
377 '<option disabled value=1>Talk</option>' . "\n" .
378 '<option disabled value=2>User</option>' . "\n" .
379 '<option disabled value=3>User talk</option>' . "\n" .
380 '<option disabled value=4>MyWiki</option>' . "\n" .
381 '<option value=5>MyWiki Talk</option>' . "\n" .
382 '<option value=6>File</option>' . "\n" .
383 '<option value=7>File talk</option>' . "\n" .
384 '<option value=8>MediaWiki</option>' . "\n" .
385 '<option value=9>MediaWiki talk</option>' . "\n" .
386 '<option value=10>Template</option>' . "\n" .
387 '<option value=11>Template talk</option>' . "\n" .
388 '<option value=14>Category</option>' . "\n" .
389 '<option value=15>Category talk</option>' . "\n" .
390 '<option value=100>Custom</option>' . "\n" .
391 '<option value=101>Custom talk</option>' . "\n" .
393 Html
::namespaceSelector( array(
394 'disable' => array( 0, 1, 2, 3, 4 )
396 'Namespace selector namespace disabling'
401 * @dataProvider provideHtml5InputTypes
403 function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
405 '<input type=' . $HTML5InputType . '>',
406 Html
::element( 'input', array( 'type' => $HTML5InputType ) ),
407 'In HTML5, HTML::element() should accept type="' . $HTML5InputType . '"'
412 * List of input element types values introduced by HTML5
413 * Full list at http://www.w3.org/TR/html-markup/input.html
415 public static function provideHtml5InputTypes() {
432 foreach ( $types as $type ) {
433 $cases[] = array( $type );
439 * Test out Html::element drops or enforces default value
440 * @covers Html::dropDefaults
441 * @dataProvider provideElementsWithAttributesHavingDefaultValues
443 function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
444 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
447 public static function provideElementsWithAttributesHavingDefaultValues() {
448 # Use cases in a concise format:
449 # <expected>, <element name>, <array of attributes> [, <message>]
450 # Will be mapped to Html::element()
453 ### Generic cases, match $attribDefault static array
454 $cases[] = array( '<area>',
455 'area', array( 'shape' => 'rect' )
458 $cases[] = array( '<button type=submit></button>',
459 'button', array( 'formaction' => 'GET' )
461 $cases[] = array( '<button type=submit></button>',
462 'button', array( 'formenctype' => 'application/x-www-form-urlencoded' )
465 $cases[] = array( '<canvas></canvas>',
466 'canvas', array( 'height' => '150' )
468 $cases[] = array( '<canvas></canvas>',
469 'canvas', array( 'width' => '300' )
471 # Also check with numeric values
472 $cases[] = array( '<canvas></canvas>',
473 'canvas', array( 'height' => 150 )
475 $cases[] = array( '<canvas></canvas>',
476 'canvas', array( 'width' => 300 )
479 $cases[] = array( '<command>',
480 'command', array( 'type' => 'command' )
483 $cases[] = array( '<form></form>',
484 'form', array( 'action' => 'GET' )
486 $cases[] = array( '<form></form>',
487 'form', array( 'autocomplete' => 'on' )
489 $cases[] = array( '<form></form>',
490 'form', array( 'enctype' => 'application/x-www-form-urlencoded' )
493 $cases[] = array( '<input>',
494 'input', array( 'formaction' => 'GET' )
496 $cases[] = array( '<input>',
497 'input', array( 'type' => 'text' )
500 $cases[] = array( '<keygen>',
501 'keygen', array( 'keytype' => 'rsa' )
504 $cases[] = array( '<link>',
505 'link', array( 'media' => 'all' )
508 $cases[] = array( '<menu></menu>',
509 'menu', array( 'type' => 'list' )
512 $cases[] = array( '<script></script>',
513 'script', array( 'type' => 'text/javascript' )
516 $cases[] = array( '<style></style>',
517 'style', array( 'media' => 'all' )
519 $cases[] = array( '<style></style>',
520 'style', array( 'type' => 'text/css' )
523 $cases[] = array( '<textarea></textarea>',
524 'textarea', array( 'wrap' => 'soft' )
529 # <link type="text/css">
530 $cases[] = array( '<link>',
531 'link', array( 'type' => 'text/css' )
534 # <input> specific handling
535 $cases[] = array( '<input type=checkbox>',
536 'input', array( 'type' => 'checkbox', 'value' => 'on' ),
537 'Default value "on" is stripped of checkboxes',
539 $cases[] = array( '<input type=radio>',
540 'input', array( 'type' => 'radio', 'value' => 'on' ),
541 'Default value "on" is stripped of radio buttons',
543 $cases[] = array( '<input type=submit value=Submit>',
544 'input', array( 'type' => 'submit', 'value' => 'Submit' ),
545 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
547 $cases[] = array( '<input type=color>',
548 'input', array( 'type' => 'color', 'value' => '' ),
550 $cases[] = array( '<input type=range>',
551 'input', array( 'type' => 'range', 'value' => '' ),
554 # <button> specific handling
555 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
556 $cases[] = array( '<button type=submit></button>',
557 'button', array( 'type' => 'submit' ),
558 'According to standard the default type is "submit". Depending on compatibility mode IE might use "button", instead.',
561 # <select> specifc handling
562 $cases[] = array( '<select multiple></select>',
563 'select', array( 'size' => '4', 'multiple' => true ),
565 # .. with numeric value
566 $cases[] = array( '<select multiple></select>',
567 'select', array( 'size' => 4, 'multiple' => true ),
569 $cases[] = array( '<select></select>',
570 'select', array( 'size' => '1', 'multiple' => false ),
572 # .. with numeric value
573 $cases[] = array( '<select></select>',
574 'select', array( 'size' => 1, 'multiple' => false ),
577 # Passing an array as value
578 $cases[] = array( '<a class="css-class-one css-class-two"></a>',
579 'a', array( 'class' => array( 'css-class-one', 'css-class-two' ) ),
580 "dropDefaults accepts values given as an array"
583 # FIXME: doDropDefault should remove defaults given in an array
584 # Expected should be '<a></a>'
585 $cases[] = array( '<a class=""></a>',
586 'a', array( 'class' => array( '', '' ) ),
587 "dropDefaults accepts values given as an array"
590 # Craft the Html elements
592 foreach ( $cases as $case ) {
596 isset( $case[3] ) ?
$case[3] : ''
602 public function testFormValidationBlacklist() {
604 Html
::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 2 ) ),
605 'Blacklist form validation attributes.'
609 Html
::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 'any' ) ),
610 'Allow special case "step=any".'