jqueryMsg: Don't fall back to simple parser when jQuery params passed
authorRoan Kattouw <roan.kattouw@gmail.com>
Mon, 15 Oct 2018 18:53:32 +0000 (11:53 -0700)
committerRoan Kattouw <roan.kattouw@gmail.com>
Tue, 16 Oct 2018 19:20:09 +0000 (12:20 -0700)
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', $( '<a>' ).text( 'World' ) ).parse();

(But if the message contained e.g. {{PLURAL:}} syntax, it did work.)

Change-Id: I445f9194bb8b2ed35baafbda30d1d0d008b64e2c

resources/src/mediawiki.jqueryMsg/mediawiki.jqueryMsg.js
tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js

index 12ef1ba..8b348c8 100644 (file)
        // 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 );
                }
 
index 8c6f788..405a60f 100644 (file)
 
                expected = '<b><a title="Bold" href="/wiki/Bold">Bold</a>!</b>';
                mw.messages.set( 'integration-test', '<b>[[Bold]]!</b>' );
+               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(),
                        'jQuery plugin $.fn.msg() works correctly'
                );
 
+               assert.strictEqual(
+                       mw.message( 'param-test', $( '<span>' ).text( 'World' ) ).parse(),
+                       'Hello <span>World</span>',
+                       'Passing a jQuery object as a parameter to a message without wikitext works correctly'
+               );
+
+               assert.strictEqual(
+                       mw.message( 'param-test', $( '<span>' ).text( 'World' ).get( 0 ) ).parse(),
+                       'Hello <span>World</span>',
+                       'Passing a DOM node as a parameter to a message without wikitext works correctly'
+               );
+
+               assert.strictEqual(
+                       mw.message(
+                               'param-test-with-link',
+                               $( '<span>' ).text( 'cruel' ),
+                               'Globe',
+                               'world'
+                       ).parse(),
+                       'Hello <span>cruel</span> <a title="Globe" href="/wiki/Globe">world</a>',
+                       'Message with a jQuery parameter and a parsed link'
+               );
+
                mw.messages.set( 'integration-test-extlink', '[$1 Link]' );
                msg = mw.message(
                        'integration-test-extlink',