Skip to content

Commit e4bcec3

Browse files
authored
fix: enhance background-position minification and handling for various cases (#1124)
1 parent 0e7301a commit e4bcec3

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/lib.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4311,12 +4311,24 @@ mod tests {
43114311
);
43124312
minify_test(
43134313
".foo { background-position: left 10px center }",
4314-
".foo{background-position:10px 50%}",
4314+
".foo{background-position:10px}",
43154315
);
43164316
minify_test(
43174317
".foo { background-position: right 10px center }",
43184318
".foo{background-position:right 10px center}",
43194319
);
4320+
minify_test(
4321+
".foo { background-position: center top 10px }",
4322+
".foo{background-position:50% 10px}",
4323+
);
4324+
minify_test(
4325+
".foo { background-position: center bottom 10px }",
4326+
".foo{background-position:center bottom 10px}",
4327+
);
4328+
minify_test(
4329+
".foo { background-position: center 10px }",
4330+
".foo{background-position:50% 10px}",
4331+
);
43204332
minify_test(
43214333
".foo { background-position: right 10px top 20px }",
43224334
".foo{background-position:right 10px top 20px}",
@@ -4337,6 +4349,26 @@ mod tests {
43374349
".foo { background-position: bottom right }",
43384350
".foo{background-position:100% 100%}",
43394351
);
4352+
minify_test(
4353+
".foo { background-position: center top }",
4354+
".foo{background-position:top}",
4355+
);
4356+
minify_test(
4357+
".foo { background-position: center bottom }",
4358+
".foo{background-position:bottom}",
4359+
);
4360+
minify_test(
4361+
".foo { background-position: left center }",
4362+
".foo{background-position:0}",
4363+
);
4364+
minify_test(
4365+
".foo { background-position: right center }",
4366+
".foo{background-position:100%}",
4367+
);
4368+
minify_test(
4369+
".foo { background-position: 20px center }",
4370+
".foo{background-position:20px}",
4371+
);
43404372

43414373
minify_test(
43424374
".foo { background: url('img-sprite.png') no-repeat bottom right }",

src/values/position.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,32 @@ impl ToCss for Position {
203203
// `center` is assumed if omitted.
204204
x_lp.to_css(dest)
205205
}
206+
(
207+
&HorizontalPosition::Side {
208+
side: HorizontalPositionKeyword::Left,
209+
offset: Some(ref x_lp),
210+
},
211+
y,
212+
) if y.is_center() => {
213+
// `left 10px center` => `10px` (omit Y when center)
214+
x_lp.to_css(dest)
215+
}
206216
(&HorizontalPosition::Side { side, offset: None }, y) if y.is_center() => {
207217
let p: LengthPercentage = side.into();
208218
p.to_css(dest)
209219
}
210220
(x, y_pos @ &VerticalPosition::Side { offset: None, .. }) if x.is_center() => y_pos.to_css(dest),
221+
(
222+
&HorizontalPosition::Center,
223+
y_pos @ &VerticalPosition::Side {
224+
side: VerticalPositionKeyword::Bottom,
225+
offset: Some(_),
226+
},
227+
) => {
228+
// `center bottom 10px` must keep the keyword form
229+
dest.write_str("center ")?;
230+
y_pos.to_css(dest)
231+
}
211232
(&HorizontalPosition::Side { side: x, offset: None }, &VerticalPosition::Side { side: y, offset: None }) => {
212233
let x: LengthPercentage = x.into();
213234
let y: LengthPercentage = y.into();

0 commit comments

Comments
 (0)