use std::collections::HashMap;
use std::fmt;
use parsec::NodeId;
use super::{Computed, Declared, Key, add, compute};
use crate::media::Environment;
use crate::parser::ComponentValue;
use crate::rules::{PropertyRule, Syntax};
use crate::tokenizer::{Token, Tokenizer};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Pseudo {
Before,
After,
Marker,
Placeholder,
FirstLine,
FirstLetter,
Selection,
Backdrop,
FileSelectorButton,
Highlight(String),
}
impl Pseudo {
pub fn parse(text: &str) -> Option<Pseudo> {
let text = text.trim();
let (name, legacy) = match text.strip_prefix("::") {
Some(n) => (n, false),
None => (text.strip_prefix(':')?, true),
};
let p = Pseudo::named(name)?;
let legacy_ok = matches!(
p,
Pseudo::Before | Pseudo::After | Pseudo::FirstLine | Pseudo::FirstLetter
);
(!legacy || legacy_ok).then_some(p)
}
fn named(name: &str) -> Option<Pseudo> {
let lower = name.to_ascii_lowercase();
if let Some(arg) = lower
.strip_prefix("highlight(")
.and_then(|a| a.strip_suffix(')'))
{
let start = name.len() - arg.len() - 1;
let arg = name[start..name.len() - 1].trim();
return (!arg.is_empty()).then(|| Pseudo::Highlight(arg.to_string()));
}
Some(match lower.as_str() {
"before" => Pseudo::Before,
"after" => Pseudo::After,
"marker" => Pseudo::Marker,
"placeholder" => Pseudo::Placeholder,
"first-line" => Pseudo::FirstLine,
"first-letter" => Pseudo::FirstLetter,
"selection" => Pseudo::Selection,
"backdrop" => Pseudo::Backdrop,
"file-selector-button" => Pseudo::FileSelectorButton,
_ => return None,
})
}
fn is_highlight(&self) -> bool {
matches!(self, Pseudo::Selection | Pseudo::Highlight(_))
}
}
impl fmt::Display for Pseudo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
Pseudo::Before => "before",
Pseudo::After => "after",
Pseudo::Marker => "marker",
Pseudo::Placeholder => "placeholder",
Pseudo::FirstLine => "first-line",
Pseudo::FirstLetter => "first-letter",
Pseudo::Selection => "selection",
Pseudo::Backdrop => "backdrop",
Pseudo::FileSelectorButton => "file-selector-button",
Pseudo::Highlight(n) => return write!(f, "::highlight({n})"),
};
write!(f, "::{name}")
}
}
#[derive(Debug, PartialEq)]
pub(super) enum Split {
No,
Pseudo(String, Pseudo),
Unstyled,
}
pub(super) fn split_pseudo(selector: &str) -> Split {
if !selector.contains(':') {
return Split::No;
}
let tokens: Vec<(Token<'_>, crate::Span)> = Tokenizer::new(selector).collect();
let mut depth = 0usize;
let mut i = 0;
while i < tokens.len() {
match &tokens[i].0 {
Token::Function(_) | Token::LeftParen | Token::LeftBracket => depth += 1,
Token::RightParen | Token::RightBracket => depth = depth.saturating_sub(1),
Token::Colon if depth == 0 => {
let double = matches!(tokens.get(i + 1), Some((Token::Colon, _)));
let at = if double { i + 2 } else { i + 1 };
let (name, end) = match tokens.get(at) {
Some((Token::Ident(n), s)) => (n.to_string(), s.end),
Some((Token::Function(n), _)) if double => {
let close = tokens[at..]
.iter()
.position(|(t, _)| *t == Token::RightParen)
.map(|k| at + k);
let Some(close) = close else {
return Split::Unstyled;
};
let inner = &selector[tokens[at].1.end..tokens[close].1.start];
(format!("{n}({inner})"), tokens[close].1.end)
}
_ => {
i += 1;
continue;
}
};
let legacy = ["before", "after", "first-line", "first-letter"]
.iter()
.any(|l| name.eq_ignore_ascii_case(l));
if !double && !legacy {
i = at + 1;
continue;
}
if !selector[end..].trim().is_empty() {
return Split::Unstyled;
}
let Some(p) = Pseudo::named(&name) else {
return Split::Unstyled;
};
let before = &selector[..tokens[i].1.start];
let origin = before.trim_end();
let bare = origin.is_empty()
|| before.ends_with(char::is_whitespace)
|| origin.ends_with(['>', '+', '~']);
return match bare {
true => Split::Pseudo(format!("{before}*").trim_start().to_string(), p),
false => Split::Pseudo(origin.to_string(), p),
};
}
_ => {}
}
i += 1;
}
Split::No
}
pub(super) struct PseudoRule {
pub pseudo: Pseudo,
pub declarations: Vec<(String, String, bool)>,
pub needs_attrs: bool,
}
impl PseudoRule {
pub fn of(e: &super::Entry<'_>) -> PseudoRule {
PseudoRule {
pseudo: e.pseudo.clone().unwrap_or(Pseudo::Before),
declarations: e
.declarations
.iter()
.map(|(n, v, imp)| (n.to_string(), crate::to_css(v), *imp))
.collect(),
needs_attrs: e
.declarations
.iter()
.any(|(n, v, _)| n.eq_ignore_ascii_case("content") && has_attr(v)),
}
}
}
#[derive(Default)]
pub(super) struct PseudoStyles {
pub rules: Vec<PseudoRule>,
pub always: Vec<(usize, Key)>,
pub matched: HashMap<NodeId, Vec<(usize, Key)>>,
pub attrs: HashMap<NodeId, Vec<(String, String)>>,
pub env: Environment,
pub registered: HashMap<String, (PropertyRule, Syntax)>,
pub root: Option<NodeId>,
}
pub(super) fn has_attr(values: &[ComponentValue<'_>]) -> bool {
values.iter().any(|v| match v {
ComponentValue::Function(f) => {
f.name.eq_ignore_ascii_case("attr") || has_attr(&f.arguments)
}
ComponentValue::Block(b) => has_attr(&b.contents),
_ => false,
})
}
fn substitute_attrs(value: &str, attrs: &[(String, String)]) -> String {
let values = crate::parse_component_values(value);
let mut out = String::new();
for v in &values {
if let ComponentValue::Function(f) = v
&& f.name.eq_ignore_ascii_case("attr")
{
let args = crate::to_css(&f.arguments);
let (name, fallback) = match args.split_once(',') {
Some((n, fb)) => (n.trim(), Some(fb.trim())),
None => (args.trim(), None),
};
let name = name.strip_suffix("raw-string").map_or(name, str::trim_end);
if !name.contains(|c: char| c.is_whitespace() || c == '|') && !name.is_empty() {
match attrs.iter().find(|(n, _)| n.eq_ignore_ascii_case(name)) {
Some((_, v)) => crate::write_string(&mut out, v).unwrap(),
None => match fallback {
Some(fb) if !fb.is_empty() => out.push_str(fb),
_ => out.push_str("\"\""),
},
}
continue;
}
}
out.push_str(&crate::to_css(std::slice::from_ref(v)));
}
out
}
fn applies(p: &Pseudo, name: &str) -> bool {
let font = name.starts_with("font");
let background = name.starts_with("background");
let decoration = name.starts_with("text-decoration")
|| name.starts_with("text-underline")
|| name.starts_with("text-emphasis")
|| name == "text-shadow";
let typesetting = matches!(
name,
"letter-spacing" | "word-spacing" | "text-transform" | "text-justify"
);
let inline_layout = matches!(
name,
"line-height"
| "vertical-align"
| "baseline-shift"
| "baseline-source"
| "alignment-baseline"
| "dominant-baseline"
);
let animation = name.starts_with("animation");
let first_line = font
|| background
|| decoration
|| typesetting
|| animation
|| matches!(name, "color" | "opacity" | "ruby-position");
match p {
Pseudo::Before | Pseudo::After | Pseudo::Backdrop | Pseudo::FileSelectorButton => true,
Pseudo::FirstLine => first_line || inline_layout,
Pseudo::Placeholder => first_line,
Pseudo::FirstLetter => {
first_line
|| inline_layout
|| name.starts_with("margin")
|| name.starts_with("padding")
|| (name.starts_with("border") && !name.starts_with("border-collapse"))
|| matches!(name, "box-shadow" | "float")
}
Pseudo::Marker => {
font || animation
|| name.starts_with("transition")
|| name.starts_with("white-space")
|| name.starts_with("text-emphasis")
|| matches!(
name,
"color"
| "content"
| "quotes"
| "direction"
| "unicode-bidi"
| "text-combine-upright"
| "line-height"
| "letter-spacing"
| "word-spacing"
| "tab-size"
| "text-transform"
| "text-indent"
| "hyphens"
| "line-break"
| "overflow-wrap"
| "word-break"
| "text-shadow"
| "text-decoration-skip-ink"
| "cursor"
)
}
Pseudo::Selection | Pseudo::Highlight(_) => highlight_applies(name),
}
}
pub(super) fn highlight_applies(name: &str) -> bool {
name.starts_with("text-decoration")
|| name.starts_with("text-underline")
|| matches!(
name,
"color"
| "background-color"
| "text-shadow"
| "stroke-color"
| "fill-color"
| "stroke-width"
)
}
impl PseudoStyles {
fn style(
&self,
el: NodeId,
p: &Pseudo,
parent: &Computed,
originating: &Computed,
root: Option<&Computed>,
parent_highlight: Option<&Computed>,
) -> Computed {
let none = Vec::new();
let matched = self.matched.get(&el).unwrap_or(&none);
let attrs = self.attrs.get(&el).map_or(&[][..], |a| &a[..]);
let mut texts: Vec<(Key, &str, std::borrow::Cow<'_, str>, bool)> = Vec::new();
for &(r, key) in self.always.iter().chain(matched) {
let rule = &self.rules[r];
if rule.pseudo != *p {
continue;
}
for (name, value, important) in &rule.declarations {
let value = if rule.needs_attrs && name.eq_ignore_ascii_case("content") {
std::borrow::Cow::Owned(substitute_attrs(value, attrs))
} else {
std::borrow::Cow::Borrowed(value.as_str())
};
texts.push((key, name, value, *important));
}
}
let parsed: Vec<Vec<ComponentValue<'_>>> = texts
.iter()
.map(|(_, _, v, _)| crate::parse_component_values(v))
.collect();
let mut declared: HashMap<&'static str, Vec<Declared<'_>>> = HashMap::new();
let mut custom: HashMap<String, Vec<(Key, Vec<ComponentValue<'_>>)>> = HashMap::new();
for ((key, name, _, important), values) in texts.iter().zip(&parsed) {
add(&mut declared, &mut custom, name, values, *important, *key);
}
declared.retain(|name, _| applies(p, name));
for list in declared.values_mut() {
list.sort_by_key(|d| d.key);
}
for list in custom.values_mut() {
list.sort_by_key(|d| d.0);
}
let mut out = compute::compute(compute::Input {
declared: &declared,
custom: &custom,
parent: Some(parent),
root,
is_root: false,
env: &self.env,
cq: parent.cq_children,
registered: &self.registered,
});
compute::pseudo_fix_up(&mut out, p, originating, parent_highlight);
out
}
}
impl super::Styled {
pub fn get_pseudo(&self, el: NodeId, p: &Pseudo, property: &str) -> Option<String> {
self.computed_pseudo(el, p)?.get(property)
}
pub fn computed_pseudo(&self, el: NodeId, p: &Pseudo) -> Option<Computed> {
let own = self.styles.get(&el)?;
let ps = &self.pseudo;
let root = ps.root.and_then(|r| self.styles.get(&r));
if p.is_highlight() {
let mut chain = vec![el];
let mut at = own.parent;
while let Some(a) = at {
let Some(c) = self.styles.get(&a) else { break };
chain.push(a);
at = c.parent;
}
let mut highlight: Option<Computed> = None;
for &n in chain.iter().rev() {
let originating = &self.styles[&n];
let h = ps.style(n, p, originating, originating, root, highlight.as_ref());
highlight = Some(h);
}
return highlight;
}
let first_line;
let parent = match p {
Pseudo::FirstLetter => {
first_line = self.computed_pseudo(el, &Pseudo::FirstLine)?;
&first_line
}
_ => own,
};
Some(ps.style(el, p, parent, own, root, None))
}
pub fn pseudo_exists(&self, el: NodeId, p: &Pseudo) -> Option<bool> {
let own = self.styles.get(&el)?;
if !matches!(p, Pseudo::Before | Pseudo::After | Pseudo::Marker) {
return None;
}
if own.get("display").as_deref() == Some("none") {
return Some(false);
}
let c = self.computed_pseudo(el, p)?;
let content = c.get("content").unwrap_or_default();
Some(match p {
Pseudo::Marker => {
let list_item = own
.get("display")
.is_some_and(|d| d.split_ascii_whitespace().any(|w| w == "list-item"));
let shown = content != "normal"
|| own.get("list-style-type").as_deref() != Some("none")
|| own.get("list-style-image").as_deref() != Some("none");
list_item && content != "none" && shown
}
_ => {
!matches!(content.as_str(), "none" | "normal")
&& c.get("display").as_deref() != Some("none")
}
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cascade::Styler;
use parsec::css::SelectorList;
fn styled(html: &str) -> (parsec::Document, super::super::Styled) {
let doc = parsec::parse(html, false);
let styled = Styler::new(Environment::default())
.with_document_styles(&doc)
.style(&doc);
(doc, styled)
}
fn at(doc: &parsec::Document, sel: &str) -> NodeId {
SelectorList::parse(sel)
.unwrap()
.select_first(doc, doc.root())
.unwrap()
}
fn get(html: &str, sel: &str, p: &str, property: &str) -> String {
let (doc, s) = styled(html);
let p = Pseudo::parse(p).unwrap();
s.get_pseudo(at(&doc, sel), &p, property)
.unwrap_or_default()
}
#[test]
fn before_and_after() {
let html = "<style>p { color: blue; margin-top: 3px } #a::before { content: 'x' counter(c) open-quote attr(title); margin-left: 2em } \
p:before { color: green } p::after { content: normal }</style><p id=a title='hi' style='font-size: 10px'>";
assert_eq!(get(html, "p", "::before", "color"), "rgb(0, 128, 0)");
assert_eq!(get(html, "p", "::before", "margin-left"), "20px");
assert_eq!(get(html, "p", "::before", "margin-top"), "0px");
assert_eq!(get(html, "p", "::after", "color"), "rgb(0, 0, 255)");
assert_eq!(
get(html, "p", "::before", "content"),
"\"x\" counter(c) open-quote \"hi\""
);
assert_eq!(get(html, "p", "::after", "content"), "none");
let (doc, s) = styled(html);
let p = at(&doc, "p");
assert_eq!(s.pseudo_exists(p, &Pseudo::Before), Some(true));
assert_eq!(s.pseudo_exists(p, &Pseudo::After), Some(false));
assert_eq!(s.pseudo_exists(p, &Pseudo::FirstLine), None);
assert_eq!(get("<q>", "q", "::before", "content"), "open-quote");
let (doc, s) = styled("<style>q { display: none }</style><q>");
assert_eq!(s.pseudo_exists(at(&doc, "q"), &Pseudo::Before), Some(false));
}
#[test]
fn cascade_order() {
let html = "<style>p::before { color: green } ::before { color: red } *::before { color: red }</style><p>";
assert_eq!(get(html, "p", "::before", "color"), "rgb(0, 128, 0)");
let html = "<style>@layer a { #x::before { color: green !important } } #x::before { color: red !important }</style><p id=x>";
assert_eq!(get(html, "p", "::before", "color"), "rgb(0, 128, 0)");
let html = "<style>*::before { content: attr(data-n) }</style><p data-n=3><i>";
assert_eq!(get(html, "p", "::before", "content"), "\"3\"");
assert_eq!(get(html, "i", "::before", "content"), "\"\"");
let html = "<style>@scope (.s) { p::before { color: green } } .c { container-type: inline-size } \
@container (width > 100px) { .c::after { color: green } }</style><div class=s><p></p></div><p class=c>";
assert_eq!(get(html, "div p", "::before", "color"), "rgb(0, 128, 0)");
assert_eq!(get(html, "body > p", "::before", "color"), "rgb(0, 0, 0)");
let doc = parsec::parse(html, false);
let s = Styler::new(Environment::default())
.with_document_styles(&doc)
.container_sizes(|_, _| Some((200.0, 10.0)))
.style(&doc);
let c = at(&doc, ".c");
assert_eq!(
s.get_pseudo(c, &Pseudo::After, "color").as_deref(),
Some("rgb(0, 128, 0)")
);
let html = "<style>p::before:hover { color: red }</style><p>";
assert_eq!(get(html, "p", "::before", "color"), "rgb(0, 0, 0)");
}
#[test]
fn the_wiki_example() {
let html = "<style>li { color: blue } li::marker { color: green; position: absolute } \
.n::before { content: '#' attr(data-n) ' ' }</style><ol><li class=n data-n=1>One</ol>";
assert_eq!(get(html, "li", "::marker", "color"), "rgb(0, 128, 0)");
assert_eq!(get(html, "li", "::marker", "position"), "static");
assert_eq!(get(html, "li", "::before", "content"), "\"#\" \"1\" \" \"");
}
#[test]
fn markers() {
let html = "<style>li { text-indent: 4px; text-transform: uppercase } .in { list-style-position: inside } \
li::marker { color: green; position: absolute; list-style-type: square; display: none } .none { list-style-type: none }</style>\
<ul><li>a<li class=in>b<li class=none>c</ul><p>";
assert_eq!(get(html, "li", "::marker", "color"), "rgb(0, 128, 0)");
assert_eq!(get(html, "li", "::marker", "unicode-bidi"), "isolate");
assert_eq!(
get(html, "li", "::marker", "font-variant-numeric"),
"tabular-nums"
);
assert_eq!(get(html, "li", "::marker", "text-transform"), "none");
assert_eq!(get(html, "li", "::marker", "text-indent"), "0px");
assert_eq!(get(html, "li", "::marker", "position"), "static");
assert_eq!(get(html, "li", "::marker", "list-style-type"), "disc");
assert_eq!(get(html, "li", "::marker", "display"), "block");
assert_eq!(get(html, ".in", "::marker", "display"), "inline");
assert_eq!(get(html, "li", "::marker", "content"), "normal");
let (doc, s) = styled(html);
assert_eq!(s.pseudo_exists(at(&doc, "li"), &Pseudo::Marker), Some(true));
assert_eq!(
s.pseudo_exists(at(&doc, ".none"), &Pseudo::Marker),
Some(false)
);
assert_eq!(s.pseudo_exists(at(&doc, "p"), &Pseudo::Marker), Some(false));
}
#[test]
fn first_line_and_letter() {
let html = "<style>p { color: blue } p::first-line { color: green; margin-left: 9px; font-size: 20px } \
p::first-letter { margin-left: 3px; float: left; position: absolute }</style><p>";
assert_eq!(get(html, "p", "::first-line", "color"), "rgb(0, 128, 0)");
assert_eq!(get(html, "p", "::first-line", "margin-left"), "0px");
assert_eq!(get(html, "p", "::first-letter", "color"), "rgb(0, 128, 0)");
assert_eq!(get(html, "p", "::first-letter", "font-size"), "20px");
assert_eq!(get(html, "p", "::first-letter", "margin-left"), "3px");
assert_eq!(get(html, "p", "::first-letter", "float"), "left");
assert_eq!(get(html, "p", "::first-letter", "position"), "static");
let html = "<style>input::placeholder { color: green; line-height: 9px; background-color: inherit } input { background-color: red }</style><input>";
assert_eq!(
get(html, "input", "::placeholder", "color"),
"rgb(0, 128, 0)"
);
assert_eq!(
get(html, "input", "::placeholder", "background-color"),
"rgb(255, 0, 0)"
);
assert_eq!(get(html, "input", "::placeholder", "line-height"), "normal");
}
#[test]
fn highlights() {
let html = "<style>body { color: blue; margin-top: 2px } div::selection { background-color: green; color: red; margin-top: 7px } \
p::highlight(x) { color: green }</style><div><p></p></div>";
assert_eq!(
get(html, "p", "::selection", "background-color"),
"rgb(0, 128, 0)"
);
assert_eq!(get(html, "p", "::selection", "color"), "rgb(255, 0, 0)");
assert_eq!(get(html, "div", "::selection", "margin-top"), "0px");
assert_eq!(get(html, "body", "::selection", "margin-top"), "2px");
assert_eq!(get(html, "body", "::selection", "color"), "rgb(0, 0, 0)");
assert_eq!(get(html, "p", "::highlight(x)", "color"), "rgb(0, 128, 0)");
assert_eq!(get(html, "p", "::highlight(y)", "color"), "rgb(0, 0, 0)");
let html = "<style>:root { color: blue }</style><p>";
assert_eq!(get(html, "p", "::selection", "color"), "rgb(0, 0, 255)");
}
#[test]
fn splits() {
use Split::*;
assert_eq!(split_pseudo("p.x"), No);
assert_eq!(split_pseudo("p:hover"), No);
assert_eq!(
split_pseudo("#t::before"),
Pseudo("#t".into(), super::Pseudo::Before)
);
assert_eq!(
split_pseudo("li:BEFORE"),
Pseudo("li".into(), super::Pseudo::Before)
);
assert_eq!(
split_pseudo("::marker"),
Pseudo("*".into(), super::Pseudo::Marker)
);
assert_eq!(
split_pseudo("a > b:not(.c)::first-line"),
Pseudo("a > b:not(.c)".into(), super::Pseudo::FirstLine)
);
assert_eq!(
split_pseudo("#t::highlight(Foo)"),
Pseudo("#t".into(), super::Pseudo::Highlight("Foo".into()))
);
assert_eq!(split_pseudo(":is(a::before)"), No);
assert_eq!(split_pseudo("[title='::before']"), No);
assert_eq!(split_pseudo("a::before:hover"), Unstyled);
assert_eq!(split_pseudo("a::before::marker"), Unstyled);
assert_eq!(split_pseudo("a::part(x)"), Unstyled);
assert_eq!(
split_pseudo("a ::before"),
Pseudo("a *".into(), super::Pseudo::Before)
);
assert_eq!(
split_pseudo("a>::after"),
Pseudo("a>*".into(), super::Pseudo::After)
);
assert_eq!(split_pseudo("a:marker"), No);
}
#[test]
fn parses_names() {
assert_eq!(Pseudo::parse("::BEFORE"), Some(Pseudo::Before));
assert_eq!(Pseudo::parse(":first-line"), Some(Pseudo::FirstLine));
assert_eq!(Pseudo::parse(":marker"), None);
assert_eq!(
Pseudo::parse("::highlight(foo)"),
Some(Pseudo::Highlight("foo".into()))
);
assert_eq!(Pseudo::parse("::nope"), None);
assert_eq!(
Pseudo::FileSelectorButton.to_string(),
"::file-selector-button"
);
}
#[test]
fn attr_in_content() {
let attrs = vec![("title".to_string(), "a\"b".to_string())];
assert_eq!(substitute_attrs("attr(title)", &attrs), "\"a\\\"b\"");
assert_eq!(
substitute_attrs("\"[\" attr(nope) \"]\"", &attrs),
"\"[\" \"\" \"]\""
);
assert_eq!(substitute_attrs("attr(nope, 'x')", &attrs), "\"x\"");
}
}