From df010dd28453e215f7a59fd731768c6f7eb60e0d Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sun, 14 Apr 2019 02:15:55 +0100 Subject: [PATCH] resourceloader: Remove redundant 'window' indirection Use global variables directly as globals, except for the specific case of accessing it when it may not exist. In those cases we use the fact that undefined properties of an object yield the `undefined` value which we can cast to false. Accessing an undefined variable would yield a ReferenceError exception. Change-Id: I1d9e9aa5845ba3c756ad6e31358d8594e003b04b --- includes/resourceloader/ResourceLoader.php | 6 ++-- resources/src/startup/startup.js | 29 ++++++++++--------- tests/phpunit/includes/OutputPageTest.php | 8 ++--- .../ResourceLoaderClientHtmlTest.php | 16 +++++----- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/includes/resourceloader/ResourceLoader.php b/includes/resourceloader/ResourceLoader.php index 3371069dab..418f532af5 100644 --- a/includes/resourceloader/ResourceLoader.php +++ b/includes/resourceloader/ResourceLoader.php @@ -1482,7 +1482,7 @@ MESSAGE; */ public static function makeLoaderConditionalScript( $script ) { // Adds a function to lazy-created RLQ - return '(window.RLQ=window.RLQ||[]).push(function(){' . + return '(RLQ=window.RLQ||[]).push(function(){' . trim( $script ) . '});'; } @@ -1496,7 +1496,7 @@ MESSAGE; */ public static function makeInlineCodeWithModule( $modules, $script ) { // Adds an array to lazy-created RLQ - return '(window.RLQ=window.RLQ||[]).push([' + return '(RLQ=window.RLQ||[]).push([' . self::encodeJsonForScript( $modules ) . ',' . 'function(){' . trim( $script ) . '}' . ']);'; @@ -1527,7 +1527,7 @@ MESSAGE; return new WrappedString( Html::inlineScript( $js, $nonce ), - "(window.RLQ=window.RLQ||[]).push(function(){", + "(RLQ=window.RLQ||[]).push(function(){", '});' ); } diff --git a/resources/src/startup/startup.js b/resources/src/startup/startup.js index 063ea09612..c251a86aa2 100644 --- a/resources/src/startup/startup.js +++ b/resources/src/startup/startup.js @@ -3,8 +3,8 @@ * * - Beware: This file MUST parse without errors on even the most ancient of browsers! */ -/* eslint-disable no-implicit-globals, vars-on-top, no-unmodified-loop-condition */ -/* global $VARS, $CODE */ +/* eslint-disable no-implicit-globals */ +/* global $VARS, $CODE, RLQ:true, NORLQ:true */ /** * See @@ -85,17 +85,17 @@ if ( !isCompatible( navigator.userAgent ) ) { .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' ); // Process any callbacks for Grade C - while ( window.NORLQ && window.NORLQ[ 0 ] ) { - window.NORLQ.shift()(); + while ( window.NORLQ && NORLQ[ 0 ] ) { + NORLQ.shift()(); } - window.NORLQ = { + NORLQ = { push: function ( fn ) { fn(); } }; // Clear and disable the Grade A queue - window.RLQ = { + RLQ = { push: function () {} }; } else { @@ -137,24 +137,25 @@ if ( !isCompatible( navigator.userAgent ) ) { // arrivals will also be processed. Late arrival can happen because // startup.js is executed asynchronously, concurrently with the streaming // response of the HTML. - var queue = window.RLQ; - window.RLQ = []; - /* global RLQ */ + RLQ = window.RLQ || []; RLQ.push = function ( fn ) { if ( typeof fn === 'function' ) { fn(); } else { - // This callback requires a module, handled in mediawiki.base. + // If the first parameter is not a function, then it is an array + // containing a list of required module names and a function. + // Do an actual push for now, as this signature is handled + // later by mediawiki.base.js. RLQ[ RLQ.length ] = fn; } }; - while ( queue && queue[ 0 ] ) { - // Re-use our new push() method - RLQ.push( queue.shift() ); + while ( RLQ[ 0 ] ) { + // Process all values gathered so far + RLQ.push( RLQ.shift() ); } // Clear and disable the Grade C queue - window.NORLQ = { + NORLQ = { push: function () {} }; }() ); diff --git a/tests/phpunit/includes/OutputPageTest.php b/tests/phpunit/includes/OutputPageTest.php index 097aef7f55..ef8766a966 100644 --- a/tests/phpunit/includes/OutputPageTest.php +++ b/tests/phpunit/includes/OutputPageTest.php @@ -2711,7 +2711,7 @@ class OutputPageTest extends MediaWikiTestCase { // Single only=scripts load [ [ 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ], - "" ], @@ -2724,14 +2724,14 @@ class OutputPageTest extends MediaWikiTestCase { // Private embed (only=scripts) [ [ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ], - "" ], // Load private module (combined) [ [ 'test.quux', ResourceLoaderModule::TYPE_COMBINED ], - "" @@ -2749,7 +2749,7 @@ class OutputPageTest extends MediaWikiTestCase { // Load two modules in separate groups [ [ [ 'test.group.foo', 'test.group.bar' ], ResourceLoaderModule::TYPE_COMBINED ], - "" diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php index 9e310d97a3..206160c7cf 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php @@ -113,7 +113,7 @@ Deprecation message.' ] . 'RLSTATE={"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready"};' . 'RLPAGEMODULES=["test"];' . '' . "\n" - . '' . "\n" . '' . "\n" @@ -190,7 +190,7 @@ Deprecation message.' ] 'test.styles.deprecated', ] ); // phpcs:disable Generic.Files.LineLength - $expected = ''; // phpcs:enable @@ -220,7 +220,7 @@ Deprecation message.' ] 'modules' => [ 'test.private' ], 'only' => ResourceLoaderModule::TYPE_COMBINED, 'extra' => [], - 'output' => '', + 'output' => '', ], [ 'context' => [], @@ -242,14 +242,14 @@ Deprecation message.' ] 'modules' => [ 'test.scripts.user' ], 'only' => ResourceLoaderModule::TYPE_SCRIPTS, 'extra' => [], - 'output' => '', + 'output' => '', ], [ 'context' => [], 'modules' => [ 'test.user' ], 'only' => ResourceLoaderModule::TYPE_COMBINED, 'extra' => [], - 'output' => '', + 'output' => '', ], [ 'context' => [ 'debug' => 'true' ], @@ -278,7 +278,7 @@ Deprecation message.' ] 'modules' => [ 'test.shouldembed' ], 'only' => ResourceLoaderModule::TYPE_COMBINED, 'extra' => [], - 'output' => '', + 'output' => '', ], [ 'context' => [], @@ -292,14 +292,14 @@ Deprecation message.' ] 'modules' => [ 'test.scripts.shouldembed' ], 'only' => ResourceLoaderModule::TYPE_SCRIPTS, 'extra' => [], - 'output' => '', + 'output' => '', ], [ 'context' => [], 'modules' => [ 'test', 'test.shouldembed' ], 'only' => ResourceLoaderModule::TYPE_COMBINED, 'extra' => [], - 'output' => '', + 'output' => '', ], [ 'context' => [], -- 2.20.1