Skip to main content

css_ast/functions/
shape_function.rs

1use super::prelude::*;
2use crate::{FillRuleStyleValue, Position, ShapeCommand};
3
4/// <https://drafts.csswg.org/css-shapes/#funcdef-basic-shape-shape>
5///
6/// ```text,ignore
7/// <shape()> = shape(
8///   <'fill-rule'>?
9///   from <position> ,
10///   <shape-command>#
11/// )
12/// ```
13#[node]
14#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
16#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
17#[derive(csskit_derives::NodeWithMetadata)]
18pub struct ShapeFunction<'a> {
19	#[atom(CssAtomSet::Shape)]
20	pub name: T![Function],
21	pub fill_rule: Option<FillRuleStyleValue<'a>>,
22	#[atom(CssAtomSet::From)]
23	pub from: T![Ident],
24	pub position: Position<'a>,
25	#[semantic_eq(skip)]
26	pub comma: T![,],
27	pub commands: CommaSeparated<'a, ShapeCommand<'a>>,
28	#[semantic_eq(skip)]
29	pub close: T![')'],
30}
31
32#[cfg(test)]
33mod tests {
34	use super::*;
35	use crate::CssAtomSet;
36	use css_parse::{assert_parse, assert_parse_error, assert_peek_false};
37
38	#[test]
39	fn test_parses() {
40		assert_parse!(CssAtomSet::ATOMS, ShapeFunction, "shape(from 5% 0%,hline to 95%,close)");
41		assert_parse!(
42			CssAtomSet::ATOMS,
43			ShapeFunction,
44			"shape(nonzero from 5px 0%,curve to 100% 5% with 100% 0%,line by -2px 3px)"
45		);
46	}
47
48	#[test]
49	fn test_errors() {
50		assert_peek_false!(CssAtomSet::ATOMS, ShapeFunction, "circle(10px)");
51		assert_parse_error!(CssAtomSet::ATOMS, ShapeFunction, "shape(from 5px 0px)");
52		assert_parse_error!(CssAtomSet::ATOMS, ShapeFunction, "shape(hline to 5px)");
53	}
54}