// 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 );
}
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',