implement builtin function str-index
This commit is contained in:
parent
4d45844b7a
commit
c0ed933850
@ -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,
|
||||
})
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
@ -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", "");
|
||||
|
Loading…
x
Reference in New Issue
Block a user