Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js |
— | — | @@ -277,6 +277,8 @@ |
278 | 278 | aborted = true; |
279 | 279 | break; |
280 | 280 | } |
| 281 | + // XXX: factor the conversion to String out into a generic _setRank |
| 282 | + // method? Would need to add to the string prototype for that.. |
281 | 283 | if ( res.token.rank === undefined && res.token.constructor === String ) { |
282 | 284 | res.token = new String ( res.token ); |
283 | 285 | } |
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.ParserFunctions.js |
— | — | @@ -25,24 +25,56 @@ |
26 | 26 | } |
27 | 27 | }; |
28 | 28 | |
| 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 | + |
29 | 55 | // TODO: Implement |
30 | 56 | // 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 ) { |
32 | 58 | this.manager.env.dp( 'switch enter: ' + target.trim() + |
33 | 59 | ' looking in ', argDict ); |
34 | 60 | target = target.trim(); |
35 | | - if ( target in argDict ) { |
| 61 | + if ( argDict[target] !== undefined ) { |
36 | 62 | this.manager.env.dp( 'switch found: ' + target + |
37 | 63 | ' res=', argDict[target] ); |
38 | 64 | 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 | + } |
47 | 79 | } |
48 | 80 | } |
49 | 81 | }; |
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js |
— | — | @@ -168,12 +168,12 @@ |
169 | 169 | var prefix = target.split(':', 1)[0].toLowerCase().trim(); |
170 | 170 | if ( prefix && 'pf_' + prefix in this.parserFunctions ) { |
171 | 171 | 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); |
175 | 175 | //this.manager.env.dp( 'entering prefix', funcArg, args ); |
176 | 176 | res = this.parserFunctions[ 'pf_' + prefix ]( funcArg, |
177 | | - tplExpandData.expandedArgs, args ); |
| 177 | + tplExpandData.expandedArgs, args, tplExpandData.origToken.attribs ); |
178 | 178 | |
179 | 179 | // XXX: support async parser functions! |
180 | 180 | if ( tplExpandData.overallAsync ) { |
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js |
— | — | @@ -34,6 +34,22 @@ |
35 | 35 | return null; |
36 | 36 | }; |
37 | 37 | |
| 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 | + |
38 | 54 | MWParserEnvironment.prototype.KVtoHash = function ( kvs ) { |
39 | 55 | if ( ! kvs ) { |
40 | 56 | console.warn( "Invalid kvs!: " + JSON.stringify( kvs, null, 2 ) ); |