}
// Register modules
- $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
+ $out .= "\n" . ResourceLoader::makeLoaderRegisterScript( $registrations );
return $out;
}
*/
public function getScript( ResourceLoaderContext $context ) {
global $IP;
+ if ( $context->getOnly() !== 'scripts' ) {
+ return '/* Requires only=script */';
+ }
$out = file_get_contents( "$IP/resources/src/startup.js" );
- if ( $context->getOnly() === 'scripts' ) {
- // Startup function
- $configuration = $this->getConfigSettings( $context );
- $registrations = $this->getModuleRegistrations( $context );
+ $pairs = array_map( function ( $value ) {
+ $value = FormatJson::encode( $value, ResourceLoader::inDebugMode(), FormatJson::ALL_OK );
// Fix indentation
- $registrations = str_replace( "\n", "\n\t", trim( $registrations ) );
- $mwMapJsCall = Xml::encodeJsCall(
- 'mw.Map',
- array( $this->getConfig()->get( 'LegacyJavaScriptGlobals' ) )
- );
- $mwConfigSetJsCall = Xml::encodeJsCall(
- 'mw.config.set',
- array( $configuration ),
- ResourceLoader::inDebugMode()
- );
-
- $out .= "var startUp = function () {\n" .
- "\tmw.config = new " .
- $mwMapJsCall . "\n" .
- "\t$registrations\n" .
- "\t" . $mwConfigSetJsCall .
- "};\n";
-
- // Conditional script injection
- $scriptTag = Html::linkedScript( self::getStartupModulesUrl( $context ) );
- $out .= "if ( isCompatible() ) {\n" .
- "\t" . Xml::encodeJsCall( 'document.write', array( $scriptTag ) ) .
- "\n}";
- }
-
- return $out;
+ $value = str_replace( "\n", "\n\t", $value );
+ return $value;
+ }, array(
+ '$VARS.wgLegacyJavaScriptGlobals' => $this->getConfig()->get( 'LegacyJavaScriptGlobals' ),
+ '$VARS.configuration' => $this->getConfigSettings( $context ),
+ '$VARS.baseModulesScript' => Html::linkedScript( self::getStartupModulesUrl( $context ) ),
+ ) );
+ $pairs['$CODE.registrations()'] = str_replace( "\n", "\n\t", trim( $this->getModuleRegistrations( $context ) ) );
+
+ return strtr( $out, $pairs );
}
/**
* - https://jquery.com/browser-support/
*/
-/*jshint unused: false */
+/*jshint unused: false, evil: true */
+/*globals mw, $VARS, $CODE */
function isCompatible( ua ) {
if ( ua === undefined ) {
ua = navigator.userAgent;
}
/**
- * The startUp() function will be auto-generated and added below.
+ * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php.
*/
+function startUp() {
+ mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals );
+
+ $CODE.registrations();
+
+ mw.config.set( $VARS.configuration );
+}
+
+// Conditional script injection
+if ( isCompatible() ) {
+ document.write( $VARS.baseModulesScript );
+}
'out' => '
mw.loader.addSource( {
"local": "/w/load.php"
-} );mw.loader.register( [] );'
+} );
+mw.loader.register( [] );'
) ),
array( array(
'msg' => 'Basic registry',
'out' => '
mw.loader.addSource( {
"local": "/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
[
"test.blank",
"wvTifjse"
'out' => '
mw.loader.addSource( {
"local": "/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
[
"test.blank",
"wvTifjse"
'out' => '
mw.loader.addSource( {
"local": "/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
[
"test.blank",
"wvTifjse"
mw.loader.addSource( {
"local": "/w/load.php",
"example": "http://example.org/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
[
"test.blank",
"wvTifjse",
'out' => '
mw.loader.addSource( {
"local": "/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
[
"test.x.core",
"wvTifjse"
mw.loader.addSource( {
"local": "/w/load.php",
"example": "http://example.org/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
[
"test.blank",
"wvTifjse"
$rl->register( $modules );
$module = new ResourceLoaderStartUpModule();
$this->assertEquals(
-'mw.loader.addSource({"local":"/w/load.php"});'
+'mw.loader.addSource({"local":"/w/load.php"});' . "\n"
. 'mw.loader.register(['
. '["test.blank","wvTifjse"],'
. '["test.min","wvTifjse",[0],null,null,'
$this->assertEquals(
'mw.loader.addSource( {
"local": "/w/load.php"
-} );mw.loader.register( [
+} );
+mw.loader.register( [
[
"test.blank",
"wvTifjse"