From: Timo Tijhof Date: Mon, 8 Sep 2014 16:31:54 +0000 (+0200) Subject: resourceloader: Condition-wrap the HTML tag instead of JS response X-Git-Tag: 1.31.0-rc.0~14041^2 X-Git-Url: http://git.cyclocoop.org/%24self?a=commitdiff_plain;h=9d390a09cdfb47cca33a0d6f4346053952e68894;p=lhc%2Fweb%2Fwiklou.git resourceloader: Condition-wrap the HTML tag instead of JS response Follows-up 9272bc6c47, 03c503da22, 1e063f6078. One can't wrap arbitrary JavaScript in an if-statement and have its inner-body mean exactly the same. Certain statements are only allowed in the top of a scope (such as hoisted function declarations). These are not allowed inside a block. They're fine in both global scope and local function scope, but not inside an if-block of any scope. The ECMAScript spec only describes what is an allowed token. Any unexpected token should result in a SyntaxError. Chrome's implementation (V8) allows function declarations in blocks and hoists them to *outside* the condition. Firefox's SpiderMonkey silently ignores the statement. Neither throw a SyntaxError. Rgular ResourceLoader responses only contain mw.loader.implement() and mw.loader.state() call which could be wrapped without issues. However such responses don't need wrapping as they're only made by mediawiki.js (in which case mw is obviously loaded). The wrapping is for legacy scripts that execute in the global scope. For those, let's wrap the script tag itself (instead of the response). That seems like the most water-tight and semantically correct solution. Had to bring in $isRaw from ResourceLoader.php, else the startup module would have been wrapped as well (added regression test). Bug: 69924 Change-Id: Iedda0464f734ba5f7a884726487f6c7e07d444f1 --- diff --git a/includes/OutputPage.php b/includes/OutputPage.php index af90ca6da4..22a601222a 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2769,7 +2769,10 @@ $templates ); $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. @@ -2779,6 +2782,8 @@ $templates $links['states'][$key] = 'ready'; } } + + $isRaw |= $module->isRaw(); } // If there are no non-empty modules, skip this group @@ -2845,6 +2850,17 @@ $templates ); } 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 or + ' +' + ), + array( + // Don't condition wrap raw modules (like the startup module) + array( 'test.raw', ResourceLoaderModule::TYPE_SCRIPTS ), + ' ' ), // Load module styles only @@ -152,14 +160,10 @@ class OutputPageTest extends MediaWikiTestCase { ' ), // 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 ), ' ' @@ -244,17 +248,21 @@ mw.loader.implement("test.quux",function($,jQuery){mw.test.baz({token:123});},{" '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 );