1#[cfg(feature = "visitable")]
2use crate::visit::{NodeId, QueryableNode};
3
4use super::prelude::*;
5
6mod features;
7pub use features::*;
8
9#[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, queryable(skip))]
13#[cfg_attr(feature = "css_feature_data", derive(::csskit_derives::ToCSSFeature), css_feature("css.at-rules.container"))]
14#[derive(csskit_derives::NodeWithMetadata)]
15#[metadata(node_kinds = AtRule, used_at_rules = Container)]
16pub struct ContainerRule<'a> {
17 #[cfg_attr(feature = "visitable", visit(skip))]
18 #[atom(CssAtomSet::Container)]
19 pub name: T![AtKeyword],
20 pub prelude: ContainerConditionList<'a>,
21 #[metadata(delegate)]
22 pub block: ContainerRulesBlock<'a>,
23}
24
25#[cfg(feature = "visitable")]
26impl<'a> QueryableNode for ContainerRule<'a> {
27 const NODE_ID: NodeId = NodeId::ContainerRule;
28}
29
30#[derive(Parse, ToSpan, ToCursors, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
31#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
32#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable))]
33#[derive(csskit_derives::NodeWithMetadata)]
34pub struct ContainerRulesBlock<'a>(#[metadata(delegate)] pub RuleList<'a, Rule<'a>, CssMetadata>);
35
36#[derive(Peek, Parse, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
37#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
38#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable))]
39pub struct ContainerConditionList<'a>(pub CommaSeparated<'a, ContainerCondition<'a>, 1>);
40
41#[derive(ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
42#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
43#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable))]
44pub struct ContainerCondition<'a> {
45 #[cfg_attr(feature = "visitable", visit(skip))]
46 pub name: Option<T![Ident]>,
47 pub condition: Option<ContainerQuery<'a>>,
48}
49
50impl<'a> Peek<'a> for ContainerCondition<'a> {
51 const PEEK_KINDSET: KindSet = KindSet::new(&[Kind::Ident, Kind::LeftParen]);
52}
53
54impl<'a> Parse<'a> for ContainerCondition<'a> {
55 fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
56 where
57 I: Iterator<Item = Cursor> + Clone,
58 {
59 let mut name = None;
60 let c = p.peek_n(1);
61 if c == Kind::Ident {
62 match p.to_atom::<CssAtomSet>(c) {
63 CssAtomSet::None | CssAtomSet::And | CssAtomSet::Not | CssAtomSet::Or => {}
64 _ => {
65 name = Some(p.parse::<T![Ident]>()?);
66 }
67 }
68 }
69 dbg!(p.peek_n(1));
70 let condition =
71 if name.is_none() { Some(p.parse::<ContainerQuery>()?) } else { p.parse_if_peek::<ContainerQuery>()? };
72 dbg!(&name, &condition);
73 Ok(Self { name, condition })
74 }
75}
76
77#[derive(ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
78#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
79#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
80#[derive(csskit_derives::NodeWithMetadata)]
81pub enum ContainerQuery<'a> {
82 Is(ContainerFeature<'a>),
83 Not(T![Ident], ContainerFeature<'a>),
84 And(Vec<'a, (ContainerFeature<'a>, Option<T![Ident]>)>),
85 Or(Vec<'a, (ContainerFeature<'a>, Option<T![Ident]>)>),
86}
87
88impl<'a> Peek<'a> for ContainerQuery<'a> {
89 fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
90 where
91 I: Iterator<Item = Cursor> + Clone,
92 {
93 <ContainerFeature>::peek(p, c) || (<T![Ident]>::peek(p, c) && p.to_atom::<CssAtomSet>(c) == CssAtomSet::Not)
94 }
95}
96
97impl<'a> Parse<'a> for ContainerQuery<'a> {
98 fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
99 where
100 I: Iterator<Item = Cursor> + Clone,
101 {
102 Self::parse_condition(p)
103 }
104}
105
106impl<'a> FeatureConditionList<'a> for ContainerQuery<'a> {
107 type FeatureCondition = ContainerFeature<'a>;
108 fn keyword_is_not<I>(p: &Parser<'a, I>, c: Cursor) -> bool
109 where
110 I: Iterator<Item = Cursor> + Clone,
111 {
112 p.equals_atom(c, &CssAtomSet::Not)
113 }
114 fn keyword_is_and<I>(p: &Parser<'a, I>, c: Cursor) -> bool
115 where
116 I: Iterator<Item = Cursor> + Clone,
117 {
118 p.equals_atom(c, &CssAtomSet::And)
119 }
120 fn keyword_is_or<I>(p: &Parser<'a, I>, c: Cursor) -> bool
121 where
122 I: Iterator<Item = Cursor> + Clone,
123 {
124 p.equals_atom(c, &CssAtomSet::Or)
125 }
126 fn build_is(feature: ContainerFeature<'a>) -> Self {
127 Self::Is(feature)
128 }
129 fn build_not(keyword: T![Ident], feature: ContainerFeature<'a>) -> Self {
130 Self::Not(keyword, feature)
131 }
132 fn build_and(feature: Vec<'a, (ContainerFeature<'a>, Option<T![Ident]>)>) -> Self {
133 Self::And(feature)
134 }
135 fn build_or(feature: Vec<'a, (ContainerFeature<'a>, Option<T![Ident]>)>) -> Self {
136 Self::Or(feature)
137 }
138}
139
140macro_rules! container_feature {
141 ( $($name: ident($typ: ident))+ ) => {
142 #[allow(clippy::large_enum_variant)] #[derive(ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
144 #[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
145 #[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit)]
146#[derive(csskit_derives::NodeWithMetadata)]
147 pub enum ContainerFeature<'a> {
148 $($name($typ),)+
149 Style(StyleQuery<'a>),
150 ScrollState(ScrollStateQuery<'a>),
151 }
152 }
153}
154
155apply_container_features!(container_feature);
156
157impl<'a> Peek<'a> for ContainerFeature<'a> {
158 fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
159 where
160 I: Iterator<Item = Cursor> + Clone,
161 {
162 let c2 = p.peek_n(2);
163 c == Kind::LeftParen && c2 == KindSet::new(&[Kind::Ident, Kind::Dimension])
164 }
165}
166
167impl<'a> Parse<'a> for ContainerFeature<'a> {
168 fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
169 where
170 I: Iterator<Item = Cursor> + Clone,
171 {
172 if p.peek::<T![Function]>() {
173 todo!();
174 }
175 let mut c = p.peek_n(2);
176 macro_rules! match_feature {
177 ( $($name: ident($typ: ident))+ ) => {
178 {
180 match p.to_atom::<CssAtomSet>(c) {
181 $(CssAtomSet::$name => {
182 let value = $typ::parse(p)?;
183 Self::$name(value)
184 },)+
185 _ => Err(Diagnostic::new(c, Diagnostic::unexpected))?
186 }
187 }
188 }
189 }
190 if c == Kind::Ident {
191 Ok(apply_container_features!(match_feature))
192 } else {
193 c = p.peek_n(3);
195 if c != Kind::Ident {
196 c = p.peek_n(4)
197 }
198 Ok(apply_container_features!(match_feature))
199 }
200 }
201}
202
203macro_rules! apply_container_features {
204 ($macro: ident) => {
205 $macro! {
206 Width(WidthContainerFeature)
208 Height(HeightContainerFeature)
209 InlineSize(InlineSizeContainerFeature)
210 BlockSize(BlockSizeContainerFeature)
211 AspectRatio(AspectRatioContainerFeature)
212 Orientation(OrientationContainerFeature)
213 }
214 };
215}
216use apply_container_features;
217
218#[cfg(test)]
219mod tests {
220 use super::*;
221 use crate::CssAtomSet;
222 use css_parse::assert_parse;
223
224 #[test]
225 fn size_test() {
226 assert_eq!(std::mem::size_of::<ContainerRule>(), 144);
227 assert_eq!(std::mem::size_of::<ContainerConditionList>(), 32);
228 assert_eq!(std::mem::size_of::<ContainerCondition>(), 536);
229 assert_eq!(std::mem::size_of::<ContainerQuery>(), 520);
230 }
231
232 #[test]
233 fn test_writes() {
234 assert_parse!(CssAtomSet::ATOMS, ContainerQuery, "(width:2px)");
235 assert_parse!(CssAtomSet::ATOMS, ContainerCondition, "(width:2px)");
236 assert_parse!(CssAtomSet::ATOMS, ContainerCondition, "(inline-size>30em)");
237 assert_parse!(CssAtomSet::ATOMS, ContainerCondition, "(1em<width<1em)");
238 assert_parse!(CssAtomSet::ATOMS, ContainerCondition, "(width > 400px)");
239 assert_parse!(CssAtomSet::ATOMS, ContainerCondition, "--container");
240 assert_parse!(CssAtomSet::ATOMS, ContainerCondition, "--container (width > 400px)");
241 assert_parse!(CssAtomSet::ATOMS, ContainerConditionList, "(width > 400px), --container (width > 400px)");
242 assert_parse!(CssAtomSet::ATOMS, ContainerRule, "@container foo{}");
243 assert_parse!(CssAtomSet::ATOMS, ContainerRule, "@container foo (width:2px){}");
244 assert_parse!(CssAtomSet::ATOMS, ContainerRule, "@container foo (10em<width<10em){}");
245 assert_parse!(CssAtomSet::ATOMS, ContainerRule, "@container foo (width:2px){body{color:black}}");
246 }
247}