Skip to main content

css_ast/functions/
rect_function.rs

1use super::prelude::*;
2use crate::{AutoOr, CalcableValue, Length};
3
4/// <https://drafts.csswg.org/css-masking-1/#funcdef-rect>
5///
6/// ```text
7/// rect() = rect( <top>, <right>, <bottom>, <left> )
8/// <top>, <right>, <bottom>, <left> = <length> | auto
9/// ```
10#[node]
11#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
13#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(all))]
14#[derive(csskit_derives::NodeWithMetadata)]
15pub struct RectFunction<'a> {
16	#[atom(CssAtomSet::Rect)]
17	#[cfg_attr(feature = "visitable", visit(skip))]
18	pub name: T![Function],
19	pub top: AutoOr<CalcableValue<'a, Length>>,
20	#[cfg_attr(feature = "visitable", visit(skip))]
21	#[semantic_eq(skip)]
22	pub comma1: Option<T![,]>,
23	pub right: AutoOr<CalcableValue<'a, Length>>,
24	#[cfg_attr(feature = "visitable", visit(skip))]
25	#[semantic_eq(skip)]
26	pub comma2: Option<T![,]>,
27	pub bottom: AutoOr<CalcableValue<'a, Length>>,
28	#[cfg_attr(feature = "visitable", visit(skip))]
29	#[semantic_eq(skip)]
30	pub comma3: Option<T![,]>,
31	pub left: AutoOr<CalcableValue<'a, Length>>,
32	#[semantic_eq(skip)]
33	pub close: T![')'],
34}
35
36#[cfg(test)]
37mod tests {
38	use super::*;
39	use crate::CssAtomSet;
40	use css_parse::{assert_parse, assert_parse_error};
41
42	#[test]
43	fn test_writes() {
44		assert_parse!(CssAtomSet::ATOMS, RectFunction, "rect(0 0 0 0)");
45		assert_parse!(CssAtomSet::ATOMS, RectFunction, "rect(10px,20px,30px,40px)");
46		assert_parse!(CssAtomSet::ATOMS, RectFunction, "rect(auto,auto,auto,auto)");
47		assert_parse!(CssAtomSet::ATOMS, RectFunction, "rect(10px,auto,30px,auto)");
48		assert_parse!(CssAtomSet::ATOMS, RectFunction, "rect(-10px,20px,-5px,0px)");
49	}
50
51	#[test]
52	fn test_substitution() {
53		assert_parse!(CssAtomSet::ATOMS, RectFunction, "rect(var(--t),20px,30px,40px)");
54		assert_parse!(CssAtomSet::ATOMS, RectFunction, "rect(calc(1px + 2px),auto,30px,auto)");
55	}
56
57	#[test]
58	fn test_errors() {
59		assert_parse_error!(CssAtomSet::ATOMS, RectFunction, "rect(10px)");
60		assert_parse_error!(CssAtomSet::ATOMS, RectFunction, "rect(10px,20px)");
61		assert_parse_error!(CssAtomSet::ATOMS, RectFunction, "rect(10%,20%,30%,40%)");
62		assert_parse_error!(CssAtomSet::ATOMS, RectFunction, "rect()");
63	}
64}