r111926 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111925‎ | r111926 | r111927 >
Date:13:47, 20 February 2012
Author:nikerabbit
Status:ok
Tags:i18nreview 
Comment:
Whitespace cleanup
Modified paths:
  • /branches/jsgrammar/resources/mediawiki/mediawiki.jqueryMsg.js (modified) (history)

Diff [purge]

Index: branches/jsgrammar/resources/mediawiki/mediawiki.jqueryMsg.js
@@ -1,7 +1,7 @@
22 /**
3 - * Experimental advanced wikitext parser-emitter.
 3+ * Experimental advanced wikitext parser-emitter.
44 * See: http://www.mediawiki.org/wiki/Extension:UploadWizard/MessageParser for docs
5 - *
 5+ *
66 * @author neilk@wikimedia.org
77 */
88
@@ -14,9 +14,9 @@
1515 * @param {Object} parser options
1616 * @return {Function} accepting ( String message key, String replacement1, String replacement2 ... ) and returning {jQuery}
1717 */
18 - function getFailableParserFn( options ) {
19 - var parser = new mw.jqueryMsg.parser( options );
20 - /**
 18+ function getFailableParserFn( options ) {
 19+ var parser = new mw.jqueryMsg.parser( options );
 20+ /**
2121 * Try to parse a key and optional replacements, returning a jQuery object that may be a tree of jQuery nodes.
2222 * If there was an error parsing, return the key and the error message (wrapped in jQuery). This should put the error right into
2323 * the interface, without causing the page to halt script execution, and it hopefully should be clearer how to fix it.
@@ -26,22 +26,22 @@
2727 */
2828 return function( args ) {
2929 var key = args[0];
30 - var argsArray = $.isArray( args[1] ) ? args[1] : $.makeArray( args ).slice( 1 );
31 - var escapedArgsArray = $.map( argsArray, function( arg ) {
 30+ var argsArray = $.isArray( args[1] ) ? args[1] : $.makeArray( args ).slice( 1 );
 31+ var escapedArgsArray = $.map( argsArray, function( arg ) {
3232 return typeof arg === 'string' ? mw.html.escape( arg ) : arg;
3333 } );
3434 try {
3535 return parser.parse( key, escapedArgsArray );
3636 } catch ( e ) {
37 - return $( '<span></span>' ).append( key + ': ' + e.message );
 37+ return $( '<span>' ).append( key + ': ' + e.message );
3838 }
3939 };
4040 }
4141
4242 /**
43 - * Class method.
 43+ * Class method.
4444 * Returns a function suitable for use as a global, to construct strings from the message key (and optional replacements).
45 - * e.g.
 45+ * e.g.
4646 * window.gM = mediaWiki.parser.getMessageFunction( options );
4747 * $( 'p#headline' ).html( gM( 'hello-user', username ) );
4848 *
@@ -51,12 +51,12 @@
5252 * @param {Array} parser options
5353 * @return {Function} function suitable for assigning to window.gM
5454 */
55 - mw.jqueryMsg.getMessageFunction = function( options ) {
 55+ mw.jqueryMsg.getMessageFunction = function( options ) {
5656 var failableParserFn = getFailableParserFn( options );
57 - /**
 57+ /**
5858 * N.B. replacements are variadic arguments or an array in second parameter. In other words:
59 - * somefunction(a, b, c, d)
60 - * is equivalent to
 59+ * somefunction(a, b, c, d)
 60+ * is equivalent to
6161 * somefunction(a, [b, c, d])
6262 *
6363 * @param {String} message key
@@ -69,10 +69,10 @@
7070 };
7171
7272 /**
73 - * Class method.
74 - * Returns a jQuery plugin which parses the message in the message key, doing replacements optionally, and appends the nodes to
75 - * the current selector. Bindings to passed-in jquery elements are preserved. Functions become click handlers for [$1 linktext] links.
76 - * e.g.
 73+ * Class method.
 74+ * Returns a jQuery plugin which parses the message in the message key, doing replacements optionally, and appends the nodes to
 75+ * the current selector. Bindings to passed-in jquery elements are preserved. Functions become click handlers for [$1 linktext] links.
 76+ * e.g.
7777 * $.fn.msg = mediaWiki.parser.getJqueryPlugin( options );
7878 * var userlink = $( '<a>' ).click( function() { alert( "hello!!") } );
7979 * $( 'p#headline' ).msg( 'hello-user', userlink );
@@ -82,12 +82,12 @@
8383 */
8484 mw.jqueryMsg.getPlugin = function( options ) {
8585 var failableParserFn = getFailableParserFn( options );
86 - /**
 86+ /**
8787 * N.B. replacements are variadic arguments or an array in second parameter. In other words:
88 - * somefunction(a, b, c, d)
89 - * is equivalent to
 88+ * somefunction(a, b, c, d)
 89+ * is equivalent to
9090 * somefunction(a, [b, c, d])
91 - *
 91+ *
9292 * We append to 'this', which in a jQuery plugin context will be the selected elements.
9393 * @param {String} message key
9494 * @param {Array} optional replacements (can also specify variadically)
@@ -102,9 +102,9 @@
103103 };
104104 };
105105
106 - var parserDefaults = {
 106+ var parserDefaults = {
107107 'magic' : {
108 - 'SITENAME' : mw.config.get( 'wgSiteName' )
 108+ 'SITENAME' : mw.config.get( 'wgSiteName' )
109109 },
110110 'messages' : mw.messages,
111111 'language' : mw.language
@@ -140,19 +140,19 @@
141141
142142 /**
143143 * Fetch the message string associated with a key, return parsed structure. Memoized.
144 - * Note that we pass '[' + key + ']' back for a missing message here.
 144+ * Note that we pass '[' + key + ']' back for a missing message here.
145145 * @param {String} key
146146 * @return {String|Array} string of '[key]' if message missing, simple string if possible, array of arrays if needs parsing
147147 */
148148 getAst: function( key ) {
149 - if ( this.astCache[ key ] === undefined ) {
 149+ if ( this.astCache[ key ] === undefined ) {
150150 var wikiText = this.settings.messages.get( key );
151151 if ( typeof wikiText !== 'string' ) {
152152 wikiText = "\\[" + key + "\\]";
153153 }
154154 this.astCache[ key ] = this.wikiTextToAst( wikiText );
155155 }
156 - return this.astCache[ key ];
 156+ return this.astCache[ key ];
157157 },
158158
159159 /*
@@ -160,15 +160,15 @@
161161 *
162162 * CAVEAT: This does not parse all wikitext. It could be more efficient, but it's pretty good already.
163163 * n.b. We want to move this functionality to the server. Nothing here is required to be on the client.
164 - *
 164+ *
165165 * @param {String} message string wikitext
166166 * @throws Error
167167 * @return {Mixed} abstract syntax tree
168168 */
169169 wikiTextToAst: function( input ) {
170 -
171 - // Indicates current position in input as we parse through it.
172 - // Shared among all parsing functions below.
 170+
 171+ // Indicates current position in input as we parse through it.
 172+ // Shared among all parsing functions below.
173173 var pos = 0;
174174
175175 // =========================================================
@@ -176,7 +176,7 @@
177177 // =========================================================
178178
179179
180 - // Try parsers until one works, if none work return null
 180+ // Try parsers until one works, if none work return null
181181 function choice( ps ) {
182182 return function() {
183183 for ( var i = 0; i < ps.length; i++ ) {
@@ -194,12 +194,12 @@
195195 function sequence( ps ) {
196196 var originalPos = pos;
197197 var result = [];
198 - for ( var i = 0; i < ps.length; i++ ) {
 198+ for ( var i = 0; i < ps.length; i++ ) {
199199 var res = ps[i]();
200200 if ( res === null ) {
201201 pos = originalPos;
202202 return null;
203 - }
 203+ }
204204 result.push( res );
205205 }
206206 return result;
@@ -219,7 +219,7 @@
220220 if ( result.length < n ) {
221221 pos = originalPos;
222222 return null;
223 - }
 223+ }
224224 return result;
225225 };
226226 }
@@ -228,7 +228,7 @@
229229 // But using this as a combinator seems to cause problems when combined with nOrMore().
230230 // May be some scoping issue
231231 function transform( p, fn ) {
232 - return function() {
 232+ return function() {
233233 var result = p();
234234 return result === null ? null : fn( result );
235235 };
@@ -236,7 +236,7 @@
237237
238238 // Helpers -- just make ps out of simpler JS builtin types
239239
240 - function makeStringParser( s ) {
 240+ function makeStringParser( s ) {
241241 var len = s.length;
242242 return function() {
243243 var result = null;
@@ -249,21 +249,21 @@
250250 }
251251
252252 function makeRegexParser( regex ) {
253 - return function() {
 253+ return function() {
254254 var matches = input.substr( pos ).match( regex );
255 - if ( matches === null ) {
 255+ if ( matches === null ) {
256256 return null;
257 - }
 257+ }
258258 pos += matches[0].length;
259259 return matches[0];
260260 };
261261 }
262 -
263262
264 - /**
265 - * ===================================================================
 263+
 264+ /**
 265+ * ===================================================================
266266 * General patterns above this line -- wikitext specific parsers below
267 - * ===================================================================
 267+ * ===================================================================
268268 */
269269
270270 // Parsing functions follow. All parsing functions work like this:
@@ -288,7 +288,7 @@
289289
290290 function escapedLiteral() {
291291 var result = sequence( [
292 - backslash,
 292+ backslash,
293293 anyCharacter
294294 ] );
295295 return result === null ? null : result[1];
@@ -304,7 +304,7 @@
305305 regularLiteralWithoutBar
306306 ] );
307307
308 - var escapedOrRegularLiteral = choice( [
 308+ var escapedOrRegularLiteral = choice( [
309309 escapedLiteral,
310310 regularLiteral
311311 ] );
@@ -315,7 +315,7 @@
316316 return result === null ? null : result.join('');
317317 }
318318
319 - // Used to define "literals" within template parameters. The pipe character is the parameter delimeter, so by default
 319+ // Used to define "literals" within template parameters. The pipe character is the parameter delimeter, so by default
320320 // it is not a literal in the parameter
321321 function literalWithoutBar() {
322322 var result = nOrMore( 1, escapedOrLiteralWithoutBar )();
@@ -327,16 +327,16 @@
328328 return result === null ? null : result.join('');
329329 }
330330
331 - var whitespace = makeRegexParser( /^\s+/ );
 331+ var whitespace = makeRegexParser( /^\s+/ );
332332 var dollar = makeStringParser( '$' );
333 - var digits = makeRegexParser( /^\d+/ );
 333+ var digits = makeRegexParser( /^\d+/ );
334334
335335 function replacement() {
336336 var result = sequence( [
337337 dollar,
338338 digits
339339 ] );
340 - if ( result === null ) {
 340+ if ( result === null ) {
341341 return null;
342342 }
343343 return [ 'REPLACE', parseInt( result[1], 10 ) - 1 ];
@@ -378,7 +378,7 @@
379379 return result;
380380 }
381381
382 - var templateName = transform(
 382+ var templateName = transform(
383383 // see $wgLegalTitleChars
384384 // not allowing : due to the need to catch "PLURAL:$1"
385385 makeRegexParser( /^[ !"$&'()*,.\/0-9;=?@A-Z\^_`a-z~\x80-\xFF+-]+/ ),
@@ -386,7 +386,7 @@
387387 );
388388
389389 function templateParam() {
390 - var result = sequence( [
 390+ var result = sequence( [
391391 pipe,
392392 nOrMore( 0, paramExpression )
393393 ] );
@@ -428,10 +428,10 @@
429429 ] );
430430 return res === null ? null : res[0].concat( res[1] );
431431 },
432 - function() {
 432+ function() {
433433 var res = sequence( [
434434 templateName,
435 - nOrMore( 0, templateParam )
 435+ nOrMore( 0, templateParam )
436436 ] );
437437 if ( res === null ) {
438438 return null;
@@ -453,7 +453,7 @@
454454 }
455455
456456 var nonWhitespaceExpression = choice( [
457 - template,
 457+ template,
458458 link,
459459 extlink,
460460 replacement,
@@ -461,19 +461,19 @@
462462 ] );
463463
464464 var paramExpression = choice( [
465 - template,
 465+ template,
466466 link,
467467 extlink,
468468 replacement,
469469 literalWithoutBar
470470 ] );
471471
472 - var expression = choice( [
 472+ var expression = choice( [
473473 template,
474474 link,
475475 extlink,
476476 replacement,
477 - literal
 477+ literal
478478 ] );
479479
480480 function start() {
@@ -490,9 +490,9 @@
491491 // finally let's do some actual work...
492492
493493 var result = start();
494 -
 494+
495495 /*
496 - * For success, the p must have gotten to the end of the input
 496+ * For success, the p must have gotten to the end of the input
497497 * and returned a non-null.
498498 * n.b. This is part of language infrastructure, so we do not throw an internationalizable message.
499499 */
@@ -501,7 +501,7 @@
502502 }
503503 return result;
504504 }
505 -
 505+
506506 };
507507
508508 /**
@@ -511,7 +511,7 @@
512512 this.language = language;
513513 var _this = this;
514514
515 - $.each( magic, function( key, val ) {
 515+ $.each( magic, function( key, val ) {
516516 _this[ key.toLowerCase() ] = function() { return val; };
517517 } );
518518
@@ -531,11 +531,11 @@
532532 ret = node;
533533 break;
534534 case 'object': // node is an array of nodes
535 - var subnodes = $.map( node.slice( 1 ), function( n ) {
 535+ var subnodes = $.map( node.slice( 1 ), function( n ) {
536536 return _this.emit( n, replacements );
537537 } );
538538 var operation = node[0].toLowerCase();
539 - if ( typeof _this[operation] === 'function' ) {
 539+ if ( typeof _this[operation] === 'function' ) {
540540 ret = _this[ operation ]( subnodes, replacements );
541541 } else {
542542 throw new Error( 'unknown operation "' + operation + '"' );
@@ -556,7 +556,7 @@
557557 };
558558
559559 // For everything in input that follows double-open-curly braces, there should be an equivalent parser
560 - // function. For instance {{PLURAL ... }} will be processed by 'plural'.
 560+ // function. For instance {{PLURAL ... }} will be processed by 'plural'.
561561 // If you have 'magic words' then configure the parser to have them upon creation.
562562 //
563563 // An emitter method takes the parent node, the array of subnodes and the array of replacements (the values that $1, $2... should translate to).
@@ -572,7 +572,7 @@
573573 */
574574 concat: function( nodes ) {
575575 var span = $( '<span>' ).addClass( 'mediaWiki_htmlEmitter' );
576 - $.each( nodes, function( i, node ) {
 576+ $.each( nodes, function( i, node ) {
577577 if ( node instanceof jQuery && node.hasClass( 'mediaWiki_htmlEmitter' ) ) {
578578 $.each( node.contents(), function( j, childNode ) {
579579 span.append( childNode );
@@ -596,12 +596,12 @@
597597 */
598598 replace: function( nodes, replacements ) {
599599 var index = parseInt( nodes[0], 10 );
600 - return index < replacements.length ? replacements[index] : '$' + ( index + 1 );
 600+ return index < replacements.length ? replacements[index] : '$' + ( index + 1 );
601601 },
602602
603 - /**
 603+ /**
604604 * Transform wiki-link
605 - * TODO unimplemented
 605+ * TODO unimplemented
606606 */
607607 wlink: function( nodes ) {
608608 return "unimplemented";
@@ -610,16 +610,16 @@
611611 /**
612612 * Transform parsed structure into external link
613613 * If the href is a jQuery object, treat it as "enclosing" the link text.
614 - * ... function, treat it as the click handler
615 - * ... string, treat it as a URI
616 - * TODO: throw an error if nodes.length > 2 ?
 614+ * ... function, treat it as the click handler
 615+ * ... string, treat it as a URI
 616+ * TODO: throw an error if nodes.length > 2 ?
617617 * @param {Array} of two elements, {jQuery|Function|String} and {String}
618618 * @return {jQuery}
619619 */
620620 link: function( nodes ) {
621621 var arg = nodes[0];
622622 var contents = nodes[1];
623 - var $el;
 623+ var $el;
624624 if ( arg instanceof jQuery ) {
625625 $el = arg;
626626 } else {
@@ -630,7 +630,7 @@
631631 $el.attr( 'href', arg.toString() );
632632 }
633633 }
634 - $el.append( contents );
 634+ $el.append( contents );
635635 return $el;
636636 },
637637
@@ -638,10 +638,10 @@
639639 * Transform parsed structure into pluralization
640640 * n.b. The first node may be a non-integer (for instance, a string representing an Arabic number).
641641 * So convert it back with the current language's convertNumber.
642 - * @param {Array} of nodes, [ {String|Number}, {String}, {String} ... ]
 642+ * @param {Array} of nodes, [ {String|Number}, {String}, {String} ... ]
643643 * @return {String} selected pluralized form according to current language
644644 */
645 - plural: function( nodes ) {
 645+ plural: function( nodes ) {
646646 var count = parseInt( this.language.convertNumber( nodes[0], true ), 10 );
647647 var forms = nodes.slice(1);
648648 return forms.length ? this.language.convertPlural( count, forms ) : '';
@@ -650,12 +650,12 @@
651651 /**
652652 * Transform parsed structure into gender
653653 * Usage {{gender:[gender| mw.user object ] | masculine|feminine|neutral}}.
654 - * @param {Array} of nodes, [ {String|mw.User}, {String}, {String} , {String} ]
 654+ * @param {Array} of nodes, [ {String|mw.User}, {String}, {String} , {String} ]
655655 * @return {String} selected gender form according to current language
656656 */
657 - gender: function( nodes ) {
 657+ gender: function( nodes ) {
658658 var gender;
659 - if ( nodes[0] && nodes[0].options instanceof mw.Map ){
 659+ if ( nodes[0] && nodes[0].options instanceof mw.Map ){
660660 gender = nodes[0].options.get( 'gender' );
661661 } else {
662662 gender = nodes[0];
@@ -666,10 +666,10 @@
667667 /**
668668 * Transform parsed structure into grammar conversion.
669669 * Invoked by putting {{grammar:form|word}} in a message
670 - * @param {Array} of nodes [{Grammar case eg: genitive}, {String word}]
 670+ * @param {Array} of nodes [{Grammar case eg: genitive}, {String word}]
671671 * @return {String} selected grammatical form according to current language
672672 */
673 - grammar: function( nodes ) {
 673+ grammar: function( nodes ) {
674674 var form = nodes[0];
675675 var word = nodes[1];
676676 return this.language.convertGrammar( word , form );
@@ -682,23 +682,23 @@
683683 // deprecated! don't rely on gM existing.
684684 // the window.gM ought not to be required - or if required, not required here. But moving it to extensions breaks it (?!)
685685 // Need to fix plugin so it could do attributes as well, then will be okay to remove this.
686 - window.gM = mw.jqueryMsg.getMessageFunction();
 686+ window.gM = mw.jqueryMsg.getMessageFunction();
687687
688688 $.fn.msg = mw.jqueryMsg.getPlugin();
689 -
 689+
690690 // Replace the default message parser with jqueryMsg
691691 var oldParser = mw.Message.prototype.parser;
692692 mw.Message.prototype.parser = function() {
693693 // TODO: should we cache the message function so we don't create a new one every time? Benchmark this maybe?
694694 // Caching is somewhat problematic, because we do need different message functions for different maps, so
695695 // we'd have to cache the parser as a member of this.map, which sounds a bit ugly.
696 -
 696+
697697 // Do not use mw.jqueryMsg unless required
698698 if ( this.map.get( this.key ).indexOf( '{{' ) < 0 ) {
699699 // Fall back to mw.msg's simple parser
700700 return oldParser.apply( this );
701701 }
702 -
 702+
703703 var messageFunction = mw.jqueryMsg.getMessageFunction( { 'messages': this.map } );
704704 return messageFunction( this.key, this.parameters );
705705 };

Status & tagging log