implement builtin function str-index

This commit is contained in:
ConnorSkees 2020-03-22 15:58:32 -04:00
parent 4d45844b7a
commit c0ed933850
2 changed files with 31 additions and 0 deletions

View File

@ -126,4 +126,24 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
}
}),
);
f.insert(
"str-index".to_owned(),
Box::new(|args, _| {
max_args!(args, 2);
let s1 = match arg!(args, 0, "string") {
Value::Ident(i, _) => i,
v => return Err(format!("$string: {} is not a string.", v).into()),
};
let substr = match arg!(args, 1, "substring") {
Value::Ident(i, _) => i,
v => return Err(format!("$substring: {} is not a string.", v).into()),
};
Ok(match s1.find(&substr) {
Some(v) => Value::Dimension(Number::from(v + 1), Unit::None),
None => Value::Null,
})
}),
);
}

View File

@ -103,3 +103,14 @@ test!(
"a {\n color: str-length(\"foo bar\");\n}\n",
"a {\n color: 7;\n}\n"
);
test!(
str_index_char,
"a {\n color: str-index(abcd, a);\n}\n",
"a {\n color: 1;\n}\n"
);
test!(
str_index_str,
"a {\n color: str-index(abcd, ab);\n}\n",
"a {\n color: 1;\n}\n"
);
test!(str_index_null, "a {\n color: str-index(abcd, X);\n}\n", "");