resourceloader: Remove redundant 'window' indirection
authorTimo Tijhof <krinklemail@gmail.com>
Sun, 14 Apr 2019 01:15:55 +0000 (02:15 +0100)
committerCatrope <roan@wikimedia.org>
Wed, 8 May 2019 21:24:30 +0000 (21:24 +0000)
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
resources/src/startup/startup.js
tests/phpunit/includes/OutputPageTest.php
tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php

index 3371069..418f532 100644 (file)
@@ -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 ),
-                       "<script$escNonce>(window.RLQ=window.RLQ||[]).push(function(){",
+                       "<script$escNonce>(RLQ=window.RLQ||[]).push(function(){",
                        '});</script>'
                );
        }
index 063ea09..c251a86 100644 (file)
@@ -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 <https://www.mediawiki.org/wiki/Compatibility#Browsers>
@@ -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 () {}
                };
        }() );
index 097aef7..ef8766a 100644 (file)
@@ -2711,7 +2711,7 @@ class OutputPageTest extends MediaWikiTestCase {
                        // Single only=scripts load
                        [
                                [ 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ],
-                               "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){"
+                               "<script nonce=\"secret\">(RLQ=window.RLQ||[]).push(function(){"
                                        . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");'
                                        . "});</script>"
                        ],
@@ -2724,14 +2724,14 @@ class OutputPageTest extends MediaWikiTestCase {
                        // Private embed (only=scripts)
                        [
                                [ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ],
-                               "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){"
+                               "<script nonce=\"secret\">(RLQ=window.RLQ||[]).push(function(){"
                                        . "mw.test.baz({token:123});\nmw.loader.state({\"test.quux\":\"ready\"});"
                                        . "});</script>"
                        ],
                        // Load private module (combined)
                        [
                                [ 'test.quux', ResourceLoaderModule::TYPE_COMBINED ],
-                               "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){"
+                               "<script nonce=\"secret\">(RLQ=window.RLQ||[]).push(function(){"
                                        . "mw.loader.implement(\"test.quux@1ev0ijv\",function($,jQuery,require,module){"
                                        . "mw.test.baz({token:123});},{\"css\":[\".mw-icon{transition:none}"
                                        . "\"]});});</script>"
@@ -2749,7 +2749,7 @@ class OutputPageTest extends MediaWikiTestCase {
                        // Load two modules in separate groups
                        [
                                [ [ 'test.group.foo', 'test.group.bar' ], ResourceLoaderModule::TYPE_COMBINED ],
-                               "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){"
+                               "<script nonce=\"secret\">(RLQ=window.RLQ||[]).push(function(){"
                                        . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?lang=en\u0026modules=test.group.bar\u0026skin=fallback");'
                                        . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?lang=en\u0026modules=test.group.foo\u0026skin=fallback");'
                                        . "});</script>"
index 9e310d9..206160c 100644 (file)
@@ -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"];'
                        . '</script>' . "\n"
-                       . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
+                       . '<script>(RLQ=window.RLQ||[]).push(function(){'
                        . 'mw.loader.implement("test.private@{blankVer}",null,{"css":[]});'
                        . '});</script>' . "\n"
                        . '<link rel="stylesheet" href="/w/load.php?lang=nl&amp;modules=test.styles.deprecated%2Cpure&amp;only=styles&amp;skin=fallback"/>' . "\n"
@@ -190,7 +190,7 @@ Deprecation message.' ]
                        'test.styles.deprecated',
                ] );
                // phpcs:disable Generic.Files.LineLength
-               $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){'
+               $expected = '<script>(RLQ=window.RLQ||[]).push(function(){'
                        . 'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");'
                        . '});</script>';
                // phpcs:enable
@@ -220,7 +220,7 @@ Deprecation message.' ]
                                'modules' => [ 'test.private' ],
                                'only' => ResourceLoaderModule::TYPE_COMBINED,
                                'extra' => [],
-                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",null,{"css":[]});});</script>',
+                               'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",null,{"css":[]});});</script>',
                        ],
                        [
                                'context' => [],
@@ -242,14 +242,14 @@ Deprecation message.' ]
                                'modules' => [ 'test.scripts.user' ],
                                'only' => ResourceLoaderModule::TYPE_SCRIPTS,
                                'extra' => [],
-                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test.scripts.user\u0026only=scripts\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
+                               'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test.scripts.user\u0026only=scripts\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
                        ],
                        [
                                'context' => [],
                                'modules' => [ 'test.user' ],
                                'only' => ResourceLoaderModule::TYPE_COMBINED,
                                'extra' => [],
-                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test.user\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
+                               'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test.user\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
                        ],
                        [
                                'context' => [ 'debug' => 'true' ],
@@ -278,7 +278,7 @@ Deprecation message.' ]
                                'modules' => [ 'test.shouldembed' ],
                                'only' => ResourceLoaderModule::TYPE_COMBINED,
                                'extra' => [],
-                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
+                               'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
                        ],
                        [
                                'context' => [],
@@ -292,14 +292,14 @@ Deprecation message.' ]
                                'modules' => [ 'test.scripts.shouldembed' ],
                                'only' => ResourceLoaderModule::TYPE_SCRIPTS,
                                'extra' => [],
-                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
+                               'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
                        ],
                        [
                                'context' => [],
                                'modules' => [ 'test', 'test.shouldembed' ],
                                'only' => ResourceLoaderModule::TYPE_COMBINED,
                                'extra' => [],
-                               'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test\u0026skin=fallback");mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
+                               'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test\u0026skin=fallback");mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
                        ],
                        [
                                'context' => [],