Handle number passed to invert()

This commit is contained in:
ConnorSkees 2020-02-16 17:00:31 -05:00
parent 948c489785
commit d4dd360682
2 changed files with 11 additions and 1 deletions

View File

@ -150,11 +150,13 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
decl!(f "invert", |args, _| {
max_args!(args, 2);
let weight = match arg!(args, 1, "weight"=Value::Dimension(Number::from(100), Unit::Percent)) {
Value::Dimension(n, u) => bound!("amount", n, u, 0, 100) / Number::from(100),
Value::Dimension(n, u) => bound!("weight", n, u, 0, 100) / Number::from(100),
v => return Err(format!("$weight: {} is not a number.", v).into()),
};
match arg!(args, 0, "color") {
Value::Color(c) => Ok(Value::Color(c.invert(weight))),
Value::Dimension(n, Unit::Percent) => Ok(Value::Ident(format!("invert({}%)", n), QuoteKind::None)),
Value::Dimension(..) => return Err("Only one argument may be passed to the plain-CSS invert() function.".into()),
v => return Err(format!("$color: {} is not a color.", v).into()),
}
});

View File

@ -258,6 +258,14 @@ 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!(
invert_weight_percent,
"a {\n color: invert(white, 20%);\n}\n",