/**
* Load and configure a ResourceLoader client on an HTML page.
*
+ * @ingroup ResourceLoader
* @since 1.28
*/
class ResourceLoaderClientHtml {
// Load from load.php?only=styles via <link rel=stylesheet>
$data['styles'][] = $name;
}
- $deprecation = $module->getDeprecationInformation();
+ $deprecation = $module->getDeprecationInformation( $context );
if ( $deprecation ) {
$data['styleDeprecations'][] = $deprecation;
}
* - Inline scripts can't be asynchronous.
* - For styles, earlier is better.
*
+ * @param string|null $nojsClass Class name that caller uses on HTML document element
* @return string|WrappedStringList HTML
*/
- public function getHeadHtml() {
+ public function getHeadHtml( $nojsClass = null ) {
$nonce = $this->options['nonce'];
$data = $this->getData();
$chunks = [];
// Change "client-nojs" class to client-js. This allows easy toggling of UI components.
// This must happen synchronously on every page view to avoid flashes of wrong content.
- // See also #getDocumentAttributes() and /resources/src/startup.js.
- $script = <<<'JAVASCRIPT'
-document.documentElement.className = document.documentElement.className
- .replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );
+ // See also startup/startup.js.
+ $nojsClass = $nojsClass ?? $this->getDocumentAttributes()['class'];
+ $jsClass = preg_replace( '/(^|\s)client-nojs(\s|$)/', '$1client-js$2', $nojsClass );
+ $jsClassJson = $this->context->encodeJson( $jsClass );
+ $script = <<<JAVASCRIPT
+document.documentElement.className = {$jsClassJson};
JAVASCRIPT;
// Inline script: Declare mw.config variables for this page.
if ( $this->config ) {
- $confJson = ResourceLoader::encodeJsonForScript( $this->config );
+ $confJson = $this->context->encodeJson( $this->config );
$script .= <<<JAVASCRIPT
RLCONF = {$confJson};
JAVASCRIPT;
// Inline script: Declare initial module states for this page.
$states = array_merge( $this->exemptStates, $data['states'] );
if ( $states ) {
- $stateJson = ResourceLoader::encodeJsonForScript( $states );
+ $stateJson = $this->context->encodeJson( $states );
$script .= <<<JAVASCRIPT
RLSTATE = {$stateJson};
JAVASCRIPT;
// Inline script: Declare general modules to load on this page.
if ( $data['general'] ) {
- $pageModulesJson = ResourceLoader::encodeJsonForScript( $data['general'] );
+ $pageModulesJson = $this->context->encodeJson( $data['general'] );
$script .= <<<JAVASCRIPT
RLPAGEMODULES = {$pageModulesJson};
JAVASCRIPT;
] );
} else {
$chunk = ResourceLoader::makeInlineScript(
- Xml::encodeJsCall( 'mw.loader.load', [ $url ] ),
+ 'mw.loader.load(' . $mainContext->encodeJson( $url ) . ');',
$nonce
);
}