forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.rs
More file actions
507 lines (467 loc) · 16.9 KB
/
container.rs
File metadata and controls
507 lines (467 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! The `@container` rule.
use cssparser::*;
use super::Location;
use super::{CssRuleList, MinifyContext};
use crate::error::{MinifyError, ParserError, PrinterError};
use crate::media_query::{
define_query_features, operation_to_css, parse_query_condition, to_css_with_parens_if_needed, FeatureToCss,
MediaFeatureType, Operator, QueryCondition, QueryConditionFlags, QueryFeature, ValueType,
};
use crate::parser::{DefaultAtRule, ParserOptions};
use crate::printer::Printer;
use crate::properties::custom::TokenList;
use crate::properties::{Property, PropertyId};
#[cfg(feature = "serde")]
use crate::serialization::ValueWrapper;
use crate::targets::{Features, Targets};
use crate::traits::{Parse, ParseWithOptions, ToCss};
use crate::values::ident::CustomIdent;
#[cfg(feature = "visitor")]
use crate::visitor::Visit;
/// A [@container](https://drafts.csswg.org/css-contain-3/#container-rule) rule.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub struct ContainerRule<'i, R = DefaultAtRule> {
/// The name of the container.
#[cfg_attr(feature = "serde", serde(borrow))]
pub name: Option<ContainerName<'i>>,
/// The container condition.
pub condition: Option<ContainerCondition<'i>>,
/// The rules within the `@container` rule.
pub rules: CssRuleList<'i, R>,
/// The location of the rule in the source file.
#[cfg_attr(feature = "visitor", skip_visit)]
pub loc: Location,
}
/// Represents a container condition.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub enum ContainerCondition<'i> {
/// A size container feature, implicitly parenthesized.
#[cfg_attr(feature = "serde", serde(borrow, with = "ValueWrapper::<ContainerSizeFeature>"))]
Feature(ContainerSizeFeature<'i>),
/// A negation of a condition.
#[cfg_attr(feature = "visitor", skip_type)]
#[cfg_attr(feature = "serde", serde(with = "ValueWrapper::<Box<ContainerCondition>>"))]
Not(Box<ContainerCondition<'i>>),
/// A set of joint operations.
#[cfg_attr(feature = "visitor", skip_type)]
Operation {
/// The operator for the conditions.
operator: Operator,
/// The conditions for the operator.
conditions: Vec<ContainerCondition<'i>>,
},
/// A style query.
#[cfg_attr(feature = "serde", serde(borrow, with = "ValueWrapper::<StyleQuery>"))]
Style(StyleQuery<'i>),
/// A scroll state query.
#[cfg_attr(feature = "serde", serde(borrow, with = "ValueWrapper::<ScrollStateQuery>"))]
ScrollState(ScrollStateQuery<'i>),
/// Unknown tokens.
#[cfg_attr(feature = "serde", serde(borrow, with = "ValueWrapper::<TokenList>"))]
Unknown(TokenList<'i>),
}
/// A container query size feature.
pub type ContainerSizeFeature<'i> = QueryFeature<'i, ContainerSizeFeatureId>;
define_query_features! {
/// A container query size feature identifier.
pub enum ContainerSizeFeatureId {
/// The [width](https://w3c.github.io/csswg-drafts/css-contain-3/#width) size container feature.
"width": Width = Length,
/// The [height](https://w3c.github.io/csswg-drafts/css-contain-3/#height) size container feature.
"height": Height = Length,
/// The [inline-size](https://w3c.github.io/csswg-drafts/css-contain-3/#inline-size) size container feature.
"inline-size": InlineSize = Length,
/// The [block-size](https://w3c.github.io/csswg-drafts/css-contain-3/#block-size) size container feature.
"block-size": BlockSize = Length,
/// The [aspect-ratio](https://w3c.github.io/csswg-drafts/css-contain-3/#aspect-ratio) size container feature.
"aspect-ratio": AspectRatio = Ratio,
/// The [orientation](https://w3c.github.io/csswg-drafts/css-contain-3/#orientation) size container feature.
"orientation": Orientation = Ident,
}
}
impl FeatureToCss for ContainerSizeFeatureId {
fn to_css_with_prefix<W>(&self, prefix: &str, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
dest.write_str(prefix)?;
self.to_css(dest)
}
}
/// Represents a style query within a container condition.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub enum StyleQuery<'i> {
/// A property declaration.
#[cfg_attr(feature = "serde", serde(borrow, with = "ValueWrapper::<Property>"))]
Declaration(Property<'i>),
/// A property name, without a value.
/// This matches if the property value is different from the initial value.
#[cfg_attr(feature = "serde", serde(with = "ValueWrapper::<PropertyId>"))]
Property(PropertyId<'i>),
/// A negation of a condition.
#[cfg_attr(feature = "visitor", skip_type)]
#[cfg_attr(feature = "serde", serde(with = "ValueWrapper::<Box<StyleQuery>>"))]
Not(Box<StyleQuery<'i>>),
/// A set of joint operations.
#[cfg_attr(feature = "visitor", skip_type)]
Operation {
/// The operator for the conditions.
operator: Operator,
/// The conditions for the operator.
conditions: Vec<StyleQuery<'i>>,
},
}
/// Represents a scroll state query within a container condition.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", rename_all = "kebab-case")
)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub enum ScrollStateQuery<'i> {
/// A size container feature, implicitly parenthesized.
#[cfg_attr(feature = "serde", serde(borrow, with = "ValueWrapper::<ScrollStateFeature>"))]
Feature(ScrollStateFeature<'i>),
/// A negation of a condition.
#[cfg_attr(feature = "visitor", skip_type)]
#[cfg_attr(feature = "serde", serde(with = "ValueWrapper::<Box<ScrollStateQuery>>"))]
Not(Box<ScrollStateQuery<'i>>),
/// A set of joint operations.
#[cfg_attr(feature = "visitor", skip_type)]
Operation {
/// The operator for the conditions.
operator: Operator,
/// The conditions for the operator.
conditions: Vec<ScrollStateQuery<'i>>,
},
}
/// A container query size feature.
pub type ScrollStateFeature<'i> = QueryFeature<'i, ScrollStateFeatureId>;
define_query_features! {
/// A container query scroll state feature identifier.
pub enum ScrollStateFeatureId {
/// The [stuck](https://drafts.csswg.org/css-conditional-5/#stuck) scroll state feature.
"stuck": Stuck = Ident,
/// The [snapped](https://drafts.csswg.org/css-conditional-5/#snapped) scroll state feature.
"snapped": Snapped = Ident,
/// The [scrollable](https://drafts.csswg.org/css-conditional-5/#scrollable) scroll state feature.
"scrollable": Scrollable = Ident,
/// The [scrolled](https://drafts.csswg.org/css-conditional-5/#scrolled) scroll state feature.
"scrolled": Scrolled = Ident,
}
}
impl FeatureToCss for ScrollStateFeatureId {
fn to_css_with_prefix<W>(&self, prefix: &str, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
dest.write_str(prefix)?;
self.to_css(dest)
}
}
impl<'i> QueryCondition<'i> for ContainerCondition<'i> {
#[inline]
fn parse_feature<'t>(
input: &mut Parser<'i, 't>,
options: &ParserOptions<'_, 'i>,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let feature = QueryFeature::parse_with_options(input, options)?;
Ok(Self::Feature(feature))
}
#[inline]
fn create_negation(condition: Box<ContainerCondition<'i>>) -> Self {
Self::Not(condition)
}
#[inline]
fn create_operation(operator: Operator, conditions: Vec<Self>) -> Self {
Self::Operation { operator, conditions }
}
fn parse_style_query<'t>(
input: &mut Parser<'i, 't>,
options: &ParserOptions<'_, 'i>,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
input.parse_nested_block(|input| {
if let Ok(res) =
input.try_parse(|input| parse_query_condition(input, QueryConditionFlags::ALLOW_OR, options))
{
return Ok(Self::Style(res));
}
Ok(Self::Style(StyleQuery::parse_feature(input, options)?))
})
}
fn parse_scroll_state_query<'t>(
input: &mut Parser<'i, 't>,
options: &ParserOptions<'_, 'i>,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
input.parse_nested_block(|input| {
if let Ok(res) =
input.try_parse(|input| parse_query_condition(input, QueryConditionFlags::ALLOW_OR, options))
{
return Ok(Self::ScrollState(res));
}
Ok(Self::ScrollState(ScrollStateQuery::parse_feature(input, options)?))
})
}
fn needs_parens(&self, parent_operator: Option<Operator>, targets: &Targets) -> bool {
match self {
ContainerCondition::Not(_) => true,
ContainerCondition::Operation { operator, .. } => Some(*operator) != parent_operator,
ContainerCondition::Feature(f) => f.needs_parens(parent_operator, targets),
ContainerCondition::Style(_) => false,
ContainerCondition::ScrollState(_) => false,
ContainerCondition::Unknown(_) => false,
}
}
}
impl<'i> QueryCondition<'i> for ScrollStateQuery<'i> {
#[inline]
fn parse_feature<'t>(
input: &mut Parser<'i, 't>,
options: &ParserOptions<'_, 'i>,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let feature = QueryFeature::parse_with_options(input, options)?;
Ok(Self::Feature(feature))
}
#[inline]
fn create_negation(condition: Box<Self>) -> Self {
Self::Not(condition)
}
#[inline]
fn create_operation(operator: Operator, conditions: Vec<Self>) -> Self {
Self::Operation { operator, conditions }
}
fn needs_parens(&self, parent_operator: Option<Operator>, targets: &Targets) -> bool {
match self {
ScrollStateQuery::Not(_) => true,
ScrollStateQuery::Operation { operator, .. } => Some(*operator) != parent_operator,
ScrollStateQuery::Feature(f) => f.needs_parens(parent_operator, targets),
}
}
}
impl<'i> QueryCondition<'i> for StyleQuery<'i> {
#[inline]
fn parse_feature<'t>(
input: &mut Parser<'i, 't>,
options: &ParserOptions<'_, 'i>,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let property_id = PropertyId::parse(input)?;
if input.try_parse(|input| input.expect_colon()).is_ok() {
input.skip_whitespace();
let feature = Self::Declaration(Property::parse(property_id, input, options)?);
let _ = input.try_parse(|input| parse_important(input));
Ok(feature)
} else {
Ok(Self::Property(property_id))
}
}
#[inline]
fn create_negation(condition: Box<Self>) -> Self {
Self::Not(condition)
}
#[inline]
fn create_operation(operator: Operator, conditions: Vec<Self>) -> Self {
Self::Operation { operator, conditions }
}
fn needs_parens(&self, parent_operator: Option<Operator>, _targets: &Targets) -> bool {
match self {
StyleQuery::Not(_) => true,
StyleQuery::Operation { operator, .. } => Some(*operator) != parent_operator,
StyleQuery::Declaration(_) | StyleQuery::Property(_) => true,
}
}
}
impl<'i> ParseWithOptions<'i> for ContainerCondition<'i> {
fn parse_with_options<'t>(
input: &mut Parser<'i, 't>,
options: &ParserOptions<'_, 'i>,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
input
.try_parse(|input| {
parse_query_condition(
input,
QueryConditionFlags::ALLOW_OR
| QueryConditionFlags::ALLOW_STYLE
| QueryConditionFlags::ALLOW_SCROLL_STATE,
options,
)
})
.or_else(|e| {
if options.error_recovery {
options.warn(e);
Ok(ContainerCondition::Unknown(TokenList::parse(input, options, 0)?))
} else {
Err(e)
}
})
}
}
impl<'i> ToCss for ContainerCondition<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
match *self {
ContainerCondition::Feature(ref f) => f.to_css(dest),
ContainerCondition::Not(ref c) => {
dest.write_str("not ")?;
to_css_with_parens_if_needed(&**c, dest, c.needs_parens(None, &dest.targets.current))
}
ContainerCondition::Operation {
ref conditions,
operator,
} => operation_to_css(operator, conditions, dest),
ContainerCondition::Style(ref query) => {
dest.write_str("style(")?;
query.to_css(dest)?;
dest.write_char(')')
}
ContainerCondition::ScrollState(ref query) => {
let needs_parens = !matches!(query, ScrollStateQuery::Feature(_));
dest.write_str("scroll-state")?;
if needs_parens {
dest.write_char('(')?;
}
query.to_css(dest)?;
if needs_parens {
dest.write_char(')')?;
}
Ok(())
}
ContainerCondition::Unknown(ref tokens) => tokens.to_css(dest, false),
}
}
}
impl<'i> ToCss for StyleQuery<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
match *self {
StyleQuery::Declaration(ref f) => f.to_css(dest, false),
StyleQuery::Property(ref f) => f.to_css(dest),
StyleQuery::Not(ref c) => {
dest.write_str("not ")?;
to_css_with_parens_if_needed(&**c, dest, c.needs_parens(None, &dest.targets.current))
}
StyleQuery::Operation {
ref conditions,
operator,
} => operation_to_css(operator, conditions, dest),
}
}
}
impl<'i> ToCss for ScrollStateQuery<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
match *self {
ScrollStateQuery::Feature(ref f) => f.to_css(dest),
ScrollStateQuery::Not(ref c) => {
dest.write_str("not ")?;
to_css_with_parens_if_needed(&**c, dest, c.needs_parens(None, &dest.targets.current))
}
ScrollStateQuery::Operation {
ref conditions,
operator,
} => operation_to_css(operator, conditions, dest),
}
}
}
/// A [`<container-name>`](https://drafts.csswg.org/css-contain-3/#typedef-container-name) in a `@container` rule.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct ContainerName<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CustomIdent<'i>);
impl<'i> Parse<'i> for ContainerName<'i> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let ident = CustomIdent::parse(input)?;
match_ignore_ascii_case! { &*ident.0,
"none" | "and" | "not" | "or" => Err(input.new_unexpected_token_error(Token::Ident(ident.0.as_ref().to_owned().into()))),
_ => Ok(ContainerName(ident))
}
}
}
impl<'i> ToCss for ContainerName<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
// Container name should not be hashed
// https://github.com/vercel/next.js/issues/71233
self.0.to_css_with_options(
dest,
match &dest.css_module {
Some(css_module) => css_module.config.container,
None => false,
},
)
}
}
impl<'i, T: Clone> ContainerRule<'i, T> {
pub(crate) fn minify(
&mut self,
context: &mut MinifyContext<'_, 'i>,
parent_is_unused: bool,
) -> Result<bool, MinifyError> {
self.rules.minify(context, parent_is_unused)?;
Ok(self.rules.0.is_empty())
}
}
impl<'a, 'i, T: ToCss> ToCss for ContainerRule<'i, T> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
#[cfg(feature = "sourcemap")]
dest.add_mapping(self.loc);
dest.write_str("@container ")?;
let has_condition = self.condition.is_some();
if let Some(name) = &self.name {
name.to_css(dest)?;
if has_condition {
dest.write_char(' ')?;
}
}
if let Some(condition) = &self.condition {
// Don't downlevel range syntax in container queries.
let exclude = dest.targets.current.exclude;
dest.targets.current.exclude.insert(Features::MediaQueries);
condition.to_css(dest)?;
dest.targets.current.exclude = exclude;
}
dest.whitespace()?;
dest.write_char('{')?;
dest.indent();
dest.newline()?;
self.rules.to_css(dest)?;
dest.dedent();
dest.newline()?;
dest.write_char('}')
}
}