mediawiki.html: mediawiki.html: Add support for numbers and booleans
authorKrinkle <krinkle@users.mediawiki.org>
Mon, 5 Sep 2011 21:51:04 +0000 (21:51 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Mon, 5 Sep 2011 21:51:04 +0000 (21:51 +0000)
* 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)

resources/mediawiki/mediawiki.js
tests/qunit/suites/resources/mediawiki/mediawiki.test.js

index cf89e03..23d8214 100644 (file)
@@ -1237,9 +1237,17 @@ window.mw = window.mediaWiki = new ( function( $ ) {
                 * Returns <div><img src="&lt;"/></div>
                 */
                this.element = function( name, attrs, contents ) {
-                       var s = '<' + name;
+                       var v, s = '<' + name;
                        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
@@ -1248,20 +1256,29 @@ window.mw = window.mediaWiki = new ( function( $ ) {
                        }
                        // 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;
index 1220ce1..bae323f 100644 (file)
@@ -200,11 +200,10 @@ test( 'mw.html', function() {
        equal(
                mw.html.element(
                        'option', {
-                               value: '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.'
        );