Skip to main content

css_ast/rules/media/features/
width.rs

1use super::prelude::*;
2use crate::units::Length;
3
4ranged_feature!(
5	#[node]
6	#[derive(ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7	#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
8	#[derive(csskit_derives::FeatureMetadata)]
9	#[feature_metadata(CssAtomSet::Width)]
10	#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
11	#[derive(csskit_derives::NodeWithMetadata)]
12	pub enum WidthMediaFeature<'a>{CssAtomSet::Width | CssAtomSet::MinWidth | CssAtomSet::MaxWidth, QueryValue<'a, Length>}
13);
14
15#[cfg(test)]
16mod tests {
17	use super::*;
18	use crate::{CssAtomSet, FeatureMetadata, RangedForm};
19	use css_parse::{assert_parse, assert_parse_error};
20
21	#[test]
22	fn test_math_function_values() {
23		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(max-width:calc(48rem - 0.02px))", |node| {
24			assert!(matches!(node, WidthMediaFeature::Max(_, _, _, QueryValue::Substituted(_), _)));
25		});
26		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width<=min(50vw,800px))");
27		assert_parse!(
28			CssAtomSet::ATOMS,
29			WidthMediaFeature,
30			"(calc(1px + 2px)<width<=env(viewport-segment-width 0 0,800px))"
31		);
32		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(width:var(--w))");
33	}
34
35	#[test]
36	fn test_feature_metadata_exact() {
37		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width:360px)", |node| {
38			assert!(matches!(
39				node.feature_metadata(),
40				crate::ConditionalFeature::Ranged { name: CssAtomSet::Width, form: RangedForm::Exact { .. } }
41			));
42		});
43	}
44
45	#[test]
46	fn test_feature_metadata_legacy_min() {
47		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(min-width:35rem)", |node| {
48			assert!(matches!(
49				node.feature_metadata(),
50				crate::ConditionalFeature::Ranged { name: CssAtomSet::Width, form: RangedForm::LegacyMin { .. } }
51			));
52		});
53	}
54
55	#[test]
56	fn test_feature_metadata_legacy_max() {
57		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(max-width:35rem)", |node| {
58			assert!(matches!(
59				node.feature_metadata(),
60				crate::ConditionalFeature::Ranged { name: CssAtomSet::Width, form: RangedForm::LegacyMax { .. } }
61			));
62		});
63	}
64
65	#[test]
66	fn test_feature_metadata_left_comparison() {
67		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width<=800px)", |node| {
68			let crate::ConditionalFeature::Ranged { name, form: RangedForm::Left { comparison, .. } } =
69				node.feature_metadata()
70			else {
71				panic!("expected Ranged::Left");
72			};
73			assert_eq!(name, CssAtomSet::Width);
74			assert_eq!(comparison.to_string(), "<=");
75		});
76	}
77
78	#[test]
79	fn test_feature_metadata_right_comparison() {
80		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px<=width)", |node| {
81			let crate::ConditionalFeature::Ranged { name, form: RangedForm::Right { comparison, .. } } =
82				node.feature_metadata()
83			else {
84				panic!("expected Ranged::Right");
85			};
86			assert_eq!(name, CssAtomSet::Width);
87			assert_eq!(comparison.to_string(), "<=");
88		});
89	}
90
91	#[test]
92	fn test_feature_metadata_range() {
93		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px<width<1400px)", |node| {
94			let crate::ConditionalFeature::Ranged { name, form: RangedForm::Range { left_cmp, right_cmp, .. } } =
95				node.feature_metadata()
96			else {
97				panic!("expected Ranged::Range");
98			};
99			assert_eq!(name, CssAtomSet::Width);
100			assert_eq!(left_cmp.to_string(), "<");
101			assert_eq!(right_cmp.to_string(), "<");
102		});
103	}
104
105	#[test]
106	fn test_writes() {
107		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width:360px)");
108		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width:35rem)");
109		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(min-width:35rem)");
110		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(max-width:35rem)");
111		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width<=800px)");
112		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width>=1400px)");
113		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width>=1400px)");
114		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width=1400px)");
115		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(1400px=width)");
116		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px<=width)");
117		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px<width<1400px)");
118		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px>width<1400px)");
119		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px>=width<=1400px)");
120		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px<=width>1400px)");
121	}
122
123	#[test]
124	fn test_errors() {
125		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(width:)");
126		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(width: > 10px)");
127		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(max-width > 10px)");
128		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(min-width > 10px)");
129		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(width: 1%)");
130		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(width: 1%)");
131		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(pointer: 1px)");
132	}
133}