From 330e626ad0822066984e67da6889c7fe13422909 Mon Sep 17 00:00:00 2001 From: Achim Fritz Date: Tue, 6 Dec 2022 09:29:24 +0100 Subject: [PATCH] [TASK] resolve relative Paths in CSS (#15) With new cms-composer-installers we can no longer use paths like /typo3conf/ext/ in our CSS-Files in urls (e.g. for background-images) and therefore we have to change the paths to relative paths. This PR resolves the relative paths in the CSS-File for inline style tag. Co-authored-by: Achim Fritz --- Classes/AssetCollector.php | 39 ++++++++++++++++++++++++--- Classes/ViewHelpers/SvgViewHelper.php | 1 + 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Classes/AssetCollector.php b/Classes/AssetCollector.php index 448d953..770730f 100644 --- a/Classes/AssetCollector.php +++ b/Classes/AssetCollector.php @@ -12,6 +12,7 @@ */ use B13\Assetcollector\Resource\ResourceCompressor; +use TYPO3\CMS\Core\Information\Typo3Version; use TYPO3\CMS\Core\SingletonInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\PathUtility; @@ -36,7 +37,7 @@ public function addInlineCss(string $inlineCss): void public function addCssFile(string $cssFile): void { - $this->cssFiles[] = GeneralUtility::getFileAbsFileName($cssFile); + $this->cssFiles[] = $cssFile; } public function addExternalCssFile(string $fileName, string $mediaType = 'all'): void @@ -123,8 +124,8 @@ public function buildInlineCssTag(): string $inlineCss = implode("\n", $this->getUniqueInlineCss()); $cssFiles = $this->getUniqueCssFiles(); foreach ($cssFiles as $cssFile) { - if (file_exists($cssFile)) { - $inlineCss .= $this->removeUtf8Bom(file_get_contents($cssFile)) . "\n"; + if (file_exists(GeneralUtility::getFileAbsFileName($cssFile))) { + $inlineCss .= $this->cssContentWithResolvedPaths($cssFile) . "\n"; } } if (trim($inlineCss) !== '') { @@ -134,6 +135,38 @@ public function buildInlineCssTag(): string return ''; } + protected function cssContentWithResolvedPaths(string $cssFile): string + { + $absoluteFile = GeneralUtility::getFileAbsFileName($cssFile); + if (file_exists($absoluteFile)) { + $content = $this->removeUtf8Bom(file_get_contents($absoluteFile)); + if ((GeneralUtility::makeInstance(Typo3Version::class))->getMajorVersion() < 11) { + return $content; + } + preg_match_all('/url\("([a-zA-Z0-9_.\-\/]+)"\)/', $content, $matches); + if (!empty($matches[1])) { + $content = $this->replacePaths($matches[1], $cssFile, $content); + } + preg_match_all('/url\(([a-zA-Z0-9_.\-\/]+)\)/', $content, $matches); + if (!empty($matches[1])) { + $content = $this->replacePaths($matches[1], $cssFile, $content); + } + } + return $content; + } + + protected function replacePaths(array $relativeToCssPaths, string $cssFile, string $content): string + { + foreach ($relativeToCssPaths as $relativeToCssPath) { + $absolute = PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($cssFile, $relativeToCssPath); + if (file_exists(GeneralUtility::getFileAbsFileName($absolute))) { + $publicWebPath = PathUtility::getPublicResourceWebPath($absolute); + $content = str_replace($relativeToCssPath, $publicWebPath, $content); + } + } + return $content; + } + public function buildJavaScriptIncludes(): string { $includes = ''; diff --git a/Classes/ViewHelpers/SvgViewHelper.php b/Classes/ViewHelpers/SvgViewHelper.php index f1130c1..4c25fc7 100644 --- a/Classes/ViewHelpers/SvgViewHelper.php +++ b/Classes/ViewHelpers/SvgViewHelper.php @@ -28,6 +28,7 @@ class SvgViewHelper extends AbstractTagBasedViewHelper public function __construct(AssetCollector $assetCollector) { + parent::__construct(); $this->assetCollector = $assetCollector; }