Skip to main content

css_parse/syntax/
function_block.rs

1use super::prelude::*;
2use crate::{ComponentValues, Result};
3
4#[node]
5#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
7pub struct FunctionBlock<'a> {
8	pub name: T![Function],
9	pub params: ComponentValues<'a>,
10	pub close: T![')'],
11}
12
13impl<'a> Peek<'a> for FunctionBlock<'a> {
14	const PEEK_KINDSET: KindSet = KindSet::new(&[Kind::Function]);
15}
16
17// https://drafts.csswg.org/css-syntax-3/#consume-function
18impl<'a> Parse<'a> for FunctionBlock<'a> {
19	fn parse<Iter>(p: &mut Parser<'a, Iter>) -> Result<Self>
20	where
21		Iter: Iterator<Item = crate::Cursor> + Clone,
22	{
23		let name = p.parse::<T![Function]>()?;
24		let params = p.parse::<ComponentValues>()?;
25		let close = p.parse::<T![')']>()?;
26		Ok(Self { name, params, close })
27	}
28}
29
30impl<'a> ToCursors for FunctionBlock<'a> {
31	fn to_cursors(&self, s: &mut impl CursorSink) {
32		ToCursors::to_cursors(&self.name, s);
33		ToCursors::to_cursors(&self.params, s);
34		ToCursors::to_cursors(&self.close, s);
35	}
36}
37
38impl<'a> ToSpan for FunctionBlock<'a> {
39	fn to_span(&self) -> Span {
40		self.name.to_span() + self.close.to_span()
41	}
42}
43
44impl<'a> SemanticEq for FunctionBlock<'a> {
45	fn semantic_eq(&self, other: &Self) -> bool {
46		self.name.semantic_eq(&other.name)
47			&& self.params.semantic_eq(&other.params)
48			&& self.close.semantic_eq(&other.close)
49	}
50}
51
52#[cfg(test)]
53mod tests {
54	use super::*;
55	use crate::EmptyAtomSet;
56	use crate::test_helpers::*;
57
58	#[test]
59	fn test_writes() {
60		assert_parse!(EmptyAtomSet::ATOMS, FunctionBlock, "foo(bar)");
61		assert_parse!(EmptyAtomSet::ATOMS, FunctionBlock, "foo(bar{})");
62	}
63}