equality does unit conversions

This commit is contained in:
ConnorSkees 2020-04-03 14:34:59 -04:00
parent f4efcf0b45
commit 2d7f69e3da
4 changed files with 29 additions and 1 deletions

View File

@ -225,7 +225,10 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
v => vec![v], v => vec![v],
}; };
let value = arg!(args, 1, "value"); let value = arg!(args, 1, "value");
let index = match list.into_iter().position(|v| v == value) { let index = match list
.into_iter()
.position(|v| v.equals(value.clone()).unwrap())
{
Some(v) => Number::from(v + 1), Some(v) => Number::from(v + 1),
None => return Ok(Value::Null), None => return Ok(Value::Null),
}; };

View File

@ -189,6 +189,21 @@ impl Value {
Self::Ident(s2, ..) => s1 == s2, Self::Ident(s2, ..) => s1 == s2,
_ => false, _ => false,
}, },
Self::Dimension(n, unit) => match other {
Self::Dimension(n2, unit2) => {
if !unit.comparable(&unit2) {
false
} else if unit == unit2 {
n == n2
} else if unit == Unit::None || unit2 == Unit::None {
false
} else {
n == (n2
* UNIT_CONVERSION_TABLE[&unit.to_string()][&unit2.to_string()].clone())
}
}
_ => false,
},
s => s == other.eval()?, s => s == other.eval()?,
}) })
} }

View File

@ -293,3 +293,8 @@ test!(
"a {\n color: index((width: 10px, height: 20px), (height 20px));\n}\n", "a {\n color: index((width: 10px, height: 20px), (height 20px));\n}\n",
"a {\n color: 2;\n}\n" "a {\n color: 2;\n}\n"
); );
test!(
index_unit_conversions,
"a {\n color: index(1px 1in 1cm, 96px);\n}\n",
"a {\n color: 2;\n}\n"
);

View File

@ -86,3 +86,8 @@ test!(
"a {\n color: -0.000000000001;\n}\n", "a {\n color: -0.000000000001;\n}\n",
"a {\n color: 0;\n}\n" "a {\n color: 0;\n}\n"
); );
test!(
equality_unit_conversions,
"a {\n color: 1in == 96px;\n}\n",
"a {\n color: true;\n}\n"
);