From 8d1844704ad6d069fca9f28e41e40ae7b49a153a Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Mon, 15 Oct 2018 11:53:32 -0700 Subject: [PATCH] jqueryMsg: Don't fall back to simple parser when jQuery params passed jqueryMsg parsing is required when the message body contains wikitext, but it is also required when one of the parameters is a jQuery object. Without this change, the following doesn't work: mw.messages.set( 'foo', 'Hello $1' ); mw.message( 'foo', $( '' ).text( 'World' ) ).parse(); (But if the message contained e.g. {{PLURAL:}} syntax, it did work.) Change-Id: I445f9194bb8b2ed35baafbda30d1d0d008b64e2c --- .../mediawiki.jqueryMsg.js | 15 +++++++++-- .../mediawiki/mediawiki.jqueryMsg.test.js | 25 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/resources/src/mediawiki.jqueryMsg/mediawiki.jqueryMsg.js b/resources/src/mediawiki.jqueryMsg/mediawiki.jqueryMsg.js index 12ef1ba475..8b348c8661 100644 --- a/resources/src/mediawiki.jqueryMsg/mediawiki.jqueryMsg.js +++ b/resources/src/mediawiki.jqueryMsg/mediawiki.jqueryMsg.js @@ -1368,8 +1368,19 @@ // Replace the default message parser with jqueryMsg oldParser = mw.Message.prototype.parser; mw.Message.prototype.parser = function () { - if ( this.format === 'plain' || !/\{\{|[<>[&]/.test( this.map.get( this.key ) ) ) { - // Fall back to mw.msg's simple parser + // Fall back to mw.msg's simple parser where possible + if ( + // Plain text output always uses the simple parser + this.format === 'plain' || + ( + // jqueryMsg parser is needed for messages containing wikitext + !/\{\{|[<>[&]/.test( this.map.get( this.key ) ) && + // jqueryMsg parser is needed when jQuery objects or DOM nodes are passed in as parameters + !this.parameters.some( function ( param ) { + return param instanceof $ || param.nodeType !== undefined; + } ) + ) + ) { return oldParser.apply( this ); } diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js index 8c6f7885b5..405a60f0f1 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js @@ -1224,6 +1224,8 @@ expected = 'Bold!'; mw.messages.set( 'integration-test', '[[Bold]]!' ); + mw.messages.set( 'param-test', 'Hello $1' ); + mw.messages.set( 'param-test-with-link', 'Hello $1 [[$2|$3]]' ); assert.strictEqual( mw.message( 'integration-test' ).parse(), @@ -1237,6 +1239,29 @@ 'jQuery plugin $.fn.msg() works correctly' ); + assert.strictEqual( + mw.message( 'param-test', $( '' ).text( 'World' ) ).parse(), + 'Hello World', + 'Passing a jQuery object as a parameter to a message without wikitext works correctly' + ); + + assert.strictEqual( + mw.message( 'param-test', $( '' ).text( 'World' ).get( 0 ) ).parse(), + 'Hello World', + 'Passing a DOM node as a parameter to a message without wikitext works correctly' + ); + + assert.strictEqual( + mw.message( + 'param-test-with-link', + $( '' ).text( 'cruel' ), + 'Globe', + 'world' + ).parse(), + 'Hello cruel world', + 'Message with a jQuery parameter and a parsed link' + ); + mw.messages.set( 'integration-test-extlink', '[$1 Link]' ); msg = mw.message( 'integration-test-extlink', -- 2.20.1