use super::matcher::Out;
use super::value::Component;
use crate::parser::ComponentValue;
use crate::tokenizer::Token;
pub(crate) const CSS_WIDE: &[&str] = &[
"initial",
"inherit",
"unset",
"revert",
"revert-layer",
"revert-rule",
"default",
];
fn values<'o, 'a>(outs: &'o [Out<'a>]) -> impl Iterator<Item = &'o Component<'a>> {
outs.iter().filter_map(|o| match o {
Out::C(c) => Some(c),
_ => None,
})
}
fn named(c: &Component<'_>, words: &[&str]) -> bool {
matches!(c, Component::Ident(s) if words.iter().any(|w| s.eq_ignore_ascii_case(w)))
}
pub(crate) const GENERIC_FAMILIES: &[&str] = &[
"serif",
"sans-serif",
"cursive",
"fantasy",
"monospace",
"system-ui",
"math",
"ui-serif",
"ui-sans-serif",
"ui-monospace",
"ui-rounded",
];
pub(crate) const TYPES: &[&str] = &[
"counter-style-name",
"counter-name",
"line-names",
"grid-line",
"animateable-feature",
"single-transition-property",
"font-family-name",
"keyframes-name",
"opentype-tag",
];
pub(crate) fn of_type(name: &str, outs: &[Out<'_>]) -> bool {
let mut v = values(outs);
match name {
"counter-style-name" | "counter-name" => !v.any(|c| named(c, &["none"])),
"line-names" => !v.any(|c| match c {
Component::Block(names) => names.iter().any(|n| named(n, &["span", "auto"])),
_ => false,
}),
"grid-line" => !v.any(|c| named(c, &["span", "auto"])),
"animateable-feature" => {
!v.any(|c| named(c, &["will-change", "none", "all", "auto"]) || named(c, CSS_WIDE))
}
"single-transition-property" => !v.any(|c| named(c, &["none"])),
"font-family-name" => !v.any(|c| named(c, GENERIC_FAMILIES) || named(c, CSS_WIDE)),
"keyframes-name" => !v.any(|c| matches!(c, Component::String(s) if s.is_empty())),
"opentype-tag" => v.all(|c| match c {
Component::String(s) => s.len() == 4 && s.bytes().all(|b| (0x20..=0x7e).contains(&b)),
_ => true,
}),
_ => true,
}
}
pub(crate) fn function(name: &str, args: &[Component<'_>]) -> bool {
match name {
"steps" => match args.first() {
Some(Component::Number { value, .. }) => {
let none = args
.last()
.is_some_and(|a| *a == Component::Keyword("jump-none"));
*value >= if none { 2.0 } else { 1.0 }
}
_ => true,
},
"symbols" => {
let symbols = args
.iter()
.filter(|a| !matches!(a, Component::Keyword(_)))
.count();
let two = args
.first()
.is_some_and(|a| matches!(a, Component::Keyword("numeric" | "alphabetic")));
let images = args
.iter()
.any(|a| matches!(a, Component::Url(_) | Component::Function { .. }));
!images && (!two || symbols >= 2)
}
"path" => match args.last() {
Some(Component::String(s)) => super::path::normalize(s).is_some(),
_ => false,
},
n if n.ends_with("radial-gradient") => {
let end = args
.iter()
.position(|a| matches!(a, Component::Literal(',') | Component::Keyword("at")))
.unwrap_or(args.len());
let sizes = args[..end]
.iter()
.filter(|a| {
matches!(
a,
Component::Dimension(..) | Component::Percentage(_) | Component::Math(_)
)
})
.count();
let has = |k: &'static str| args[..end].contains(&Component::Keyword(k));
!(has("circle") && sizes == 2) && !(has("ellipse") && sizes == 1)
}
_ => true,
}
}
pub(crate) const PROPERTIES: &[&str] = &[
"color-scheme",
"view-transition-class",
"flex",
"flex-basis",
"font-language-override",
"grid-template-areas",
"cursor",
"offset-path",
];
pub(crate) fn property(name: &str, raw: &[ComponentValue<'_>], c: &[Component<'_>]) -> bool {
match name {
"color-scheme" => !c
.iter()
.any(|x| named(x, &["normal", "light", "dark", "only"])),
"view-transition-class" => !c.iter().any(|x| named(x, &["none"])),
"flex" => {
let numbers: Vec<f64> = raw
.iter()
.filter_map(|v| match v {
ComponentValue::Token(Token::Number(n), _) => Some(n.value),
_ => None,
})
.collect();
numbers.len() < 3 || numbers[2] == 0.0
}
"flex-basis" => !c.iter().any(|x| {
matches!(
x,
Component::Function {
name: "anchor-size",
..
}
)
}),
"font-language-override" => c.iter().all(|x| match x {
Component::String(s) => {
(1..=4).contains(&s.len())
&& s.bytes().all(|b| (0x20..=0x7e).contains(&b))
&& !s.trim_end_matches(' ').is_empty()
}
_ => true,
}),
"grid-template-areas" => areas(c),
"cursor" => !c.iter().any(|x| match x {
Component::Function {
name: "image-set",
arguments,
} => arguments
.iter()
.any(|a| matches!(a, Component::Function { .. })),
_ => false,
}),
"offset-path" => !raw.iter().any(|v| match v {
ComponentValue::Function(f) if f.name.eq_ignore_ascii_case("path") => f
.arguments
.iter()
.any(|a| matches!(a, ComponentValue::Token(Token::Comma, _))),
_ => false,
}),
_ => true,
}
}
fn areas(c: &[Component<'_>]) -> bool {
let rows: Vec<Vec<String>> = c
.iter()
.filter_map(|x| match x {
Component::String(s) => Some(
s.split([' ', '\t', '\n', '\r', '\x0c'])
.filter(|w| !w.is_empty())
.flat_map(|w| {
let mut out = Vec::new();
let mut rest = w;
while !rest.is_empty() {
let dots = rest.starts_with('.');
let end = rest.find(|ch| (ch == '.') != dots).unwrap_or(rest.len());
out.push(if dots {
".".to_string()
} else {
rest[..end].to_string()
});
rest = &rest[end..];
}
out
})
.collect(),
),
_ => None,
})
.collect();
if rows.is_empty() || rows.iter().any(Vec::is_empty) {
return c.iter().all(|x| !matches!(x, Component::String(_)));
}
let width = rows[0].len();
if rows.iter().any(|r| r.len() != width) {
return false;
}
let mut seen: std::collections::HashMap<&str, (usize, usize, usize, usize, usize)> =
std::collections::HashMap::new();
for (y, row) in rows.iter().enumerate() {
for (x, cell) in row.iter().enumerate() {
if cell == "." {
continue;
}
let s = seen.entry(cell).or_insert((x, x, y, y, 0));
s.0 = s.0.min(x);
s.1 = s.1.max(x);
s.2 = s.2.min(y);
s.3 = s.3.max(y);
s.4 += 1;
}
}
seen.values()
.all(|&(x0, x1, y0, y1, n)| n == (x1 - x0 + 1) * (y1 - y0 + 1))
}