Skip to main content

css_ast/types/
fill_layer.rs

1use super::prelude::*;
2use crate::{Color, FillOrigin, Paint, PositionAndSize, RepeatStyle};
3use css_parse::Box;
4
5/// Represents a single layer of the `fill` shorthand.
6///
7/// The `fill` property is a shorthand for `fill-image`, `fill-position`,
8/// `fill-size`, `fill-repeat`, `fill-origin`, and `fill-color`, modelled on
9/// `background`'s `<bg-layer>#` grammar (fill has no `<bg-clip>`/`<attachment>`
10/// equivalent). The `color` field is only meaningful on the final layer, but
11/// we parse it permissively on every layer, matching `BgLayer`'s approach.
12///
13/// <https://drafts.csswg.org/fill-stroke-3/#fill>
14#[node]
15#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
16#[parse(all_must_occur)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
18#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
19#[derive(csskit_derives::NodeWithMetadata)]
20pub struct FillLayer<'a> {
21	pub image: Option<Paint<'a>>,
22	pub position: Option<Box<'a, PositionAndSize<'a>>>,
23	pub repeat: Option<RepeatStyle>,
24	pub origin: Option<FillOrigin>,
25	pub color: Option<Color<'a>>,
26}
27
28#[cfg(test)]
29mod tests {
30	use super::*;
31	use crate::CssAtomSet;
32	use css_parse::{assert_parse, assert_peek_false};
33
34	#[test]
35	fn test_writes() {
36		assert_parse!(CssAtomSet::ATOMS, FillLayer, "none");
37		assert_parse!(CssAtomSet::ATOMS, FillLayer, "url(foo.svg)");
38		assert_parse!(CssAtomSet::ATOMS, FillLayer, "currentcolor");
39		assert_parse!(CssAtomSet::ATOMS, FillLayer, "center");
40		assert_parse!(CssAtomSet::ATOMS, FillLayer, "center / cover");
41		assert_parse!(CssAtomSet::ATOMS, FillLayer, "repeat-x");
42		assert_parse!(CssAtomSet::ATOMS, FillLayer, "fill-box");
43		assert_parse!(CssAtomSet::ATOMS, FillLayer, "url(foo.svg) center no-repeat fill-box red");
44	}
45
46	#[test]
47	fn test_errors() {
48		assert_peek_false!(CssAtomSet::ATOMS, FillLayer, "");
49	}
50}