Skip to content

Commit 41eefe4

Browse files
committed
Write url placeholders with quotes
parcel-bundler/parcel#7556
1 parent 56e6bf0 commit 41eefe4

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/values/url.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,30 @@ impl ToCss for Url {
2626
None
2727
};
2828

29-
let url = if let Some(dep) = &dep {
30-
&dep.placeholder
31-
} else {
32-
&self.url
33-
};
29+
// If adding dependencies, always write url() with quotes so that the placeholder can
30+
// be replaced without escaping more easily. Quotes may be removed later during minification.
31+
if let Some(dep) = dep {
32+
dest.write_str("url(")?;
33+
serialize_string(&dep.placeholder, dest)?;
34+
dest.write_char(')')?;
35+
36+
if let Some(dependencies) = &mut dest.dependencies {
37+
dependencies.push(Dependency::Url(dep))
38+
}
39+
40+
return Ok(())
41+
}
3442

3543
use cssparser::ToCss;
3644
if dest.minify {
3745
let mut buf = String::new();
38-
Token::UnquotedUrl(CowRcStr::from(url.as_ref())).to_css(&mut buf)?;
46+
Token::UnquotedUrl(CowRcStr::from(self.url.as_ref())).to_css(&mut buf)?;
3947

4048
// If the unquoted url is longer than it would be quoted (e.g. `url("...")`)
4149
// then serialize as a string and choose the shorter version.
42-
if buf.len() > url.len() + 7 {
50+
if buf.len() > self.url.len() + 7 {
4351
let mut buf2 = String::new();
44-
serialize_string(&url, &mut buf2)?;
52+
serialize_string(&self.url, &mut buf2)?;
4553
if buf2.len() + 5 < buf.len() {
4654
dest.write_str("url(")?;
4755
dest.write_str(&buf2)?;
@@ -51,11 +59,7 @@ impl ToCss for Url {
5159

5260
dest.write_str(&buf)?;
5361
} else {
54-
Token::UnquotedUrl(CowRcStr::from(url.as_ref())).to_css(dest)?;
55-
}
56-
57-
if let Some(dependencies) = &mut dest.dependencies {
58-
dependencies.push(Dependency::Url(dep.unwrap()))
62+
Token::UnquotedUrl(CowRcStr::from(self.url.as_ref())).to_css(dest)?;
5963
}
6064

6165
Ok(())

0 commit comments

Comments
 (0)