Skip to content

Commit 7843eff

Browse files
committed
Optimize animations
1 parent 1cf651f commit 7843eff

File tree

6 files changed

+455
-4
lines changed

6 files changed

+455
-4
lines changed

src/declaration.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::properties::{
1010
margin_padding::*,
1111
outline::OutlineHandler,
1212
border::BorderHandler,
13-
transition::TransitionHandler
13+
transition::TransitionHandler,
14+
animation::AnimationHandler
1415
};
1516

1617
#[derive(Debug, Clone, PartialEq)]
@@ -54,7 +55,8 @@ pub struct DeclarationHandler {
5455
scroll_margin: ScrollMarginHandler,
5556
scroll_padding: ScrollPaddingHandler,
5657
font: FontHandler,
57-
transition: TransitionHandler
58+
transition: TransitionHandler,
59+
animation: AnimationHandler
5860
}
5961

6062
impl DeclarationHandler {
@@ -77,7 +79,8 @@ impl DeclarationHandler {
7779
self.scroll_margin.handle_property(property) ||
7880
self.scroll_padding.handle_property(property) ||
7981
self.font.handle_property(property) ||
80-
self.transition.handle_property(property)
82+
self.transition.handle_property(property) ||
83+
self.animation.handle_property(property)
8184
}
8285

8386
pub fn finalize(&mut self) -> Vec<Declaration> {
@@ -93,6 +96,7 @@ impl DeclarationHandler {
9396
.chain(self.scroll_padding.finalize().drain(..))
9497
.chain(self.font.finalize().drain(..))
9598
.chain(self.transition.finalize().drain(..))
99+
.chain(self.animation.finalize().drain(..))
96100
.map(|property| Declaration { property, important })
97101
.collect()
98102
}

src/lib.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,4 +1384,100 @@ mod tests {
13841384
}
13851385
"#});
13861386
}
1387+
1388+
#[test]
1389+
fn test_animation() {
1390+
minify_test(".foo { animation-name: test }", ".foo{animation-name:test}");
1391+
minify_test(".foo { animation-name: \"test\" }", ".foo{animation-name:test}");
1392+
minify_test(".foo { animation-name: foo, bar }", ".foo{animation-name:foo,bar}");
1393+
minify_test(".foo { animation-duration: 100ms }", ".foo{animation-duration:.1s}");
1394+
minify_test(".foo { animation-duration: 100ms, 2000ms }", ".foo{animation-duration:.1s,2s}");
1395+
minify_test(".foo { animation-timing-function: ease }", ".foo{animation-timing-function:ease}");
1396+
minify_test(".foo { animation-timing-function: cubic-bezier(0.42, 0, 1, 1) }", ".foo{animation-timing-function:ease-in}");
1397+
minify_test(".foo { animation-timing-function: ease, cubic-bezier(0.42, 0, 1, 1) }", ".foo{animation-timing-function:ease,ease-in}");
1398+
minify_test(".foo { animation-iteration-count: 5 }", ".foo{animation-iteration-count:5}");
1399+
minify_test(".foo { animation-iteration-count: 2.5 }", ".foo{animation-iteration-count:2.5}");
1400+
minify_test(".foo { animation-iteration-count: 2.0 }", ".foo{animation-iteration-count:2}");
1401+
minify_test(".foo { animation-iteration-count: infinite }", ".foo{animation-iteration-count:infinite}");
1402+
minify_test(".foo { animation-iteration-count: 1, infinite }", ".foo{animation-iteration-count:1,infinite}");
1403+
minify_test(".foo { animation-direction: reverse }", ".foo{animation-direction:reverse}");
1404+
minify_test(".foo { animation-direction: alternate, reverse }", ".foo{animation-direction:alternate,reverse}");
1405+
minify_test(".foo { animation-play-state: paused }", ".foo{animation-play-state:paused}");
1406+
minify_test(".foo { animation-play-state: running, paused }", ".foo{animation-play-state:running,paused}");
1407+
minify_test(".foo { animation-delay: 100ms }", ".foo{animation-delay:.1s}");
1408+
minify_test(".foo { animation-delay: 100ms, 2000ms }", ".foo{animation-delay:.1s,2s}");
1409+
minify_test(".foo { animation-fill-mode: forwards }", ".foo{animation-fill-mode:forwards}");
1410+
minify_test(".foo { animation-fill-mode: Backwards,forwards }", ".foo{animation-fill-mode:backwards,forwards}");
1411+
minify_test(".foo { animation: 3s ease-in 1s infinite reverse both running slidein }", ".foo{animation:slidein 3s ease-in 1s infinite reverse both}");
1412+
minify_test(".foo { animation: 3s slidein paused ease 1s 1 reverse both }", ".foo{animation:slidein 3s 1s reverse both paused}");
1413+
minify_test(".foo { animation: 3s ease ease }", ".foo{animation:ease 3s ease}");
1414+
minify_test(".foo { animation: 3s cubic-bezier(0.25, 0.1, 0.25, 1) foo }", ".foo{animation:foo 3s}");
1415+
minify_test(".foo { animation: foo 0s 3s infinite }", ".foo{animation:foo 0s 3s infinite}");
1416+
minify_test(".foo { animation: none }", ".foo{animation:none}");
1417+
test(r#"
1418+
.foo {
1419+
animation-name: foo;
1420+
animation-duration: 0.09s;
1421+
animation-timing-function: ease-in-out;
1422+
animation-iteration-count: 2;
1423+
animation-direction: alternate;
1424+
animation-play-state: running;
1425+
animation-delay: 100ms;
1426+
animation-fill-mode: forwards;
1427+
}
1428+
"#, indoc! {r#"
1429+
.foo {
1430+
animation: foo 90ms ease-in-out .1s 2 alternate forwards;
1431+
}
1432+
"#});
1433+
test(r#"
1434+
.foo {
1435+
animation-name: foo, bar;
1436+
animation-duration: 0.09s, 200ms;
1437+
animation-timing-function: ease-in-out, ease;
1438+
animation-iteration-count: 2, 1;
1439+
animation-direction: alternate, normal;
1440+
animation-play-state: running, paused;
1441+
animation-delay: 100ms, 0s;
1442+
animation-fill-mode: forwards, none;
1443+
}
1444+
"#, indoc! {r#"
1445+
.foo {
1446+
animation: foo 90ms ease-in-out .1s 2 alternate forwards, bar .2s paused;
1447+
}
1448+
"#});
1449+
test(r#"
1450+
.foo {
1451+
animation: bar 200ms;
1452+
animation-timing-function: ease-in-out;
1453+
}
1454+
"#, indoc! {r#"
1455+
.foo {
1456+
animation: bar .2s ease-in-out;
1457+
}
1458+
"#});
1459+
test(r#"
1460+
.foo {
1461+
animation-name: foo, bar;
1462+
animation-duration: 0.09s;
1463+
animation-timing-function: ease-in-out;
1464+
animation-iteration-count: 2;
1465+
animation-direction: alternate;
1466+
animation-play-state: running;
1467+
animation-delay: 100ms;
1468+
animation-fill-mode: forwards;
1469+
}
1470+
"#, indoc! {r#"
1471+
.foo {
1472+
animation-name: foo, bar;
1473+
animation-duration: 90ms;
1474+
animation-timing-function: ease-in-out;
1475+
animation-iteration-count: 2;
1476+
animation-direction: alternate;
1477+
animation-play-state: running;
1478+
animation-delay: .1s;
1479+
animation-fill-mode: forwards;
1480+
}
1481+
"#});
1482+
}
13871483
}

src/macros.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ macro_rules! enum_property {
2929
}
3030
}
3131
}
32+
33+
impl $name {
34+
pub fn from_str(s: &str) -> Option<Self> {
35+
match s {
36+
$(
37+
s if s.eq_ignore_ascii_case(stringify!($x)) => Some($name::$x),
38+
)+
39+
_ => None
40+
}
41+
}
42+
}
3243
};
3344
($name: ident, $( ($str: expr, $id: ident) ),+) => {
3445
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -60,6 +71,17 @@ macro_rules! enum_property {
6071
}
6172
}
6273
}
74+
75+
impl $name {
76+
pub fn from_str(s: &str) -> Option<Self> {
77+
match s {
78+
$(
79+
s if s.eq_ignore_ascii_case($str) => Some($name::$id),
80+
)+
81+
_ => None
82+
}
83+
}
84+
}
6385
};
6486
}
6587

0 commit comments

Comments
 (0)