"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "resources/lib/jquery.i18n/src/jquery.i18n.parser.js" between
mediawiki-1.31.1.tar.gz and mediawiki-1.32.0.tar.gz

About: MediaWiki is a wiki engine (the collaborative editing software that runs for e.g. Wikipedia, the free encyclopedia).

jquery.i18n.parser.js  (mediawiki-1.31.1):jquery.i18n.parser.js  (mediawiki-1.32.0)
/** /*!
* jQuery Internationalization library * jQuery Internationalization library
* *
* Copyright (C) 2011-2013 Santhosh Thottingal, Neil Kandalgaonkar * Copyright (C) 2011-2013 Santhosh Thottingal, Neil Kandalgaonkar
* *
* jquery.i18n is dual licensed GPLv2 or later and MIT. You don't have to do * jquery.i18n is dual licensed GPLv2 or later and MIT. You don't have to do
* anything special to choose one license or the other and you don't have to * anything special to choose one license or the other and you don't have to
* notify anyone which license you are using. You are free to use * notify anyone which license you are using. You are free to use
* UniversalLanguageSelector in commercial projects as long as the copyright * UniversalLanguageSelector in commercial projects as long as the copyright
* header is left intact. See files GPL-LICENSE and MIT-LICENSE for details. * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details.
* *
* @licence GNU General Public Licence 2.0 or later * @licence GNU General Public Licence 2.0 or later
* @licence MIT License * @licence MIT License
*/ */
( function ( $ ) { ( function ( $ ) {
'use strict'; 'use strict';
var MessageParser = function ( options ) { var MessageParser = function ( options ) {
this.options = $.extend( {}, $.i18n.parser.defaults, options ); this.options = $.extend( {}, $.i18n.parser.defaults, options );
this.language = $.i18n.languages[String.locale] || $.i18n.languag es['default']; this.language = $.i18n.languages[ String.locale ] || $.i18n.langu ages[ 'default' ];
this.emitter = $.i18n.parser.emitter; this.emitter = $.i18n.parser.emitter;
}; };
MessageParser.prototype = { MessageParser.prototype = {
constructor: MessageParser, constructor: MessageParser,
simpleParse: function ( message, parameters ) { simpleParse: function ( message, parameters ) {
return message.replace( /\$(\d+)/g, function ( str, match ) { return message.replace( /\$(\d+)/g, function ( str, match ) {
var index = parseInt( match, 10 ) - 1; var index = parseInt( match, 10 ) - 1;
return parameters[index] !== undefined ? paramete rs[index] : '$' + match; return parameters[ index ] !== undefined ? parame ters[ index ] : '$' + match;
} ); } );
}, },
parse: function ( message, replacements ) { parse: function ( message, replacements ) {
if ( message.indexOf( '{{' ) < 0 ) { if ( message.indexOf( '{{' ) < 0 ) {
return this.simpleParse( message, replacements ); return this.simpleParse( message, replacements );
} }
this.emitter.language = $.i18n.languages[$.i18n().locale] this.emitter.language = $.i18n.languages[ $.i18n().locale
|| ] ||
$.i18n.languages['default']; $.i18n.languages[ 'default' ];
return this.emitter.emit( this.ast( message ), replacemen ts ); return this.emitter.emit( this.ast( message ), replacemen ts );
}, },
ast: function ( message ) { ast: function ( message ) {
var pipe, colon, backslash, anyCharacter, dollar, digits, regularLiteral, var pipe, colon, backslash, anyCharacter, dollar, digits, regularLiteral,
regularLiteralWithoutBar, regularLiteralWithoutSp ace, escapedOrLiteralWithoutBar, regularLiteralWithoutBar, regularLiteralWithoutSp ace, escapedOrLiteralWithoutBar,
escapedOrRegularLiteral, templateContents, templa teName, openTemplate, escapedOrRegularLiteral, templateContents, templa teName, openTemplate,
closeTemplate, expression, paramExpression, resul t, closeTemplate, expression, paramExpression, resul t,
pos = 0; pos = 0;
// Try parsers until one works, if none work return null // Try parsers until one works, if none work return null
function choice( parserSyntax ) { function choice( parserSyntax ) {
return function () { return function () {
var i, result; var i, result;
for ( i = 0; i < parserSyntax.length; i++ ) { for ( i = 0; i < parserSyntax.length; i++ ) {
result = parserSyntax[i](); result = parserSyntax[ i ]();
if ( result !== null ) { if ( result !== null ) {
return result; return result;
} }
} }
return null; return null;
}; };
} }
// Try several parserSyntax-es in a row. // Try several parserSyntax-es in a row.
// All must succeed; otherwise, return null. // All must succeed; otherwise, return null.
// This is the only eager one. // This is the only eager one.
function sequence( parserSyntax ) { function sequence( parserSyntax ) {
var i, res, var i, res,
originalPos = pos, originalPos = pos,
result = []; result = [];
for ( i = 0; i < parserSyntax.length; i++ ) { for ( i = 0; i < parserSyntax.length; i++ ) {
res = parserSyntax[i](); res = parserSyntax[ i ]();
if ( res === null ) { if ( res === null ) {
pos = originalPos; pos = originalPos;
return null; return null;
} }
result.push( res ); result.push( res );
} }
skipping to change at line 126 skipping to change at line 126
} }
// Helpers -- just make parserSyntax out of simpler JS bu iltin types // Helpers -- just make parserSyntax out of simpler JS bu iltin types
function makeStringParser( s ) { function makeStringParser( s ) {
var len = s.length; var len = s.length;
return function () { return function () {
var result = null; var result = null;
if ( message.substr( pos, len ) === s ) { if ( message.slice( pos, pos + len ) === s ) {
result = s; result = s;
pos += len; pos += len;
} }
return result; return result;
}; };
} }
function makeRegexParser( regex ) { function makeRegexParser( regex ) {
return function () { return function () {
var matches = message.substr( pos ).match ( regex ); var matches = message.slice( pos ).match( regex );
if ( matches === null ) { if ( matches === null ) {
return null; return null;
} }
pos += matches[0].length; pos += matches[ 0 ].length;
return matches[0]; return matches[ 0 ];
}; };
} }
pipe = makeStringParser( '|' ); pipe = makeStringParser( '|' );
colon = makeStringParser( ':' ); colon = makeStringParser( ':' );
backslash = makeStringParser( '\\' ); backslash = makeStringParser( '\\' );
anyCharacter = makeRegexParser( /^./ ); anyCharacter = makeRegexParser( /^./ );
dollar = makeStringParser( '$' ); dollar = makeStringParser( '$' );
digits = makeRegexParser( /^\d+/ ); digits = makeRegexParser( /^\d+/ );
regularLiteral = makeRegexParser( /^[^{}\[\]$\\]/ ); regularLiteral = makeRegexParser( /^[^{}[\]$\\]/ );
regularLiteralWithoutBar = makeRegexParser( /^[^{}\[\]$\\ regularLiteralWithoutBar = makeRegexParser( /^[^{}[\]$\\|
|]/ ); ]/ );
regularLiteralWithoutSpace = makeRegexParser( /^[^{}\[\]$ regularLiteralWithoutSpace = makeRegexParser( /^[^{}[\]$\
\s]/ ); s]/ );
// There is a general pattern: // There is a general pattern:
// parse a thing; // parse a thing;
// if it worked, apply transform, // if it worked, apply transform,
// otherwise return null. // otherwise return null.
// But using this as a combinator seems to cause problems // But using this as a combinator seems to cause problems
// when combined with nOrMore(). // when combined with nOrMore().
// May be some scoping issue. // May be some scoping issue.
function transform( p, fn ) { function transform( p, fn ) {
return function () { return function () {
skipping to change at line 192 skipping to change at line 192
function literal() { function literal() {
var result = nOrMore( 1, escapedOrRegularLiteral )(); var result = nOrMore( 1, escapedOrRegularLiteral )();
return result === null ? null : result.join( '' ) ; return result === null ? null : result.join( '' ) ;
} }
function escapedLiteral() { function escapedLiteral() {
var result = sequence( [ backslash, anyCharacter ] ); var result = sequence( [ backslash, anyCharacter ] );
return result === null ? null : result[1]; return result === null ? null : result[ 1 ];
} }
choice( [ escapedLiteral, regularLiteralWithoutSpace ] ); choice( [ escapedLiteral, regularLiteralWithoutSpace ] );
escapedOrLiteralWithoutBar = choice( [ escapedLiteral, re gularLiteralWithoutBar ] ); escapedOrLiteralWithoutBar = choice( [ escapedLiteral, re gularLiteralWithoutBar ] );
escapedOrRegularLiteral = choice( [ escapedLiteral, regul arLiteral ] ); escapedOrRegularLiteral = choice( [ escapedLiteral, regul arLiteral ] );
function replacement() { function replacement() {
var result = sequence( [ dollar, digits ] ); var result = sequence( [ dollar, digits ] );
if ( result === null ) { if ( result === null ) {
return null; return null;
} }
return [ 'REPLACE', parseInt( result[1], 10 ) - 1 ]; return [ 'REPLACE', parseInt( result[ 1 ], 10 ) - 1 ];
} }
templateName = transform( templateName = transform(
// see $wgLegalTitleChars // see $wgLegalTitleChars
// not allowing : due to the need to catch "PLURA L:$1" // not allowing : due to the need to catch "PLURA L:$1"
makeRegexParser( /^[ !"$&'()*,.\/0-9;=?@A-Z\^_`a- z~\x80-\xFF+\-]+/ ), makeRegexParser( /^[ !"$&'()*,./0-9;=?@A-Z^_`a-z~ \x80-\xFF+-]+/ ),
function ( result ) { function ( result ) {
return result.toString(); return result.toString();
} }
); );
function templateParam() { function templateParam() {
var expr, var expr,
result = sequence( [ pipe, nOrMore( 0, pa ramExpression ) ] ); result = sequence( [ pipe, nOrMore( 0, pa ramExpression ) ] );
if ( result === null ) { if ( result === null ) {
return null; return null;
} }
expr = result[1]; expr = result[ 1 ];
// use a "CONCAT" operator if there are multiple nodes, // use a "CONCAT" operator if there are multiple nodes,
// otherwise return the first node, raw. // otherwise return the first node, raw.
return expr.length > 1 ? [ 'CONCAT' ].concat( exp r ) : expr[0]; return expr.length > 1 ? [ 'CONCAT' ].concat( exp r ) : expr[ 0 ];
} }
function templateWithReplacement() { function templateWithReplacement() {
var result = sequence( [ templateName, colon, rep lacement ] ); var result = sequence( [ templateName, colon, rep lacement ] );
return result === null ? null : [ result[0], resu lt[2] ]; return result === null ? null : [ result[ 0 ], re sult[ 2 ] ];
} }
function templateWithOutReplacement() { function templateWithOutReplacement() {
var result = sequence( [ templateName, colon, par amExpression ] ); var result = sequence( [ templateName, colon, par amExpression ] );
return result === null ? null : [ result[0], resu lt[2] ]; return result === null ? null : [ result[ 0 ], re sult[ 2 ] ];
} }
templateContents = choice( [ templateContents = choice( [
function () { function () {
var res = sequence( [ var res = sequence( [
// templates can have placeholder s for dynamic // templates can have placeholder s for dynamic
// replacement eg: {{PLURAL:$1|on e car|$1 cars}} // replacement eg: {{PLURAL:$1|on e car|$1 cars}}
// or no placeholders eg: // or no placeholders eg:
// {{GRAMMAR:genitive|{{SITENAME} }} // {{GRAMMAR:genitive|{{SITENAME} }}
choice( [ templateWithReplacement , templateWithOutReplacement ] ), choice( [ templateWithReplacement , templateWithOutReplacement ] ),
nOrMore( 0, templateParam ) nOrMore( 0, templateParam )
] ); ] );
return res === null ? null : res[0].conca t( res[1] ); return res === null ? null : res[ 0 ].con cat( res[ 1 ] );
}, },
function () { function () {
var res = sequence( [ templateName, nOrMo re( 0, templateParam ) ] ); var res = sequence( [ templateName, nOrMo re( 0, templateParam ) ] );
if ( res === null ) { if ( res === null ) {
return null; return null;
} }
return [ res[0] ].concat( res[1] ); return [ res[ 0 ] ].concat( res[ 1 ] );
} }
] ); ] );
openTemplate = makeStringParser( '{{' ); openTemplate = makeStringParser( '{{' );
closeTemplate = makeStringParser( '}}' ); closeTemplate = makeStringParser( '}}' );
function template() { function template() {
var result = sequence( [ openTemplate, templateCo ntents, closeTemplate ] ); var result = sequence( [ openTemplate, templateCo ntents, closeTemplate ] );
return result === null ? null : result[1]; return result === null ? null : result[ 1 ];
} }
expression = choice( [ template, replacement, literal ] ) ; expression = choice( [ template, replacement, literal ] ) ;
paramExpression = choice( [ template, replacement, litera lWithoutBar ] ); paramExpression = choice( [ template, replacement, litera lWithoutBar ] );
function start() { function start() {
var result = nOrMore( 0, expression )(); var result = nOrMore( 0, expression )();
if ( result === null ) { if ( result === null ) {
return null; return null;
} }
return [ 'CONCAT' ].concat( result ); return [ 'CONCAT' ].concat( result );
} }
result = start(); result = start();
/* /*
* For success, the pos must have gotten to the end of th e input * For success, the pos must have gotten to the end of th e input
* and returned a non-null. * and returned a non-null.
* n.b. This is part of language infrastructure, so we do * n.b. This is part of language infrastructure, so we do
not throw an internationalizable message. not throw an
* internationalizable message.
*/ */
if ( result === null || pos !== message.length ) { if ( result === null || pos !== message.length ) {
throw new Error( 'Parse error at position ' + pos .toString() + ' in input: ' + message ); throw new Error( 'Parse error at position ' + pos .toString() + ' in input: ' + message );
} }
return result; return result;
} }
}; };
 End of changes. 22 change blocks. 
29 lines changed or deleted 30 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)