Improve mediaWiki.parser() (which is a very strange name for a function that expands...
authorRoan Kattouw <catrope@users.mediawiki.org>
Fri, 8 Oct 2010 10:49:32 +0000 (10:49 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Fri, 8 Oct 2010 10:49:32 +0000 (10:49 +0000)
resources/mediawiki/mediawiki.js

index 5b548f9..f56adc2 100644 (file)
@@ -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;
        };