str-len is a count of characters, not bytes

This commit is contained in:
ConnorSkees 2020-03-22 18:24:09 -04:00
parent c392c33ad8
commit d19c112765
2 changed files with 11 additions and 1 deletions

View File

@ -35,7 +35,7 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
Box::new(|args, _| {
max_args!(args, 1);
match arg!(args, 0, "string") {
Value::Ident(i, _) => Ok(Value::Dimension(Number::from(i.len()), Unit::None)),
Value::Ident(i, _) => Ok(Value::Dimension(Number::from(i.chars().count()), Unit::None)),
v => Err(format!("$string: {} is not a string.", v).into()),
}
}),

View File

@ -103,6 +103,16 @@ test!(
"a {\n color: str-length(\"foo bar\");\n}\n",
"a {\n color: 7;\n}\n"
);
test!(
str_len_double_wide,
"a {\n color: str-length(\"👭\");\n}\n",
"@charset \"UTF-8\";\na {\n color: 1;\n}\n"
);
test!(
str_len_combining,
"a {\n color: str-length(\"c\\0308\");\n}\n",
"a {\n color: 2;\n}\n"
);
test!(
str_index_char,
"a {\n color: str-index(abcd, a);\n}\n",