From: Roan Kattouw Date: Fri, 8 Oct 2010 10:49:32 +0000 (+0000) Subject: Improve mediaWiki.parser() (which is a very strange name for a function that expands... X-Git-Tag: 1.31.0-rc.0~34582 X-Git-Url: http://git.cyclocoop.org/url?a=commitdiff_plain;h=d670151e1734ed6118141753925b1964a65ac795;p=lhc%2Fweb%2Fwiklou.git Improve mediaWiki.parser() (which is a very strange name for a function that expands $1, $2, ... in i18n messages) to use .replace() with a callback, so it will handle edge cases like more than 9 parameters ($10 would be treated as $1 followed by a literal 0) or a parameter's value containing $n (which would then get substituted again). Code written by Neil Kandalgaonkar and trivially modified by me. --- diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index 5b548f957f..f56adc2564 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -175,9 +175,10 @@ window.mediaWiki = new ( function( $ ) { */ this.parser = function( text, options ) { if ( typeof options === 'object' && typeof options.parameters === 'object' ) { - for ( var p = 0; p < options.parameters.length; p++ ) { - text = text.replace( '\$' + ( parseInt( p ) + 1 ), options.parameters[p] ); - } + text = text.replace( /\$(\d+)/g, function( str, match ) { + var index = parseInt( match, 10 ) - 1; + return index in options.parameters ? options.parameters[index] : '$' + match; + } ); } return text; };