r112110 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112109‎ | r112110 | r112111 >
Date:14:57, 22 February 2012
Author:gwicke
Status:deferred
Tags:
Comment:
Basic fall-through support for #switch parser function
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/ext.core.ParserFunctions.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js
@@ -277,6 +277,8 @@
278278 aborted = true;
279279 break;
280280 }
 281+ // XXX: factor the conversion to String out into a generic _setRank
 282+ // method? Would need to add to the string prototype for that..
281283 if ( res.token.rank === undefined && res.token.constructor === String ) {
282284 res.token = new String ( res.token );
283285 }
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.ParserFunctions.js
@@ -25,24 +25,56 @@
2626 }
2727 };
2828
 29+ParserFunctions.prototype._switchLookupFallback = function ( kvs, key ) {
 30+ if ( ! kvs ) {
 31+ return null;
 32+ }
 33+ var kv;
 34+ for ( var i = 0, l = kvs.length; i < l; i++ ) {
 35+ kv = kvs[i];
 36+ // XXX: tokensToString actually strips too much here! Anything
 37+ // non-stringish should not match at all.
 38+ if ( this.manager.env.tokensToString( kv.v ) === key ) {
 39+ // found. now look for the next entry with a non-empty key.
 40+ for ( var j = i; j < l; j++) {
 41+ kv = kvs[j];
 42+ // XXX: make sure the key is always one of these!
 43+ if ( kv.k !== '' && kv.k !== [] ) {
 44+ return kv;
 45+ }
 46+ }
 47+ // no fall-through found, return.
 48+ return null;
 49+ }
 50+ }
 51+ // value not found!
 52+ return null;
 53+};
 54+
2955 // TODO: Implement
3056 // http://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#Grouping_results
31 -ParserFunctions.prototype['pf_#switch'] = function ( target, argList, argDict ) {
 57+ParserFunctions.prototype['pf_#switch'] = function ( target, argList, argDict, unnamedArgs ) {
3258 this.manager.env.dp( 'switch enter: ' + target.trim() +
3359 ' looking in ', argDict );
3460 target = target.trim();
35 - if ( target in argDict ) {
 61+ if ( argDict[target] !== undefined ) {
3662 this.manager.env.dp( 'switch found: ' + target +
3763 ' res=', argDict[target] );
3864 return argDict[target];
39 - } else if ( '#default' in argDict ) {
40 - return argDict['#default'];
41 - } else {
42 - var lastKV = argList[argList.length - 1];
43 - if ( lastKV && ! lastKV.v.length ) {
44 - return lastKV.v;
45 - } else {
46 - return [];
 65+ } else {
 66+ var fallThrough = this._switchLookupFallback( unnamedArgs, target );
 67+ //console.warn( 'fallThrough: ' + JSON.stringify( [ unnamedArgs, fallThrough ] ) );
 68+ if ( fallThrough !== null ) {
 69+ return fallThrough.v;
 70+ } else if ( '#default' in argDict ) {
 71+ return argDict['#default'];
 72+ } else {
 73+ var lastKV = argList[argList.length - 1];
 74+ if ( lastKV && ! lastKV.v.length ) {
 75+ return lastKV.v;
 76+ } else {
 77+ return [];
 78+ }
4779 }
4880 }
4981 };
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js
@@ -168,12 +168,12 @@
169169 var prefix = target.split(':', 1)[0].toLowerCase().trim();
170170 if ( prefix && 'pf_' + prefix in this.parserFunctions ) {
171171 var funcArg = target.substr( prefix.length + 1 );
172 - this.manager.env.tp( 'func prefix: ' + prefix +
173 - ' args=' + JSON.stringify( tplExpandData.expandedArgs, null, 2) +
174 - ' funcArg=' + funcArg);
 172+ this.manager.env.tp( 'func prefix: ', prefix,
 173+ ' args=', tplExpandData.expandedArgs,
 174+ ' funcArg=', funcArg);
175175 //this.manager.env.dp( 'entering prefix', funcArg, args );
176176 res = this.parserFunctions[ 'pf_' + prefix ]( funcArg,
177 - tplExpandData.expandedArgs, args );
 177+ tplExpandData.expandedArgs, args, tplExpandData.origToken.attribs );
178178
179179 // XXX: support async parser functions!
180180 if ( tplExpandData.overallAsync ) {
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js
@@ -34,6 +34,22 @@
3535 return null;
3636 };
3737
 38+MWParserEnvironment.prototype.lookupValue = function ( kvs, key ) {
 39+ if ( ! kvs ) {
 40+ return null;
 41+ }
 42+ var kv;
 43+ for ( var i = 0, l = kvs.length; i < l; i++ ) {
 44+ kv = kvs[i];
 45+ if ( kv.v === key ) {
 46+ // found, return it.
 47+ return kv;
 48+ }
 49+ }
 50+ // nothing found!
 51+ return null;
 52+};
 53+
3854 MWParserEnvironment.prototype.KVtoHash = function ( kvs ) {
3955 if ( ! kvs ) {
4056 console.warn( "Invalid kvs!: " + JSON.stringify( kvs, null, 2 ) );

Status & tagging log