From a8e97984d4c437522c3fabbf375ecca6f67e6a62 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 20 Apr 2020 01:56:53 -0400 Subject: [PATCH] subtraction occurs when no space between operands --- src/value/parse.rs | 35 ++++++++++++++++++++++++++++++----- tests/subtraction.rs | 22 +++++++++++++++++++++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/value/parse.rs b/src/value/parse.rs index d8b066f..47bd750 100644 --- a/src/value/parse.rs +++ b/src/value/parse.rs @@ -193,6 +193,7 @@ fn eat_op>( super_selector: &Selector, op: Spanned, space_separated: &mut Vec>, + last_was_whitespace: bool, ) -> SassResult<()> { match op.node { Op::Not => { @@ -221,8 +222,14 @@ fn eat_op>( } } Op::Minus => { - if devour_whitespace(iter) { + if devour_whitespace(iter) || !last_was_whitespace { let right = single_value(iter, scope, super_selector, op.span)?; + if !last_was_whitespace && right.node == Value::Null { + space_separated.push( + right.map_node(|_| Value::Ident("-null".to_string(), QuoteKind::None)), + ); + return Ok(()); + } if let Some(left) = space_separated.pop() { space_separated.push(Spanned { node: Value::BinaryOp(Box::new(left.node), op.node, Box::new(right.node)), @@ -331,20 +338,36 @@ impl Value { Some(Token { pos, .. }) => *pos, None => todo!("Expected expression."), }; + devour_whitespace(toks); while toks.peek().is_some() { intermediate_values.push(Self::parse_intermediate_value(toks, scope, super_selector)?); } + let mut last_was_whitespace = false; let mut space_separated = Vec::new(); let mut comma_separated = Vec::new(); let mut iter = intermediate_values.into_iter().peekable(); while let Some(val) = iter.next() { match val { - IntermediateValue::Value(v) => space_separated.push(v), - IntermediateValue::Op(op) => { - eat_op(&mut iter, scope, super_selector, op, &mut space_separated)?; + IntermediateValue::Value(v) => { + last_was_whitespace = false; + space_separated.push(v) + } + IntermediateValue::Op(op) => { + eat_op( + &mut iter, + scope, + super_selector, + op, + &mut space_separated, + last_was_whitespace, + )?; + } + IntermediateValue::Whitespace => { + last_was_whitespace = true; + continue; } - IntermediateValue::Whitespace => continue, IntermediateValue::Comma => { + last_was_whitespace = false; if space_separated.len() == 1 { comma_separated.push(space_separated.pop().unwrap()); } else { @@ -366,6 +389,7 @@ impl Value { } } IntermediateValue::Bracketed(t) => { + last_was_whitespace = false; if t.node.is_empty() { space_separated.push( Value::List(Vec::new(), ListSeparator::Space, Brackets::Bracketed) @@ -384,6 +408,7 @@ impl Value { ) } IntermediateValue::Paren(t) => { + last_was_whitespace = false; parse_paren(t, scope, super_selector, &mut space_separated)?; } } diff --git a/tests/subtraction.rs b/tests/subtraction.rs index f5fc3df..88a602f 100644 --- a/tests/subtraction.rs +++ b/tests/subtraction.rs @@ -170,6 +170,26 @@ test!( ); test!( number_minus_minus_number, - "a {\n color: 1 - - 2;;\n}\n", + "a {\n color: 1 - - 2;\n}\n", "a {\n color: 3;\n}\n" ); +test!( + sub_no_space, + "a {\n color: 10-10;\n}\n", + "a {\n color: 0;\n}\n" +); +test!( + sub_space_on_left, + "a {\n color: 10 -10;\n}\n", + "a {\n color: 10 -10;\n}\n" +); +test!( + sub_space_on_right, + "a {\n color: 10- 10;\n}\n", + "a {\n color: 0;\n}\n" +); +test!( + sub_space_on_both, + "a {\n color: 10 - 10;\n}\n", + "a {\n color: 0;\n}\n" +);