From e04d6c318695a06f2cdd43f4e7785bd2eec123ca Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sun, 25 Aug 2019 17:24:55 +0100 Subject: [PATCH] resourceloader: Compile documentElement.className server-side Reduces output by not needlessly performing a change client-side for which we already know the result server-side. Bug: T231168 Change-Id: I4b8749f976d04d24f85236ddd641c7a4c7f6c23a --- includes/OutputPage.php | 7 ++++--- .../resourceloader/ResourceLoaderClientHtml.php | 13 ++++++++----- .../resourceloader/ResourceLoaderClientHtmlTest.php | 8 ++++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 3f2dcf7f4b..d63d376b62 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -3027,10 +3027,11 @@ class OutputPage extends ContextSource { $sitedir = MediaWikiServices::getInstance()->getContentLanguage()->getDir(); $pieces = []; - $pieces[] = Html::htmlHeader( Sanitizer::mergeAttributes( + $htmlAttribs = Sanitizer::mergeAttributes( $this->getRlClient()->getDocumentAttributes(), $sk->getHtmlElementAttributes() - ) ); + ); + $pieces[] = Html::htmlHeader( $htmlAttribs ); $pieces[] = Html::openElement( 'head' ); if ( $this->getHTMLTitle() == '' ) { @@ -3050,7 +3051,7 @@ class OutputPage extends ContextSource { } $pieces[] = Html::element( 'title', null, $this->getHTMLTitle() ); - $pieces[] = $this->getRlClient()->getHeadHtml(); + $pieces[] = $this->getRlClient()->getHeadHtml( $htmlAttribs['class'] ?? null ); $pieces[] = $this->buildExemptModules(); $pieces = array_merge( $pieces, array_values( $this->getHeadLinksArray() ) ); $pieces = array_merge( $pieces, array_values( $this->mHeadItems ) ); diff --git a/includes/resourceloader/ResourceLoaderClientHtml.php b/includes/resourceloader/ResourceLoaderClientHtml.php index e17b393a57..151b5fd502 100644 --- a/includes/resourceloader/ResourceLoaderClientHtml.php +++ b/includes/resourceloader/ResourceLoaderClientHtml.php @@ -240,19 +240,22 @@ class ResourceLoaderClientHtml { * - 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 = ResourceLoader::encodeJsonForScript( $jsClass ); + $script = <<'; // phpcs:enable @@ -152,7 +152,7 @@ Deprecation message.' ] ); // phpcs:disable Generic.Files.LineLength - $expected = '' . "\n" + $expected = '' . "\n" . ''; // phpcs:enable @@ -169,7 +169,7 @@ Deprecation message.' ] ); // phpcs:disable Generic.Files.LineLength - $expected = '' . "\n" + $expected = '' . "\n" . ''; // phpcs:enable -- 2.20.1