From 849182027431ddfe32060a99704a143d8312924c Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sat, 29 Aug 2015 00:20:53 +0200 Subject: [PATCH] resourceloader: Remove global startUp() callback Instead of a hardcoded JSONP-style global callback, use script load handlers. This uses the same logic as jQuery uses internally (ajax/script.js), which we call inside mediawiki.js. Inline those two lines here since this the code is what loads jQuery itself. Bug: T39894 Change-Id: I02b0d16a6ec3081dc551bfd9c3ae652707b7edcf --- resources/Resources.php | 1 - resources/src/mediawiki/mediawiki.startUp.js | 11 ---- resources/src/startup.js | 69 +++++++++++--------- 3 files changed, 39 insertions(+), 42 deletions(-) delete mode 100644 resources/src/mediawiki/mediawiki.startUp.js diff --git a/resources/Resources.php b/resources/Resources.php index 7794911e76..e21deb9d98 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -827,7 +827,6 @@ return array( 'resources/lib/phpjs-sha1/sha1.js', 'resources/src/mediawiki/mediawiki.js', 'resources/src/mediawiki/mediawiki.errorLogger.js', - 'resources/src/mediawiki/mediawiki.startUp.js', ), 'debugScripts' => 'resources/src/mediawiki/mediawiki.log.js', 'targets' => array( 'desktop', 'mobile' ), diff --git a/resources/src/mediawiki/mediawiki.startUp.js b/resources/src/mediawiki/mediawiki.startUp.js deleted file mode 100644 index 028784c29b..0000000000 --- a/resources/src/mediawiki/mediawiki.startUp.js +++ /dev/null @@ -1,11 +0,0 @@ -/*! - * Auto-register from pre-loaded startup scripts - */ -( function ( $ ) { - 'use strict'; - - if ( $.isFunction( window.startUp ) ) { - window.startUp(); - window.startUp = undefined; - } -}( jQuery ) ); diff --git a/resources/src/startup.js b/resources/src/startup.js index 3b79bd39d0..eb4dc9fc21 100644 --- a/resources/src/startup.js +++ b/resources/src/startup.js @@ -68,39 +68,48 @@ function isCompatible( ua ) { ); } -/** - * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php. - */ -function startUp() { - mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals ); +// Conditional script injection +( function () { + if ( !isCompatible() ) { + // Undo class swapping in case of an unsupported browser. + // See OutputPage::getHeadScripts(). + document.documentElement.className = document.documentElement.className + .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' ); + } + + /** + * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php. + */ + function startUp() { + mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals ); - $CODE.registrations(); + $CODE.registrations(); - mw.config.set( $VARS.configuration ); + mw.config.set( $VARS.configuration ); - // Must be after mw.config.set because these callbacks may use mw.loader which - // needs to have values 'skin', 'debug' etc. from mw.config. - window.RLQ = window.RLQ || []; - while ( RLQ.length ) { - RLQ.shift()(); + // Must be after mw.config.set because these callbacks may use mw.loader which + // needs to have values 'skin', 'debug' etc. from mw.config. + window.RLQ = window.RLQ || []; + while ( RLQ.length ) { + RLQ.shift()(); + } + window.RLQ = { + push: function ( fn ) { + fn(); + } + }; } - window.RLQ = { - push: function ( fn ) { - fn(); + + var script = document.createElement( 'script' ); + script.src = $VARS.baseModulesUri; + script.onload = script.onreadystatechange = function () { + if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) { + // Clean up + script.onload = script.onreadystatechange = null; + script = null; + // Callback + startUp(); } }; -} - -// Conditional script injection -if ( isCompatible() ) { - ( function () { - var script = document.createElement( 'script' ); - script.src = $VARS.baseModulesUri; - document.getElementsByTagName( 'head' )[0].appendChild( script ); - }() ); -} else { - // Undo class swapping in case of an unsupported browser. - // See OutputPage::getHeadScripts(). - document.documentElement.className = document.documentElement.className - .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' ); -} + document.getElementsByTagName( 'head' )[0].appendChild( script ); +}() ); -- 2.20.1