diff --git a/src/builtin/list.rs b/src/builtin/list.rs index 635903b..6d148b6 100644 --- a/src/builtin/list.rs +++ b/src/builtin/list.rs @@ -193,4 +193,20 @@ pub(crate) fn register(f: &mut HashMap) { })) }), ); + f.insert( + "index".to_owned(), + Box::new(|mut args, _| { + max_args!(args, 2); + let list = match arg!(args, 0, "list") { + Value::List(v, ..) => v, + v => vec![v], + }; + let value = arg!(args, 1, "value"); + let index = match list.into_iter().position(|v| v == value) { + Some(v) => Number::from(v + 1), + None => return Ok(Value::Null), + }; + Ok(Value::Dimension(index, Unit::None)) + }), + ); } diff --git a/tests/list.rs b/tests/list.rs index 32de4dc..4300b17 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -189,3 +189,18 @@ test!( deeply_nested_square_braces, "a {\n color: [[[[[[a]]]]]];\n}\n" ); +test!( + index_found_space_separated, + "a {\n color: index(1px solid red, solid);\n}\n", + "a {\n color: 2;\n}\n" +); +test!( + index_not_found_space_separated, + "a {\n color: index(1px solid red, dashed);\n}\n", + "" +); +// test!( +// index_found_map, +// "a {\n color: index((width: 10px, height: 20px), (height 20px));\n}\n", +// "a {\n color: 2;\n}\n" +// );