resourceloader: Move style deprecation warnings to <body>
authorTimo Tijhof <krinklemail@gmail.com>
Tue, 22 May 2018 17:58:04 +0000 (18:58 +0100)
committerKrinkle <krinklemail@gmail.com>
Tue, 22 May 2018 22:14:22 +0000 (22:14 +0000)
Follows-up If35a106c7. These log messages are not criticial and
should not be in the <head> competing with stylesheets and article
content. Move them to the end of <body> instead, nearby other
low-priority script tags.

The getBodyHtml() method from ClientHtml was empty, but has been
non-empty in the past. It's fine to repopulate.

Also, while ClientHtml::getBodyHtml was empty, there are additional
RLQ scripts created by OutputPage that do exist even without this.
Namely, there is a <script> for wgPageParseReport, and one for
wgBackendResponseTime etc.

Change-Id: Ibda7091bdcd5ed207395b20196cdc33df926a24c

includes/resourceloader/ResourceLoaderClientHtml.php
tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php

index 3ba63cf..b49a2da 100644 (file)
@@ -146,7 +146,7 @@ class ResourceLoaderClientHtml {
                                'general' => [],
                        ],
                        // Deprecations for style-only modules
-                       'styledeprecations' => [],
+                       'styleDeprecations' => [],
                ];
 
                foreach ( $this->modules as $name ) {
@@ -213,7 +213,7 @@ class ResourceLoaderClientHtml {
                        }
                        $deprecation = $module->getDeprecationInformation();
                        if ( $deprecation ) {
-                               $data['styledeprecations'][] = $deprecation;
+                               $data['styleDeprecations'][] = $deprecation;
                        }
                }
 
@@ -318,14 +318,6 @@ class ResourceLoaderClientHtml {
                        );
                }
 
-               // Deprecations for only=styles modules
-               if ( $data['styledeprecations'] ) {
-                       $chunks[] = ResourceLoader::makeInlineScript(
-                               implode( '', $data['styledeprecations'] ),
-                               $nonce
-                       );
-               }
-
                // External stylesheets (only=styles)
                if ( $data['styles'] ) {
                        $chunks[] = $this->getLoad(
@@ -366,7 +358,18 @@ class ResourceLoaderClientHtml {
         * @return string|WrappedStringList HTML
         */
        public function getBodyHtml() {
-               return '';
+               $data = $this->getData();
+               $chunks = [];
+
+               // Deprecations for only=styles modules
+               if ( $data['styleDeprecations'] ) {
+                       $chunks[] = ResourceLoader::makeInlineScript(
+                               implode( '', $data['styleDeprecations'] ),
+                               $this->options['nonce']
+                       );
+               }
+
+               return WrappedStringList::join( "\n", $chunks );
        }
 
        private function getContext( $group, $type ) {
index 9b03c5c..7cd6983 100644 (file)
@@ -176,7 +176,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase {
                                        'test.user',
                                ],
                        ],
-                       'styledeprecations' => [
+                       'styleDeprecations' => [
                                Xml::encodeJsCall(
                                        'mw.log.warn',
                                        [ 'This page is using the deprecated ResourceLoader module "test.styles.deprecated".
@@ -228,7 +228,6 @@ Deprecation message.' ]
                        . 'mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
                        . 'mw.loader.load(["test"]);'
                        . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
-                       . 'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");'
                        . '});</script>' . "\n"
                        . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.deprecated%2Cpure&amp;only=styles&amp;skin=fallback"/>' . "\n"
                        . '<style>.private{}</style>' . "\n"
@@ -304,18 +303,23 @@ Deprecation message.' ]
                $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',
                        'test.private.bottom',
                ] );
+               $client->setModuleStyles( [
+                       'test.styles.deprecated',
+               ] );
                $client->setModuleScripts( [
                        'test.scripts',
                ] );
-
-               $expected = '';
-               $expected = self::expandVariables( $expected );
+               // phpcs:disable Generic.Files.LineLength
+               $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){'
+                       . 'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");'
+                       . '});</script>';
+               // phpcs:enable
 
                $this->assertEquals( $expected, $client->getBodyHtml() );
        }