context-based variables.
&$lessVars: array of variables already added
+'ResourceLoaderJqueryMsgModuleMagicWords': Called in
+ResourceLoaderJqueryMsgModule to allow adding magic words for jQueryMsg.
+The value should be a string, and they can depend only on the
+ResourceLoaderContext.
+$context: ResourceLoaderContext
+&$magicWords: Associative array mapping all-caps magic word to a string value
+
'ResourceLoaderRegisterModules': Right before modules information is required,
such as when responding to a resource
loader request or generating HTML output.
)
);
- $dataScript = Xml::encodeJsCall( 'mw.jqueryMsg.setParserDefaults', [ $parserDefaults ] );
+ $mainDataScript = Xml::encodeJsCall( 'mw.jqueryMsg.setParserDefaults', [ $parserDefaults ] );
- return $fileScript . $dataScript;
+ // Associative array mapping magic words (e.g. SITENAME)
+ // to their values.
+ $magicWords = [
+ 'SITENAME' => $this->getConfig()->get( 'Sitename' ),
+ ];
+
+ Hooks::run( 'ResourceLoaderJqueryMsgModuleMagicWords', [ $context, &$magicWords ] );
+
+ $magicWordExtendData = [
+ 'magic' => $magicWords,
+ ];
+
+ $magicWordDataScript = Xml::encodeJsCall( 'mw.jqueryMsg.setParserDefaults', [
+ $magicWordExtendData,
+ /* deep= */ true
+ ] );
+
+ return $fileScript . $mainDataScript . $magicWordDataScript;
}
/**
parserDefaults = {
magic: {
PAGENAME: mw.config.get( 'wgPageName' ),
- PAGENAMEE: mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) ),
- SITENAME: mw.config.get( 'wgSiteName' )
+ PAGENAMEE: mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) )
},
// Whitelist for allowed HTML elements in wikitext.
// Self-closing tags are not currently supported.
* parsers, pass the relevant options to mw.jqueryMsg.parser.
*
* @private
- * @param {Object} data
+ * @param {Object} data New data to extend parser defaults with
+ * @param {boolean} [deep=false] Whether the extend is done recursively (deep)
*/
- mw.jqueryMsg.setParserDefaults = function ( data ) {
- $.extend( parserDefaults, data );
+ mw.jqueryMsg.setParserDefaults = function ( data, deep ) {
+ if ( deep ) {
+ $.extend( true, parserDefaults, data );
+ } else {
+ $.extend( parserDefaults, data );
+ }
};
/**
);
} );
+ QUnit.test( 'setParserDefaults', function ( assert ) {
+ mw.jqueryMsg.setParserDefaults( {
+ magic: {
+ FOO: 'foo',
+ BAR: 'bar'
+ }
+ } );
+
+ assert.deepEqual(
+ mw.jqueryMsg.getParserDefaults().magic,
+ {
+ FOO: 'foo',
+ BAR: 'bar'
+ },
+ 'setParserDefaults is shallow by default'
+ );
+
+ mw.jqueryMsg.setParserDefaults(
+ {
+ magic: {
+ BAZ: 'baz'
+ }
+ },
+ true
+ );
+
+ assert.deepEqual(
+ mw.jqueryMsg.getParserDefaults().magic,
+ {
+ FOO: 'foo',
+ BAR: 'bar',
+ BAZ: 'baz'
+ },
+ 'setParserDefaults is deep if requested'
+ );
+ } );
}( mediaWiki, jQuery ) );