plain-css invert accepts numbers of any unit

This commit is contained in:
Connor Skees 2020-11-16 03:09:40 -05:00
parent f92a071434
commit 27eeaeef08
2 changed files with 70 additions and 19 deletions

View File

@ -1,5 +1,6 @@
use super::{Builtin, GlobalFunctionMap}; use super::{Builtin, GlobalFunctionMap};
use codemap::Spanned;
use num_traits::One; use num_traits::One;
use crate::{ 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<Value> { pub(crate) fn invert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?; args.max_args(2)?;
let weight = match args.default_arg( let weight = match args.get(1, "weight") {
1, Some(Err(e)) => return Err(e),
"weight", Some(Ok(Spanned {
Value::Dimension(Some(Number::from(100)), Unit::Percent, true), node: Value::Dimension(Some(n), u, _),
)? { ..
Value::Dimension(Some(n), u, _) => bound!(args, "weight", n, u, 0, 100) / Number::from(100), })) => Some(bound!(args, "weight", n, u, 0, 100) / Number::from(100)),
Value::Dimension(None, ..) => todo!(), Some(Ok(Spanned {
v => { node: Value::Dimension(None, ..),
..
})) => todo!(),
None => None,
Some(Ok(v)) => {
return Err(( return Err((
format!( format!(
"$weight: {} is not a number.", "$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")? { match args.get_err(0, "color")? {
Value::Color(c) => Ok(Value::Color(Box::new(c.invert(weight)))), Value::Color(c) => Ok(Value::Color(Box::new(
Value::Dimension(Some(n), Unit::Percent, _) => { c.invert(weight.unwrap_or(Number::one())),
Ok(Value::String(format!("invert({}%)", n), QuoteKind::None)) ))),
} Value::Dimension(Some(n), u, _) => {
Value::Dimension(None, ..) => todo!(), if weight.is_some() {
Value::Dimension(..) => Err(( Err((
"Only one argument may be passed to the plain-CSS invert() function.", "Only one argument may be passed to the plain-CSS invert() function.",
args.span(), args.span(),
) ))?;
.into()), }
Ok(Value::String(
format!("invert({}{})", n, u),
QuoteKind::None,
))
}
Value::Dimension(None, u, _) => {
Ok(Value::String(format!("invert(NaN{})", u), QuoteKind::None))
}
v => Err(( v => Err((
format!("$color: {} is not a color.", v.inspect(args.span())?), format!("$color: {} is not a color.", v.inspect(args.span())?),
args.span(), args.span(),

View File

@ -245,8 +245,46 @@ test!(
"a {\n color: invert(white);\n}\n", "a {\n color: invert(white);\n}\n",
"a {\n color: black;\n}\n" "a {\n color: black;\n}\n"
); );
test!(invert_number, "a {\n color: invert(10%);\n}\n"); test!(
test!(invert_number_casing, "a {\n color: iNveRt(10%);\n}\n"); 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!( test!(
invert_weight_percent, invert_weight_percent,
"a {\n color: invert(white, 20%);\n}\n", "a {\n color: invert(white, 20%);\n}\n",