Skip to main content

css_parse/syntax/
simple_block.rs

1use super::prelude::*;
2use crate::{Result, syntax::ComponentValues};
3
4#[node]
5#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
7pub struct SimpleBlock<'a> {
8	pub open: T![PairWiseStart],
9	pub values: ComponentValues<'a>,
10	pub close: Option<T![PairWiseEnd]>,
11}
12
13impl<'a> Peek<'a> for SimpleBlock<'a> {
14	const PEEK_KINDSET: KindSet = KindSet::PAIRWISE_START;
15}
16
17impl<'a, M: NodeMetadata> NodeWithMetadata<M> for SimpleBlock<'a> {
18	fn metadata(&self) -> M {
19		M::default()
20	}
21}
22
23// https://drafts.csswg.org/css-syntax-3/#consume-a-simple-block
24impl<'a> Parse<'a> for SimpleBlock<'a> {
25	fn parse<Iter>(p: &mut Parser<'a, Iter>) -> Result<Self>
26	where
27		Iter: Iterator<Item = crate::Cursor> + Clone,
28	{
29		let open = p.parse::<T![PairWiseStart]>()?;
30		let stop = p.set_stop(KindSet::new(&[open.end()]));
31		let values = p.parse::<ComponentValues>();
32		p.set_stop(stop);
33		let values = values?;
34		if <T![PairWiseEnd]>::peek(p, p.peek_n(1)) {
35			return Ok(Self { open, values, close: p.parse::<T![PairWiseEnd]>().ok() });
36		}
37		Ok(Self { open, values, close: None })
38	}
39}
40
41impl<'a> ToCursors for SimpleBlock<'a> {
42	fn to_cursors(&self, s: &mut impl CursorSink) {
43		ToCursors::to_cursors(&self.open, s);
44		ToCursors::to_cursors(&self.values, s);
45		ToCursors::to_cursors(&self.close, s);
46	}
47}
48
49impl<'a> ToSpan for SimpleBlock<'a> {
50	fn to_span(&self) -> Span {
51		self.open.to_span() + if let Some(close) = self.close { close.to_span() } else { self.values.to_span() }
52	}
53}
54
55impl<'a> SemanticEq for SimpleBlock<'a> {
56	fn semantic_eq(&self, other: &Self, source_text: &str) -> bool {
57		self.open.semantic_eq(&other.open, source_text)
58			&& self.values.semantic_eq(&other.values, source_text)
59			&& self.close.semantic_eq(&other.close, source_text)
60	}
61}
62
63#[cfg(test)]
64mod tests {
65	use super::*;
66	use crate::EmptyAtomSet;
67	use crate::test_helpers::*;
68
69	#[test]
70	fn test_writes() {
71		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "[foo]");
72		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "(one two three)");
73		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "{}");
74		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "{foo}");
75		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "{foo:bar}");
76		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "{one(two)}");
77		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "(one(two))");
78		// Incomplete but recoverable
79		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "[foo");
80		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "{foo:bar");
81		assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "(one(two)");
82		// assert_parse!(EmptyAtomSet::ATOMS, SimpleBlock, "(one(two");
83	}
84
85	#[test]
86	fn test_peek() {
87		assert_peek_false!(EmptyAtomSet::ATOMS, SimpleBlock, "foo");
88		assert_peek_false!(EmptyAtomSet::ATOMS, SimpleBlock, "");
89	}
90}