From b00cd0b12d9688694f6487b4b323a1b096dfca37 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Mon, 27 Jul 2015 15:47:05 -0700 Subject: [PATCH] resourceloader: Move startup code from PHP to startup.js The code is easier to maintain in an actual JavaScript file. Especially with how variables were declared and concatenated in a different order. Change-Id: I758acb78de1cdf2128e81c86f992807ef0dbf444 --- .../ResourceLoaderStartUpModule.php | 47 +++++++------------ resources/src/startup.js | 17 ++++++- .../ResourceLoaderStartUpModuleTest.php | 26 ++++++---- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/includes/resourceloader/ResourceLoaderStartUpModule.php b/includes/resourceloader/ResourceLoaderStartUpModule.php index a578ece687..16424a0031 100644 --- a/includes/resourceloader/ResourceLoaderStartUpModule.php +++ b/includes/resourceloader/ResourceLoaderStartUpModule.php @@ -268,7 +268,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule { } // Register modules - $out .= ResourceLoader::makeLoaderRegisterScript( $registrations ); + $out .= "\n" . ResourceLoader::makeLoaderRegisterScript( $registrations ); return $out; } @@ -321,40 +321,25 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule { */ 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 ); } /** diff --git a/resources/src/startup.js b/resources/src/startup.js index 5ee295bd91..ac428541ca 100644 --- a/resources/src/startup.js +++ b/resources/src/startup.js @@ -25,7 +25,8 @@ performance.mark( 'mediaWikiStartUp' ); * - 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; @@ -68,5 +69,17 @@ function isCompatible( ua ) { } /** - * 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 ); +} diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderStartUpModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderStartUpModuleTest.php index 490f5c650e..cb91614226 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderStartUpModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderStartUpModuleTest.php @@ -10,7 +10,8 @@ class ResourceLoaderStartUpModuleTest extends ResourceLoaderTestCase { 'out' => ' mw.loader.addSource( { "local": "/w/load.php" -} );mw.loader.register( [] );' +} ); +mw.loader.register( [] );' ) ), array( array( 'msg' => 'Basic registry', @@ -20,7 +21,8 @@ mw.loader.addSource( { 'out' => ' mw.loader.addSource( { "local": "/w/load.php" -} );mw.loader.register( [ +} ); +mw.loader.register( [ [ "test.blank", "wvTifjse" @@ -37,7 +39,8 @@ mw.loader.addSource( { 'out' => ' mw.loader.addSource( { "local": "/w/load.php" -} );mw.loader.register( [ +} ); +mw.loader.register( [ [ "test.blank", "wvTifjse" @@ -65,7 +68,8 @@ mw.loader.addSource( { 'out' => ' mw.loader.addSource( { "local": "/w/load.php" -} );mw.loader.register( [ +} ); +mw.loader.register( [ [ "test.blank", "wvTifjse" @@ -87,7 +91,8 @@ mw.loader.addSource( { mw.loader.addSource( { "local": "/w/load.php", "example": "http://example.org/w/load.php" -} );mw.loader.register( [ +} ); +mw.loader.register( [ [ "test.blank", "wvTifjse", @@ -123,7 +128,8 @@ mw.loader.addSource( { 'out' => ' mw.loader.addSource( { "local": "/w/load.php" -} );mw.loader.register( [ +} ); +mw.loader.register( [ [ "test.x.core", "wvTifjse" @@ -219,7 +225,8 @@ mw.loader.addSource( { mw.loader.addSource( { "local": "/w/load.php", "example": "http://example.org/w/load.php" -} );mw.loader.register( [ +} ); +mw.loader.register( [ [ "test.blank", "wvTifjse" @@ -342,7 +349,7 @@ mw.loader.addSource( { $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,' @@ -364,7 +371,8 @@ mw.loader.addSource( { $this->assertEquals( 'mw.loader.addSource( { "local": "/w/load.php" -} );mw.loader.register( [ +} ); +mw.loader.register( [ [ "test.blank", "wvTifjse" -- 2.20.1