Skip to main content

css_parse/syntax/
declaration_group.rs

1use crate::{
2	CursorSink, DeclarationOrBad, DeclarationValue, NodeMetadata, NodeWithMetadata, SemanticEq, Span, ToCursors,
3	ToSpan, Vec,
4};
5use csskit_proc_macro::node;
6
7/// A group of declarations that can be interleaved with rules.
8///
9/// Per [CSS Syntax ยง 5.4.4](https://drafts.csswg.org/css-syntax-3/#consume-block-contents),
10/// blocks return a list containing either rules or lists of declarations. This allows
11/// declarations to be properly interleaved with nested rules while maintaining their order.
12///
13/// For example, in `a { color: red; b { } color: blue; }`, the declarations need to be
14/// grouped separately before and after the nested `b` rule.
15#[node]
16#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent))]
18pub struct DeclarationGroup<'a, D, M>
19where
20	D: DeclarationValue<'a, M>,
21	M: NodeMetadata,
22{
23	pub declarations: Vec<'a, DeclarationOrBad<'a, D, M>>,
24}
25
26impl<'a, D, M> ToCursors for DeclarationGroup<'a, D, M>
27where
28	D: DeclarationValue<'a, M> + ToCursors,
29	M: NodeMetadata,
30{
31	fn to_cursors(&self, s: &mut impl CursorSink) {
32		for decl in &self.declarations {
33			decl.to_cursors(s);
34		}
35	}
36}
37
38impl<'a, D, M> ToSpan for DeclarationGroup<'a, D, M>
39where
40	D: DeclarationValue<'a, M> + ToSpan,
41	M: NodeMetadata,
42{
43	fn to_span(&self) -> Span {
44		self.declarations.to_span()
45	}
46}
47
48impl<'a, D, M> SemanticEq for DeclarationGroup<'a, D, M>
49where
50	D: DeclarationValue<'a, M>,
51	M: NodeMetadata,
52{
53	fn semantic_eq(&self, other: &Self) -> bool {
54		self.declarations.semantic_eq(&other.declarations)
55	}
56}
57
58impl<'a, D, M> NodeWithMetadata<M> for DeclarationGroup<'a, D, M>
59where
60	D: DeclarationValue<'a, M>,
61	M: NodeMetadata,
62{
63	fn metadata(&self) -> M {
64		let mut meta = M::default();
65		for decl in &self.declarations {
66			meta = meta.merge(decl.metadata());
67		}
68		meta
69	}
70}