use super::shadow::ShadowList;
const LENGTH_UNITS: [&str; 16] = [
"cm", "mm", "Q", "in", "pc", "pt", "px", "em", "ex", "ch", "rem", "lh", "vw", "vh", "vmin",
"vmax",
];
const LINE_WIDTHS: [&str; 3] = ["thin", "medium", "thick"];
const LINE_STYLES: [&str; 10] = [
"solid", "dashed", "dotted", "double", "groove", "ridge", "inset", "outset", "hidden", "none",
];
const ANGLES: [&str; 4] = ["deg", "grad", "rad", "turn"];
const GRADIENT_TYPES: [&str; 5] = [
"linear-gradient",
"radial-gradient",
"repeating-linear-gradient",
"repeating-radial-gradient",
"conic-gradient",
];
const VALID_POSITIONS: [&str; 5] = ["center", "top", "right", "bottom", "left"];
const ABSOLUTE_SIZES: [&str; 8] = [
"xx-small",
"x-small",
"small",
"medium",
"large",
"x-large",
"xx-large",
"xxx-large",
];
const RELATIVE_SIZES: [&str; 2] = ["larger", "smaller"];
const NAMED_COLORS: [&str; 150] = [
"transparent",
"currentColor",
"antiquewhite",
"aliceblue",
"aqua",
"aquamarine",
"azure",
"beige",
"bisque",
"black",
"blanchedalmond",
"blue",
"blueviolet",
"brown",
"burlywood",
"cadetblue",
"chartreuse",
"chocolate",
"coral",
"cornflowerblue",
"cornsilk",
"crimson",
"cyan",
"darkblue",
"darkcyan",
"darkgoldenrod",
"darkgray",
"darkgreen",
"darkgrey",
"darkkhaki",
"darkmagenta",
"darkolivegreen",
"darkorange",
"darkorchid",
"darkred",
"darksalmon",
"darkseagreen",
"darkslateblue",
"darkslategray",
"darkslategrey",
"darkturquoise",
"darkviolet",
"deeppink",
"deepskyblue",
"dimgray",
"dimgrey",
"dodgerblue",
"firebrick",
"floralwhite",
"forestgreen",
"fuchsia",
"gainsboro",
"ghostwhite",
"gold",
"goldenrod",
"gray",
"green",
"greenyellow",
"grey",
"honeydew",
"hotpink",
"indianred",
"indigo",
"ivory",
"khaki",
"lavender",
"lavenderblush",
"lawngreen",
"lemonchiffon",
"lightblue",
"lightcoral",
"lightcyan",
"lightgoldenrodyellow",
"lightgray",
"lightgreen",
"lightgrey",
"lightpink",
"lightsalmon",
"lightseagreen",
"lightskyblue",
"lightslategray",
"lightslategrey",
"lightsteelblue",
"lightyellow",
"lime",
"limegreen",
"linen",
"magenta",
"maroon",
"mediumaquamarine",
"mediumblue",
"mediumorchid",
"mediumpurple",
"mediumseagreen",
"mediumslateblue",
"mediumspringgreen",
"mediumturquoise",
"mediumvioletred",
"midnightblue",
"mintcream",
"mistyrose",
"moccasin",
"navajowhite",
"navy",
"oldlace",
"olive",
"olivedrab",
"orange",
"orangered",
"orchid",
"palegoldenrod",
"palegreen",
"paleturquoise",
"palevioletred",
"papayawhip",
"peachpuff",
"peru",
"pink",
"plum",
"powderblue",
"purple",
"rebeccapurple",
"red",
"rosybrown",
"royalblue",
"saddlebrown",
"salmon",
"sandybrown",
"seagreen",
"seashell",
"sienna",
"silver",
"skyblue",
"slateblue",
"slategray",
"slategrey",
"snow",
"springgreen",
"steelblue",
"tan",
"teal",
"thistle",
"tomato",
"turquoise",
"violet",
"wheat",
"white",
"whitesmoke",
"yellow",
"yellowgreen",
];
fn is_matching_base(value: &str) -> bool {
is_matching_var(value)
|| [
"inherit",
"initial",
"revert",
"revert-layer",
"unset",
"fill",
"max-content",
"min-content",
"fit-content",
]
.contains(&value)
}
pub fn is_matching_all(_value: &str) -> bool {
true
}
pub fn is_matching_url(value: &str) -> bool {
value.starts_with("url(")
}
pub fn is_matching_var(value: &str) -> bool {
value.starts_with("var(")
}
pub fn is_matching_shadow(value: &str) -> bool {
ShadowList::parse(value).is_some()
}
pub fn is_matching_absolute_size(value: &str) -> bool {
ABSOLUTE_SIZES.contains(&value)
}
pub fn is_matching_relative_size(value: &str) -> bool {
RELATIVE_SIZES.contains(&value)
}
pub fn is_matching_line_width(value: &str) -> bool {
LINE_WIDTHS.contains(&value)
}
pub fn is_matching_line_style(value: &str) -> bool {
LINE_STYLES.contains(&value)
}
pub fn is_matching_computational_css_function(value: &str) -> bool {
value.starts_with("min(")
|| value.starts_with("max(")
|| value.starts_with("clamp(")
|| value.starts_with("calc(")
}
pub fn is_matching_color(value: &str) -> bool {
(value.starts_with('#') && (value.len() == 4 || value.len() == 7))
|| ["rgb(", "rgba(", "hsl(", "hsla(", "hwb(", "lch(", "lab("]
.iter()
.any(|e| value.starts_with(e))
|| NAMED_COLORS.iter().any(|c| &value == c)
|| is_matching_base(value)
}
pub fn is_matching_length(value: &str) -> bool {
value.split(' ').all(|v| {
v == "0"
|| LENGTH_UNITS.iter().any(|u| v.ends_with(u))
|| is_matching_computational_css_function(value)
}) || is_matching_base(value)
}
pub fn is_matching_number(value: &str) -> bool {
value.parse::<f32>().is_ok()
|| is_matching_computational_css_function(value)
|| is_matching_base(value)
}
pub fn is_matching_percentage(value: &str) -> bool {
value.ends_with('%') || is_matching_computational_css_function(value) || is_matching_base(value)
}
pub fn is_matching_time(value: &str) -> bool {
value.ends_with('s')
|| value.ends_with("ms")
|| is_matching_computational_css_function(value)
|| is_matching_base(value)
}
pub fn is_matching_gradient(value: &str) -> bool {
GRADIENT_TYPES.iter().any(|t| value.starts_with(t)) || is_matching_base(value)
}
pub fn is_matching_position(value: &str) -> bool {
value
.split(' ')
.all(|v| VALID_POSITIONS.contains(&v) || is_matching_length(v) || is_matching_percentage(v))
|| is_matching_base(value)
}
pub fn is_matching_angle(value: &str) -> bool {
ANGLES.iter().any(|a| value.ends_with(a))
|| is_matching_computational_css_function(value)
|| is_matching_base(value)
}
pub fn is_matching_image(value: &str) -> bool {
is_matching_url(value)
|| is_matching_gradient(value)
|| ["element(", "image(", "cross-fade(", "image-set("]
.iter()
.any(|e| value.starts_with(e))
|| is_matching_base(value)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_matching_color_test() {
assert!(is_matching_color("blue"));
assert!(is_matching_color("#333"));
assert!(is_matching_color("#121212"));
assert!(is_matching_color("rgb(12.12,12,12)"));
assert!(is_matching_color("rgb(12 12 12)"));
assert!(is_matching_color("rgb(12 12 12/0.1)"));
assert!(is_matching_color("rgb(12 12 12 / 0.1)"));
assert!(is_matching_color("rgb(var(--blue),12,12)"));
assert!(is_matching_color("rgb(12 12 12 / var(--opacity))"));
assert!(is_matching_color("rgba(12,12,12,0.12)"));
assert!(is_matching_color("hsl(360,100%,50%)"));
assert!(is_matching_color("hsl(3.14rad,100%,50%)"));
assert!(is_matching_color("hsl(3.14rad 100% 50%/0.42)"));
assert!(is_matching_color("hsl(var(--hue) 12% 42%/var(--opacity))"));
assert!(is_matching_color("hsla(360,100%,50%,0.12)"));
}
#[test]
fn is_matching_length_test() {
assert!(is_matching_length("300px"));
assert!(!is_matching_length("50%"));
assert!(is_matching_length("30vw"));
assert!(is_matching_length("min(10%,10px)"));
assert!(is_matching_length("0"));
}
#[test]
fn is_matching_shadow_with_functions_test() {
assert!(is_matching_shadow("10px 10px min(1px,2px) 10px rgb(1,1,1)"));
assert!(is_matching_shadow("inset 0 -3em 3em rgba(0,0,0,0.1),0 0 0 2px rgb(255,255,255),0.3em 0.3em 1em rgba(0,0,0,0.3)"));
assert!(is_matching_shadow(
"var(--a, 0 0 1px rgb(0, 0, 0)), 0 0 1px rgb(0, 0, 0)"
));
}
}