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() {
46 global $wgWellFormedXml;
50 Html
::element( 'img', null, '' ),
51 'No close 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', array(), '' ),
63 'Close tag for empty element (array, string)'
66 $wgWellFormedXml = true;
70 Html
::element( 'img', null, '' ),
71 'Self-closing tag for short-tag elements (wgWellFormedXml = true)'
75 public function testExpandAttributesSkipsNullAndFalse() {
79 Html
::expandAttributes( array( 'foo' => null ) ),
80 'skip keys with null value'
83 Html
::expandAttributes( array( 'foo' => false ) ),
84 'skip keys with false value'
86 $this->assertNotEmpty(
87 Html
::expandAttributes( array( 'foo' => '' ) ),
88 'keep keys with an empty string'
92 public function testExpandAttributesForBooleans() {
93 global $wgHtml5, $wgWellFormedXml;
97 Html
::expandAttributes( array( 'selected' => false ) ),
98 'Boolean attributes do not generates output when value is false'
102 Html
::expandAttributes( array( 'selected' => null ) ),
103 'Boolean attributes do not generates output when value is null'
108 Html
::expandAttributes( array( 'selected' => true ) ),
109 'Boolean attributes have no value when value is true'
113 Html
::expandAttributes( array( 'selected' ) ),
114 'Boolean attributes have no value when value is true (passed as numerical array)'
117 $wgWellFormedXml = true;
121 Html
::expandAttributes( array( 'selected' => true ) ),
122 'Boolean attributes have empty string value when value is true (wgWellFormedXml)'
128 ' selected="selected"',
129 Html
::expandAttributes( array( 'selected' => true ) ),
130 'Boolean attributes have their key as value when value is true (wgWellFormedXml, wgHTML5 = false)'
135 * Test for Html::expandAttributes()
136 * Please note it output a string prefixed with a space!
138 public function testExpandAttributesVariousExpansions() {
139 global $wgWellFormedXml;
144 Html
::expandAttributes( array( 'empty_string' => '' ) ),
145 'Empty string is always quoted'
149 Html
::expandAttributes( array( 'key' => 'value' ) ),
150 'Simple string value needs no quotes'
154 Html
::expandAttributes( array( 'one' => 1 ) ),
155 'Number 1 value needs no quotes'
159 Html
::expandAttributes( array( 'zero' => 0 ) ),
160 'Number 0 value needs no quotes'
163 $wgWellFormedXml = true;
167 Html
::expandAttributes( array( 'empty_string' => '' ) ),
168 'Attribtue values are always quoted (wgWellFormedXml): Empty string'
172 Html
::expandAttributes( array( 'key' => 'value' ) ),
173 'Attribtue values are always quoted (wgWellFormedXml): Simple string'
177 Html
::expandAttributes( array( 'one' => 1 ) ),
178 'Attribtue values are always quoted (wgWellFormedXml): Number 1'
182 Html
::expandAttributes( array( 'zero' => 0 ) ),
183 'Attribtue values are always quoted (wgWellFormedXml): Number 0'
188 * Html::expandAttributes has special features for HTML
189 * attributes that use space separated lists and also
190 * allows arrays to be used as values.
192 public function testExpandAttributesListValueAttributes() {
195 ' class="redundant spaces here"',
196 Html
::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
197 'Normalization should strip redundant spaces'
201 Html
::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
202 'Normalization should remove duplicates in string-lists'
204 ### "EMPTY" ARRAY VALUES
207 Html
::expandAttributes( array( 'class' => array() ) ),
208 'Value with an empty array'
212 Html
::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
213 'Array with null, empty string and spaces'
215 ### NON-EMPTY ARRAY VALUES
218 Html
::expandAttributes( array( 'class' => array(
225 'Normalization should remove duplicates in the array'
229 Html
::expandAttributes( array( 'class' => array(
235 'Normalization should remove duplicates in string-lists in the array'
240 * Test feature added by r96188, let pass attributes values as
241 * a PHP array. Restricted to class,rel, accesskey.
243 function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
245 ' class="booltrue one"',
246 Html
::expandAttributes( array( 'class' => array(
250 # Method use isset() internally, make sure we do discard
251 # attributes values which have been assigned well known values
253 'boolfalse' => false,
261 * How do we handle duplicate keys in HTML attributes expansion?
262 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
263 * The later will take precedence.
265 * Feature added by r96188
267 function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
270 Html
::expandAttributes( array( 'class' => array(
278 function testNamespaceSelector() {
280 '<select id=namespace name=namespace>' . "\n" .
281 '<option value=0>(Main)</option>' . "\n" .
282 '<option value=1>Talk</option>' . "\n" .
283 '<option value=2>User</option>' . "\n" .
284 '<option value=3>User talk</option>' . "\n" .
285 '<option value=4>MyWiki</option>' . "\n" .
286 '<option value=5>MyWiki Talk</option>' . "\n" .
287 '<option value=6>File</option>' . "\n" .
288 '<option value=7>File talk</option>' . "\n" .
289 '<option value=8>MediaWiki</option>' . "\n" .
290 '<option value=9>MediaWiki talk</option>' . "\n" .
291 '<option value=10>Template</option>' . "\n" .
292 '<option value=11>Template talk</option>' . "\n" .
293 '<option value=14>Category</option>' . "\n" .
294 '<option value=15>Category talk</option>' . "\n" .
295 '<option value=100>Custom</option>' . "\n" .
296 '<option value=101>Custom talk</option>' . "\n" .
298 Html
::namespaceSelector(),
299 'Basic namespace selector without custom options'
303 '<label for=mw-test-namespace>Select a namespace:</label> ' .
304 '<select id=mw-test-namespace name=wpNamespace>' . "\n" .
305 '<option value=all>all</option>' . "\n" .
306 '<option value=0>(Main)</option>' . "\n" .
307 '<option value=1>Talk</option>' . "\n" .
308 '<option value=2 selected>User</option>' . "\n" .
309 '<option value=3>User talk</option>' . "\n" .
310 '<option value=4>MyWiki</option>' . "\n" .
311 '<option value=5>MyWiki Talk</option>' . "\n" .
312 '<option value=6>File</option>' . "\n" .
313 '<option value=7>File talk</option>' . "\n" .
314 '<option value=8>MediaWiki</option>' . "\n" .
315 '<option value=9>MediaWiki talk</option>' . "\n" .
316 '<option value=10>Template</option>' . "\n" .
317 '<option value=11>Template talk</option>' . "\n" .
318 '<option value=14>Category</option>' . "\n" .
319 '<option value=15>Category talk</option>' . "\n" .
320 '<option value=100>Custom</option>' . "\n" .
321 '<option value=101>Custom talk</option>' . "\n" .
323 Html
::namespaceSelector(
324 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
325 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
327 'Basic namespace selector with custom values'
331 '<label for=namespace>Select a namespace:</label> ' .
332 '<select id=namespace name=namespace>' . "\n" .
333 '<option value=0>(Main)</option>' . "\n" .
334 '<option value=1>Talk</option>' . "\n" .
335 '<option value=2>User</option>' . "\n" .
336 '<option value=3>User talk</option>' . "\n" .
337 '<option value=4>MyWiki</option>' . "\n" .
338 '<option value=5>MyWiki Talk</option>' . "\n" .
339 '<option value=6>File</option>' . "\n" .
340 '<option value=7>File talk</option>' . "\n" .
341 '<option value=8>MediaWiki</option>' . "\n" .
342 '<option value=9>MediaWiki talk</option>' . "\n" .
343 '<option value=10>Template</option>' . "\n" .
344 '<option value=11>Template talk</option>' . "\n" .
345 '<option value=14>Category</option>' . "\n" .
346 '<option value=15>Category talk</option>' . "\n" .
347 '<option value=100>Custom</option>' . "\n" .
348 '<option value=101>Custom talk</option>' . "\n" .
350 Html
::namespaceSelector(
351 array( 'label' => 'Select a namespace:' )
353 'Basic namespace selector with a custom label but no id attribtue for the <select>'
357 function testCanFilterOutNamespaces() {
359 '<select id=namespace name=namespace>' . "\n" .
360 '<option value=2>User</option>' . "\n" .
361 '<option value=4>MyWiki</option>' . "\n" .
362 '<option value=5>MyWiki Talk</option>' . "\n" .
363 '<option value=6>File</option>' . "\n" .
364 '<option value=7>File talk</option>' . "\n" .
365 '<option value=8>MediaWiki</option>' . "\n" .
366 '<option value=9>MediaWiki talk</option>' . "\n" .
367 '<option value=10>Template</option>' . "\n" .
368 '<option value=11>Template talk</option>' . "\n" .
369 '<option value=14>Category</option>' . "\n" .
370 '<option value=15>Category talk</option>' . "\n" .
372 Html
::namespaceSelector(
373 array( 'exclude' => array( 0, 1, 3, 100, 101 ) )
375 'Namespace selector namespace filtering.'
379 function testCanDisableANamespaces() {
381 '<select id=namespace name=namespace>' . "\n" .
382 '<option disabled value=0>(Main)</option>' . "\n" .
383 '<option disabled value=1>Talk</option>' . "\n" .
384 '<option disabled value=2>User</option>' . "\n" .
385 '<option disabled value=3>User talk</option>' . "\n" .
386 '<option disabled value=4>MyWiki</option>' . "\n" .
387 '<option value=5>MyWiki Talk</option>' . "\n" .
388 '<option value=6>File</option>' . "\n" .
389 '<option value=7>File talk</option>' . "\n" .
390 '<option value=8>MediaWiki</option>' . "\n" .
391 '<option value=9>MediaWiki talk</option>' . "\n" .
392 '<option value=10>Template</option>' . "\n" .
393 '<option value=11>Template talk</option>' . "\n" .
394 '<option value=14>Category</option>' . "\n" .
395 '<option value=15>Category talk</option>' . "\n" .
396 '<option value=100>Custom</option>' . "\n" .
397 '<option value=101>Custom talk</option>' . "\n" .
399 Html
::namespaceSelector( array(
400 'disable' => array( 0, 1, 2, 3, 4 )
402 'Namespace selector namespace disabling'
407 * @dataProvider provideHtml5InputTypes
409 function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
411 '<input type=' . $HTML5InputType . '>',
412 Html
::element( 'input', array( 'type' => $HTML5InputType ) ),
413 'In HTML5, HTML::element() should accept type="' . $HTML5InputType . '"'
418 * List of input element types values introduced by HTML5
419 * Full list at http://www.w3.org/TR/html-markup/input.html
421 function provideHtml5InputTypes() {
438 foreach( $types as $type ) {
439 $cases[] = array( $type );
445 * Test out Html::element drops default value
446 * @cover Html::dropDefaults
447 * @dataProvider provideElementsWithAttributesHavingDefaultValues
449 function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
450 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
453 public static function provideElementsWithAttributesHavingDefaultValues() {
454 # Use cases in a concise format:
455 # <expected>, <element name>, <array of attributes> [, <message>]
456 # Will be mapped to Html::element()
459 ### Generic cases, match $attribDefault static array
460 $cases[] = array( '<area>',
461 'area', array( 'shape' => 'rect' )
464 $cases[] = array( '<button></button>',
465 'button', array( 'formaction' => 'GET' )
467 $cases[] = array( '<button></button>',
468 'button', array( 'formenctype' => 'application/x-www-form-urlencoded' )
470 $cases[] = array( '<button></button>',
471 'button', array( 'type' => 'submit' )
474 $cases[] = array( '<canvas></canvas>',
475 'canvas', array( 'height' => '150' )
477 $cases[] = array( '<canvas></canvas>',
478 'canvas', array( 'width' => '300' )
480 # Also check with numeric values
481 $cases[] = array( '<canvas></canvas>',
482 'canvas', array( 'height' => 150 )
484 $cases[] = array( '<canvas></canvas>',
485 'canvas', array( 'width' => 300 )
488 $cases[] = array( '<command>',
489 'command', array( 'type' => 'command' )
492 $cases[] = array( '<form></form>',
493 'form', array( 'action' => 'GET' )
495 $cases[] = array( '<form></form>',
496 'form', array( 'autocomplete' => 'on' )
498 $cases[] = array( '<form></form>',
499 'form', array( 'enctype' => 'application/x-www-form-urlencoded' )
502 $cases[] = array( '<input>',
503 'input', array( 'formaction' => 'GET' )
505 $cases[] = array( '<input>',
506 'input', array( 'type' => 'text' )
509 $cases[] = array( '<keygen>',
510 'keygen', array( 'keytype' => 'rsa' )
513 $cases[] = array( '<link>',
514 'link', array( 'media' => 'all' )
517 $cases[] = array( '<menu></menu>',
518 'menu', array( 'type' => 'list' )
521 $cases[] = array( '<script></script>',
522 'script', array( 'type' => 'text/javascript' )
525 $cases[] = array( '<style></style>',
526 'style', array( 'media' => 'all' )
528 $cases[] = array( '<style></style>',
529 'style', array( 'type' => 'text/css' )
532 $cases[] = array( '<textarea></textarea>',
533 'textarea', array( 'wrap' => 'soft' )
538 # <link type="text/css">
539 $cases[] = array( '<link>',
540 'link', array( 'type' => 'text/css' )
543 # <input> specific handling
544 $cases[] = array( '<input type=checkbox>',
545 'input', array( 'type' => 'checkbox', 'value' => 'on' ),
546 'Default value "on" is stripped of checkboxes',
548 $cases[] = array( '<input type=radio>',
549 'input', array( 'type' => 'radio', 'value' => 'on' ),
550 'Default value "on" is stripped of radio buttons',
552 $cases[] = array( '<input type=submit value=Submit>',
553 'input', array( 'type' => 'submit', 'value' => 'Submit' ),
554 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
556 $cases[] = array( '<input type=color>',
557 'input', array( 'type' => 'color', 'value' => '' ),
559 $cases[] = array( '<input type=range>',
560 'input', array( 'type' => 'range', 'value' => '' ),
563 # <select> specifc handling
564 $cases[] = array( '<select multiple></select>',
565 'select', array( 'size' => '4', 'multiple' => true ),
567 # .. with numeric value
568 $cases[] = array( '<select multiple></select>',
569 'select', array( 'size' => 4, 'multiple' => true ),
571 $cases[] = array( '<select></select>',
572 'select', array( 'size' => '1', 'multiple' => false ),
574 # .. with numeric value
575 $cases[] = array( '<select></select>',
576 'select', array( 'size' => 1, 'multiple' => false ),
579 # Passing an array as value
580 $cases[] = array( '<a class="css-class-one css-class-two"></a>',
581 'a', array( 'class' => array( 'css-class-one', 'css-class-two' ) ),
582 "dropDefaults accepts values given as an array"
585 # FIXME: doDropDefault should remove defaults given in an array
586 # Expected should be '<a></a>'
587 $cases[] = array( '<a class=""></a>',
588 'a', array( 'class' => array( '', '' ) ),
589 "dropDefaults accepts values given as an array"
592 # Craft the Html elements
594 foreach( $cases as $case ) {
598 isset( $case[3] ) ?
$case[3] : ''