resourceloader: Add ResourceLoader::makeInlineScript utility and use it

Plucked from the e86e5f846 which got reverted.

Change-Id: I4bba3f3c31c5181867378ae174537429b49a50df
This commit is contained in:
Timo Tijhof 2015-03-25 04:48:02 +00:00 committed by Ori.livneh
parent bddd43811e
commit 9838eeb6f2
6 changed files with 41 additions and 44 deletions

View file

@ -3749,7 +3749,7 @@ HTML
}
$script .= '});';
$wgOut->addScript( Html::inlineScript( ResourceLoader::makeLoaderConditionalScript( $script ) ) );
$wgOut->addScript( ResourceLoader::makeInlineScript( $script ) );
$toolbar = '<div id="toolbar"></div>';

View 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 ) )
);
}

View 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 ) )
);
}

View 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.

View 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 '';

View 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 ) ) . '}'
)
) )
) );
}