Use a `/*!` comment for the CSS header
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 3 Feb 2021 13:42:32 +0000 (14:42 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 3 Feb 2021 15:00:43 +0000 (16:00 +0100)
This change stops the `Compressed` formatter from removing the comment,
allowing us to remove the `insertHeader()` logic which looked somewhat fragile.

wcfsetup/install/files/lib/system/style/StyleCompiler.class.php

index dcfdca70bf43e1f660ce9ed7b2dcceb3997a9ba0..4dcf531d2b3d2a79decebebecf18730e3fa24573 100644 (file)
@@ -162,7 +162,8 @@ final class StyleCompiler extends SingletonFactory
             $files[] = $customCustomSCSSFile;
         }
 
-        $scss = $this->bootstrap();
+        $scss = "/*!\n\nstylesheet for '" . $styleName . "', generated on " . \gmdate('r') . " -- DO NOT EDIT\n\n*/\n";
+        $scss .= $this->bootstrap();
         foreach ($files as $file) {
             $scss .= $this->prepareFile($file);
         }
@@ -177,10 +178,6 @@ final class StyleCompiler extends SingletonFactory
                 $variables
             );
 
-            $header = "/* stylesheet for '" . $styleName . "', generated on " . \gmdate('r') . " -- DO NOT EDIT */";
-
-            $css = $this->injectHeader($header, $css);
-
             $this->writeCss(FileUtil::addTrailingSlash($testFileDir) . 'style', $css);
         } catch (\Exception $e) {
             return $e;
@@ -269,7 +266,8 @@ final class StyleCompiler extends SingletonFactory
         $parameters = ['scss' => ''];
         EventHandler::getInstance()->fireAction($this, 'compile', $parameters);
 
-        $scss = $this->bootstrap();
+        $scss = "/*!\n\nstylesheet for '" . $style->styleName . "', generated on " . \gmdate('r') . " -- DO NOT EDIT\n\n*/\n";
+        $scss .= $this->bootstrap();
         foreach ($this->getFiles() as $file) {
             $scss .= $this->prepareFile($file);
         }
@@ -283,10 +281,6 @@ final class StyleCompiler extends SingletonFactory
             $variables
         );
 
-        $header = "/* stylesheet for '" . $style->styleName . "', generated on " . \gmdate('r') . " -- DO NOT EDIT */";
-
-        $css = $this->injectHeader($header, $css);
-
         $this->writeCss($this->getFilenameForStyle($style), $css);
     }
 
@@ -330,7 +324,8 @@ final class StyleCompiler extends SingletonFactory
 
         $variables['style_image_path'] = "'../images/'";
 
-        $scss = $this->bootstrap();
+        $scss = "/*!\n\nstylesheet for the admin panel, generated on " . \gmdate('r') . " -- DO NOT EDIT\n\n*/\n";
+        $scss .= $this->bootstrap();
         foreach ($files as $file) {
             $scss .= $this->prepareFile($file);
         }
@@ -345,25 +340,9 @@ final class StyleCompiler extends SingletonFactory
         $css = \str_replace('../icon/', '../../icon/', $css);
         $css = \preg_replace('~\.\./images/~', '../../images/', $css);
 
-        $header = "/* stylesheet for the admin panel, generated on " . \gmdate('r') . " -- DO NOT EDIT */";
-
-        $css = $this->injectHeader($header, $css);
-
         $this->writeCss(WCF_DIR . 'acp/style/style', $css);
     }
 
-    /**
-     * Injects the given header string into the given css while ensuring
-     * that the charset at-rule remains at the beginning.
-     */
-    private function injectHeader(string $header, string $css): string
-    {
-        // Strip charset at-rule.
-        $css = \preg_replace('~^@charset "UTF-8";\r?\n~', '', $css);
-
-        return '@charset "UTF-8";' . "\n\n{$header}\n\n{$css}";
-    }
-
     /**
      * Returns a list of common stylesheets provided by the core.
      *