From 27eeaeef08a513926481c7df999f66a731243e98 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Mon, 16 Nov 2020 03:09:40 -0500 Subject: [PATCH] plain-css invert accepts numbers of any unit --- src/builtin/functions/color/hsl.rs | 47 +++++++++++++++++++----------- tests/color.rs | 42 ++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 19 deletions(-) diff --git a/src/builtin/functions/color/hsl.rs b/src/builtin/functions/color/hsl.rs index b97ef24..83b2543 100644 --- a/src/builtin/functions/color/hsl.rs +++ b/src/builtin/functions/color/hsl.rs @@ -1,5 +1,6 @@ use super::{Builtin, GlobalFunctionMap}; +use codemap::Spanned; use num_traits::One; use crate::{ @@ -455,14 +456,18 @@ pub(crate) fn complement(mut args: CallArgs, parser: &mut Parser<'_>) -> SassRes pub(crate) fn invert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { args.max_args(2)?; - let weight = match args.default_arg( - 1, - "weight", - Value::Dimension(Some(Number::from(100)), Unit::Percent, true), - )? { - Value::Dimension(Some(n), u, _) => bound!(args, "weight", n, u, 0, 100) / Number::from(100), - Value::Dimension(None, ..) => todo!(), - v => { + let weight = match args.get(1, "weight") { + Some(Err(e)) => return Err(e), + Some(Ok(Spanned { + node: Value::Dimension(Some(n), u, _), + .. + })) => Some(bound!(args, "weight", n, u, 0, 100) / Number::from(100)), + Some(Ok(Spanned { + node: Value::Dimension(None, ..), + .. + })) => todo!(), + None => None, + Some(Ok(v)) => { return Err(( format!( "$weight: {} is not a number.", @@ -474,16 +479,24 @@ pub(crate) fn invert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult< } }; match args.get_err(0, "color")? { - Value::Color(c) => Ok(Value::Color(Box::new(c.invert(weight)))), - Value::Dimension(Some(n), Unit::Percent, _) => { - Ok(Value::String(format!("invert({}%)", n), QuoteKind::None)) + Value::Color(c) => Ok(Value::Color(Box::new( + c.invert(weight.unwrap_or(Number::one())), + ))), + Value::Dimension(Some(n), u, _) => { + if weight.is_some() { + Err(( + "Only one argument may be passed to the plain-CSS invert() function.", + args.span(), + ))?; + } + Ok(Value::String( + format!("invert({}{})", n, u), + QuoteKind::None, + )) + } + Value::Dimension(None, u, _) => { + Ok(Value::String(format!("invert(NaN{})", u), QuoteKind::None)) } - Value::Dimension(None, ..) => todo!(), - Value::Dimension(..) => Err(( - "Only one argument may be passed to the plain-CSS invert() function.", - args.span(), - ) - .into()), v => Err(( format!("$color: {} is not a color.", v.inspect(args.span())?), args.span(), diff --git a/tests/color.rs b/tests/color.rs index 15d74cd..e2e5269 100644 --- a/tests/color.rs +++ b/tests/color.rs @@ -245,8 +245,46 @@ test!( "a {\n color: invert(white);\n}\n", "a {\n color: black;\n}\n" ); -test!(invert_number, "a {\n color: invert(10%);\n}\n"); -test!(invert_number_casing, "a {\n color: iNveRt(10%);\n}\n"); +test!( + plain_invert_no_unit, + "a {\n color: invert(1);\n}\n", + "a {\n color: invert(1);\n}\n" +); +test!( + plain_invert_unit_percent, + "a {\n color: invert(10%);\n}\n", + "a {\n color: invert(10%);\n}\n" +); +test!( + plain_invert_unit_deg, + "a {\n color: invert(1deg);\n}\n", + "a {\n color: invert(1deg);\n}\n" +); +test!( + plain_invert_negative, + "a {\n color: invert(-1);\n}\n", + "a {\n color: invert(-1);\n}\n" +); +test!( + plain_invert_float, + "a {\n color: invert(1.5);\n}\n", + "a {\n color: invert(1.5);\n}\n" +); +test!( + plain_invert_arithmetic, + "a {\n color: invert(1 + 1);\n}\n", + "a {\n color: invert(2);\n}\n" +); +test!( + plain_invert_nan, + "a {\n color: invert((0 / 0));\n}\n", + "a {\n color: invert(NaN);\n}\n" +); +error!( + plain_invert_two_args, + "a {\n color: invert(1, 50%);\n}\n", + "Error: Only one argument may be passed to the plain-CSS invert() function." +); test!( invert_weight_percent, "a {\n color: invert(white, 20%);\n}\n",