do not panic on modulo by 0

This commit is contained in:
Connor Skees 2020-08-20 11:21:12 -04:00
parent d240151f8c
commit 921b6e4f8d
2 changed files with 49 additions and 1 deletions

View File

@ -649,9 +649,18 @@ impl<'a, 'b: 'a> ValueVisitor<'a, 'b> {
Value::Dimension(Some(n2), u2, _) => {
if !u.comparable(&u2) {
return Err(
(format!("Incompatible units {} and {}.", u2, u), self.span).into()
(format!("Incompatible units {} and {}.", u, u2), self.span).into()
);
}
if n2.is_zero() {
return Ok(Value::Dimension(
None,
if u == Unit::None { u2 } else { u },
true,
));
}
if u == u2 {
Value::Dimension(Some(n % n2), u, true)
} else if u == Unit::None {

View File

@ -26,3 +26,42 @@ test!(
"a {\n color: 10 % 2;\n}\n",
"a {\n color: 0;\n}\n"
);
test!(
zero_mod_zero,
"a {\n color: 0 % 0;\n}\n",
"a {\n color: NaN;\n}\n"
);
test!(
positive_mod_zero,
"a {\n color: 1 % 0;\n}\n",
"a {\n color: NaN;\n}\n"
);
test!(
positive_unit_mod_zero,
"a {\n color: 1px % 0;\n}\n",
"a {\n color: NaNpx;\n}\n"
);
test!(
positive_mod_zero_unit,
"a {\n color: 1 % 0px;\n}\n",
"a {\n color: NaNpx;\n}\n"
);
test!(
positive_unit_mod_zero_unit_same,
"a {\n color: 1px % 0px;\n}\n",
"a {\n color: NaNpx;\n}\n"
);
test!(
positive_unit_mod_zero_unit_different_compatible_takes_first_1,
"a {\n color: 1px % 0in;\n}\n",
"a {\n color: NaNpx;\n}\n"
);
test!(
positive_unit_mod_zero_unit_different_compatible_takes_first_2,
"a {\n color: 1in % 0px;\n}\n",
"a {\n color: NaNin;\n}\n"
);
error!(
positive_unit_mod_zero_unit_incompatible_units,
"a {\n color: 1rem % 0px;\n}\n", "Error: Incompatible units rem and px."
);