diff --git a/src/builtin/meta.rs b/src/builtin/meta.rs index 0ab0ab7..1daf253 100644 --- a/src/builtin/meta.rs +++ b/src/builtin/meta.rs @@ -1,6 +1,7 @@ use std::collections::BTreeMap; use super::Builtin; +use crate::common::QuoteKind; use crate::value::Value; pub(crate) fn register(f: &mut BTreeMap) { @@ -35,4 +36,12 @@ pub(crate) fn register(f: &mut BTreeMap) { _ => Some(Value::False), } }); + decl!(f "unit", |args| { + let number = arg!(args, 0, "number"); + let unit = match number { + Value::Dimension(_, u) => u.to_string(), + _ => String::new() + }; + Some(Value::Ident(unit, QuoteKind::Double)) + }); } \ No newline at end of file diff --git a/tests/meta.rs b/tests/meta.rs index 8383ebd..68cb07a 100644 --- a/tests/meta.rs +++ b/tests/meta.rs @@ -38,3 +38,23 @@ test!( "a {\n color: feature-exists($feature: at-error)\n}\n", "a {\n color: true;\n}\n" ); +test!( + unit_px, + "a {\n color: unit(1px)\n}\n", + "a {\n color: \"px\";\n}\n" +); +test!( + unit_none, + "a {\n color: unit(1)\n}\n", + "a {\n color: \"\";\n}\n" +); +test!( + unit_non_numeric, + "a {\n color: unit(red)\n}\n", + "a {\n color: \"\";\n}\n" +); +test!( + unit_named_args, + "a {\n color: unit($number: 1px)\n}\n", + "a {\n color: \"px\";\n}\n" +);