$rlClient = new ResourceLoaderClientHtml( $context, [
'target' => $this->getTarget(),
+ 'nonce' => $this->getCSPNonce(),
] );
$rlClient->setConfig( $this->getJSVars() );
$rlClient->setModules( $this->getModules( /*filter*/ true ) );
}
$pieces[] = Html::element( 'title', null, $this->getHTMLTitle() );
- $pieces[] = $this->getRlClient()->getHeadHtml( $this->getCSPNonce() );
+ $pieces[] = $this->getRlClient()->getHeadHtml();
$pieces[] = $this->buildExemptModules();
$pieces = array_merge( $pieces, array_values( $this->getHeadLinksArray() ) );
$pieces = array_merge( $pieces, array_values( $this->mHeadItems ) );
* startup module if the client has adequate support for MediaWiki JavaScript code.
*
* @param string $script JavaScript code
- * @param string $nonce Content-security-policy nonce, from OutputPage::getCSPNonce()
+ * @param string $nonce [optional] Content-Security-Policy nonce (from OutputPage::getCSPNonce)
* @return WrappedString HTML
*/
public static function makeInlineScript( $script, $nonce = null ) {
* @param ResourceLoaderContext $context
* @param array $options [optional] Array of options
* - 'target': Custom parameter passed to StartupModule.
+ * - 'nonce': From OutputPage::getCSPNonce().
*/
public function __construct( ResourceLoaderContext $context, array $options = [] ) {
$this->context = $context;
$this->resourceLoader = $context->getResourceLoader();
- $this->options = $options;
+ $this->options = $options + [
+ 'target' => null,
+ 'nonce' => null,
+ ];
}
/**
* - Inline scripts can't be asynchronous.
* - For styles, earlier is better.
*
- * @param string $nonce From OutputPage::getCSPNonce()
* @return string|WrappedStringList HTML
*/
- public function getHeadHtml( $nonce ) {
+ public function getHeadHtml() {
+ $nonce = $this->options['nonce'];
$data = $this->getData();
$chunks = [];
// Async scripts. Once the startup is loaded, inline RLQ scripts will run.
// Pass-through a custom 'target' from OutputPage (T143066).
- $startupQuery = isset( $this->options['target'] )
+ $startupQuery = $this->options['target'] !== null
? [ 'target' => (string)$this->options['target'] ]
: [];
$chunks[] = $this->getLoad(
* @param ResourceLoaderContext $mainContext
* @param array $modules One or more module names
* @param string $only ResourceLoaderModule TYPE_ class constant
- * @param array $extraQuery Array with extra query parameters for the request
- * @param string $nonce See OutputPage::getCSPNonce() [Since 1.32]
+ * @param array $extraQuery [optional] Array with extra query parameters for the request
+ * @param string $nonce [optional] Content-Security-Policy nonce (from OutputPage::getCSPNonce)
* @return string|WrappedStringList HTML
*/
public static function makeLoad( ResourceLoaderContext $mainContext, array $modules, $only,
- array $extraQuery, $nonce
+ array $extraQuery = [], $nonce = null
) {
$rl = $mainContext->getResourceLoader();
$chunks = [];
$context = self::makeContext();
$context->getResourceLoader()->register( self::makeSampleModules() );
- $client = new ResourceLoaderClientHtml( $context );
+ $client = new ResourceLoaderClientHtml( $context, [
+ 'nonce' => false,
+ ] );
$client->setConfig( [ 'key' => 'value' ] );
$client->setModules( [
'test',
// phpcs:enable
$expected = self::expandVariables( $expected );
- $this->assertEquals( $expected, $client->getHeadHtml( false ) );
+ $this->assertEquals( $expected, $client->getHeadHtml() );
}
/**
. '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback&target=example"></script>';
// phpcs:enable
- $this->assertEquals( $expected, $client->getHeadHtml( false ) );
+ $this->assertEquals( $expected, $client->getHeadHtml() );
}
/**
. '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
// phpcs:enable
- $this->assertEquals( $expected, $client->getHeadHtml( false ) );
+ $this->assertEquals( $expected, $client->getHeadHtml() );
}
/**