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