Skip to main content

css_ast/functions/
polygon_function.rs

1use super::prelude::*;
2use crate::{CalcableValue, FillRuleStyleValue, Length, LengthPercentage};
3use css_parse::Box;
4
5/// <https://drafts.csswg.org/css-shapes/#funcdef-basic-shape-polygon>
6///
7/// ```text,ignore
8/// <polygon()> = polygon(
9///   <'fill-rule'>?
10///   [ round <length> ]? ,
11///   [<length-percentage> <length-percentage>]#
12/// )
13/// ```
14#[node]
15#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
17#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
18#[derive(csskit_derives::NodeWithMetadata)]
19pub struct PolygonFunction<'a> {
20	#[atom(CssAtomSet::Polygon)]
21	pub name: T![Function],
22	pub fill_rule: Option<FillRuleStyleValue<'a>>,
23	pub round: Option<Box<'a, PolygonRound<'a>>>,
24	#[semantic_eq(skip)]
25	pub comma: Option<T![,]>,
26	pub points: CommaSeparated<'a, (CalcableValue<'a, LengthPercentage>, CalcableValue<'a, LengthPercentage>)>,
27	#[semantic_eq(skip)]
28	pub close: T![')'],
29}
30
31#[node]
32#[derive(Parse, Peek, ToSpan, ToCursors, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
33#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
34#[derive(csskit_derives::NodeWithMetadata)]
35pub struct PolygonRound<'a> {
36	#[atom(CssAtomSet::Round)]
37	pub keyword: T![Ident],
38	pub radius: CalcableValue<'a, Length>,
39}
40
41#[cfg(test)]
42mod tests {
43	use super::*;
44	use crate::CssAtomSet;
45	use css_parse::{assert_parse, assert_parse_error, assert_peek_false};
46
47	#[test]
48	fn test_parses() {
49		assert_parse!(CssAtomSet::ATOMS, PolygonFunction, "polygon(0px 0px,100% 0px,100% 100%)");
50		assert_parse!(CssAtomSet::ATOMS, PolygonFunction, "polygon(nonzero,0 0,50% 0,100% 100%)");
51		assert_parse!(CssAtomSet::ATOMS, PolygonFunction, "polygon(evenodd round 5px,0 0,50% 0,100% 100%)");
52		assert_parse!(CssAtomSet::ATOMS, PolygonFunction, "polygon(round calc(5px),calc(0% + 1px) var(--y),100% 100%)");
53	}
54
55	#[test]
56	fn test_errors() {
57		assert_peek_false!(CssAtomSet::ATOMS, PolygonFunction, "circle(10px)");
58		assert_parse_error!(CssAtomSet::ATOMS, PolygonFunction, "polygon(0px)");
59	}
60}