Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@
*/
final class CssAssetUrlCompiler implements AssetCompilerInterface
{
// https://regex101.com/r/BOJ3vG/1
public const ASSET_URL_PATTERN = '/url\(\s*["\']?(?!(?:\/|\#|%23|data|http|\/\/))([^"\'\s?#)]+)([#?][^"\')]+)?\s*["\']?\)/';
// https://regex101.com/r/BOJ3vG/2
public const ASSET_URL_PATTERN = <<<'REGEX'
{
(?|
(url\()\s*+["']?(?!(?:/|\#|%23|data|http|//))([^"')\s?#]++)(?:[?#][^"')]++)?["']?\s*+(\))
|
(@import\s++)["'](?!(?:/|\#|%23|data|http|//))([^"')\s?#]++)(?:[?#][^"')]++)?["']
)
}x
REGEX;

public function __construct(
private readonly string $missingImportMode = self::MISSING_IMPORT_WARN,
Expand All @@ -33,7 +41,7 @@
) {
}

public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string

Check failure on line 44 in src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidNullableReturnType

src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php:44:102: InvalidNullableReturnType: The declared return type 'string' for Symfony\Component\AssetMapper\Compiler\CssAssetUrlCompiler::compile is not nullable, but 'null|string' contains null (see https://psalm.dev/144)

Check failure on line 44 in src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidNullableReturnType

src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php:44:102: InvalidNullableReturnType: The declared return type 'string' for Symfony\Component\AssetMapper\Compiler\CssAssetUrlCompiler::compile is not nullable, but 'null|string' contains null (see https://psalm.dev/144)
{
preg_match_all('/\/\*|\*\//', $content, $commentMatches, \PREG_OFFSET_CAPTURE);

Expand All @@ -48,7 +56,7 @@
}
}

return preg_replace_callback(self::ASSET_URL_PATTERN, function ($matches) use ($asset, $assetMapper, $commentBlocks) {

Check failure on line 59 in src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php

View workflow job for this annotation

GitHub Actions / Psalm

NullableReturnStatement

src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php:59:16: NullableReturnStatement: The declared return type 'string' for Symfony\Component\AssetMapper\Compiler\CssAssetUrlCompiler::compile is not nullable, but the function returns 'null|string' (see https://psalm.dev/139)

Check failure on line 59 in src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php

View workflow job for this annotation

GitHub Actions / Psalm

NullableReturnStatement

src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php:59:16: NullableReturnStatement: The declared return type 'string' for Symfony\Component\AssetMapper\Compiler\CssAssetUrlCompiler::compile is not nullable, but the function returns 'null|string' (see https://psalm.dev/139)
$matchPos = $matches[0][1];

// Ignore matches inside comments
Expand All @@ -62,16 +70,16 @@
}

try {
$resolvedSourcePath = Path::join(\dirname($asset->sourcePath), $matches[1]);
$resolvedSourcePath = Path::join(\dirname($asset->sourcePath), $matches[2][0]);
} catch (RuntimeException $e) {
$this->handleMissingImport(\sprintf('Error processing import in "%s": ', $asset->sourcePath).$e->getMessage(), $e);

return $matches[0];
return $matches[0][0];
}
$dependentAsset = $assetMapper->getAssetFromSourcePath($resolvedSourcePath);

if (null === $dependentAsset) {
$message = \sprintf('Unable to find asset "%s" referenced in "%s". The file "%s" ', $matches[1], $asset->sourcePath, $resolvedSourcePath);
$message = \sprintf('Unable to find asset "%s" referenced in "%s". The file "%s" ', $matches[2][0], $asset->sourcePath, $resolvedSourcePath);
if (is_file($resolvedSourcePath)) {
$message .= 'exists, but it is not in a mapped asset path. Add it to the "paths" config.';
} else {
Expand All @@ -80,14 +88,14 @@
$this->handleMissingImport($message);

// return original, unchanged path
return $matches[0];
return $matches[0][0];
}

$asset->addDependency($dependentAsset);
$relativePath = Path::makeRelative($dependentAsset->publicPath, \dirname($asset->publicPathWithoutDigest));

return 'url("'.$relativePath.'")';
}, $content);
return $matches[1][0].'"'.$relativePath.'"'.($matches[3][0] ?? '');
}, $content, -1, $count, \PREG_OFFSET_CAPTURE);
}

public function supports(MappedAsset $asset): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ private function makeImportsBare(string $content, array &$dependencies, array &$
}

preg_match_all(CssAssetUrlCompiler::ASSET_URL_PATTERN, $content, $matches);
foreach ($matches[1] as $path) {
foreach ($matches[2] as $path) {
if (str_starts_with($path, 'data:')) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ public static function provideCompileTests(): iterable
body {
background: url("images/foo.png");
}
EOF
,
EOF,
'expectedOutput' => <<<EOF
body {
background: url("images/foo.123456.png");
}
EOF
,
EOF,
'expectedDependencies' => ['images/foo.png'],
];

Expand Down Expand Up @@ -144,6 +142,80 @@ public static function provideCompileTests(): iterable
}',
'expectedDependencies' => ['images/foo.png'],
];

yield 'import_without_url_double_quotes' => [
'input' => '@import "more-styles.css"',
'expectedOutput' => '@import "more-styles.abcd123.css"',
'expectedDependencies' => ['more-styles.css'],
];

yield 'import_without_url_single_quotes' => [
'input' => "@import 'more-styles.css'",
'expectedOutput' => '@import "more-styles.abcd123.css"',
'expectedDependencies' => ['more-styles.css'],
];

yield 'import_without_url_with_media_query' => [
'input' => '@import "more-styles.css" screen',
'expectedOutput' => '@import "more-styles.abcd123.css" screen',
'expectedDependencies' => ['more-styles.css'],
];

yield 'import_without_url_with_media_query_single_quotes' => [
'input' => "@import 'more-styles.css' screen and (max-width: 600px)",
'expectedOutput' => '@import "more-styles.abcd123.css" screen and (max-width: 600px)',
'expectedDependencies' => ['more-styles.css'],
];

yield 'import_without_url_with_dot_slash' => [
'input' => '@import "./more-styles.css"',
'expectedOutput' => '@import "more-styles.abcd123.css"',
'expectedDependencies' => ['more-styles.css'],
];

yield 'import_without_url_with_dot_dot_slash' => [
'input' => '@import "../assets/more-styles.css"',
'expectedOutput' => '@import "more-styles.abcd123.css"',
'expectedDependencies' => ['more-styles.css'],
];

yield 'import_without_url_absolute_path_ignored' => [
'input' => '@import "/path/to/more-styles.css"',
'expectedOutput' => '@import "/path/to/more-styles.css"',
'expectedDependencies' => [],
];

yield 'import_without_url_http_url_ignored' => [
'input' => '@import "https://cdn.io/more-styles.css"',
'expectedOutput' => '@import "https://cdn.io/more-styles.css"',
'expectedDependencies' => [],
];

yield 'import_without_url_data_uri_ignored' => [
'input' => '@import "data:text/css;base64,LmJvZHkgeyBiYWNrZ3JvdW5kOiByZWQ7IH0="',
'expectedOutput' => '@import "data:text/css;base64,LmJvZHkgeyBiYWNrZ3JvdW5kOiByZWQ7IH0="',
'expectedDependencies' => [],
];

yield 'import_without_url_in_comments_ignored' => [
'input' => 'body { background: url("images/foo.png"); } /* @import "more-styles.css" */',
'expectedOutput' => 'body { background: url("images/foo.123456.png"); } /* @import "more-styles.css" */',
'expectedDependencies' => ['images/foo.png'],
];

yield 'mixed_url_and_import_without_url' => [
'input' => <<<EOF
@import "more-styles.css";
body { background: url("images/foo.png"); }
@import url(./more-styles.css);
EOF,
'expectedOutput' => <<<EOF
@import "more-styles.abcd123.css";
body { background: url("images/foo.123456.png"); }
@import url("more-styles.abcd123.css");
EOF,
'expectedDependencies' => ['more-styles.css', 'images/foo.png', 'more-styles.css'],
];
}

public function testCompileFindsRelativeFilesViaSourcePath()
Expand Down Expand Up @@ -223,5 +295,23 @@ public static function provideStrictModeTests(): iterable
'input' => "background-image: url(\'data:image/png;base64,iVBORw0KG\')",
'expectedExceptionMessage' => null,
];

yield 'importing_non_existent_file_without_url_throws_exception' => [
'sourceLogicalName' => 'styles.css',
'input' => '@import "non-existent.css"',
'expectedExceptionMessage' => 'Unable to find asset "non-existent.css" referenced in "/path/to/styles.css".',
];

yield 'importing_absolute_file_path_without_url_is_ignored' => [
'sourceLogicalName' => 'styles.css',
'input' => '@import "/path/to/non-existent.css"',
'expectedExceptionMessage' => null,
];

yield 'importing_a_url_without_url_is_ignored' => [
'sourceLogicalName' => 'styles.css',
'input' => '@import "https://cdn.io/non-existent.css"',
'expectedExceptionMessage' => null,
];
}
}
Loading