mediawiki.jqueryMsg: Phase out redundant data module and minor clean up

Follows-up 4a3e50a54.

* Merge mediawiki.jqueryMsg.data and mediawiki.jqueryMsg modules.

  There's no need for this to be a separate module. The data is not for public consumption,
  it's provided to jqueryMsg only.

* Remove unused default-default values for 'allowedHtmlElements'.

* Remove conditionals around data providing at initial run-time. Instead, expose
  private method can call that. This way, we don't have two code paths claim
  ownership over the namespace. And it makes the module easier to test and re-use
  by not requiring the data to exist at first run time.

* Fix getDefinitionSummary() implementation to append data instead of setting
  arbitary keys in parent data. ResourceLoader documentation of getDefinitionSummary()
  has been updated to reflect this practice.

Change-Id: I40006d39514a997dce4930756a3dac84a0c9bb83
This commit is contained in:
Timo Tijhof 2015-06-08 15:47:21 +01:00
parent fb406cd28d
commit 2dd9ec21dc
4 changed files with 36 additions and 34 deletions

View file

@ -993,7 +993,7 @@ $wgAutoloadLocalClasses = array(
'ResourceLoaderFilePath' => __DIR__ . '/includes/resourceloader/ResourceLoaderFilePath.php',
'ResourceLoaderImage' => __DIR__ . '/includes/resourceloader/ResourceLoaderImage.php',
'ResourceLoaderImageModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderImageModule.php',
'ResourceLoaderJqueryMsgDataModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderJqueryMsgDataModule.php',
'ResourceLoaderJqueryMsgModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderJqueryMsgModule.php',
'ResourceLoaderLanguageDataModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderLanguageDataModule.php',
'ResourceLoaderLanguageNamesModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderLanguageNamesModule.php',
'ResourceLoaderModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderModule.php',

View file

@ -1,6 +1,6 @@
<?php
/**
* Resource loader module for populating mediawiki.jqueryMsg data.
* ResourceLoader module for mediawiki.jqueryMsg that provides generated data.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -22,21 +22,20 @@
*/
/**
* ResourceLoader module for populating mediawiki.jqueryMsg data.
* ResourceLoader module for mediawiki.jqueryMsg and its generated data
*/
class ResourceLoaderJqueryMsgDataModule extends ResourceLoaderModule {
protected $targets = array( 'desktop', 'mobile' );
class ResourceLoaderJqueryMsgModule extends ResourceLoaderFileModule {
/**
* @param ResourceLoaderContext $context
* @return string JavaScript code
*/
public function getScript( ResourceLoaderContext $context ) {
$jsData = array();
$fileScript = parent::getScript( $context );
$tagData = Sanitizer::getRecognizedTagData();
$jsData['allowedHtmlElements'] = array_merge(
$parserDefaults = array();
$parserDefaults['allowedHtmlElements'] = array_merge(
array_keys( $tagData['htmlpairs'] ),
array_diff(
array_keys( $tagData['htmlsingle'] ),
@ -44,10 +43,9 @@ class ResourceLoaderJqueryMsgDataModule extends ResourceLoaderModule {
)
);
return "if ( !mw.jqueryMsg ) {\n" .
"\tmw.jqueryMsg = {};\n" .
"}\n" .
"mw.jqueryMsg.data = " . Xml::encodeJsVar( $jsData ) . ";\n";
$dataScript = Xml::encodeJsCall( 'mw.jqueryMsg.setParserDefaults', array( $parserDefaults ) );
return $fileScript . $dataScript;
}
/**
@ -55,8 +53,10 @@ class ResourceLoaderJqueryMsgDataModule extends ResourceLoaderModule {
* @return array|null
*/
public function getDefinitionSummary( ResourceLoaderContext $context ) {
$ret = parent::getDefinitionSummary( $context );
$ret['hash'] = md5( $this->getScript( $context ) );
return $ret;
$summary = parent::getDefinitionSummary( $context );
$summary[] = array(
'sanitizerData' => Sanitizer::getRecognizedTagData()
);
return $summary;
}
}

View file

@ -59,9 +59,6 @@ return array(
// Scripts for the dynamic language specific data, like grammar forms.
'mediawiki.language.data' => array( 'class' => 'ResourceLoaderLanguageDataModule' ),
// Dynamic data for mediawiki.jqueryMsg, such as allowed tags
'mediawiki.jqueryMsg.data' => array( 'class' => 'ResourceLoaderJqueryMsgDataModule' ),
/* MediaWiki base skinning modules */
/**
@ -1297,9 +1294,10 @@ return array(
),
'mediawiki.jqueryMsg' => array(
// Add data for mediawiki.jqueryMsg, such as allowed tags
'class' => 'ResourceLoaderJqueryMsgModule',
'scripts' => 'resources/src/mediawiki/mediawiki.jqueryMsg.js',
'dependencies' => array(
'mediawiki.jqueryMsg.data',
'mediawiki.util',
'mediawiki.language',
'dom-level2-shim',

View file

@ -17,14 +17,10 @@
magic: {
'SITENAME': mw.config.get( 'wgSiteName' )
},
// This is a whitelist like Sanitizer.php.
// Whitelist for allowed HTML elements in wikitext.
// Self-closing tags are not currently supported.
// The simplified default here is overridden below by data supplied
// by the mediawiki.jqueryMsg.data module.
allowedHtmlElements: [
'b',
'i'
],
// Can be populated via setPrivateData().
allowedHtmlElements: [],
// Key tag name, value allowed attributes for that tag.
// See Sanitizer::setupAttributeWhitelist
allowedHtmlCommonAttributes: [
@ -132,16 +128,24 @@
};
}
// Use data from mediawiki.jqueryMsg.data to override defaults, if
// available
if ( mw.jqueryMsg && mw.jqueryMsg.data ) {
if ( mw.jqueryMsg.data.allowedHtmlElements ) {
parserDefaults.allowedHtmlElements = mw.jqueryMsg.data.allowedHtmlElements;
}
}
mw.jqueryMsg = {};
/**
* Initialize parser defaults.
*
* ResourceLoaderJqueryMsgModule calls this to provide default values from
* Sanitizer.php for allowed HTML elements. To override this data for individual
* parsers, pass the relevant options to mw.jqueryMsg.parser.
*
* @private
* @param {Object} data
*/
mw.jqueryMsg.setParserDefaults = function ( data ) {
if ( data.allowedHtmlElements ) {
parserDefaults.allowedHtmlElements = data.allowedHtmlElements;
}
};
/**
* Returns a function suitable for use as a global, to construct strings from the message key (and optional replacements).
* e.g.