8000 feature #59544 [AssetMapper] Fix CssCompiler matches url in comments … · priyadi/symfony@b6f477c · GitHub
[go: up one dir, main page]

Skip to content

Commit b6f477c

Browse files
committed
feature symfony#59544 [AssetMapper] Fix CssCompiler matches url in comments (smnandre)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [AssetMapper] Fix CssCompiler matches url in comments | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix symfony#59512 | License | MIT Add a check to avoid matching url in comments Commits ------- 9a43703 [AssetMapper] Fix CssCompiler matches url in comments
2 parents ef15506 + 9a43703 commit b6f477c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/Symfony/Component/AssetMapper/Compiler/CssAssetUrlCompiler.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,32 @@ public function __construct(
3535

3636
public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string
3737
{
38-
return preg_replace_callback(self::ASSET_URL_PATTERN, function ($matches) use ($asset, $assetMapper) {
38+
preg_match_all('/\/\*|\*\//', $content, $commentMatches, \PREG_OFFSET_CAPTURE);
39+
40+
$start = null;
41+
$commentBlocks = [];
42+
foreach ($commentMatches[0] as $match) {
43+
if ('/*' === $match[0]) {
44+
$start = $match[1];
45+
} elseif ($start) {
46+
$commentBlocks[] = [$start, $match[1]];
47+
$start = null;
48+
}
49+
}
50+
51+
return preg_replace_callback(self::ASSET_URL_PATTERN, function ($matches) use ($asset, $assetMapper, $commentBlocks) {
52+
$matchPos = $matches[0][1];
53+
54+
// Ignore matchs inside comments
55+
foreach ($commentBlocks as $block) {
56+
if ($matchPos > $block[0]) {
57+
if ($matchPos < $block[1]) {
58+
return $matches[0][0];
59+
}
60+
break;
61+
}
62+
}
63+
3964
try {
4065
$resolvedSourcePath = Path::join(\dirname($asset->sourcePath), $matches[1]);
4166
} catch (RuntimeException $e) {

src/Symfony/Component/AssetMapper/Tests/Compiler/CssAssetUrlCompilerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,36 @@ public static function provideCompileTests(): iterable
114114
'expectedOutput' => 'body { background: url("https://cdn.io/images/bar.png"); }',
115115
'expectedDependencies' => [],
116116
];
117+
118+
yield 'ignore_comments' => [
119+
'input' => 'body { background: url("images/foo.png"); /* background: url("images/bar.png"); */ }',
120+
'expectedOutput' => 'body { background: url("images/foo.123456.png"); /* background: url("images/bar.png"); */ }',
121+
'expectedDependencies' => ['images/foo.png'],
122+
];
123+
124+
yield 'ignore_comment_after_rule' => [
125+
'input' => 'body { background: url("images/foo.png"); } /* url("images/need-ignore.png") */',
126+
'expectedOutput' => 'body { background: url("images/foo.123456.png"); } /* url("images/need-ignore.png") */',
127+
'expectedDependencies' => ['images/foo.png'],
128+
];
129+
130+
yield 'ignore_comment_within_rule' => [
131+
'input' => 'body { background: url("images/foo.png") /* url("images/need-ignore.png") */; }',
132+
'expectedOutput' => 'body { background: url("images/foo.123456.png") /* url("images/need-ignore.png") */; }',
133+
'expectedDependencies' => ['images/foo.png'],
134+
];
135+
136+
yield 'ignore_multiline_comment_after_rule' => [
137+
'input' => 'body {
138+
background: url("images/foo.png"); /*
139+
url("images/need-ignore.png") */
140+
}',
141+
'expectedOutput' => 'body {
142+
background: url("images/foo.123456.png"); /*
143+
url("images/need-ignore.png") */
144+
}',
145+
'expectedDependencies' => ['images/foo.png'],
146+
];
117147
}
118148

119149
public function testCompileFindsRelativeFilesViaSourcePath()

0 commit comments

Comments
 (0)
0