2020-03-03 19:51:02 -05:00
|
|
|
use std::collections::HashMap;
|
2020-02-02 21:09:29 -05:00
|
|
|
|
2020-02-14 14:23:54 -05:00
|
|
|
use num_bigint::BigInt;
|
2020-03-22 22:28:54 -04:00
|
|
|
use num_traits::{Signed, ToPrimitive, Zero};
|
2020-02-14 14:23:54 -05:00
|
|
|
|
2020-04-05 18:20:58 -04:00
|
|
|
#[cfg(feature = "random")]
|
|
|
|
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
|
|
|
|
2020-02-08 20:32:10 -05:00
|
|
|
use super::Builtin;
|
2020-02-08 21:19:54 -05:00
|
|
|
use crate::common::QuoteKind;
|
2020-03-19 16:24:31 -04:00
|
|
|
use crate::unit::Unit;
|
2020-02-08 20:38:37 -05:00
|
|
|
use crate::value::{Number, Value};
|
2020-02-08 20:32:10 -05:00
|
|
|
|
2020-03-03 19:51:02 -05:00
|
|
|
pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
|
2020-03-16 10:35:38 -04:00
|
|
|
f.insert(
|
|
|
|
"to-upper-case".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(1)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
match arg!(args, scope, super_selector, 0, "string") {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Ident(i, q) => Ok(Value::Ident(i.to_ascii_uppercase(), q)),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => Err((
|
|
|
|
format!(
|
|
|
|
"$string: {} is not a string.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into()),
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
|
|
|
"to-lower-case".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(1)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
match arg!(args, scope, super_selector, 0, "string") {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Ident(i, q) => Ok(Value::Ident(i.to_ascii_lowercase(), q)),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => Err((
|
|
|
|
format!(
|
|
|
|
"$string: {} is not a string.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into()),
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
|
|
|
"str-length".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(1)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
match arg!(args, scope, super_selector, 0, "string") {
|
2020-03-22 22:28:54 -04:00
|
|
|
Value::Ident(i, _) => Ok(Value::Dimension(
|
|
|
|
Number::from(i.chars().count()),
|
|
|
|
Unit::None,
|
|
|
|
)),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => Err((
|
|
|
|
format!(
|
|
|
|
"$string: {} is not a string.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into()),
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
|
|
|
"quote".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(1)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
match arg!(args, scope, super_selector, 0, "string") {
|
2020-04-18 13:19:30 -04:00
|
|
|
Value::Ident(i, _) => Ok(Value::Ident(i, QuoteKind::Quoted)),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => Err((
|
|
|
|
format!(
|
|
|
|
"$string: {} is not a string.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into()),
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
|
|
|
"unquote".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(1)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
match arg!(args, scope, super_selector, 0, "string") {
|
2020-03-16 10:35:38 -04:00
|
|
|
i @ Value::Ident(..) => Ok(i.unquote()),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => Err((
|
|
|
|
format!(
|
|
|
|
"$string: {} is not a string.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into()),
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
|
|
|
"str-slice".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(3)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
let (string, quotes) = match arg!(args, scope, super_selector, 0, "string") {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Ident(s, q) => (s, q),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$string: {} is not a string.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-03-16 10:35:38 -04:00
|
|
|
};
|
2020-03-23 14:11:06 -04:00
|
|
|
let str_len = string.chars().count();
|
2020-04-04 18:17:04 -04:00
|
|
|
let start = match arg!(args, scope, super_selector, 1, "start-at") {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Dimension(n, Unit::None) if n.is_decimal() => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((format!("{} is not an int.", n), args.span()).into())
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
2020-03-22 22:28:54 -04:00
|
|
|
Value::Dimension(n, Unit::None) if n.is_positive() => {
|
2020-03-23 14:33:44 -04:00
|
|
|
n.to_integer().to_usize().unwrap_or(str_len + 1)
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
2020-03-22 22:28:54 -04:00
|
|
|
Value::Dimension(n, Unit::None) if n.is_zero() => 1_usize,
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Dimension(n, Unit::None) if n < -Number::from(str_len) => 1_usize,
|
|
|
|
Value::Dimension(n, Unit::None) => (BigInt::from(str_len + 1) + n.to_integer())
|
|
|
|
.to_usize()
|
|
|
|
.unwrap(),
|
|
|
|
v @ Value::Dimension(..) => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$start: Expected {} to have no units.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$start-at: {} is not a number.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
};
|
2020-04-04 18:17:04 -04:00
|
|
|
let mut end = match arg!(args, scope, super_selector, 2, "end-at" = Value::Null) {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Dimension(n, Unit::None) if n.is_decimal() => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((format!("{} is not an int.", n), args.span()).into())
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
2020-03-22 22:28:54 -04:00
|
|
|
Value::Dimension(n, Unit::None) if n.is_positive() => {
|
2020-03-23 14:33:44 -04:00
|
|
|
n.to_integer().to_usize().unwrap_or(str_len + 1)
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
2020-03-22 22:28:54 -04:00
|
|
|
Value::Dimension(n, Unit::None) if n.is_zero() => 0_usize,
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Dimension(n, Unit::None) if n < -Number::from(str_len) => 0_usize,
|
|
|
|
Value::Dimension(n, Unit::None) => (BigInt::from(str_len + 1) + n.to_integer())
|
|
|
|
.to_usize()
|
2020-03-23 14:33:44 -04:00
|
|
|
.unwrap_or(str_len + 1),
|
2020-03-16 10:35:38 -04:00
|
|
|
v @ Value::Dimension(..) => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$end: Expected {} to have no units.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
Value::Null => str_len,
|
2020-04-12 19:37:12 -04:00
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$end-at: {} is not a number.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-03-16 10:35:38 -04:00
|
|
|
};
|
2020-02-14 14:23:54 -05:00
|
|
|
|
2020-03-16 10:35:38 -04:00
|
|
|
if end > str_len {
|
|
|
|
end = str_len;
|
2020-02-14 14:23:54 -05:00
|
|
|
}
|
2020-03-16 10:35:38 -04:00
|
|
|
|
2020-03-23 14:16:27 -04:00
|
|
|
if start > end || start > str_len {
|
2020-04-18 13:19:30 -04:00
|
|
|
Ok(Value::Ident(String::new(), quotes))
|
2020-03-16 10:35:38 -04:00
|
|
|
} else {
|
2020-03-23 23:06:05 -04:00
|
|
|
Ok(Value::Ident(
|
|
|
|
string
|
|
|
|
.chars()
|
|
|
|
.skip(start - 1)
|
|
|
|
.take(end - start + 1)
|
|
|
|
.collect(),
|
2020-04-18 13:19:30 -04:00
|
|
|
quotes,
|
2020-03-23 23:06:05 -04:00
|
|
|
))
|
2020-02-14 14:23:54 -05:00
|
|
|
}
|
2020-03-16 10:35:38 -04:00
|
|
|
}),
|
|
|
|
);
|
2020-03-22 15:58:32 -04:00
|
|
|
f.insert(
|
|
|
|
"str-index".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(2)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
let s1 = match arg!(args, scope, super_selector, 0, "string") {
|
2020-03-22 15:58:32 -04:00
|
|
|
Value::Ident(i, _) => i,
|
2020-04-12 19:37:12 -04:00
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$string: {} is not a string.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-03-22 15:58:32 -04:00
|
|
|
};
|
|
|
|
|
2020-04-04 18:17:04 -04:00
|
|
|
let substr = match arg!(args, scope, super_selector, 1, "substring") {
|
2020-03-22 15:58:32 -04:00
|
|
|
Value::Ident(i, _) => i,
|
2020-04-12 19:37:12 -04:00
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$substring: {} is not a string.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-03-22 15:58:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(match s1.find(&substr) {
|
|
|
|
Some(v) => Value::Dimension(Number::from(v + 1), Unit::None),
|
|
|
|
None => Value::Null,
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
);
|
2020-03-22 16:14:45 -04:00
|
|
|
f.insert(
|
|
|
|
"str-insert".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(3)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
let (s1, quotes) = match arg!(args, scope, super_selector, 0, "string") {
|
2020-04-18 13:19:30 -04:00
|
|
|
Value::Ident(i, q) => (i, q),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$string: {} is not a string.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-03-22 16:14:45 -04:00
|
|
|
};
|
|
|
|
|
2020-04-04 18:17:04 -04:00
|
|
|
let substr = match arg!(args, scope, super_selector, 1, "insert") {
|
2020-03-22 16:14:45 -04:00
|
|
|
Value::Ident(i, _) => i,
|
2020-04-12 19:37:12 -04:00
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$insert: {} is not a string.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-03-22 16:14:45 -04:00
|
|
|
};
|
|
|
|
|
2020-04-04 18:17:04 -04:00
|
|
|
let index = match arg!(args, scope, super_selector, 2, "index") {
|
2020-03-22 16:48:57 -04:00
|
|
|
Value::Dimension(n, Unit::None) if n.is_decimal() => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((format!("$index: {} is not an int.", n), args.span()).into())
|
2020-03-22 16:48:57 -04:00
|
|
|
}
|
|
|
|
Value::Dimension(n, Unit::None) => n,
|
|
|
|
v @ Value::Dimension(..) => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$index: Expected {} to have no units.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!("$index: {} is not a number.", v.to_css_string(args.span())?),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
2020-03-22 16:48:57 -04:00
|
|
|
}
|
2020-03-22 16:14:45 -04:00
|
|
|
};
|
|
|
|
|
2020-03-22 18:13:55 -04:00
|
|
|
if s1.is_empty() {
|
|
|
|
return Ok(Value::Ident(substr, quotes));
|
|
|
|
}
|
|
|
|
|
2020-03-23 14:11:06 -04:00
|
|
|
let len = s1.chars().count();
|
2020-03-22 16:48:57 -04:00
|
|
|
|
2020-03-22 17:49:21 -04:00
|
|
|
// Insert substring at char position, rather than byte position
|
|
|
|
let insert = |idx, s1: String, s2| {
|
|
|
|
s1.chars()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, c)| {
|
|
|
|
if i + 1 == idx {
|
|
|
|
c.to_string() + s2
|
|
|
|
} else if idx == 0 && i == 0 {
|
|
|
|
s2.to_string() + &c.to_string()
|
|
|
|
} else {
|
|
|
|
c.to_string()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<String>()
|
|
|
|
};
|
|
|
|
|
2020-03-22 22:28:54 -04:00
|
|
|
let string = if index.is_positive() {
|
2020-03-22 17:49:21 -04:00
|
|
|
insert(
|
2020-03-23 13:57:00 -04:00
|
|
|
index
|
|
|
|
.to_integer()
|
|
|
|
.to_usize()
|
|
|
|
.unwrap_or(len + 1)
|
|
|
|
.min(len + 1)
|
|
|
|
- 1,
|
2020-03-22 17:49:21 -04:00
|
|
|
s1,
|
2020-03-22 16:48:57 -04:00
|
|
|
&substr,
|
2020-03-22 17:49:21 -04:00
|
|
|
)
|
2020-03-22 22:28:54 -04:00
|
|
|
} else if index.is_zero() {
|
2020-03-22 17:49:21 -04:00
|
|
|
insert(0, s1, &substr)
|
2020-03-22 16:48:57 -04:00
|
|
|
} else {
|
2020-03-23 13:57:00 -04:00
|
|
|
let idx = index.abs().to_integer().to_usize().unwrap_or(len + 1);
|
2020-03-22 17:13:38 -04:00
|
|
|
if idx > len {
|
2020-03-22 17:49:21 -04:00
|
|
|
insert(0, s1, &substr)
|
2020-03-22 17:13:38 -04:00
|
|
|
} else {
|
2020-03-22 17:49:21 -04:00
|
|
|
insert(len - idx + 1, s1, &substr)
|
2020-03-22 17:13:38 -04:00
|
|
|
}
|
2020-03-22 17:49:21 -04:00
|
|
|
};
|
2020-03-22 16:14:45 -04:00
|
|
|
|
|
|
|
Ok(Value::Ident(string, quotes))
|
|
|
|
}),
|
|
|
|
);
|
2020-04-05 18:20:58 -04:00
|
|
|
#[cfg(feature = "random")]
|
|
|
|
f.insert(
|
|
|
|
"unique-id".to_owned(),
|
|
|
|
Builtin::new(|args, _, _| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(0)?;
|
2020-04-05 18:20:58 -04:00
|
|
|
let mut rng = thread_rng();
|
|
|
|
let string = std::iter::repeat(())
|
|
|
|
.map(|()| rng.sample(Alphanumeric))
|
|
|
|
.take(7)
|
|
|
|
.collect();
|
|
|
|
Ok(Value::Ident(string, QuoteKind::None))
|
|
|
|
}),
|
|
|
|
);
|
2020-02-08 20:32:10 -05:00
|
|
|
}
|