Skip to content

Commit 31ea74e

Browse files
committed
fix: issue where -0 lost its sign
1 parent 9196e9e commit 31ea74e

5 files changed

Lines changed: 88 additions & 11 deletions

File tree

src/lib.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7795,6 +7795,10 @@ mod tests {
77957795
".infinity11-6 { z-index: calc(2147483647 + 2 - 1) }",
77967796
".infinity11-6{z-index:calc(1/0)}", // 2147483647
77977797
);
7798+
minify_test(
7799+
".infinity11-7 { z-index: calc(1 - 2147483649) }",
7800+
".infinity11-7{z-index:calc(-1/0)}", // Negative overflow: 1 + (-2147483649) = -2147483648
7801+
);
77987802
minify_test(
77997803
".infinity12-1 { z-index: calc(calc(1/0) + infinity) }",
78007804
".infinity12-1{z-index:calc(1/0)}",
@@ -7821,8 +7825,64 @@ mod tests {
78217825
".infinity6{order:calc(1/0)}",
78227826
);
78237827
minify_test(
7824-
".infinity-flex { flex: calc(6 / 0); }",
7825-
".infinity-flex{flex:calc(1/0)}",
7828+
".infinity-new2 { transition-timing-function: steps(calc(infinity), jump-start) }",
7829+
".infinity-new2{transition-timing-function:steps(calc(1/0),start)}",
7830+
);
7831+
minify_test(
7832+
".infinity-new3 { transition: steps(calc(infinity), jump-start) }",
7833+
".infinity-new3{transition:all steps(calc(1/0),start)}",
7834+
);
7835+
7836+
// Test Infinity <length> - division by zero should preserve sign
7837+
minify_test(
7838+
".infinity-dim1 { width: calc(100px / 0) }",
7839+
".infinity-dim1{width:calc(100px/0)}",
7840+
);
7841+
minify_test(
7842+
".infinity-dim2 { width: calc(100px / -0) }",
7843+
".infinity-dim2{width:calc(100px/-0)}",
7844+
);
7845+
minify_test(
7846+
".infinity-dim3 { width: calc(-100px / 0) }",
7847+
".infinity-dim3{width:calc(-100px/0)}",
7848+
);
7849+
minify_test(
7850+
".infinity-dim4 { width: calc(-100px / -0) }",
7851+
".infinity-dim4{width:calc(-100px/-0)}",
7852+
);
7853+
minify_test(
7854+
".infinity-dim5 { width: calc(-0px); height: calc(-0); }",
7855+
".infinity-dim5{width:0;height:0}",
7856+
);
7857+
7858+
// Test Infinity <number>
7859+
minify_test(
7860+
".number1 { line-height: calc(9/0) }",
7861+
".number1{line-height:calc(1/0)}",
7862+
);
7863+
minify_test(
7864+
".number2 { line-height: calc(0.0002/0) }",
7865+
".number2{line-height:calc(1/0)}",
7866+
);
7867+
minify_test(
7868+
".number3 { line-height: calc(-1/0) }",
7869+
".number3{line-height:calc(-1/0)}",
7870+
);
7871+
minify_test(
7872+
".number3-1 { line-height: calc(1/-0) }",
7873+
".number3-1{line-height:calc(-1/0)}",
7874+
);
7875+
minify_test(
7876+
".number3-1 { flex: calc(0/-0) }",
7877+
".number3-1{flex:0}",
7878+
);
7879+
minify_test(
7880+
".number4 { flex: calc(1 * infinity) }",
7881+
".number4{flex:calc(1/0)}",
7882+
);
7883+
minify_test(
7884+
".number5 { flex: calc(-1 * infinity) }",
7885+
".number5{flex:calc(-1/0)}",
78267886
);
78277887

78287888
// TODO Support orphans prop

src/properties/position.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ pub enum ZIndex {
9191

9292
impl<'i> Parse<'i> for ZIndex {
9393
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
94-
if input.try_parse(|input| input.expect_ident_cloned()).is_ok() {
94+
if input
95+
.try_parse(|input| input.expect_ident_matching("auto"))
96+
.is_ok()
97+
{
9598
return Ok(ZIndex::Auto);
9699
}
97100

src/rules/font_palette_values.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl<'i> Parse<'i> for BasePalette {
187187
if i.is_negative() {
188188
return Err(input.new_custom_error(ParserError::InvalidValue));
189189
}
190-
return Ok(BasePalette::Integer(i32::from(i) as u16));
190+
return Ok(BasePalette::Integer(*i as u16));
191191
}
192192

193193
let location = input.current_source_location();
@@ -226,7 +226,7 @@ impl<'i> Parse<'i> for OverrideColors {
226226
}
227227

228228
Ok(OverrideColors {
229-
index: i32::from(index) as u16,
229+
index: (*index) as u16,
230230
color,
231231
})
232232
}
@@ -237,7 +237,7 @@ impl ToCss for OverrideColors {
237237
where
238238
W: std::fmt::Write,
239239
{
240-
CSSInteger(i32::from(self.index)).to_css(dest)?;
240+
CSSInteger(self.index as i32).to_css(dest)?;
241241
dest.write_char(' ')?;
242242
self.color.to_css(dest)
243243
}

src/values/calc.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,10 @@ impl<
629629
}
630630
// Non-Number (like Length) / 0 = infinity
631631
// For Length values, we keep them as-is and let ToCss handle serialization
632-
node = Calc::Product(1.0, Box::new(node));
632+
// Use f32::INFINITY as marker for division by zero
633+
// In ToCss, we'll output /0 or /-0 based on the marker
634+
let marker = if f32::is_sign_negative(val) { -f32::INFINITY } else { f32::INFINITY };
635+
node = Calc::Product(marker, Box::new(node));
633636
continue;
634637
}
635638
// Division by infinity
@@ -1010,9 +1013,18 @@ impl<V: ToCss + std::ops::Mul<f32, Output = V> + TrySign + Clone + std::fmt::Deb
10101013
}
10111014
}
10121015
Calc::Product(num, calc) => {
1013-
// Special case: Product(1, value) represents value/0 (infinity)
1014-
// Serialize as calc(value/0) instead of calc(1 * value)
1015-
if *num == 1.0 {
1016+
// Special case: Product(INFINITY, value) represents value/0 (division by positive zero)
1017+
// Special case: Product(-INFINITY, value) represents value/-0 (division by negative zero)
1018+
// Serialize as calc(value/0) or calc(value/-0) instead of calc(INFINITY * value)
1019+
if num.is_infinite() {
1020+
calc.to_css(dest)?;
1021+
dest.delim('/', true)?;
1022+
if f32::is_sign_negative(*num) {
1023+
dest.write_str("-0")
1024+
} else {
1025+
dest.write_str("0")
1026+
}
1027+
} else if *num == 1.0 {
10161028
calc.to_css(dest)?;
10171029
dest.delim('/', true)?;
10181030
dest.write_str("0")

src/values/number.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,10 @@ impl AddInternal for CSSInteger {
265265
fn add(self, other: Self) -> Self {
266266
let result = self.0.saturating_add(other.0);
267267
// Check for overflow and produce infinity
268-
if result == i32::MAX || result == i32::MIN {
268+
if result == i32::MAX {
269269
CSSInteger(i32::MAX)
270+
} else if result == i32::MIN {
271+
CSSInteger(i32::MIN)
270272
} else {
271273
CSSInteger(result)
272274
}

0 commit comments

Comments
 (0)