* Tests introduced in r96305 work now
* (bug 30774) - mediawiki.html: Add support for numbers and booleans
--
* Removed unneeded value-attribute in one the tests
* Changed if-else intro a switch. (to avoid calling the typeof operator multiple times and making the code a bit more readable)
* Returns <div><img src="<"/></div>
*/
this.element = function( name, attrs, contents ) {
* Returns <div><img src="<"/></div>
*/
this.element = function( name, attrs, contents ) {
for ( var attrName in attrs ) {
for ( var attrName in attrs ) {
- s += ' ' + attrName + '="' + this.escape( attrs[attrName] ) + '"';
+ v = attrs[attrName];
+ // Convert name=true, to name=name
+ if ( v === true ) {
+ v = attrName;
+ // Skip name=false
+ } else if ( v === false ) {
+ continue;
+ }
+ s += ' ' + attrName + '="' + this.escape( v ) + '"';
}
if ( contents === undefined || contents === null ) {
// Self close tag
}
if ( contents === undefined || contents === null ) {
// Self close tag
}
// Regular open tag
s += '>';
}
// Regular open tag
s += '>';
- if ( typeof contents === 'string' ) {
- // Escaped
- s += this.escape( contents );
- } else if ( contents instanceof this.Raw ) {
- // Raw HTML inclusion
- s += contents.value;
- } else if ( contents instanceof this.Cdata ) {
- // CDATA
- if ( /<\/[a-zA-z]/.test( contents.value ) ) {
- throw new Error( 'mw.html.element: Illegal end tag found in CDATA' );
- }
- s += contents.value;
- } else {
- throw new Error( 'mw.html.element: Invalid type of contents' );
+ switch ( typeof contents ) {
+ case 'string':
+ // Escaped
+ s += this.escape( contents );
+ break;
+ case 'number':
+ case 'boolean':
+ // Convert to string
+ s += '' + contents;
+ break;
+ default:
+ if ( contents instanceof this.Raw ) {
+ // Raw HTML inclusion
+ s += contents.value;
+ } else if ( contents instanceof this.Cdata ) {
+ // CDATA
+ if ( /<\/[a-zA-z]/.test( contents.value ) ) {
+ throw new Error( 'mw.html.element: Illegal end tag found in CDATA' );
+ }
+ s += contents.value;
+ } else {
+ throw new Error( 'mw.html.element: Invalid type of contents' );
+ }
}
s += '</' + name + '>';
return s;
}
s += '</' + name + '>';
return s;
equal(
mw.html.element(
'option', {
equal(
mw.html.element(
'option', {
selected: true
}, 'Foo'
),
selected: true
}, 'Foo'
),
- '<option value="foo" selected="selected">Foo</option>',
+ '<option selected="selected">Foo</option>',
'Attributes may have boolean values. True copies the attribute name to the value.'
);
'Attributes may have boolean values. True copies the attribute name to the value.'
);