‪TYPO3CMS  ‪main
ScssProcessor.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use ScssPhp\ScssPhp\Compiler;
21 
29 {
30  public function ‪__construct(
31  protected readonly Compiler $scssCompiler
32  ) {
33  }
34 
38  public function ‪compileToCss(string $scssSource): string
39  {
40  return $this->scssCompiler->compileString($scssSource)->getCss();
41  }
42 
46  public function ‪prefixCssForScss(string $cssPrefix, string $cssSource): string
47  {
48  // replace body and html with the prefix
49  foreach (['html', 'body'] as $cssSelector) {
50  $cssSource = preg_replace('/(?<![-.#\w])\b' . preg_quote($cssSelector, '/') . '\b(?!-)/i', '&', $cssSource);
51  }
52 
53  // Some CSS minifier remove the semicolon before the curly brace
54  // While this is valid CSS, the ScssPHP Parser is unable to identify
55  // the end of a declaration block. We are adding them, to avoid
56  // parsing errors. Superfluous semicolons will be dropped by the parser.
57  $cssSource = str_replace('}', ';}', $cssSource);
58 
59  // add prefix to all CSS definitions by wrapping it in SCSS group
60  $cssSource = sprintf("%s {\n%s\n}", $cssPrefix, $cssSource);
61 
62  // Moving CSS variables assigned to :root to the new parent.
63  $cssSource = str_replace(':root', '&', $cssSource);
64 
65  return $cssSource;
66  }
67 }
‪TYPO3\CMS\RteCKEditor\Service\ScssProcessor\prefixCssForScss
‪prefixCssForScss(string $cssPrefix, string $cssSource)
Definition: ScssProcessor.php:46
‪TYPO3\CMS\RteCKEditor\Service
Definition: ScssProcessor.php:18
‪TYPO3\CMS\RteCKEditor\Service\ScssProcessor\compileToCss
‪compileToCss(string $scssSource)
Definition: ScssProcessor.php:38
‪TYPO3\CMS\RteCKEditor\Service\ScssProcessor
Definition: ScssProcessor.php:29
‪TYPO3\CMS\RteCKEditor\Service\ScssProcessor\__construct
‪__construct(protected readonly Compiler $scssCompiler)
Definition: ScssProcessor.php:30