Index: trunk/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.test.js |
— | — | @@ -200,11 +200,10 @@ |
201 | 201 | equal( |
202 | 202 | mw.html.element( |
203 | 203 | 'option', { |
204 | | - value: 'foo', |
205 | 204 | selected: true |
206 | 205 | }, 'Foo' |
207 | 206 | ), |
208 | | - '<option value="foo" selected="selected">Foo</option>', |
| 207 | + '<option selected="selected">Foo</option>', |
209 | 208 | 'Attributes may have boolean values. True copies the attribute name to the value.' |
210 | 209 | ); |
211 | 210 | |
Index: trunk/phase3/resources/mediawiki/mediawiki.js |
— | — | @@ -1237,9 +1237,17 @@ |
1238 | 1238 | * Returns <div><img src="<"/></div> |
1239 | 1239 | */ |
1240 | 1240 | this.element = function( name, attrs, contents ) { |
1241 | | - var s = '<' + name; |
| 1241 | + var v, s = '<' + name; |
1242 | 1242 | for ( var attrName in attrs ) { |
1243 | | - s += ' ' + attrName + '="' + this.escape( attrs[attrName] ) + '"'; |
| 1243 | + v = attrs[attrName]; |
| 1244 | + // Convert name=true, to name=name |
| 1245 | + if ( v === true ) { |
| 1246 | + v = attrName; |
| 1247 | + // Skip name=false |
| 1248 | + } else if ( v === false ) { |
| 1249 | + continue; |
| 1250 | + } |
| 1251 | + s += ' ' + attrName + '="' + this.escape( v ) + '"'; |
1244 | 1252 | } |
1245 | 1253 | if ( contents === undefined || contents === null ) { |
1246 | 1254 | // Self close tag |
— | — | @@ -1248,20 +1256,29 @@ |
1249 | 1257 | } |
1250 | 1258 | // Regular open tag |
1251 | 1259 | s += '>'; |
1252 | | - if ( typeof contents === 'string' ) { |
1253 | | - // Escaped |
1254 | | - s += this.escape( contents ); |
1255 | | - } else if ( contents instanceof this.Raw ) { |
1256 | | - // Raw HTML inclusion |
1257 | | - s += contents.value; |
1258 | | - } else if ( contents instanceof this.Cdata ) { |
1259 | | - // CDATA |
1260 | | - if ( /<\/[a-zA-z]/.test( contents.value ) ) { |
1261 | | - throw new Error( 'mw.html.element: Illegal end tag found in CDATA' ); |
1262 | | - } |
1263 | | - s += contents.value; |
1264 | | - } else { |
1265 | | - throw new Error( 'mw.html.element: Invalid type of contents' ); |
| 1260 | + switch ( typeof contents ) { |
| 1261 | + case 'string': |
| 1262 | + // Escaped |
| 1263 | + s += this.escape( contents ); |
| 1264 | + break; |
| 1265 | + case 'number': |
| 1266 | + case 'boolean': |
| 1267 | + // Convert to string |
| 1268 | + s += '' + contents; |
| 1269 | + break; |
| 1270 | + default: |
| 1271 | + if ( contents instanceof this.Raw ) { |
| 1272 | + // Raw HTML inclusion |
| 1273 | + s += contents.value; |
| 1274 | + } else if ( contents instanceof this.Cdata ) { |
| 1275 | + // CDATA |
| 1276 | + if ( /<\/[a-zA-z]/.test( contents.value ) ) { |
| 1277 | + throw new Error( 'mw.html.element: Illegal end tag found in CDATA' ); |
| 1278 | + } |
| 1279 | + s += contents.value; |
| 1280 | + } else { |
| 1281 | + throw new Error( 'mw.html.element: Invalid type of contents' ); |
| 1282 | + } |
1266 | 1283 | } |
1267 | 1284 | s += '</' + name + '>'; |
1268 | 1285 | return s; |