From 9838eeb6f271a24e612cab9c41ba9954b2b0c41f Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 25 Mar 2015 04:48:02 +0000 Subject: [PATCH] resourceloader: Add ResourceLoader::makeInlineScript utility and use it Plucked from the e86e5f846 which got reverted. Change-Id: I4bba3f3c31c5181867378ae174537429b49a50df --- includes/EditPage.php | 2 +- includes/OutputPage.php | 42 +++++++-------------- includes/debug/MWDebug.php | 6 +-- includes/resourceloader/ResourceLoader.php | 15 ++++++++ includes/skins/Skin.php | 4 +- includes/specials/SpecialJavaScriptTest.php | 16 ++++---- 6 files changed, 41 insertions(+), 44 deletions(-) diff --git a/includes/EditPage.php b/includes/EditPage.php index 1679c3b5b7..b0da562394 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -3749,7 +3749,7 @@ HTML } $script .= '});'; - $wgOut->addScript( Html::inlineScript( ResourceLoader::makeLoaderConditionalScript( $script ) ) ); + $wgOut->addScript( ResourceLoader::makeInlineScript( $script ) ); $toolbar = '
'; diff --git a/includes/OutputPage.php b/includes/OutputPage.php index cac89f4acd..28d55e430d 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2854,10 +2854,8 @@ class OutputPage extends ContextSource { $resourceLoader->makeModuleResponse( $context, $grpModules ) ); } else { - $links['html'] .= Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( - $resourceLoader->makeModuleResponse( $context, $grpModules ) - ) + $links['html'] .= ResourceLoader::makeInlineScript( + $resourceLoader->makeModuleResponse( $context, $grpModules ) ); } $links['html'] .= "\n"; @@ -2896,10 +2894,8 @@ class OutputPage extends ContextSource { if ( $only === ResourceLoaderModule::TYPE_STYLES ) { $link = Html::linkedStyle( $url ); } elseif ( $loadCall ) { - $link = Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( - Xml::encodeJsCall( 'mw.loader.load', array( $url, 'text/javascript', true ) ) - ) + $link = ResourceLoader::makeInlineScript( + Xml::encodeJsCall( 'mw.loader.load', array( $url, 'text/javascript', true ) ) ); } else { $link = Html::linkedScript( $url ); @@ -2908,10 +2904,8 @@ class OutputPage extends ContextSource { // browsers not supported by the startup module would unconditionally // execute this module. Otherwise users will get "ReferenceError: mw is // undefined" or "jQuery is undefined" from e.g. a "site" module. - $link = Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( - Xml::encodeJsCall( 'document.write', array( $link ) ) - ) + $link = ResourceLoader::makeInlineScript( + Xml::encodeJsCall( 'document.write', array( $link ) ) ); } @@ -2955,10 +2949,8 @@ class OutputPage extends ContextSource { } if ( count( $states ) ) { - $html = Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( - ResourceLoader::makeLoaderStateScript( $states ) - ) + $html = ResourceLoader::makeInlineScript( + ResourceLoader::makeLoaderStateScript( $states ) ) . "\n" . $html; } @@ -2977,10 +2969,8 @@ class OutputPage extends ContextSource { $links[] = $this->makeResourceLoaderLink( 'startup', ResourceLoaderModule::TYPE_SCRIPTS, true ); // Load config before anything else - $links[] = Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( - ResourceLoader::makeConfigSetScript( $this->getJSVars() ) - ) + $links[] = ResourceLoader::makeInlineScript( + ResourceLoader::makeConfigSetScript( $this->getJSVars() ) ); // Load embeddable private modules before any loader links @@ -3004,10 +2994,8 @@ class OutputPage extends ContextSource { // Only load modules that have marked themselves for loading at the top $modules = $this->getModules( true, 'top' ); if ( $modules ) { - $links[] = Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( - Xml::encodeJsCall( 'mw.loader.load', array( $modules ) ) - ) + $links[] = ResourceLoader::makeInlineScript( + Xml::encodeJsCall( 'mw.loader.load', array( $modules ) ) ); } @@ -3047,10 +3035,8 @@ class OutputPage extends ContextSource { // Only load modules that have marked themselves for loading at the bottom $modules = $this->getModules( true, 'bottom' ); if ( $modules ) { - $links[] = Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( - Xml::encodeJsCall( 'mw.loader.load', array( $modules, null, true ) ) - ) + $links[] = ResourceLoader::makeInlineScript( + Xml::encodeJsCall( 'mw.loader.load', array( $modules, null, true ) ) ); } diff --git a/includes/debug/MWDebug.php b/includes/debug/MWDebug.php index ae2d9954a0..1249ebae80 100644 --- a/includes/debug/MWDebug.php +++ b/includes/debug/MWDebug.php @@ -432,10 +432,8 @@ class MWDebug { // Cannot use OutputPage::addJsConfigVars because those are already outputted // by the time this method is called. - $html = Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( - ResourceLoader::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) ) - ) + $html = ResourceLoader::makeInlineScript( + ResourceLoader::makeConfigSetScript( array( 'debugInfo' => $debugInfo ) ) ); } diff --git a/includes/resourceloader/ResourceLoader.php b/includes/resourceloader/ResourceLoader.php index 3d5cc51590..f8d8d2f54b 100644 --- a/includes/resourceloader/ResourceLoader.php +++ b/includes/resourceloader/ResourceLoader.php @@ -1346,6 +1346,7 @@ MESSAGE; * Returns JS code which runs given JS code if the client-side framework is * present. * + * @deprecated since 1.25; use makeInlineScript instead * @param string $script JavaScript code * @return string */ @@ -1353,6 +1354,20 @@ MESSAGE; return "if(window.mw){\n" . trim( $script ) . "\n}"; } + /** + * Construct an inline script tag with given JS code. + * + * The code will be wrapped in a closure, and it will be executed by ResourceLoader + * only if the client has adequate support for MediaWiki JavaScript code. + * + * @param string $script JavaScript code + * @return string HTML + */ + public static function makeInlineScript( $script ) { + $js = self::makeLoaderConditionalScript( $script ); + return Html::inlineScript( $js ); + } + /** * Returns JS code which will set the MediaWiki configuration array to * the given value. diff --git a/includes/skins/Skin.php b/includes/skins/Skin.php index ac7a85ba44..07a2e87801 100644 --- a/includes/skins/Skin.php +++ b/includes/skins/Skin.php @@ -365,8 +365,8 @@ abstract class Skin extends ContextSource { */ static function makeVariablesScript( $data ) { if ( $data ) { - return Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( ResourceLoader::makeConfigSetScript( $data ) ) + return ResourceLoader::makeInlineScript( + ResourceLoader::makeConfigSetScript( $data ) ); } else { return ''; diff --git a/includes/specials/SpecialJavaScriptTest.php b/includes/specials/SpecialJavaScriptTest.php index ecb166a435..e9639e1944 100644 --- a/includes/specials/SpecialJavaScriptTest.php +++ b/includes/specials/SpecialJavaScriptTest.php @@ -168,15 +168,13 @@ HTML; // The testrunner configures QUnit and essentially depends on it. However, test suites // are reusable in environments that preload QUnit (or a compatibility interface to // another framework). Therefore we have to load it ourselves. - $out->addHtml( Html::inlineScript( - ResourceLoader::makeLoaderConditionalScript( - Xml::encodeJsCall( 'mw.loader.using', array( - array( 'jquery.qunit', 'jquery.qunit.completenessTest' ), - new XmlJsCode( - 'function () {' . Xml::encodeJsCall( 'mw.loader.load', array( $modules ) ) . '}' - ) - ) ) - ) + $out->addHtml( ResourceLoader::makeInlineScript( + Xml::encodeJsCall( 'mw.loader.using', array( + array( 'jquery.qunit', 'jquery.qunit.completenessTest' ), + new XmlJsCode( + 'function () {' . Xml::encodeJsCall( 'mw.loader.load', array( $modules ) ) . '}' + ) + ) ) ) ); } -- 2.20.1