Skip to content

Commit 86ac8f0

Browse files
authored
Add support for named timeline ranges in @Keyframes (parcel-bundler#787)
1 parent be53018 commit 86ac8f0

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6925,6 +6925,21 @@ mod tests {
69256925
"@keyframes test{to{background:#00f}}",
69266926
);
69276927

6928+
// named animation range percentages
6929+
minify_test(
6930+
r#"
6931+
@keyframes test {
6932+
entry 0% {
6933+
background: blue
6934+
}
6935+
exit 100% {
6936+
background: green
6937+
}
6938+
}
6939+
"#,
6940+
"@keyframes test{entry 0%{background:#00f}exit 100%{background:green}}",
6941+
);
6942+
69286943
// CSS-wide keywords and `none` cannot remove quotes.
69296944
minify_test(
69306945
r#"
@@ -6948,6 +6963,18 @@ mod tests {
69486963
"@keyframes \"none\"{0%{background:green}}",
69496964
);
69506965

6966+
// named animation ranges cannot be used with to or from
6967+
minify_test(
6968+
r#"
6969+
@keyframes test {
6970+
entry to {
6971+
background: blue
6972+
}
6973+
}
6974+
"#,
6975+
"@keyframes test{}",
6976+
);
6977+
69516978
// CSS-wide keywords without quotes throws an error.
69526979
error_test(
69536980
r#"

src/rules/keyframes.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::declaration::DeclarationBlock;
88
use crate::error::{ParserError, PrinterError};
99
use crate::parser::ParserOptions;
1010
use crate::printer::Printer;
11+
use crate::properties::animation::TimelineRangeName;
1112
use crate::properties::custom::{CustomProperty, UnparsedProperty};
1213
use crate::properties::Property;
1314
use crate::targets::Targets;
@@ -265,6 +266,34 @@ impl<'i> ToCss for KeyframesRule<'i> {
265266
}
266267
}
267268

269+
/// A percentage of a given timeline range
270+
#[derive(Debug, PartialEq, Clone)]
271+
#[cfg_attr(feature = "visitor", derive(Visit))]
272+
#[cfg_attr(
273+
feature = "serde",
274+
derive(serde::Serialize, serde::Deserialize),
275+
serde(tag = "type", rename_all = "camelCase")
276+
)]
277+
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
278+
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
279+
pub struct TimelineRangePercentage {
280+
/// A named timeline range
281+
timeline_range_name: TimelineRangeName,
282+
/// The percentage progress between the start and end of the rage
283+
percentage: Percentage
284+
}
285+
286+
impl<'i> Parse<'i> for TimelineRangePercentage {
287+
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
288+
let timeline_range_name = TimelineRangeName::parse(input)?;
289+
let percentage = Percentage::parse(input)?;
290+
Ok(TimelineRangePercentage {
291+
timeline_range_name,
292+
percentage
293+
})
294+
}
295+
}
296+
268297
/// A [keyframe selector](https://drafts.csswg.org/css-animations/#typedef-keyframe-selector)
269298
/// within an `@keyframes` rule.
270299
#[derive(Debug, PartialEq, Clone, Parse)]
@@ -283,6 +312,8 @@ pub enum KeyframeSelector {
283312
From,
284313
/// The `to` keyword. Equivalent to 100%.
285314
To,
315+
/// A [named timeline range selector](https://drafts.csswg.org/scroll-animations-1/#named-range-keyframes)
316+
TimelineRangePercentage(TimelineRangePercentage)
286317
}
287318

288319
impl ToCss for KeyframeSelector {
@@ -306,6 +337,14 @@ impl ToCss for KeyframeSelector {
306337
}
307338
}
308339
KeyframeSelector::To => dest.write_str("to"),
340+
KeyframeSelector::TimelineRangePercentage(TimelineRangePercentage {
341+
timeline_range_name,
342+
percentage
343+
}) => {
344+
timeline_range_name.to_css(dest)?;
345+
dest.write_char(' ')?;
346+
percentage.to_css(dest)
347+
}
309348
}
310349
}
311350
}

0 commit comments

Comments
 (0)