Skip to content

Commit f4cd618

Browse files
committed
Minify position keywords to percentages if possible
1 parent c03d373 commit f4cd618

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/lib.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,8 @@ mod tests {
10301030
}
10311031
"#
10321032
});
1033+
1034+
minify_test(".foo { background-position: bottom left }", ".foo{background-position:0% 100%}");
10331035
}
10341036

10351037
#[test]
@@ -3485,11 +3487,11 @@ mod tests {
34853487
);
34863488
minify_test(
34873489
".foo { background: radial-gradient(at top left, yellow, blue) }",
3488-
".foo{background:radial-gradient(at left top,#ff0,#00f)}"
3490+
".foo{background:radial-gradient(at 0% 0%,#ff0,#00f)}"
34893491
);
34903492
minify_test(
34913493
".foo { background: radial-gradient(5em circle at top left, yellow, blue) }",
3492-
".foo{background:radial-gradient(5em at left top,#ff0,#00f)}"
3494+
".foo{background:radial-gradient(5em at 0% 0%,#ff0,#00f)}"
34933495
);
34943496
minify_test(
34953497
".foo { background: radial-gradient(circle at 100%, #333, #333 50%, #eee 75%, #333 75%) }",
@@ -3862,8 +3864,8 @@ mod tests {
38623864
indoc! {r#"
38633865
.foo {
38643866
background-image: -webkit-gradient(radial, left top, 0, left top, 20, from(red), to(#00f));
3865-
background-image: -webkit-radial-gradient(20px at left top, red, #00f);
3866-
background-image: radial-gradient(20px at left top, red, #00f);
3867+
background-image: -webkit-radial-gradient(20px at 0% 0%, red, #00f);
3868+
background-image: radial-gradient(20px at 0% 0%, red, #00f);
38673869
}
38683870
"#},
38693871
Browsers {
@@ -3892,13 +3894,13 @@ mod tests {
38923894
r#"
38933895
.foo {
38943896
background-image: -webkit-gradient(radial, left top, 0, left top, 20, from(red), to(#00f));
3895-
background-image: -webkit-radial-gradient(20px at left top, red, #00f);
3896-
background-image: radial-gradient(20px at left top, red, #00f);
3897+
background-image: -webkit-radial-gradient(20px at 0% 0%, red, #00f);
3898+
background-image: radial-gradient(20px at 0% 0%, red, #00f);
38973899
}
38983900
"#,
38993901
indoc! {r#"
39003902
.foo {
3901-
background-image: radial-gradient(20px at left top, red, #00f);
3903+
background-image: radial-gradient(20px at 0% 0%, red, #00f);
39023904
}
39033905
"#},
39043906
Browsers {
@@ -3910,13 +3912,13 @@ mod tests {
39103912
r#"
39113913
.foo {
39123914
background: -webkit-gradient(radial, left top, 0, left top, 20, from(red), to(#00f));
3913-
background: -webkit-radial-gradient(20px at left top, red, #00f);
3914-
background: radial-gradient(20px at left top, red, #00f);
3915+
background: -webkit-radial-gradient(20px at 0% 0%, red, #00f);
3916+
background: radial-gradient(20px at 0% 0%, red, #00f);
39153917
}
39163918
"#,
39173919
indoc! {r#"
39183920
.foo {
3919-
background: radial-gradient(20px at left top, red, #00f);
3921+
background: radial-gradient(20px at 0% 0%, red, #00f);
39203922
}
39213923
"#},
39223924
Browsers {

src/values/position.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ impl ToCss for Position {
150150
x_lp.to_css(dest)
151151
},
152152
(
153-
x_pos @ &HorizontalPosition::Side(_, None),
153+
&HorizontalPosition::Side(side, None),
154154
&VerticalPosition::Center,
155155
) => {
156-
x_pos.to_css(dest)
156+
let p: Percentage = side.into();
157+
p.to_css(dest)
157158
},
158159
(
159160
&HorizontalPosition::Center,
@@ -169,17 +170,28 @@ impl ToCss for Position {
169170
x_lp.to_css(dest)
170171
},
171172
(
172-
x_pos @ &HorizontalPosition::Side(_, None),
173+
&HorizontalPosition::Side(side, None),
173174
&VerticalPosition::Length(LengthPercentage::Percentage(Percentage(y_lp))),
174175
) if y_lp == 0.5 => {
175-
x_pos.to_css(dest)
176+
let p: Percentage = side.into();
177+
p.to_css(dest)
176178
},
177179
(
178180
&HorizontalPosition::Length(LengthPercentage::Percentage(Percentage(x_lp))),
179181
y_pos @ &VerticalPosition::Side(_, None),
180182
) if x_lp == 0.5 => {
181183
y_pos.to_css(dest)
182184
},
185+
(
186+
&HorizontalPosition::Side(x, None),
187+
&VerticalPosition::Side(y, None)
188+
) => {
189+
let x: Percentage = x.into();
190+
let y: Percentage = y.into();
191+
x.to_css(dest)?;
192+
dest.write_str(" ")?;
193+
y.to_css(dest)
194+
},
183195
(x_pos, y_pos) => {
184196
x_pos.to_css(dest)?;
185197
dest.write_str(" ")?;

0 commit comments

Comments
 (0)