From 6b03760213f099f57bbaaf1ba0f5d2b4f9606d9a Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Mon, 6 Mar 2017 22:01:15 +1100 Subject: [PATCH] Simplify animation shorthand parsing code --- .../style/properties/shorthand/box.mako.rs | 95 ++++++------------- tests/unit/style/properties/serialization.rs | 6 +- 2 files changed, 31 insertions(+), 70 deletions(-) diff --git a/components/style/properties/shorthand/box.mako.rs b/components/style/properties/shorthand/box.mako.rs index 0fa958952cd3..1639dd475f37 100644 --- a/components/style/properties/shorthand/box.mako.rs +++ b/components/style/properties/shorthand/box.mako.rs @@ -153,32 +153,26 @@ macro_rules! try_parse_one { animation-fill-mode animation-play-state" allowed_in_keyframe_block="False" spec="https://drafts.csswg.org/css-animations/#propdef-animation"> + <% + props = "name duration timing_function delay iteration_count \ + direction fill_mode play_state".split() + %> use parser::Parse; - use properties::longhands::{animation_name, animation_duration, animation_timing_function}; - use properties::longhands::{animation_delay, animation_iteration_count, animation_direction}; - use properties::longhands::{animation_fill_mode, animation_play_state}; + % for prop in props: + use properties::longhands::animation_${prop}; + % endfor pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result { struct SingleAnimation { - animation_name: animation_name::SingleSpecifiedValue, - animation_duration: animation_duration::SingleSpecifiedValue, - animation_timing_function: animation_timing_function::SingleSpecifiedValue, - animation_delay: animation_delay::SingleSpecifiedValue, - animation_iteration_count: animation_iteration_count::SingleSpecifiedValue, - animation_direction: animation_direction::SingleSpecifiedValue, - animation_fill_mode: animation_fill_mode::SingleSpecifiedValue, - animation_play_state: animation_play_state::SingleSpecifiedValue, + % for prop in props: + animation_${prop}: animation_${prop}::SingleSpecifiedValue, + % endfor } fn parse_one_animation(context: &ParserContext, input: &mut Parser) -> Result { - let mut duration = None; - let mut timing_function = None; - let mut delay = None; - let mut iteration_count = None; - let mut direction = None; - let mut fill_mode = None; - let mut play_state = None; - let mut name = None; + % for prop in props: + let mut ${prop} = None; + % endfor // NB: Name must be the last one here so that keywords valid for other // longhands are not interpreted as names. @@ -199,58 +193,30 @@ macro_rules! try_parse_one { } Ok(SingleAnimation { - animation_name: - name.unwrap_or_else(animation_name::single_value::get_initial_specified_value), - animation_duration: - duration.unwrap_or_else(animation_duration::single_value::get_initial_value), - animation_timing_function: - timing_function.unwrap_or_else(animation_timing_function::single_value - ::get_initial_specified_value), - animation_delay: - delay.unwrap_or_else(animation_delay::single_value::get_initial_value), - animation_iteration_count: - iteration_count.unwrap_or_else(animation_iteration_count::single_value::get_initial_value), - animation_direction: - direction.unwrap_or_else(animation_direction::single_value::get_initial_value), - animation_fill_mode: - fill_mode.unwrap_or_else(animation_fill_mode::single_value::get_initial_value), - animation_play_state: - play_state.unwrap_or_else(animation_play_state::single_value::get_initial_value), + % for prop in props: + animation_${prop}: ${prop}.unwrap_or_else(animation_${prop}::single_value + ::get_initial_specified_value), + % endfor }) } - let mut names = vec![]; - let mut durations = vec![]; - let mut timing_functions = vec![]; - let mut delays = vec![]; - let mut iteration_counts = vec![]; - let mut directions = vec![]; - let mut fill_modes = vec![]; - let mut play_states = vec![]; + % for prop in props: + let mut ${prop}s = vec![]; + % endfor if input.try(|input| input.expect_ident_matching("none")).is_err() { let results = try!(input.parse_comma_separated(|i| parse_one_animation(context, i))); for result in results.into_iter() { - names.push(result.animation_name); - durations.push(result.animation_duration); - timing_functions.push(result.animation_timing_function); - delays.push(result.animation_delay); - iteration_counts.push(result.animation_iteration_count); - directions.push(result.animation_direction); - fill_modes.push(result.animation_fill_mode); - play_states.push(result.animation_play_state); + % for prop in props: + ${prop}s.push(result.animation_${prop}); + % endfor } } Ok(Longhands { - animation_name: animation_name::SpecifiedValue(names), - animation_duration: animation_duration::SpecifiedValue(durations), - animation_timing_function: animation_timing_function::SpecifiedValue(timing_functions), - animation_delay: animation_delay::SpecifiedValue(delays), - animation_iteration_count: animation_iteration_count::SpecifiedValue(iteration_counts), - animation_direction: animation_direction::SpecifiedValue(directions), - animation_fill_mode: animation_fill_mode::SpecifiedValue(fill_modes), - animation_play_state: animation_play_state::SpecifiedValue(play_states), + % for prop in props: + animation_${prop}: animation_${prop}::SpecifiedValue(${prop}s), + % endfor }) } @@ -262,14 +228,9 @@ macro_rules! try_parse_one { return Ok(()); } - <% - subproperties = "duration timing_function delay direction \ - fill_mode iteration_count play_state".split() - %> - // If any value list length is differs then we don't do a shorthand serialization // either. - % for name in subproperties: + % for name in props[1:]: if len != self.animation_${name}.0.len() { return Ok(()) } @@ -280,7 +241,7 @@ macro_rules! try_parse_one { try!(write!(dest, ", ")); } - % for name in subproperties: + % for name in props[1:]: self.animation_${name}.0[i].to_css(dest)?; dest.write_str(" ")?; % endfor diff --git a/tests/unit/style/properties/serialization.rs b/tests/unit/style/properties/serialization.rs index 9e99169c54de..9433d97ed3d6 100644 --- a/tests/unit/style/properties/serialization.rs +++ b/tests/unit/style/properties/serialization.rs @@ -983,7 +983,7 @@ mod shorthand_serialization { let serialization = block.to_css_string(); - assert_eq!(serialization, "animation: 1s ease-in 0s normal forwards infinite paused bounce;") + assert_eq!(serialization, "animation: 1s ease-in 0s infinite normal forwards paused bounce;") } #[test] @@ -1001,8 +1001,8 @@ mod shorthand_serialization { let serialization = block.to_css_string(); assert_eq!(serialization, - "animation: 1s ease-in 0s normal forwards infinite paused bounce, \ - 0.2s linear 1s reverse backwards 2 running roll;"); + "animation: 1s ease-in 0s infinite normal forwards paused bounce, \ + 0.2s linear 1s 2 reverse backwards running roll;"); } #[test]