r96307 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96306‎ | r96307 | r96308 >
Date:21:51, 5 September 2011
Author:krinkle
Status:ok
Tags:
Comment:
mediawiki.html: mediawiki.html: Add support for numbers and booleans
* 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)
Modified paths:
  • /trunk/phase3/resources/mediawiki/mediawiki.js (modified) (history)
  • /trunk/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.test.js (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.test.js
@@ -200,11 +200,10 @@
201201 equal(
202202 mw.html.element(
203203 'option', {
204 - value: 'foo',
205204 selected: true
206205 }, 'Foo'
207206 ),
208 - '<option value="foo" selected="selected">Foo</option>',
 207+ '<option selected="selected">Foo</option>',
209208 'Attributes may have boolean values. True copies the attribute name to the value.'
210209 );
211210
Index: trunk/phase3/resources/mediawiki/mediawiki.js
@@ -1237,9 +1237,17 @@
12381238 * Returns <div><img src="&lt;"/></div>
12391239 */
12401240 this.element = function( name, attrs, contents ) {
1241 - var s = '<' + name;
 1241+ var v, s = '<' + name;
12421242 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 ) + '"';
12441252 }
12451253 if ( contents === undefined || contents === null ) {
12461254 // Self close tag
@@ -1248,20 +1256,29 @@
12491257 }
12501258 // Regular open tag
12511259 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+ }
12661283 }
12671284 s += '</' + name + '>';
12681285 return s;

Follow-up revisions

RevisionCommit summaryAuthorDate
r96308release-notes for r96307krinkle21:53, 5 September 2011
r96310mediawiki.html: Attribute values may also be numbers (rather than boolean or ...krinkle23:10, 5 September 2011
r96321* (bug 29916) add follow-up fetching to CR API...reedy11:31, 6 September 2011
r104753MFT r96307, r96310reedy21:27, 30 November 2011
r104785release-notes for r104753...krinkle23:31, 30 November 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r96305mediawiki.html: Add broken tests for numbers and booleans...krinkle21:25, 5 September 2011

Status & tagging log