support min and max as arguments to min and max

This commit is contained in:
Connor Skees 2021-07-11 02:13:05 -04:00
parent c40eec8728
commit 7ad2da040f
2 changed files with 34 additions and 5 deletions

View File

@ -202,7 +202,7 @@ impl<'a> Parser<'a> {
}
'm' | 'M' => {
self.toks.advance_cursor();
match self.toks.peek() {
let inner_fn_name = match self.toks.peek() {
Some(Token { kind: 'i', .. }) | Some(Token { kind: 'I', .. }) => {
self.toks.advance_cursor();
if !matches!(
@ -211,7 +211,8 @@ impl<'a> Parser<'a> {
) {
return Ok(None);
}
buf.push_str("min(");
"min"
}
Some(Token { kind: 'a', .. }) | Some(Token { kind: 'A', .. }) => {
self.toks.advance_cursor();
@ -221,10 +222,11 @@ impl<'a> Parser<'a> {
) {
return Ok(None);
}
buf.push_str("max(");
"max"
}
_ => return Ok(None),
}
};
self.toks.advance_cursor();
@ -232,7 +234,9 @@ impl<'a> Parser<'a> {
return Ok(None);
}
if let Some(val) = self.try_parse_min_max(fn_name, false)? {
self.toks.advance_cursor();
if let Some(val) = self.try_parse_min_max(inner_fn_name, true)? {
buf.push_str(&val);
} else {
return Ok(None);

View File

@ -105,3 +105,28 @@ error!(
"$a: 1px;\n$b: 2%;\na {\n color: max($a, $b);\n}\n", "Error: Incompatible units px and %."
);
// todo: special functions, min(calc(1), $b);
test!(
min_containing_max,
"a {\n color: min(1, max(2));\n}\n",
"a {\n color: min(1, max(2));\n}\n"
);
test!(
max_containing_min,
"a {\n color: max(1, min(2));\n}\n",
"a {\n color: max(1, min(2));\n}\n"
);
test!(
min_containing_max_as_only_arg,
"a {\n color: min(max(1px, 2px));\n}\n",
"a {\n color: min(max(1px, 2px));\n}\n"
);
test!(
max_containing_min_as_only_arg,
"a {\n color: max(min(1px, 2px));\n}\n",
"a {\n color: max(min(1px, 2px));\n}\n"
);
test!(
extremely_nested_min_and_max,
"a {\n color: min(max(min(max(min(min(1), max(2))))), min(max(min(3))));\n}\n",
"a {\n color: min(max(min(max(min(min(1), max(2))))), min(max(min(3))));\n}\n"
);