Skip to main content

css_ast/types/
ratio.rs

1use super::prelude::*;
2use crate::{NumericValue, units::CSSInt};
3
4/// <https://drafts.csswg.org/css-values-4/#ratios>
5///
6/// ```text,ignore
7/// <ratio> = <number [0,∞]> [ / <number [0,∞]> ]?
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 struct Ratio<'a> {
15	pub numerator: NumericValue<'a, CSSInt>,
16	#[semantic_eq(skip)]
17	pub slash: Option<T![/]>,
18	pub denominator: Option<NumericValue<'a, CSSInt>>,
19}
20
21#[cfg(test)]
22mod tests {
23	use super::*;
24	use crate::CssAtomSet;
25	use css_parse::{assert_parse, assert_parse_error};
26
27	#[test]
28	fn test_writes() {
29		assert_parse!(CssAtomSet::ATOMS, Ratio, "1/1");
30		assert_parse!(CssAtomSet::ATOMS, Ratio, "5/3");
31		assert_parse!(CssAtomSet::ATOMS, Ratio, "5");
32	}
33
34	#[test]
35	fn test_substitution() {
36		assert_parse!(CssAtomSet::ATOMS, Ratio, "var(--n)");
37		assert_parse!(CssAtomSet::ATOMS, Ratio, "calc(1 + 1)/3");
38		assert_parse!(CssAtomSet::ATOMS, Ratio, "5/var(--d)");
39	}
40
41	#[test]
42	fn test_errors() {
43		assert_parse_error!(CssAtomSet::ATOMS, Ratio, "5 : 3");
44		assert_parse_error!(CssAtomSet::ATOMS, Ratio, "5 / 1 / 1");
45	}
46
47	// #[cfg(feature = "serde")]
48	// #[test]
49	// fn test_serializes() {
50	// 	assert_json!(Ratio, "5/3", {
51	// 		"node": [5, 3],
52	// 		"start": 0,
53	// 		"end": 5
54	// 	});
55	// }
56}