Skip to main content

css_ast/functions/
attr_function.rs

1use super::prelude::*;
2use crate::Syntax;
3use css_parse::ComponentValues;
4
5/// <https://drafts.csswg.org/css-values-5/#attr-notation>
6///
7/// ```text,ignore
8/// attr() = attr( <attr-name> <attr-type>? , <declaration-value>?)
9/// <attr-type> = type( <syntax> ) | raw-string | <attr-unit>
10/// ```
11#[node]
12#[derive(Parse, Peek, ToSpan, ToCursors, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
14#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
15#[derive(csskit_derives::NodeWithMetadata)]
16pub struct AttrFunction<'a> {
17	#[atom(CssAtomSet::Attr)]
18	pub name: T![Function],
19	pub params: AttrFunctionParams<'a>,
20	#[semantic_eq(skip)]
21	pub close: T![')'],
22}
23
24#[node]
25#[derive(Parse, Peek, ToSpan, ToCursors, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
27#[derive(csskit_derives::NodeWithMetadata)]
28pub struct AttrFunctionParams<'a>(
29	AttrName,
30	Option<AttrType<'a>>,
31	#[semantic_eq(skip)] Option<T![,]>,
32	Option<ComponentValues<'a>>,
33);
34
35/// ```text,ignore
36/// <attr-name> = [ <ident-token>? '|' ]? <ident-token>
37/// ```
38#[node]
39#[derive(ToSpan, ToCursors, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
40#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
41#[derive(csskit_derives::NodeWithMetadata)]
42pub struct AttrName(pub Option<T![Ident]>, #[semantic_eq(skip)] pub Option<T![|]>, pub Option<T![Ident]>);
43
44impl<'a> Peek<'a> for AttrName {
45	const PEEK_KINDSET: KindSet = KindSet::new(&[Kind::Ident]);
46}
47
48impl<'a> Parse<'a> for AttrName {
49	fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
50	where
51		I: Iterator<Item = Cursor> + Clone,
52	{
53		let a = p.parse_if_peek::<T![Ident]>()?;
54		let b = p.parse_if_peek::<T![|]>()?;
55
56		if a.is_some() && b.is_none() {
57			return Ok(Self(None, None, a));
58		}
59
60		if a.is_none() && b.is_some() {
61			return Ok(Self(None, b, Some(p.parse::<T![Ident]>()?)));
62		}
63
64		if a.is_none() && b.is_none() {
65			Err(Diagnostic::new(p.next(), Diagnostic::expected_ident))?
66		}
67
68		debug_assert!(a.is_some() && b.is_some());
69
70		Ok(Self(a, b, Some(p.parse::<T![Ident]>()?)))
71	}
72}
73
74/// ```text,ignore
75/// <attr-type> = type( <syntax> ) | raw-string | <attr-unit>
76/// ```
77#[node]
78#[derive(Parse, Peek, ToSpan, ToCursors, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
79#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
80#[derive(csskit_derives::NodeWithMetadata)]
81pub enum AttrType<'a> {
82	#[atom(CssAtomSet::Type)]
83	Type(T![Function], Syntax<'a>, #[semantic_eq(skip)] T![')']),
84	#[atom(CssAtomSet::RawString)]
85	RawString(T![Ident]),
86	Unit(T![Ident]),
87}
88
89#[cfg(test)]
90mod tests {
91	use super::*;
92	use crate::CssAtomSet;
93	use css_parse::{assert_parse, assert_parse_error, assert_peek_false};
94
95	#[test]
96	fn test_writes() {
97		assert_parse!(CssAtomSet::ATOMS, AttrFunction, "attr(foo)");
98		assert_parse!(CssAtomSet::ATOMS, AttrFunction, "attr(foo)");
99		assert_parse!(CssAtomSet::ATOMS, AttrFunction, "attr(bar px)");
100		assert_parse!(CssAtomSet::ATOMS, AttrFunction, "attr(foo|bar px)");
101		assert_parse!(CssAtomSet::ATOMS, AttrFunction, "attr(foo|bar)");
102		assert_parse!(CssAtomSet::ATOMS, AttrFunction, "attr(|bar)");
103		assert_parse!(CssAtomSet::ATOMS, AttrFunction, "attr(|bar px)");
104	}
105
106	#[test]
107	fn test_errors() {
108		assert_parse_error!(CssAtomSet::ATOMS, AttrName, "a|b|c");
109		assert_peek_false!(CssAtomSet::ATOMS, AttrFunction, "attrr(foo)");
110		assert_parse_error!(CssAtomSet::ATOMS, AttrFunction, "attr()");
111		assert_parse_error!(CssAtomSet::ATOMS, AttrFunction, "attr(|)");
112	}
113}