From: Bartosz DziewoƄski Date: Tue, 3 Nov 2015 23:48:11 +0000 (+0100) Subject: mediawiki.jqueryMsg: Extend mw.Message with new #parseDom method X-Git-Tag: 1.31.0-rc.0~8943^2 X-Git-Url: http://git.cyclocoop.org/%22.%24h.%22?a=commitdiff_plain;h=652d4ded91ce20002da4b4f381af663649b97013;p=lhc%2Fweb%2Fwiklou.git mediawiki.jqueryMsg: Extend mw.Message with new #parseDom method Unlike #parse, this one returns a jQuery set of DOM nodes and not a HTML string, for two benefits: * Allows to skip unnecessary HTML serialization and reconstruction when we just want to insert the message contents somewhere. Old way: $( '#foo' ).html( mw.message( 'foo' ).parse() ); New way: $( '#foo' ).append( mw.message( 'foo' ).parseDom() ); This is roughly equivalent to the old alternative: $( '#foo' ).msg( 'foo' ); But that way can't be used if you start with a mw.Message object, or don't yet know where the elements will be inserted. * Allows to preserve the event handlers when a function is passed as message parameter. mw.messages.set( 'foo', '[$1 Click me!]' ); $( '#foo' ).append( mw.message( 'foo', function () { console.log( 'Link clicked!' ); } ).parseDom() ); Change-Id: Ia09131c2ffc1d149ca6b1c32dcd918a539c3a719 --- diff --git a/resources/src/mediawiki/mediawiki.jqueryMsg.js b/resources/src/mediawiki/mediawiki.jqueryMsg.js index 2fe831e448..ba2b6849c7 100644 --- a/resources/src/mediawiki/mediawiki.jqueryMsg.js +++ b/resources/src/mediawiki/mediawiki.jqueryMsg.js @@ -1279,4 +1279,20 @@ return messageFunction( this.key, this.parameters ); }; + /** + * Parse the message to DOM nodes, rather than HTML string like #parse. + * + * This method is only available when jqueryMsg is loaded. + * + * @method parseDom + * @member mw.Message + * @return {jQuery} + */ + mw.Message.prototype.parseDom = ( function () { + var reusableParent = $( '
' ); + return function () { + return reusableParent.msg( this.key, this.parameters ).contents().detach(); + }; + } )(); + }( mediaWiki, jQuery ) );