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