make division with number as lhs more robust

This commit is contained in:
ConnorSkees 2020-05-24 08:17:08 -04:00
parent cbec20f753
commit 2aaaf59e4f
2 changed files with 58 additions and 2 deletions

View File

@ -624,10 +624,21 @@ impl Value {
Self::String(s, q) => {
Value::String(format!("{}{}/{}{}{}", num, unit, q, s, q), QuoteKind::None)
}
Self::BinaryOp(..) | Self::Paren(..) => {
Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => {
Self::Dimension(num, unit).div(other.eval(span)?.node, span)?
}
_ => todo!(),
Self::List(..) | Self::True | Self::False | Self::Important | Self::Color(..) => {
Value::String(format!("{}{}/{}", num, unit, other.to_css_string(span)?), QuoteKind::None)
}
Self::Null => Value::String(format!("{}{}/", num, unit), QuoteKind::None),
Self::Map(..) | Self::Function(..) => {
return Err((
format!("{} isn't a valid CSS value.", other.inspect(span)?),
span,
)
.into())
}
Self::ArgList(..) => todo!(),
},
Self::Color(c) => match other {
Self::String(s, q) => {

View File

@ -28,3 +28,48 @@ test!(
"a {\n slash-after-comma: (1, / 2);\n}\n",
"a {\n slash-after-comma: 1, /2;\n}\n"
);
test!(
num_div_space_list,
"a {\n color: 1 / (a b);\n}\n",
"a {\n color: 1/a b;\n}\n"
);
test!(
num_div_comma_list,
"a {\n color: 1 / (a, b);\n}\n",
"a {\n color: 1/a, b;\n}\n"
);
test!(
num_div_true,
"a {\n color: 1 / true;\n}\n",
"a {\n color: 1/true;\n}\n"
);
test!(
num_div_false,
"a {\n color: 1 / false;\n}\n",
"a {\n color: 1/false;\n}\n"
);
test!(
num_div_important,
"a {\n color: 1 / !important;\n}\n",
"a {\n color: 1/!important;\n}\n"
);
test!(
num_div_null,
"a {\n color: 1 / null;\n}\n",
"a {\n color: 1/;\n}\n"
);
test!(
num_div_named_color,
"a {\n color: 1 / red;\n}\n",
"a {\n color: 1/red;\n}\n"
);
error!(
num_div_map,
"a {\n color: 1 / (a: b);\n}\n",
"Error: (a: b) isn't a valid CSS value."
);
error!(
num_div_function,
"a {\n color: 1 / get-function(lighten);\n}\n",
"Error: get-function(\"lighten\") isn't a valid CSS value."
);