From 2aaaf59e4f3ac411cfcdb4cd18e8a0fa2c3d2e48 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 24 May 2020 08:17:08 -0400 Subject: [PATCH] make division with number as lhs more robust --- src/value/ops.rs | 15 +++++++++++++-- tests/division.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/value/ops.rs b/src/value/ops.rs index a738145..a0d5275 100644 --- a/src/value/ops.rs +++ b/src/value/ops.rs @@ -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) => { diff --git a/tests/division.rs b/tests/division.rs index a6f36b6..4668463 100644 --- a/tests/division.rs +++ b/tests/division.rs @@ -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." +);