From: Tim Düsterhus Date: Wed, 3 Feb 2021 13:10:43 +0000 (+0100) Subject: Add StyleCompiler::injectHeader() X-Git-Tag: 5.4.0_Alpha_1~316^2~7 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=42868005e29ceb37e773dd137bde5d8f23e2b308;p=GitHub%2FWoltLab%2FWCF.git Add StyleCompiler::injectHeader() --- diff --git a/wcfsetup/install/files/lib/system/style/StyleCompiler.class.php b/wcfsetup/install/files/lib/system/style/StyleCompiler.class.php index 85025951f0..366922e3c8 100644 --- a/wcfsetup/install/files/lib/system/style/StyleCompiler.class.php +++ b/wcfsetup/install/files/lib/system/style/StyleCompiler.class.php @@ -168,14 +168,10 @@ final class StyleCompiler extends SingletonFactory $files, $variables, $individualScss . (!empty($parameters['scss']) ? "\n" . $parameters['scss'] : ''), - static function ($content) use ($styleName) { + function ($css) use ($styleName) { $header = "/* stylesheet for '" . $styleName . "', generated on " . \gmdate('r') . " -- DO NOT EDIT */"; - return '@charset "UTF-8";' . "\n\n{$header}\n\n" . \preg_replace( - '~^@charset "UTF-8";\r?\n~', - '', - $content - ); + return $this->injectHeader($header, $css); } ); } catch (\Exception $e) { @@ -270,14 +266,10 @@ final class StyleCompiler extends SingletonFactory $this->getFiles(), $variables, $individualScss . (!empty($parameters['scss']) ? "\n" . $parameters['scss'] : ''), - static function ($content) use ($style) { + function ($css) use ($style) { $header = "/* stylesheet for '" . $style->styleName . "', generated on " . \gmdate('r') . " -- DO NOT EDIT */"; - return '@charset "UTF-8";' . "\n\n{$header}\n\n" . \preg_replace( - '~^@charset "UTF-8";\r?\n~', - '', - $content - ); + return $this->injectHeader($header, $css); } ); } @@ -327,23 +319,31 @@ final class StyleCompiler extends SingletonFactory $files, $variables, '', - static function ($content) { + function ($css) { // fix relative paths - $content = \str_replace('../font/', '../../font/', $content); - $content = \str_replace('../icon/', '../../icon/', $content); - $content = \preg_replace('~\.\./images/~', '../../images/', $content); + $css = \str_replace('../font/', '../../font/', $css); + $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 */"; - return '@charset "UTF-8";' . "\n\n{$header}\n\n" . \preg_replace( - '~^@charset "UTF-8";\r?\n~', - '', - $content - ); + return $this->injectHeader($header, $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. *