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
'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',
+++ /dev/null
-<?php
-/**
- * Resource loader module for populating mediawiki.jqueryMsg 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
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @author Brad Jorsch
- */
-
-/**
- * ResourceLoader module for populating mediawiki.jqueryMsg data.
- */
-class ResourceLoaderJqueryMsgDataModule extends ResourceLoaderModule {
-
- protected $targets = array( 'desktop', 'mobile' );
-
- /**
- * @param ResourceLoaderContext $context
- * @return string JavaScript code
- */
- public function getScript( ResourceLoaderContext $context ) {
- $jsData = array();
-
- $tagData = Sanitizer::getRecognizedTagData();
- $jsData['allowedHtmlElements'] = array_merge(
- array_keys( $tagData['htmlpairs'] ),
- array_diff(
- array_keys( $tagData['htmlsingle'] ),
- array_keys( $tagData['htmlsingleonly'] )
- )
- );
-
- return "if ( !mw.jqueryMsg ) {\n" .
- "\tmw.jqueryMsg = {};\n" .
- "}\n" .
- "mw.jqueryMsg.data = " . Xml::encodeJsVar( $jsData ) . ";\n";
- }
-
- /**
- * @param ResourceLoaderContext $context
- * @return array|null
- */
- public function getDefinitionSummary( ResourceLoaderContext $context ) {
- $ret = parent::getDefinitionSummary( $context );
- $ret['hash'] = md5( $this->getScript( $context ) );
- return $ret;
- }
-}
--- /dev/null
+<?php
+/**
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @author Brad Jorsch
+ */
+
+/**
+ * ResourceLoader module for mediawiki.jqueryMsg and its generated data
+ */
+class ResourceLoaderJqueryMsgModule extends ResourceLoaderFileModule {
+
+ /**
+ * @param ResourceLoaderContext $context
+ * @return string JavaScript code
+ */
+ public function getScript( ResourceLoaderContext $context ) {
+ $fileScript = parent::getScript( $context );
+
+ $tagData = Sanitizer::getRecognizedTagData();
+ $parserDefaults = array();
+ $parserDefaults['allowedHtmlElements'] = array_merge(
+ array_keys( $tagData['htmlpairs'] ),
+ array_diff(
+ array_keys( $tagData['htmlsingle'] ),
+ array_keys( $tagData['htmlsingleonly'] )
+ )
+ );
+
+ $dataScript = Xml::encodeJsCall( 'mw.jqueryMsg.setParserDefaults', array( $parserDefaults ) );
+
+ return $fileScript . $dataScript;
+ }
+
+ /**
+ * @param ResourceLoaderContext $context
+ * @return array|null
+ */
+ public function getDefinitionSummary( ResourceLoaderContext $context ) {
+ $summary = parent::getDefinitionSummary( $context );
+ $summary[] = array(
+ 'sanitizerData' => Sanitizer::getRecognizedTagData()
+ );
+ return $summary;
+ }
+}
// 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 */
/**
),
'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',
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: [
};
}
- // 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.