resourceloader: Add ResourceLoader::makeInlineScript utility and use it
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 25 Mar 2015 04:48:02 +0000 (04:48 +0000)
committerOri.livneh <ori@wikimedia.org>
Mon, 20 Apr 2015 19:45:27 +0000 (19:45 +0000)
Plucked from the e86e5f846 which got reverted.

Change-Id: I4bba3f3c31c5181867378ae174537429b49a50df

includes/EditPage.php
includes/OutputPage.php
includes/debug/MWDebug.php
includes/resourceloader/ResourceLoader.php
includes/skins/Skin.php
includes/specials/SpecialJavaScriptTest.php

index 1679c3b..b0da562 100644 (file)
@@ -3749,7 +3749,7 @@ HTML
                }
 
                $script .= '});';
-               $wgOut->addScript( Html::inlineScript( ResourceLoader::makeLoaderConditionalScript( $script ) ) );
+               $wgOut->addScript( ResourceLoader::makeInlineScript( $script ) );
 
                $toolbar = '<div id="toolbar"></div>';
 
index cac89f4..28d55e4 100644 (file)
@@ -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 ) )
                        );
                }
 
index ae2d995..1249eba 100644 (file)
@@ -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 ) )
                        );
                }
 
index 3d5cc51..f8d8d2f 100644 (file)
@@ -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.
index ac7a85b..07a2e87 100644 (file)
@@ -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 '';
index ecb166a..e9639e1 100644 (file)
@@ -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 ) ) . '}'
+                               )
+                       ) )
                ) );
        }