use core::ops::{BitAnd, BitOr};
use cssparser::{ParseError, Parser, Token};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PseudoClasses(u8);
impl PseudoClasses {
pub const NONE: Self = Self(0);
pub const HOVER: Self = Self(1);
pub const ACTIVE: Self = Self(1 << 1);
pub const FOCUS: Self = Self(1 << 2);
pub const DISABLED: Self = Self(1 << 3);
pub const ENGAGED: Self = Self(1 << 4);
pub const CHECKED: Self = Self(1 << 5);
pub const OPEN: Self = Self(1 << 6);
pub const REFUSED: Self = Self(1 << 7);
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
Some(cssparser::match_ignore_ascii_case! { name,
"hover" => Self::HOVER,
"active" => Self::ACTIVE,
"focus" => Self::FOCUS,
"disabled" => Self::DISABLED,
"engaged" => Self::ENGAGED,
"checked" => Self::CHECKED,
"open" => Self::OPEN,
"refused" => Self::REFUSED,
_ => return None,
})
}
#[must_use]
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[must_use]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
}
impl BitOr for PseudoClasses {
type Output = Self;
fn bitor(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
impl BitAnd for PseudoClasses {
type Output = Self;
fn bitand(self, other: Self) -> Self {
Self(self.0 & other.0)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Specificity {
Type,
Class,
Id,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct Compound {
pub type_name: Option<Box<str>>,
pub id: Option<Box<str>>,
pub classes: Vec<Box<str>>,
pub pseudo: PseudoClasses,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Combinator {
Descendant,
Child,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Selector {
pub(crate) subject: Compound,
pub(crate) ancestors: Vec<(Combinator, Compound)>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Bucket<'a> {
Id(&'a str),
Class(&'a str),
Type(&'a str),
Universal,
}
pub(crate) trait Element: Sized {
fn type_name(&self) -> &str;
fn id(&self) -> Option<&str>;
fn has_class(&self, class: &str) -> bool;
fn pseudo(&self) -> PseudoClasses;
fn parent(&self) -> Option<Self>;
}
impl Compound {
fn matches(&self, element: &impl Element) -> bool {
self.type_name
.as_deref()
.is_none_or(|name| element.type_name().eq_ignore_ascii_case(name))
&& self.id.as_deref().is_none_or(|id| element.id() == Some(id))
&& self.classes.iter().all(|class| element.has_class(class))
&& element.pseudo().contains(self.pseudo)
}
const fn is_empty(&self) -> bool {
self.type_name.is_none()
&& self.id.is_none()
&& self.classes.is_empty()
&& self.pseudo.is_empty()
}
}
impl Selector {
pub fn parse_list(css: &str) -> Result<Vec<Self>, String> {
let mut parser = Parser::new(css);
parse_selector_list(&mut parser).map_err(|error| match error.kind {
cssparser::ParseErrorKind::Custom(message) => message,
cssparser::ParseErrorKind::Basic(kind) => kind.to_string(),
})
}
#[must_use]
pub fn specificity(&self) -> Specificity {
let compounds =
|| core::iter::once(&self.subject).chain(self.ancestors.iter().map(|(_, c)| c));
if compounds().any(|compound| compound.id.is_some()) {
Specificity::Id
} else if compounds()
.any(|compound| !compound.classes.is_empty() || !compound.pseudo.is_empty())
{
Specificity::Class
} else {
Specificity::Type
}
}
pub(crate) fn subject_pseudo(&self) -> PseudoClasses {
self.subject.pseudo
}
pub(crate) fn ancestor_pseudo(&self) -> PseudoClasses {
self.ancestors
.iter()
.fold(PseudoClasses::NONE, |all, (_, compound)| {
all | compound.pseudo
})
}
pub(crate) fn has_ancestors(&self) -> bool {
!self.ancestors.is_empty()
}
pub(crate) fn bucket(&self) -> Bucket<'_> {
let subject = &self.subject;
if let Some(id) = subject.id.as_deref() {
Bucket::Id(id)
} else if let Some(class) = subject.classes.first() {
Bucket::Class(class)
} else if let Some(name) = subject.type_name.as_deref() {
Bucket::Type(name)
} else {
Bucket::Universal
}
}
pub(crate) fn matches(&self, element: &impl Element) -> bool {
self.subject.matches(element) && self.ancestors_match(0, element)
}
fn ancestors_match(&self, from: usize, element: &impl Element) -> bool {
let Some((combinator, compound)) = self.ancestors.get(from) else {
return true;
};
match combinator {
Combinator::Child => element.parent().is_some_and(|parent| {
compound.matches(&parent) && self.ancestors_match(from + 1, &parent)
}),
Combinator::Descendant => {
let mut next = element.parent();
while let Some(ancestor) = next {
if compound.matches(&ancestor) && self.ancestors_match(from + 1, &ancestor) {
return true;
}
next = ancestor.parent();
}
false
}
}
}
}
pub(crate) type SelectorError = ParseError<String>;
fn refuse<T>(message: impl Into<String>) -> Result<T, SelectorError> {
Err(ParseError::custom(message.into()))
}
pub(crate) fn parse_selector_list<'i>(
input: &mut Parser<'i>,
) -> Result<Vec<Selector>, SelectorError> {
input.parse_comma_separated(parse_selector)
}
fn parse_selector<'i>(input: &mut Parser<'i>) -> Result<Selector, SelectorError> {
let mut compounds = vec![parse_compound(input)?];
let mut combinators = Vec::new();
loop {
let mut spaced = false;
loop {
let before = input.state();
match input.next_including_whitespace() {
Ok(Token::WhiteSpace(_)) => spaced = true,
Ok(_) => {
input.reset(&before);
break;
}
Err(_) => break,
}
}
let before = input.state();
let combinator = match input.next_including_whitespace() {
Err(_) => break,
Ok(Token::Delim('>')) => {
input.skip_whitespace();
Combinator::Child
}
Ok(Token::Delim(delim @ ('+' | '~'))) => {
return refuse(format!("the `{delim}` sibling combinator is not supported"));
}
Ok(_) if spaced => {
input.reset(&before);
Combinator::Descendant
}
Ok(token) => {
let token = token.clone();
return refuse(format!("unexpected {token:?} in a selector"));
}
};
combinators.push(combinator);
compounds.push(parse_compound(input)?);
}
let subject = compounds
.pop()
.expect("a selector has at least one compound");
let ancestors = combinators
.into_iter()
.rev()
.zip(compounds.into_iter().rev())
.collect();
Ok(Selector { subject, ancestors })
}
fn parse_compound<'i>(input: &mut Parser<'i>) -> Result<Compound, SelectorError> {
let mut compound = Compound::default();
let mut universal = false;
let mut first = true;
loop {
let before = input.state();
let Ok(token) = input.next_including_whitespace() else {
break;
};
match token.clone() {
Token::Ident(name) if first => {
compound.type_name = Some(name.to_ascii_lowercase().into_boxed_str());
}
Token::Delim('*') if first => universal = true,
Token::IDHash(id) => {
if compound.id.is_some() {
return refuse(format!(
"a compound selector names two ids, the second `#{id}`"
));
}
compound.id = Some(Box::from(&*id));
}
Token::Hash(hash) => return refuse(format!("`#{hash}` is not a valid id")),
Token::Delim('.') => match input.next_including_whitespace() {
Ok(Token::Ident(class)) => compound.classes.push(Box::from(&**class)),
_ => return refuse("`.` is not followed by a class name"),
},
Token::Colon => match input.next_including_whitespace() {
Ok(Token::Ident(name)) => match PseudoClasses::from_name(name) {
Some(pseudo) => compound.pseudo = compound.pseudo | pseudo,
None => return refuse(format!("the pseudo-class `:{name}` is not supported")),
},
Ok(Token::Colon) => return refuse("pseudo-elements are not supported"),
Ok(Token::Function(name)) => {
return refuse(format!("the pseudo-class `:{name}()` is not supported"));
}
_ => return refuse("`:` is not followed by a pseudo-class"),
},
Token::SquareBracketBlock => return refuse("attribute selectors are not supported"),
_ => {
input.reset(&before);
break;
}
}
first = false;
}
if compound.is_empty() && !universal {
let found = input.next().cloned();
return match found {
Ok(token) => refuse(format!("expected a selector, found {token:?}")),
Err(_) => refuse("expected a selector"),
};
}
Ok(compound)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct NodeSelector<'a> {
pub type_name: Option<&'a str>,
pub id: Option<&'a str>,
tail: &'a str,
text: &'a str,
}
impl<'a> NodeSelector<'a> {
pub const EMPTY: Self = Self {
type_name: None,
id: None,
tail: "",
text: "",
};
pub const fn text(self) -> &'a str {
self.text
}
pub fn parse(text: &'a str) -> Result<Self, ()> {
let is_name = |name: &str| {
!name.is_empty()
&& name.chars().all(|ch| {
ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' || !ch.is_ascii()
})
};
let (head, tail) = text.split_at(text.find(['#', '.']).unwrap_or(text.len()));
let type_name = match head {
"" => None,
head if is_name(head) => Some(head),
_ => return Err(()),
};
let mut id = None;
for (marker, name) in segments(tail) {
if !is_name(name) || marker == b'#' && id.replace(name).is_some() {
return Err(());
}
}
Ok(Self {
type_name,
id,
tail,
text,
})
}
pub fn classes(self) -> impl Iterator<Item = &'a str> {
segments(self.tail).filter_map(|(marker, name)| (marker == b'.').then_some(name))
}
}
fn segments(tail: &str) -> impl Iterator<Item = (u8, &str)> {
let mut rest = tail;
core::iter::from_fn(move || {
let marker = *rest.as_bytes().first()?;
let body = &rest[1..];
let end = body.find(['#', '.']).unwrap_or(body.len());
rest = &body[end..];
Some((marker, &body[..end]))
})
}
#[cfg(test)]
mod tests {
use super::*;
struct Row {
kind: &'static str,
selector: &'static str,
pseudo: PseudoClasses,
parent: Option<usize>,
}
#[derive(Clone, Copy)]
struct TableElement<'a> {
rows: &'a [Row],
at: usize,
}
impl Element for TableElement<'_> {
fn type_name(&self) -> &str {
let row = &self.rows[self.at];
NodeSelector::parse(row.selector)
.expect("valid")
.type_name
.unwrap_or(row.kind)
}
fn id(&self) -> Option<&str> {
NodeSelector::parse(self.rows[self.at].selector)
.expect("valid")
.id
}
fn has_class(&self, class: &str) -> bool {
NodeSelector::parse(self.rows[self.at].selector)
.expect("valid")
.classes()
.any(|own| own == class)
}
fn pseudo(&self) -> PseudoClasses {
self.rows[self.at].pseudo
}
fn parent(&self) -> Option<Self> {
self.rows[self.at].parent.map(|at| Self {
rows: self.rows,
at,
})
}
}
fn one(css: &str) -> Selector {
let mut list = Selector::parse_list(css).unwrap_or_else(|why| panic!("{css}: {why}"));
assert_eq!(list.len(), 1, "{css}");
list.remove(0)
}
const TREE: &[Row] = &[
Row {
kind: "block",
selector: "#stats.panel",
pseudo: PseudoClasses::NONE,
parent: None,
},
Row {
kind: "block",
selector: "list.rows",
pseudo: PseudoClasses(PseudoClasses::HOVER.0 | PseudoClasses::OPEN.0),
parent: Some(0),
},
Row {
kind: "block",
selector: ".row.selected",
pseudo: PseudoClasses::HOVER,
parent: Some(1),
},
Row {
kind: "span",
selector: ".label",
pseudo: PseudoClasses::CHECKED,
parent: Some(2),
},
Row {
kind: "span",
selector: "#value",
pseudo: PseudoClasses::ACTIVE,
parent: Some(2),
},
];
#[test]
fn each_selector_matches_exactly_the_nodes_it_names() {
let cases: &[(&str, &[usize])] = &[
("block", &[0, 2]),
("span", &[3, 4]),
("LIST", &[1]),
("*", &[0, 1, 2, 3, 4]),
("#stats", &[0]),
("block#stats.panel", &[0]),
("span#stats", &[]),
(".row", &[2]),
(".row.selected", &[2]),
(".row.missing", &[]),
(":hover", &[1, 2]),
(".row:hover", &[2]),
(".label:hover", &[]),
("span:active", &[4]),
(":focus", &[]),
(":disabled", &[]),
(":engaged", &[]),
(":checked", &[3]),
("span:checked", &[3]),
(".row:checked", &[]),
(":open", &[1]),
("list:open > .row", &[2]),
(":open:checked", &[]),
("#stats span", &[3, 4]),
("#stats > span", &[]),
(".row > span", &[3, 4]),
("list > .row > #value", &[4]),
(".panel .rows .selected .label", &[3]),
(".panel block span", &[3, 4]),
(".panel > * span", &[3, 4]),
(".panel > * > * > span", &[3, 4]),
(".panel > block span", &[]),
("list:hover .label", &[3]),
(".panel:hover span", &[]),
];
for (css, want) in cases {
let selector = one(css);
let got: Vec<usize> = (0..TREE.len())
.filter(|&at| selector.matches(&TableElement { rows: TREE, at }))
.collect();
assert_eq!(&got, want, "`{css}`");
}
}
#[test]
fn specificity_is_the_tier_of_the_strongest_component() {
let cases = [
("block", Specificity::Type),
("block span", Specificity::Type),
("*", Specificity::Type),
(".a", Specificity::Class),
(".a.b.c.d", Specificity::Class),
(":hover", Specificity::Class),
("block > span:active", Specificity::Class),
("#x", Specificity::Id),
("#x span", Specificity::Id),
("block .a #x", Specificity::Id),
];
for (css, want) in cases {
assert_eq!(one(css).specificity(), want, "`{css}`");
}
assert!(Specificity::Id > Specificity::Class && Specificity::Class > Specificity::Type);
}
#[test]
fn lists_whitespace_comments_and_escapes_parse() {
let list = Selector::parse_list(" .a ,/* note */#b>span , c d ").expect("parses");
assert_eq!(list.len(), 3);
assert_eq!(list[1].subject.type_name.as_deref(), Some("span"));
assert_eq!(list[1].ancestors[0].0, Combinator::Child);
assert_eq!(list[2].ancestors[0].0, Combinator::Descendant);
let escaped = one(r".a\:b");
assert_eq!(escaped.subject.classes, [Box::from("a:b")]);
let escaped = one(r"#\31 23");
assert_eq!(escaped.subject.id.as_deref(), Some("123"));
}
#[test]
fn everything_outside_the_grammar_is_refused_by_name() {
let cases = [
("a + b", "sibling"),
("a ~ b", "sibling"),
(":nth-child(2)", "not supported"),
(":visited", "`:visited`"),
("::before", "pseudo-elements"),
("[href]", "attribute"),
("#1a", "not a valid id"),
("#a#b", "two ids"),
(". a", "class name"),
("a,", "expected a selector"),
("a >", "expected a selector"),
("> a", "expected a selector"),
("", "expected a selector"),
];
for (css, message) in cases {
let error = Selector::parse_list(css).expect_err(css);
assert!(
error.contains(message),
"`{css}` refused with {error:?}, not {message:?}"
);
}
}
#[test]
fn pseudo_dependencies_are_split_between_the_node_and_its_ancestors() {
let selector = one(".list:hover > .row:active:focus");
assert_eq!(
selector.subject_pseudo(),
PseudoClasses::ACTIVE | PseudoClasses::FOCUS
);
assert_eq!(selector.ancestor_pseudo(), PseudoClasses::HOVER);
assert!(selector.has_ancestors());
assert!(!one(".row").has_ancestors());
assert!(one(".row").subject_pseudo().is_empty());
}
#[test]
fn a_selector_is_bucketed_by_its_subjects_strongest_part() {
assert_eq!(one("block .a #x.b").bucket(), Bucket::Id("x"));
assert_eq!(one("#x .b.c").bucket(), Bucket::Class("b"));
assert_eq!(one(".a SPAN").bucket(), Bucket::Type("span"));
assert_eq!(one(".a :hover").bucket(), Bucket::Universal);
}
#[test]
fn a_node_selector_splits_into_its_parts() {
let parsed = NodeSelector::parse("button#ok.primary.wide").expect("valid");
assert_eq!(parsed.type_name, Some("button"));
assert_eq!(parsed.id, Some("ok"));
assert_eq!(parsed.classes().collect::<Vec<_>>(), ["primary", "wide"]);
let parsed = NodeSelector::parse(".a#id.b").expect("valid");
assert_eq!((parsed.type_name, parsed.id), (None, Some("id")));
assert_eq!(parsed.classes().collect::<Vec<_>>(), ["a", "b"]);
let empty = NodeSelector::parse("").expect("valid");
assert_eq!(
(empty.type_name, empty.id, empty.classes().count()),
(None, None, 0)
);
for bad in [
"#", ".", "a b", "#a#b", ".a#b#c", "a:hover", "..a", "#a.", "a>b",
] {
assert!(NodeSelector::parse(bad).is_err(), "`{bad}` was accepted");
}
}
}