Skip to main content

css_ast/types/
svg_paint.rs

1use super::prelude::*;
2use crate::{Integer, NumericValue, Positive};
3
4/// <https://drafts.csswg.org/fill-stroke-3/#typedef-svg-paint>
5///
6/// ```text,ignore
7/// <svg-paint> = child | child( <integer> )
8/// ```
9#[node]
10#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
12#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
13#[derive(csskit_derives::NodeWithMetadata)]
14pub enum SvgPaint<'a> {
15	#[atom(CssAtomSet::Child)]
16	Child(T![Ident]),
17	ChildFunction(SvgPaintChildFunction<'a>),
18}
19
20/// `child( <integer> )` — refers to the nth child paint server element
21/// (1-indexed). Arguments less than 1 are invalid.
22///
23/// <https://drafts.csswg.org/fill-stroke-3/#typedef-svg-paint>
24#[node]
25#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
27#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(children))]
28#[derive(csskit_derives::NodeWithMetadata)]
29pub struct SvgPaintChildFunction<'a> {
30	#[atom(CssAtomSet::Child)]
31	pub function: T![Function],
32	pub index: NumericValue<'a, Positive<Integer>>,
33	#[semantic_eq(skip)]
34	pub close: T![')'],
35}
36
37#[cfg(test)]
38mod tests {
39	use super::*;
40	use crate::CssAtomSet;
41	use css_parse::{assert_parse, assert_parse_error, assert_peek_false};
42
43	#[test]
44	fn test_writes() {
45		assert_parse!(CssAtomSet::ATOMS, SvgPaint, "child");
46		assert_parse!(CssAtomSet::ATOMS, SvgPaint, "child(1)");
47		assert_parse!(CssAtomSet::ATOMS, SvgPaint, "child(3)");
48	}
49
50	#[test]
51	fn test_substitution() {
52		assert_parse!(CssAtomSet::ATOMS, SvgPaint, "child(var(--n))");
53		assert_parse!(CssAtomSet::ATOMS, SvgPaint, "child(calc(1 + 1))");
54	}
55
56	#[test]
57	fn test_errors() {
58		assert_peek_false!(CssAtomSet::ATOMS, SvgPaint, "");
59		assert_peek_false!(CssAtomSet::ATOMS, SvgPaint, "parent");
60		assert_parse_error!(CssAtomSet::ATOMS, SvgPaint, "child(0)");
61		assert_parse_error!(CssAtomSet::ATOMS, SvgPaint, "child(-1)");
62	}
63}