Merge "RCFilters: Filter duplicates when filtering for multiple tags"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
index c11fe5b..2f29200 100644 (file)
@@ -178,7 +178,7 @@ class ResourceLoader implements LoggerAwareInterface {
         * @return string Filtered data, or a comment containing an error message
         */
        public static function filter( $filter, $data, array $options = [] ) {
-               if ( strpos( $data, ResourceLoader::FILTER_NOMIN ) !== false ) {
+               if ( strpos( $data, self::FILTER_NOMIN ) !== false ) {
                        return $data;
                }
 
@@ -1079,7 +1079,7 @@ MESSAGE;
                                                                // mw.loader.implement will use globalEval if scripts is a string.
                                                                // Minify manually here, because general response minification is
                                                                // not effective due it being a string literal, not a function.
-                                                               if ( !ResourceLoader::inDebugMode() ) {
+                                                               if ( !self::inDebugMode() ) {
                                                                        $scripts = self::filter( 'minify-js', $scripts ); // T107377
                                                                }
                                                        } else {
@@ -1100,7 +1100,12 @@ MESSAGE;
                                        $strContent = self::filter( $filter, $strContent );
                                }
 
-                               $out .= $strContent;
+                               if ( $context->getOnly() === 'scripts' ) {
+                                       // Use a linebreak between module scripts (T162719)
+                                       $out .= $this->ensureNewline( $strContent );
+                               } else {
+                                       $out .= $strContent;
+                               }
 
                        } catch ( Exception $e ) {
                                $this->outputErrorAndLog( $e, 'Generating module package failed: {exception}' );
@@ -1128,18 +1133,32 @@ MESSAGE;
                                if ( !$context->getDebug() ) {
                                        $stateScript = self::filter( 'minify-js', $stateScript );
                                }
-                               $out .= $stateScript;
+                               // Use a linebreak between module script and state script (T162719)
+                               $out = $this->ensureNewline( $out ) . $stateScript;
                        }
                } else {
                        if ( count( $states ) ) {
                                $this->errors[] = 'Problematic modules: ' .
-                                       FormatJson::encode( $states, ResourceLoader::inDebugMode() );
+                                       FormatJson::encode( $states, self::inDebugMode() );
                        }
                }
 
                return $out;
        }
 
+       /**
+        * Ensure the string is either empty or ends in a line break
+        * @param string $str
+        * @return string
+        */
+       private function ensureNewline( $str ) {
+               $end = substr( $str, -1 );
+               if ( $end === false || $end === "\n" ) {
+                       return $str;
+               }
+               return $str . "\n";
+       }
+
        /**
         * Get names of modules that use a certain message.
         *
@@ -1195,7 +1214,7 @@ MESSAGE;
                ];
                self::trimArray( $module );
 
-               return Xml::encodeJsCall( 'mw.loader.implement', $module, ResourceLoader::inDebugMode() );
+               return Xml::encodeJsCall( 'mw.loader.implement', $module, self::inDebugMode() );
        }
 
        /**
@@ -1209,7 +1228,7 @@ MESSAGE;
                return Xml::encodeJsCall(
                        'mw.messages.set',
                        [ (object)$messages ],
-                       ResourceLoader::inDebugMode()
+                       self::inDebugMode()
                );
        }
 
@@ -1266,13 +1285,13 @@ MESSAGE;
                        return Xml::encodeJsCall(
                                'mw.loader.state',
                                [ $name ],
-                               ResourceLoader::inDebugMode()
+                               self::inDebugMode()
                        );
                } else {
                        return Xml::encodeJsCall(
                                'mw.loader.state',
                                [ $name, $state ],
-                               ResourceLoader::inDebugMode()
+                               self::inDebugMode()
                        );
                }
        }
@@ -1298,7 +1317,7 @@ MESSAGE;
                return Xml::encodeJsCall(
                        "( function ( name, version, dependencies, group, source ) {\n\t$script\n} )",
                        [ $name, $version, $dependencies, $group, $source ],
-                       ResourceLoader::inDebugMode()
+                       self::inDebugMode()
                );
        }
 
@@ -1390,7 +1409,7 @@ MESSAGE;
                        return Xml::encodeJsCall(
                                'mw.loader.register',
                                [ $name ],
-                               ResourceLoader::inDebugMode()
+                               self::inDebugMode()
                        );
                } else {
                        $registration = [ $name, $version, $dependencies, $group, $source, $skip ];
@@ -1398,7 +1417,7 @@ MESSAGE;
                        return Xml::encodeJsCall(
                                'mw.loader.register',
                                $registration,
-                               ResourceLoader::inDebugMode()
+                               self::inDebugMode()
                        );
                }
        }
@@ -1422,13 +1441,13 @@ MESSAGE;
                        return Xml::encodeJsCall(
                                'mw.loader.addSource',
                                [ $id ],
-                               ResourceLoader::inDebugMode()
+                               self::inDebugMode()
                        );
                } else {
                        return Xml::encodeJsCall(
                                'mw.loader.addSource',
                                [ $id, $loadUrl ],
-                               ResourceLoader::inDebugMode()
+                               self::inDebugMode()
                        );
                }
        }
@@ -1475,7 +1494,7 @@ MESSAGE;
                return Xml::encodeJsCall(
                        'mw.config.set',
                        [ $configuration ],
-                       ResourceLoader::inDebugMode()
+                       self::inDebugMode()
                );
        }