);
$context = new ResourceLoaderContext( $resourceLoader, new FauxRequest( $query ) );
- // Extract modules that know they're empty
+
+ // Extract modules that know they're empty and see if we have one or more
+ // raw modules
+ $isRaw = false;
foreach ( $grpModules as $key => $module ) {
// Inline empty modules: since they're empty, just mark them as 'ready' (bug 46857)
// If we're only getting the styles, we don't need to do anything for empty modules.
$links['states'][$key] = 'ready';
}
}
+
+ $isRaw |= $module->isRaw();
}
// If there are no non-empty modules, skip this group
);
} else {
$link = Html::linkedScript( $url );
+ if ( $context->getOnly() === 'scripts' && !$context->getRaw() && !$isRaw ) {
+ // Wrap only=script requests in a conditional as 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 ) )
+ )
+ );
+ }
// For modules requested directly in the html via <link> or <script>,
// tell mw.loader they are being loading to prevent duplicate requests.
if ( count( $states ) ) {
$out .= self::makeLoaderStateScript( $states );
}
-
- if ( $context->getOnly() === 'scripts' ) {
- // In only=script requests for modules that are not raw (e.g. not the startup module)
- // ensure the execution is conditional to avoid situations where browsers with an
- // unsupported environment do unconditionally execute a module's scripts. Otherwise users
- // will get things like "ReferenceError: mw is undefined" or "jQuery is undefined" from
- // legacy scripts loaded with only=scripts (such as the 'site' module).
- $out = self::makeLoaderConditionalScript( $out );
- }
} else {
if ( count( $states ) ) {
$exceptions .= self::makeComment(
protected $script = '';
protected $styles = '';
protected $skipFunction = null;
+ protected $isRaw = false;
protected $targets = array( 'test' );
public function __construct( $options = array() ) {
public function getSkipFunction() {
return $this->skipFunction;
}
+
+ public function isRaw() {
+ return $this->isRaw;
+ }
}
class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
// Load module script only
array(
array( 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ),
- '<script src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.foo&only=scripts&skin=fallback&*"></script>
+ '<script>if(window.mw){
+document.write("\u003Cscript src=\"http://127.0.0.1:8080/w/load.php?debug=false\u0026amp;lang=en\u0026amp;modules=test.foo\u0026amp;only=scripts\u0026amp;skin=fallback\u0026amp;*\"\u003E\u003C/script\u003E");
+}</script>
+'
+ ),
+ array(
+ // Don't condition wrap raw modules (like the startup module)
+ array( 'test.raw', ResourceLoaderModule::TYPE_SCRIPTS ),
+ '<script src="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.raw&only=scripts&skin=fallback&*"></script>
'
),
// Load module styles only
'
),
// Load private module (only=scripts)
- // This is asserted for completion (would get two condition wrappers),
- // though in practice we'd never embed a module with only=scripts,
- // that mode is reserved for hardcoded requests. Embedded modules
- // would always be combined.
array(
array( 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ),
'<script>if(window.mw){
-if(window.mw){mw.test.baz({token:123});mw.loader.state({"test.quux":"ready"});}
+mw.test.baz({token:123});mw.loader.state({"test.quux":"ready"});
}</script>
'
'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
'group' => 'private',
)),
+ 'test.raw' => new ResourceLoaderTestModule( array(
+ 'script' => 'mw.test.baz( { token: 123 } );',
+ 'isRaw' => true,
+ )),
'test.noscript' => new ResourceLoaderTestModule( array(
'styles' => '.mw-test-noscript { content: "style"; }',
'group' => 'noscript',
)),
'test.group.bar' => new ResourceLoaderTestModule( array(
- 'styles' => '.mw-group-bar { content: "style"; }',
- 'group' => 'bar',
+ 'styles' => '.mw-group-bar { content: "style"; }',
+ 'group' => 'bar',
)),
'test.group.foo' => new ResourceLoaderTestModule( array(
- 'styles' => '.mw-group-foo { content: "style"; }',
- 'group' => 'foo',
+ 'styles' => '.mw-group-foo { content: "style"; }',
+ 'group' => 'foo',
)),
) );
$links = $method->invokeArgs( $out, $args );