From: jenkins-bot Date: Wed, 19 Sep 2018 19:08:46 +0000 (+0000) Subject: Merge "mw.jqueryMsg: Handle non-string parameters to functions that expect strings... X-Git-Tag: 1.34.0-rc.0~4053 X-Git-Url: http://git.cyclocoop.org//%27http:/code.google.com/p/ie7-js//%27?a=commitdiff_plain;h=7c4b637cca308651cb98d40b2bd7dca187160e81;hp=-c;p=lhc%2Fweb%2Fwiklou.git Merge "mw.jqueryMsg: Handle non-string parameters to functions that expect strings better" --- 7c4b637cca308651cb98d40b2bd7dca187160e81 diff --combined resources/src/mediawiki.jqueryMsg/mediawiki.jqueryMsg.js index b1a89268dc,04d1b4269e..12ef1ba475 --- a/resources/src/mediawiki.jqueryMsg/mediawiki.jqueryMsg.js +++ b/resources/src/mediawiki.jqueryMsg/mediawiki.jqueryMsg.js @@@ -6,7 -6,7 +6,7 @@@ * @author neilk@wikimedia.org * @author mflaschen@wikimedia.org */ -( function ( mw, $ ) { +( function () { /** * @class mw.jqueryMsg * @singleton @@@ -82,7 -82,7 +82,7 @@@ if ( typeof children[ i ] !== 'object' ) { children[ i ] = document.createTextNode( children[ i ] ); } - if ( children[ i ] instanceof jQuery && children[ i ].hasClass( 'mediaWiki_htmlEmitter' ) ) { + if ( children[ i ] instanceof $ && children[ i ].hasClass( 'mediaWiki_htmlEmitter' ) ) { children[ i ] = children[ i ].contents(); } } @@@ -114,7 -114,7 +114,7 @@@ * @return {string} Textual value of input */ function textify( input ) { - if ( input instanceof jQuery ) { + if ( input instanceof $ ) { input = input.text(); } return String( input ); @@@ -1042,7 -1042,7 +1042,7 @@@ * * @param {Array} nodes List of one element, integer, n >= 0 * @param {Array} replacements List of at least n strings - * @return {string} replacement + * @return {string|jQuery} replacement */ replace: function ( nodes, replacements ) { var index = parseInt( nodes[ 0 ], 10 ); @@@ -1115,7 -1115,7 +1115,7 @@@ * Handles an (already-validated) HTML element. * * @param {Array} nodes Nodes to process when creating element - * @return {jQuery|Array} jQuery node for valid HTML or array for disallowed element + * @return {jQuery} */ htmlelement: function ( nodes ) { var tagName, attributes, contents, $element; @@@ -1144,7 -1144,7 +1144,7 @@@ var $el, arg = nodes[ 0 ], contents = nodes[ 1 ]; - if ( arg instanceof jQuery && !arg.hasClass( 'mediaWiki_htmlEmitter' ) ) { + if ( arg instanceof $ && !arg.hasClass( 'mediaWiki_htmlEmitter' ) ) { $el = arg; } else { $el = $( '' ); @@@ -1173,18 -1173,18 +1173,18 @@@ * So convert it back with the current language's convertNumber. * * @param {Array} nodes List of nodes, [ {string|number}, {string}, {string} ... ] - * @return {string} selected pluralized form according to current language + * @return {string|jQuery} selected pluralized form according to current language */ plural: function ( nodes ) { var forms, firstChild, firstChildText, explicitPluralFormNumber, formIndex, form, count, explicitPluralForms = {}; - count = parseFloat( this.language.convertNumber( nodes[ 0 ], true ) ); + count = parseFloat( this.language.convertNumber( textify( nodes[ 0 ] ), true ) ); forms = nodes.slice( 1 ); for ( formIndex = 0; formIndex < forms.length; formIndex++ ) { form = forms[ formIndex ]; - if ( form instanceof jQuery && form.hasClass( 'mediaWiki_htmlEmitter' ) ) { + if ( form instanceof $ && form.hasClass( 'mediaWiki_htmlEmitter' ) ) { // This is a nested node, may be an explicit plural form like 5=[$2 linktext] firstChild = form.contents().get( 0 ); if ( firstChild && firstChild.nodeType === Node.TEXT_NODE ) { @@@ -1225,7 -1225,7 +1225,7 @@@ * - a gender string ('male', 'female' or 'unknown') * * @param {Array} nodes List of nodes, [ {string|mw.user}, {string}, {string}, {string} ] - * @return {string} Selected gender form according to current language + * @return {string|jQuery} Selected gender form according to current language */ gender: function ( nodes ) { var gender, @@@ -1241,7 -1241,7 +1241,7 @@@ if ( maybeUser && maybeUser.options instanceof mw.Map ) { gender = maybeUser.options.get( 'gender' ); } else { - gender = maybeUser; + gender = textify( maybeUser ); } return this.language.gender( gender, forms ); @@@ -1252,23 -1252,30 +1252,30 @@@ * Invoked by putting `{{grammar:form|word}}` in a message * * @param {Array} nodes List of nodes [{Grammar case eg: genitive}, {string word}] - * @return {string} selected grammatical form according to current language + * @return {string|jQuery} selected grammatical form according to current language */ grammar: function ( nodes ) { var form = nodes[ 0 ], word = nodes[ 1 ]; - return word && form && this.language.convertGrammar( word, form ); + // These could be jQuery objects (passed as message parameters), + // in which case we can't transform them (like rawParams() in PHP). + if ( typeof form === 'string' && typeof word === 'string' ) { + return this.language.convertGrammar( word, form ); + } + return word; }, /** * Tranform parsed structure into a int: (interface language) message include * Invoked by putting `{{int:othermessage}}` into a message * + * TODO Syntax in the included message is not parsed, this seems like a bug? + * * @param {Array} nodes List of nodes * @return {string} Other message */ 'int': function ( nodes ) { - var msg = nodes[ 0 ]; + var msg = textify( nodes[ 0 ] ); return mw.jqueryMsg.getMessageFunction()( msg.charAt( 0 ).toLowerCase() + msg.slice( 1 ) ); }, @@@ -1294,13 -1301,18 +1301,18 @@@ * separator, according to the current language. * * @param {Array} nodes List of nodes - * @return {number|string} Formatted number + * @return {number|string|jQuery} Formatted number */ formatnum: function ( nodes ) { var isInteger = !!nodes[ 1 ] && nodes[ 1 ] === 'R', number = nodes[ 0 ]; - return this.language.convertNumber( number, isInteger ); + // These could be jQuery objects (passed as message parameters), + // in which case we can't transform them (like rawParams() in PHP). + if ( typeof number === 'string' || typeof number === 'number' ) { + return this.language.convertNumber( number, isInteger ); + } + return number; }, /** @@@ -1388,4 -1400,4 +1400,4 @@@ }; }() ); -}( mediaWiki, jQuery ) ); +}() ); diff --combined tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js index 7e6ceb782e,ff83b980d5..8c6f7885b5 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js @@@ -1,4 -1,4 +1,4 @@@ -( function ( mw, $ ) { +( function () { /* eslint-disable camelcase */ var formatText, formatParse, formatnumTests, specialCharactersPageName, expectedListUsers, expectedListUsersSitename, expectedLinkPagenamee, expectedEntrypoints, @@@ -1157,6 -1157,8 +1157,8 @@@ QUnit.test( 'Non-string parameters to various functions', function ( assert ) { var i, cases; + // For jquery-param-int + mw.messages.set( 'x', 'y' ); // For jquery-param-grammar mw.language.setData( 'en', 'grammarTransformations', { test: [ @@@ -1183,12 -1185,12 +1185,12 @@@ { key: 'jquery-param-grammar', msg: '{{GRAMMAR:test|$1}}', - expected: '{{GRAMMAR:test|$1}}' + expected: 'x' }, { key: 'jquery-param-int', msg: '{{int:$1}}', - expected: '{{int:$1}}' + expected: 'y' }, { key: 'jquery-param-ns', @@@ -1198,7 -1200,7 +1200,7 @@@ { key: 'jquery-param-formatnum', msg: '{{formatnum:$1}}', - expected: '[object Object]' + expected: 'x' }, { key: 'jquery-param-case', @@@ -1284,4 -1286,4 +1286,4 @@@ 'setParserDefaults is deep if requested' ); } ); -}( mediaWiki, jQuery ) ); +}() );