Skip to content

Commit 07c1e79

Browse files
committed
Improve minification of background position (fixes parcel-bundler#41)
1 parent b2f5c90 commit 07c1e79

File tree

3 files changed

+61
-44
lines changed

3 files changed

+61
-44
lines changed

src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ mod tests {
17361736
.foo {
17371737
background-position: center center;
17381738
}
1739-
"#, indoc! {".foo{background-position:50% 50%}"
1739+
"#, indoc! {".foo{background-position:50%}"
17401740
});
17411741

17421742
test(r#"
@@ -1791,7 +1791,7 @@ mod tests {
17911791
minify_test(".foo { background-position: bottom right }", ".foo{background-position:100% 100%}");
17921792

17931793
minify_test(".foo { background: url('img-sprite.png') no-repeat bottom right }", ".foo{background:url(img-sprite.png) 100% 100% no-repeat}");
1794-
minify_test(".foo { background: transparent }", ".foo{background:#0000}");
1794+
minify_test(".foo { background: transparent }", ".foo{background:0 0}");
17951795

17961796
minify_test(".foo { background: url(\"data:image/svg+xml,%3Csvg width='168' height='24' xmlns='http://www.w3.org/2000/svg'%3E%3C/svg%3E\") }", ".foo{background:url(\"data:image/svg+xml,%3Csvg width='168' height='24' xmlns='http://www.w3.org/2000/svg'%3E%3C/svg%3E\")}");
17971797

@@ -1889,6 +1889,9 @@ mod tests {
18891889
safari: Some(14 << 16),
18901890
..Browsers::default()
18911891
});
1892+
1893+
minify_test(".foo { background: none center }", ".foo{background:50%}");
1894+
minify_test(".foo { background: none }", ".foo{background:0 0}");
18921895
}
18931896

18941897
#[test]
@@ -5374,7 +5377,7 @@ mod tests {
53745377
"#,
53755378
indoc! {r#"
53765379
.foo {
5377-
background-image: radial-gradient(20px at 0% 0%, red, #00f);
5380+
background-image: radial-gradient(20px at 0 0, red, #00f);
53785381
}
53795382
"#},
53805383
Browsers {
@@ -5392,7 +5395,7 @@ mod tests {
53925395
"#,
53935396
indoc! {r#"
53945397
.foo {
5395-
background: radial-gradient(20px at 0% 0%, red, #00f);
5398+
background: radial-gradient(20px at 0 0, red, #00f);
53965399
}
53975400
"#},
53985401
Browsers {

src/properties/background.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl ToCss for Background {
319319
has_output = true;
320320
}
321321

322-
if self.position != Position::default() || self.size != BackgroundSize::default() {
322+
if !self.position.is_zero() || self.size != BackgroundSize::default() {
323323
if has_output {
324324
dest.write_str(" ")?;
325325
}
@@ -372,7 +372,12 @@ impl ToCss for Background {
372372

373373
// If nothing was output, then this is the initial value, e.g. background: transparent
374374
if !has_output {
375-
self.color.to_css(dest)?;
375+
if dest.minify {
376+
// `0 0` is the shortest valid background value
377+
self.position.to_css(dest)?;
378+
} else {
379+
dest.write_str("none")?;
380+
}
376381
}
377382

378383
Ok(())

src/values/position.rs

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use cssparser::*;
22
use crate::traits::{Parse, ToCss};
33
use crate::macros::enum_property;
44
use crate::printer::Printer;
5-
use super::length::{LengthPercentage, LengthValue};
5+
use super::length::{LengthPercentage};
66
use super::percentage::Percentage;
77
use crate::error::{ParserError, PrinterError};
88

@@ -22,10 +22,11 @@ impl Position {
2222
}
2323

2424
pub fn is_center(&self) -> bool {
25-
*self == Position::center() || *self == Position {
26-
x: HorizontalPosition::Length(LengthPercentage::Percentage(Percentage(0.5))),
27-
y: VerticalPosition::Length(LengthPercentage::Percentage(Percentage(0.5)))
28-
}
25+
self.x.is_center() && self.y.is_center()
26+
}
27+
28+
pub fn is_zero(&self) -> bool {
29+
self.x.is_zero() && self.y.is_zero()
2930
}
3031
}
3132

@@ -136,8 +137,8 @@ impl ToCss for Position {
136137
},
137138
(
138139
x_pos @ &HorizontalPosition::Side(side, Some(_)),
139-
&VerticalPosition::Center
140-
) if side != HorizontalPositionKeyword::Left => {
140+
y
141+
) if side != HorizontalPositionKeyword::Left && y.is_center() => {
141142
// If there is a side keyword with an offset, "center" must be a keyword not a percentage.
142143
x_pos.to_css(dest)?;
143144
dest.write_str(" center")
@@ -151,44 +152,28 @@ impl ToCss for Position {
151152
dest.write_str(" ")?;
152153
y_pos.to_css(dest)
153154
},
154-
(
155-
&HorizontalPosition::Length(ref x_lp),
156-
&VerticalPosition::Center
157-
) => {
158-
// `center` is assumed if omitted.
159-
x_lp.to_css(dest)
160-
},
161-
(
162-
&HorizontalPosition::Side(side, None),
163-
&VerticalPosition::Center,
164-
) => {
165-
let p: LengthPercentage = side.into();
166-
p.to_css(dest)
167-
},
168-
(
169-
&HorizontalPosition::Center,
170-
y_pos @ &VerticalPosition::Side(_, None),
171-
) => {
172-
y_pos.to_css(dest)
155+
(x, y) if x.is_center() && y.is_center() => {
156+
// `center center` => 50%
157+
x.to_css(dest)
173158
},
174159
(
175160
&HorizontalPosition::Length(ref x_lp),
176-
&VerticalPosition::Length(LengthPercentage::Percentage(Percentage(y_lp)))
177-
) if y_lp == 0.5 => {
178-
// 50% is equivalent to `center`, which may be omitted.
161+
y
162+
) if y.is_center() => {
163+
// `center` is assumed if omitted.
179164
x_lp.to_css(dest)
180165
},
181166
(
182167
&HorizontalPosition::Side(side, None),
183-
&VerticalPosition::Length(LengthPercentage::Percentage(Percentage(y_lp))),
184-
) if y_lp == 0.5 => {
168+
y,
169+
) if y.is_center() => {
185170
let p: LengthPercentage = side.into();
186171
p.to_css(dest)
187172
},
188173
(
189-
&HorizontalPosition::Length(LengthPercentage::Percentage(Percentage(x_lp))),
174+
x,
190175
y_pos @ &VerticalPosition::Side(_, None),
191-
) if x_lp == 0.5 => {
176+
) if x.is_center() => {
192177
y_pos.to_css(dest)
193178
},
194179
(
@@ -202,16 +187,21 @@ impl ToCss for Position {
202187
y.to_css(dest)
203188
},
204189
(x_pos, y_pos) => {
205-
let zero = LengthPercentage::Dimension(LengthValue::Px(0.0));
190+
let zero = LengthPercentage::zero();
206191
let fifty = LengthPercentage::Percentage(Percentage(0.5));
207192
let x_len = match &x_pos {
208193
HorizontalPosition::Side(HorizontalPositionKeyword::Left, len) => {
209194
if let Some(len) = len {
210-
Some(len)
195+
if *len == 0.0 {
196+
Some(&zero)
197+
} else {
198+
Some(len)
199+
}
211200
} else {
212201
Some(&zero)
213202
}
214203
},
204+
HorizontalPosition::Length(len) if *len == 0.0 => Some(&zero),
215205
HorizontalPosition::Length(len) => Some(len),
216206
HorizontalPosition::Center => Some(&fifty),
217207
_ => None
@@ -220,11 +210,16 @@ impl ToCss for Position {
220210
let y_len = match &y_pos {
221211
VerticalPosition::Side(VerticalPositionKeyword::Top, len) => {
222212
if let Some(len) = len {
223-
Some(len)
213+
if *len == 0.0 {
214+
Some(&zero)
215+
} else {
216+
Some(len)
217+
}
224218
} else {
225219
Some(&zero)
226220
}
227221
},
222+
VerticalPosition::Length(len) if *len == 0.0 => Some(&zero),
228223
VerticalPosition::Length(len) => Some(len),
229224
VerticalPosition::Center => Some(&fifty),
230225
_ => None
@@ -254,6 +249,20 @@ pub enum PositionComponent<S> {
254249
Side(S, Option<LengthPercentage>),
255250
}
256251

252+
impl<S> PositionComponent<S> {
253+
fn is_center(&self) -> bool {
254+
match self {
255+
PositionComponent::Center => true,
256+
PositionComponent::Length(LengthPercentage::Percentage(Percentage(p))) => *p == 0.5,
257+
_ => false
258+
}
259+
}
260+
261+
fn is_zero(&self) -> bool {
262+
matches!(self, PositionComponent::Length(len) if *len == 0.0)
263+
}
264+
}
265+
257266
impl<S: Parse> Parse for PositionComponent<S> {
258267
fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
259268
if input.try_parse(|i| i.expect_ident_matching("center")).is_ok() {
@@ -302,7 +311,7 @@ enum_property!(HorizontalPositionKeyword,
302311
impl Into<LengthPercentage> for HorizontalPositionKeyword {
303312
fn into(self) -> LengthPercentage {
304313
match self {
305-
HorizontalPositionKeyword::Left => LengthPercentage::Dimension(LengthValue::Px(0.0)),
314+
HorizontalPositionKeyword::Left => LengthPercentage::zero(),
306315
HorizontalPositionKeyword::Right => LengthPercentage::Percentage(Percentage(1.0))
307316
}
308317
}
@@ -316,7 +325,7 @@ enum_property!(VerticalPositionKeyword,
316325
impl Into<LengthPercentage> for VerticalPositionKeyword {
317326
fn into(self) -> LengthPercentage {
318327
match self {
319-
VerticalPositionKeyword::Top => LengthPercentage::Dimension(LengthValue::Px(0.0)),
328+
VerticalPositionKeyword::Top => LengthPercentage::zero(),
320329
VerticalPositionKeyword::Bottom => LengthPercentage::Percentage(Percentage(1.0))
321330
}
322331
}

0 commit comments

Comments
 (0)