r113360 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113359‎ | r113360 | r113361 >
Date:11:44, 8 March 2012
Author:gwicke
Status:deferred
Tags:
Comment:
A few fixes to parser functions and template expansion. Trim whitespace off
template arguments, let the last duplicate key win and fake pagenamee slightly
better.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/ext.core.ParserFunctions.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/parse.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/parse.js
@@ -50,7 +50,7 @@
5151 wgScriptPath: argv.wgScriptPath,
5252 wgScriptExtension: argv.wgScriptExtension,
5353 // XXX: add options for this!
54 - wgUploadPath: 'http://upload.wikimedia.org/wikipedia/commons/thumb',
 54+ wgUploadPath: 'http://upload.wikimedia.org/wikipedia/commons',
5555 fetchTemplates: argv.fetchTemplates,
5656 // enable/disable debug output using this switch
5757 debug: argv.debug,
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.ParserFunctions.js
@@ -39,9 +39,7 @@
4040 var kv;
4141 for ( var i = 0, l = kvs.length; i < l; i++ ) {
4242 kv = kvs[i];
43 - // XXX: tokensToString actually strips too much here! Anything
44 - // non-stringish should not match at all.
45 - if ( this.manager.env.tokensToString( kv.v ) === key ) {
 43+ if ( this.manager.env.tokensToString( kv.v, true ) === key ) {
4644 // found. now look for the next entry with a non-empty key.
4745 for ( var j = i; j < l; j++) {
4846 kv = kvs[j];
@@ -61,7 +59,7 @@
6260 // TODO: Implement
6361 // http://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#Grouping_results
6462 ParserFunctions.prototype['pf_#switch'] = function ( target, argList, argDict, unnamedArgs ) {
65 - this.manager.env.dp( 'switch enter: ' + target.trim() +
 63+ this.manager.env.dp( 'switch enter', target.trim(),
6664 ' looking in ', argDict );
6765 target = target.trim();
6866 if ( argDict[target] !== undefined ) {
@@ -396,7 +394,7 @@
397395 return [ target ];
398396 };
399397 ParserFunctions.prototype['pf_pagenamee'] = function ( target, argList, argDict ) {
400 - return [ target ];
 398+ return [ target.split(':', 2)[1] || '' ];
401399 };
402400 ParserFunctions.prototype['pf_fullpagename'] = function ( target, argList, argDict ) {
403401 return target && [target] || ["http://example.com/fixme/"];
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.environment.js
@@ -57,6 +57,46 @@
5858 return null;
5959 };
6060
 61+/**
 62+ * Trim space and newlines from leading and trailing text tokens.
 63+ */
 64+MWParserEnvironment.prototype.tokenTrim = function ( tokens ) {
 65+ var l = tokens.length,
 66+ i, token;
 67+ // strip leading space
 68+ for ( i = 0; i < l; i++ ) {
 69+ token = tokens[i];
 70+ if ( token.constructor === String ) {
 71+ token = token.replace( /^\s+/, '' );
 72+ tokens[i] = token;
 73+ if ( token !== '' ) {
 74+ break;
 75+ }
 76+ } else {
 77+ break;
 78+ }
 79+ }
 80+ // strip trailing space
 81+ for ( i = l - 1; i >= 0; i-- ) {
 82+ token = tokens[i];
 83+ if ( token.constructor === String ) {
 84+ token = token.replace( /\s+$/, '' );
 85+ tokens[i] = token;
 86+ if ( token !== '' ) {
 87+ break;
 88+ }
 89+ } else {
 90+ break;
 91+ }
 92+ }
 93+ return tokens;
 94+};
 95+
 96+
 97+/**
 98+ * Convert an array of key-value pairs into a hash of keys to values. For
 99+ * duplicate keys, the last entry wins.
 100+ */
61101 MWParserEnvironment.prototype.KVtoHash = function ( kvs ) {
62102 if ( ! kvs ) {
63103 console.warn( "Invalid kvs!: " + JSON.stringify( kvs, null, 2 ) );
@@ -66,9 +106,9 @@
67107 for ( var i = 0, l = kvs.length; i < l; i++ ) {
68108 var kv = kvs[i],
69109 key = this.tokensToString( kv.k ).trim();
70 - if( res[key] === undefined ) {
71 - res[key] = kv.v;
72 - }
 110+ //if( res[key] === undefined ) {
 111+ res[key] = this.tokenTrim( kv.v );
 112+ //}
73113 }
74114 //console.warn( 'KVtoHash: ' + JSON.stringify( res ));
75115 return res;
@@ -203,7 +243,8 @@
204244 var tstring = JSON.stringify( token );
205245 this.dp ( 'MWParserEnvironment.tokensToString, non-text token: ' +
206246 tstring + JSON.stringify( tokens, null, 2 ) );
207 - //out.push( tstring );
 247+ //console.trace();
 248+ out.push( tstring );
208249 }
209250 }
210251 //console.warn( 'MWParserEnvironment.tokensToString result: ' + out.join('') );

Status & tagging log