Skip to content

Commit

Permalink
[TASK] resolve relative Paths in CSS (#15)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
achimfritz and Achim Fritz authored Dec 6, 2022
1 parent 3a96341 commit 330e626
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
39 changes: 36 additions & 3 deletions Classes/AssetCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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) !== '') {
Expand All @@ -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 = '';
Expand Down
1 change: 1 addition & 0 deletions Classes/ViewHelpers/SvgViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SvgViewHelper extends AbstractTagBasedViewHelper

public function __construct(AssetCollector $assetCollector)
{
parent::__construct();
$this->assetCollector = $assetCollector;
}

Expand Down

0 comments on commit 330e626

Please sign in to comment.