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, Copy, 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{CssAtomSet::Width | CssAtomSet::MinWidth | CssAtomSet::MaxWidth, 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_feature_metadata_exact() {
23		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width:360px)", |node| {
24			assert!(matches!(
25				node.feature_metadata(),
26				crate::ConditionalFeature::Ranged { name: CssAtomSet::Width, form: RangedForm::Exact { .. } }
27			));
28		});
29	}
30
31	#[test]
32	fn test_feature_metadata_legacy_min() {
33		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(min-width:35rem)", |node| {
34			assert!(matches!(
35				node.feature_metadata(),
36				crate::ConditionalFeature::Ranged { name: CssAtomSet::Width, form: RangedForm::LegacyMin { .. } }
37			));
38		});
39	}
40
41	#[test]
42	fn test_feature_metadata_legacy_max() {
43		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(max-width:35rem)", |node| {
44			assert!(matches!(
45				node.feature_metadata(),
46				crate::ConditionalFeature::Ranged { name: CssAtomSet::Width, form: RangedForm::LegacyMax { .. } }
47			));
48		});
49	}
50
51	#[test]
52	fn test_feature_metadata_left_comparison() {
53		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width<=800px)", |node| {
54			let crate::ConditionalFeature::Ranged { name, form: RangedForm::Left { comparison, .. } } =
55				node.feature_metadata()
56			else {
57				panic!("expected Ranged::Left");
58			};
59			assert_eq!(name, CssAtomSet::Width);
60			assert_eq!(comparison.to_string(), "<=");
61		});
62	}
63
64	#[test]
65	fn test_feature_metadata_right_comparison() {
66		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px<=width)", |node| {
67			let crate::ConditionalFeature::Ranged { name, form: RangedForm::Right { comparison, .. } } =
68				node.feature_metadata()
69			else {
70				panic!("expected Ranged::Right");
71			};
72			assert_eq!(name, CssAtomSet::Width);
73			assert_eq!(comparison.to_string(), "<=");
74		});
75	}
76
77	#[test]
78	fn test_feature_metadata_range() {
79		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px<width<1400px)", |node| {
80			let crate::ConditionalFeature::Ranged { name, form: RangedForm::Range { left_cmp, right_cmp, .. } } =
81				node.feature_metadata()
82			else {
83				panic!("expected Ranged::Range");
84			};
85			assert_eq!(name, CssAtomSet::Width);
86			assert_eq!(left_cmp.to_string(), "<");
87			assert_eq!(right_cmp.to_string(), "<");
88		});
89	}
90
91	#[test]
92	fn test_writes() {
93		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width:360px)");
94		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width:35rem)");
95		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(min-width:35rem)");
96		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(max-width:35rem)");
97		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width<=800px)");
98		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width>=1400px)");
99		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width>=1400px)");
100		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(width=1400px)");
101		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(1400px=width)");
102		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px<=width)");
103		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px<width<1400px)");
104		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px>width<1400px)");
105		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px>=width<=1400px)");
106		assert_parse!(CssAtomSet::ATOMS, WidthMediaFeature, "(100px<=width>1400px)");
107	}
108
109	#[test]
110	fn test_errors() {
111		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(width:)");
112		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(width: > 10px)");
113		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(max-width > 10px)");
114		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(min-width > 10px)");
115		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(width: 1%)");
116		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(width: 1%)");
117		assert_parse_error!(CssAtomSet::ATOMS, WidthMediaFeature, "(pointer: 1px)");
118	}
119}