Skip to content

Commit eff8ce1

Browse files
committed
Parse the @starting-style rule
1 parent d44a73c commit eff8ce1

File tree

6 files changed

+220
-3
lines changed

6 files changed

+220
-3
lines changed

node/ast.d.ts

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* and run json-schema-to-typescript to regenerate this file.
66
*/
77

8+
export type String = string;
89
/**
910
* A CSS rule.
1011
*/
@@ -80,6 +81,10 @@ export type Rule<D = Declaration, M = MediaQuery> = | {
8081
type: "container";
8182
value: ContainerRule<D, M>;
8283
}
84+
| {
85+
type: "starting-style";
86+
value: StartingStyleRule<D, M>;
87+
}
8388
| {
8489
type: "ignored";
8590
}
@@ -221,7 +226,6 @@ export type MediaFeatureId =
221226
| "device-aspect-ratio"
222227
| "-webkit-device-pixel-ratio"
223228
| "-moz-device-pixel-ratio";
224-
export type String = string;
225229
/**
226230
* [media feature value](https://drafts.csswg.org/mediaqueries/#typedef-mf-value) within a media query.
227231
*
@@ -1220,6 +1224,9 @@ export type PropertyId =
12201224
property: "box-sizing";
12211225
vendorPrefix: VendorPrefix;
12221226
}
1227+
| {
1228+
property: "aspect-ratio";
1229+
}
12231230
| {
12241231
property: "overflow";
12251232
}
@@ -1989,6 +1996,10 @@ export type PropertyId =
19891996
| {
19901997
property: "text-shadow";
19911998
}
1999+
| {
2000+
property: "text-size-adjust";
2001+
vendorPrefix: VendorPrefix;
2002+
}
19922003
| {
19932004
property: "box-decoration-break";
19942005
vendorPrefix: VendorPrefix;
@@ -2353,6 +2364,10 @@ export type Declaration =
23532364
value: BoxSizing;
23542365
vendorPrefix: VendorPrefix;
23552366
}
2367+
| {
2368+
property: "aspect-ratio";
2369+
value: AspectRatio;
2370+
}
23562371
| {
23572372
property: "overflow";
23582373
value: Overflow;
@@ -3356,6 +3371,11 @@ export type Declaration =
33563371
property: "text-shadow";
33573372
value: TextShadow[];
33583373
}
3374+
| {
3375+
property: "text-size-adjust";
3376+
value: TextSizeAdjust;
3377+
vendorPrefix: VendorPrefix;
3378+
}
33593379
| {
33603380
property: "box-decoration-break";
33613381
value: BoxDecorationBreak;
@@ -5111,7 +5131,15 @@ export type FontSize =
51115131
*
51125132
* See [FontSize](FontSize).
51135133
*/
5114-
export type AbsoluteFontSize = "xx-small" | "x-small" | "small" | "medium" | "large" | "x-large" | "xx-large";
5134+
export type AbsoluteFontSize =
5135+
| "xx-small"
5136+
| "x-small"
5137+
| "small"
5138+
| "medium"
5139+
| "large"
5140+
| "x-large"
5141+
| "xx-large"
5142+
| "xxx-large";
51155143
/**
51165144
* A [relative font size](https://www.w3.org/TR/css-fonts-3/#relative-size-value), as used in the `font-size` property.
51175145
*
@@ -5594,6 +5622,22 @@ export type TextEmphasisPositionHorizontal = "left" | "right";
55945622
* See [TextEmphasisPosition](TextEmphasisPosition).
55955623
*/
55965624
export type TextEmphasisPositionVertical = "over" | "under";
5625+
/**
5626+
* A value for the [text-size-adjust](https://w3c.github.io/csswg-drafts/css-size-adjust/#adjustment-control) property.
5627+
*/
5628+
export type TextSizeAdjust =
5629+
| {
5630+
type: "auto";
5631+
}
5632+
| {
5633+
type: "none";
5634+
}
5635+
| (
5636+
| {
5637+
type: "percentage";
5638+
}
5639+
| number
5640+
);
55975641
/**
55985642
* A value for the [box-decoration-break](https://www.w3.org/TR/css-break-3/#break-decoration) property.
55995643
*/
@@ -6143,7 +6187,9 @@ export type Combinator =
61436187
| ("child" | "descendant" | "next-sibling" | "later-sibling")
61446188
| "pseudo-element"
61456189
| "slot-assignment"
6146-
| "part";
6190+
| "part"
6191+
| "deep-descendant"
6192+
| "deep";
61476193
export type NamespaceConstraint =
61486194
| {
61496195
type: "any";
@@ -6311,6 +6357,21 @@ export type PseudoClass =
63116357
kind: "fullscreen";
63126358
vendorPrefix: VendorPrefix;
63136359
}
6360+
| {
6361+
kind: "open";
6362+
}
6363+
| {
6364+
kind: "closed";
6365+
}
6366+
| {
6367+
kind: "modal";
6368+
}
6369+
| {
6370+
kind: "picture-in-picture";
6371+
}
6372+
| {
6373+
kind: "popover-open";
6374+
}
63146375
| {
63156376
kind: "defined";
63166377
}
@@ -7064,6 +7125,10 @@ export type DefaultAtRule = null;
70647125
* // Serialize it to a string. let res = stylesheet.to_css(PrinterOptions::default()).unwrap(); assert_eq!(res.code, ".foo, .bar {\n color: red;\n}\n"); ```
70657126
*/
70667127
export interface StyleSheet<D = Declaration, M = MediaQuery> {
7128+
/**
7129+
* The license comments that appeared at the start of the file.
7130+
*/
7131+
licenseComments: String[];
70677132
/**
70687133
* A list of top-level rules within the style sheet.
70697134
*/
@@ -7440,6 +7505,19 @@ export interface BoxShadow {
74407505
*/
74417506
yOffset: Length;
74427507
}
7508+
/**
7509+
* A value for the [aspect-ratio](https://drafts.csswg.org/css-sizing-4/#aspect-ratio) property.
7510+
*/
7511+
export interface AspectRatio {
7512+
/**
7513+
* The `auto` keyword.
7514+
*/
7515+
auto: boolean;
7516+
/**
7517+
* A preferred aspect ratio for the box, specified as width / height.
7518+
*/
7519+
ratio?: Ratio | null;
7520+
}
74437521
/**
74447522
* A value for the [overflow](https://www.w3.org/TR/css-overflow-3/#overflow-properties) shorthand property.
74457523
*/
@@ -9049,6 +9127,19 @@ export interface ContainerRule<D = Declaration, M = MediaQuery> {
90499127
*/
90509128
rules: Rule<D, M>[];
90519129
}
9130+
/**
9131+
* A [@starting-style](https://drafts.csswg.org/css-transitions-2/#defining-before-change-style-the-starting-style-rule) rule.
9132+
*/
9133+
export interface StartingStyleRule<D = Declaration, M = MediaQuery> {
9134+
/**
9135+
* The location of the rule in the source file.
9136+
*/
9137+
loc: Location;
9138+
/**
9139+
* Nested rules within the `@starting-style` rule.
9140+
*/
9141+
rules: Rule<D, M>[];
9142+
}
90529143
/**
90539144
* An unknown at-rule, stored as raw tokens.
90549145
*/

node/src/transformer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ impl<'i> Visitor<'i, AtRule<'i>> for JsVisitor {
286286
CssRule::MozDocument(..) => "moz-document",
287287
CssRule::Nesting(..) => "nesting",
288288
CssRule::Viewport(..) => "viewport",
289+
CssRule::StartingStyle(..) => "starting-style",
289290
CssRule::Unknown(v) => {
290291
let name = v.name.as_ref();
291292
if let Some(visit) = rule_map.custom(stage, "unknown", name) {

src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26108,4 +26108,41 @@ mod tests {
2610826108
.foo{color:red}"#},
2610926109
);
2611026110
}
26111+
26112+
#[test]
26113+
fn test_starting_style() {
26114+
minify_test(
26115+
r#"
26116+
@starting-style {
26117+
h1 {
26118+
background: yellow;
26119+
}
26120+
}
26121+
"#,
26122+
"@starting-style{h1{background:#ff0}}",
26123+
);
26124+
minify_test("@starting-style {}", "");
26125+
26126+
nesting_test(
26127+
r#"
26128+
h1 {
26129+
background: red;
26130+
@starting-style {
26131+
background: yellow;
26132+
}
26133+
}
26134+
"#,
26135+
indoc! {r#"
26136+
h1 {
26137+
background: red;
26138+
}
26139+
26140+
@starting-style {
26141+
h1 {
26142+
background: #ff0;
26143+
}
26144+
}
26145+
"#},
26146+
);
26147+
}
2611126148
}

src/parser.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::rules::container::{ContainerCondition, ContainerName, ContainerRule};
77
use crate::rules::font_palette_values::FontPaletteValuesRule;
88
use crate::rules::layer::{LayerBlockRule, LayerStatementRule};
99
use crate::rules::property::PropertyRule;
10+
use crate::rules::starting_style::StartingStyleRule;
1011
use crate::rules::viewport::ViewportRule;
1112
use crate::rules::{
1213
counter_style::CounterStyleRule,
@@ -190,6 +191,8 @@ pub enum AtRulePrelude<'i, T> {
190191
Property(DashedIdent<'i>),
191192
/// A @container prelude.
192193
Container(Option<ContainerName<'i>>, ContainerCondition<'i>),
194+
/// A @starting-style prelude.
195+
StartingStyle,
193196
/// An unknown prelude.
194197
Unknown(CowArcStr<'i>, TokenList<'i>),
195198
/// A custom prelude.
@@ -512,6 +515,9 @@ impl<'a, 'o, 'b, 'i, T: crate::traits::AtRuleParser<'i>> AtRuleParser<'i> for Ne
512515
let condition = ContainerCondition::parse(input)?;
513516
Ok(AtRulePrelude::Container(name, condition))
514517
},
518+
"starting-style" => {
519+
Ok(AtRulePrelude::StartingStyle)
520+
},
515521
_ => parse_custom_at_rule_prelude(&name, input, self.options, self.at_rule_parser)
516522
}
517523
}
@@ -625,6 +631,10 @@ impl<'a, 'o, 'b, 'i, T: crate::traits::AtRuleParser<'i>> AtRuleParser<'i> for Ne
625631
// These rules don't have blocks.
626632
Err(input.new_unexpected_token_error(Token::CurlyBracketBlock))
627633
}
634+
AtRulePrelude::StartingStyle => Ok(CssRule::StartingStyle(StartingStyleRule {
635+
rules: self.parse_nested_rules(input)?,
636+
loc,
637+
})),
628638
AtRulePrelude::FontFeatureValues | AtRulePrelude::Nest(..) => unreachable!(),
629639
AtRulePrelude::Unknown(name, prelude) => Ok(CssRule::Unknown(UnknownAtRule {
630640
name,
@@ -883,6 +893,9 @@ impl<'a, 'o, 'i, T: crate::traits::AtRuleParser<'i>> AtRuleParser<'i> for StyleR
883893
let name = input.try_parse(LayerName::parse).ok();
884894
Ok(AtRulePrelude::LayerBlock(name))
885895
},
896+
"starting-style" => {
897+
Ok(AtRulePrelude::StartingStyle)
898+
},
886899
"nest" => {
887900
self.options.warn(input.new_custom_error(ParserError::DeprecatedNestRule));
888901
let selector_parser = SelectorParser {
@@ -942,6 +955,13 @@ impl<'a, 'o, 'i, T: crate::traits::AtRuleParser<'i>> AtRuleParser<'i> for StyleR
942955
}));
943956
Ok(())
944957
}
958+
AtRulePrelude::StartingStyle => {
959+
self.rules.0.push(CssRule::StartingStyle(StartingStyleRule {
960+
rules: parse_nested_at_rule(input, self.options, self.at_rule_parser)?,
961+
loc,
962+
}));
963+
Ok(())
964+
}
945965
AtRulePrelude::Nest(selectors) => {
946966
let (declarations, rules) = parse_declarations_and_nested_rules(input, self.options, self.at_rule_parser)?;
947967
self.rules.0.push(CssRule::Nesting(NestingRule {

src/rules/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub mod namespace;
5050
pub mod nesting;
5151
pub mod page;
5252
pub mod property;
53+
pub mod starting_style;
5354
pub mod style;
5455
pub mod supports;
5556
pub mod unknown;
@@ -90,6 +91,7 @@ use namespace::NamespaceRule;
9091
use nesting::NestingRule;
9192
use page::PageRule;
9293
use smallvec::{smallvec, SmallVec};
94+
use starting_style::StartingStyleRule;
9395
use std::collections::{HashMap, HashSet};
9496
use std::hash::{BuildHasherDefault, Hasher};
9597
use style::StyleRule;
@@ -166,6 +168,8 @@ pub enum CssRule<'i, R = DefaultAtRule> {
166168
Property(PropertyRule<'i>),
167169
/// A `@container` rule.
168170
Container(ContainerRule<'i, R>),
171+
/// A `@starting-style` rule.
172+
StartingStyle(StartingStyleRule<'i, R>),
169173
/// A placeholder for a rule that was removed.
170174
Ignored,
171175
/// An unknown at-rule.
@@ -302,6 +306,10 @@ impl<'i, 'de: 'i, R: serde::Deserialize<'de>> serde::Deserialize<'de> for CssRul
302306
let rule = ContainerRule::deserialize(deserializer)?;
303307
Ok(CssRule::Container(rule))
304308
}
309+
"starting-style" => {
310+
let rule = StartingStyleRule::deserialize(deserializer)?;
311+
Ok(CssRule::StartingStyle(rule))
312+
}
305313
"ignored" => Ok(CssRule::Ignored),
306314
"unknown" => {
307315
let rule = UnknownAtRule::deserialize(deserializer)?;
@@ -339,6 +347,7 @@ impl<'a, 'i, T: ToCss> ToCss for CssRule<'i, T> {
339347
CssRule::LayerStatement(layer) => layer.to_css(dest),
340348
CssRule::LayerBlock(layer) => layer.to_css(dest),
341349
CssRule::Property(property) => property.to_css(dest),
350+
CssRule::StartingStyle(rule) => rule.to_css(dest),
342351
CssRule::Container(container) => container.to_css(dest),
343352
CssRule::Unknown(unknown) => unknown.to_css(dest),
344353
CssRule::Custom(rule) => rule.to_css(dest).map_err(|_| PrinterError {
@@ -754,6 +763,11 @@ impl<'i, T: Clone> CssRuleList<'i, T> {
754763
continue;
755764
}
756765
}
766+
CssRule::StartingStyle(rule) => {
767+
if rule.minify(context, parent_is_unused)? {
768+
continue;
769+
}
770+
}
757771
CssRule::FontPaletteValues(f) => {
758772
if context.unused_symbols.contains(f.name.0.as_ref()) {
759773
continue;

0 commit comments

Comments
 (0)