r112113 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112112‎ | r112113 | r112114 >
Date:15:59, 22 February 2012
Author:gwicke
Status:deferred
Tags:
Comment:
Add some support for functionhooks in test parser and parserTests.js, and
tweak a few parser functions.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/ext.core.ParserFunctions.js (modified) (history)
  • /trunk/extensions/VisualEditor/tests/parser/parserTests.js (modified) (history)
  • /trunk/extensions/VisualEditor/tests/parser/parserTests.pegjs (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/parser/parserTests.pegjs
@@ -34,7 +34,8 @@
3535 article /
3636 test /
3737 line /
38 - hooks
 38+ hooks /
 39+ functionhooks
3940
4041
4142
@@ -77,7 +78,25 @@
7879 end_article =
7980 "!!" ws? "endarticle" ws? eol
8081
 82+// function hooks
8183
 84+functionhooks = start_functionhooks text:text end_functionhooks
 85+{
 86+ return {
 87+ type: 'functionhooks',
 88+ text: text
 89+ }
 90+}
 91+
 92+start_functionhooks =
 93+ "!!" ws? "functionhooks" ":"? ws? eol
 94+
 95+end_functionhooks =
 96+ "!!" ws? "endfunctionhooks" ":"? ws? eol
 97+
 98+end_test =
 99+ "!!" ws? "end" ws? eol
 100+
82101 test =
83102 start_test
84103 title:text
Index: trunk/extensions/VisualEditor/tests/parser/parserTests.js
@@ -180,7 +180,7 @@
181181 console.log(e2);
182182 }
183183
184 - this.cases = this.getTests();
 184+ this.cases = this.getTests() || [];
185185
186186 this.articles = {};
187187
@@ -592,6 +592,11 @@
593593 this.comments.push( item.comment );
594594 process.nextTick( this.processCase.bind( this, i + 1 ) );
595595 break;
 596+ case 'hooks':
 597+ console.warn('parserTests: Unhandled hook ' + JSON.stringify( item ) );
 598+ case 'functionhooks':
 599+ console.warn('parserTests: Unhandled functionhook '
 600+ + JSON.stringify( item ) );
596601 default:
597602 this.comments = [];
598603 process.nextTick( this.processCase.bind( this, i + 1 ) );
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.ParserFunctions.js
@@ -7,6 +7,13 @@
88 * matching a lower-cased template name prefix up to the first colon will
99 * override that template.
1010 *
 11+ * TODO: Implement these more thoroughly, and test against
 12+ * extensions/ParserFunction/
 13+ * convertTests.txt
 14+ * exprTests.txt
 15+ * funcsParserTests.txt
 16+ * stringFunctionTests.txt
 17+ *
1118 * @author Gabriel Wicke <gwicke@wikimedia.org>
1219 */
1320
@@ -34,7 +41,7 @@
3542 kv = kvs[i];
3643 // XXX: tokensToString actually strips too much here! Anything
3744 // non-stringish should not match at all.
38 - if ( this.manager.env.tokensToString( kv.v ) === key ) {
 45+ if ( this.manager.env.tokensToString( kv.v ).trim() === key ) {
3946 // found. now look for the next entry with a non-empty key.
4047 for ( var j = i; j < l; j++) {
4148 kv = kvs[j];
@@ -304,11 +311,15 @@
305312 ParserFunctions.prototype['pf_#ifexpr'] = function ( target, argList, argDict ) {
306313 this.manager.env.dp( '#ifexp: ' + JSON.stringify( argList ) );
307314 var res;
308 - try {
309 - var f = new Function ( 'return (' + target + ')' );
310 - res = f();
311 - } catch ( e ) {
312 - return [ 'class="error" in expression ' + target ];
 315+ if ( target ) {
 316+ try {
 317+ var f = new Function ( 'return (' + target + ')' );
 318+ res = f();
 319+ } catch ( e ) {
 320+ return [ 'class="error" in expression ' + target ];
 321+ }
 322+ } else {
 323+ res = target;
313324 }
314325 if ( res ) {
315326 return ( argList[0] && argList[0].v ) || [];
@@ -320,16 +331,20 @@
321332 if ( target.indexOf( 'class="error"' ) >= 0 ) {
322333 return ( argList[0] && argList[0].v ) || [];
323334 } else {
324 - return ( argList[1] && argList[1].v ) || [];
 335+ return argList[1] && argList[1].v || [ target ] ;
325336 }
326337 };
327338 ParserFunctions.prototype['pf_#expr'] = function ( target, argList, argDict ) {
328339 var res;
329 - try {
330 - var f = new Function ( 'return (' + target + ')' );
331 - res = f();
332 - } catch ( e ) {
333 - return [ 'class="error" in expression ' + target ];
 340+ if ( target ) {
 341+ try {
 342+ var f = new Function ( 'return (' + target + ')' );
 343+ res = f();
 344+ } catch ( e ) {
 345+ return [ 'class="error" in expression ' + target ];
 346+ }
 347+ } else {
 348+ res = '';
334349 }
335350 return [ res.toString() ];
336351 };

Status & tagging log