mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Simplify animation shorthand parsing code
This commit is contained in:
parent
a06b3f2039
commit
1bb1eeeb53
2 changed files with 31 additions and 70 deletions
|
@ -153,32 +153,26 @@ macro_rules! try_parse_one {
|
||||||
animation-fill-mode animation-play-state"
|
animation-fill-mode animation-play-state"
|
||||||
allowed_in_keyframe_block="False"
|
allowed_in_keyframe_block="False"
|
||||||
spec="https://drafts.csswg.org/css-animations/#propdef-animation">
|
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 parser::Parse;
|
||||||
use properties::longhands::{animation_name, animation_duration, animation_timing_function};
|
% for prop in props:
|
||||||
use properties::longhands::{animation_delay, animation_iteration_count, animation_direction};
|
use properties::longhands::animation_${prop};
|
||||||
use properties::longhands::{animation_fill_mode, animation_play_state};
|
% endfor
|
||||||
|
|
||||||
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
|
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
|
||||||
struct SingleAnimation {
|
struct SingleAnimation {
|
||||||
animation_name: animation_name::SingleSpecifiedValue,
|
% for prop in props:
|
||||||
animation_duration: animation_duration::SingleSpecifiedValue,
|
animation_${prop}: animation_${prop}::SingleSpecifiedValue,
|
||||||
animation_timing_function: animation_timing_function::SingleSpecifiedValue,
|
% endfor
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_one_animation(context: &ParserContext, input: &mut Parser) -> Result<SingleAnimation,()> {
|
fn parse_one_animation(context: &ParserContext, input: &mut Parser) -> Result<SingleAnimation,()> {
|
||||||
let mut duration = None;
|
% for prop in props:
|
||||||
let mut timing_function = None;
|
let mut ${prop} = None;
|
||||||
let mut delay = None;
|
% endfor
|
||||||
let mut iteration_count = None;
|
|
||||||
let mut direction = None;
|
|
||||||
let mut fill_mode = None;
|
|
||||||
let mut play_state = None;
|
|
||||||
let mut name = None;
|
|
||||||
|
|
||||||
// NB: Name must be the last one here so that keywords valid for other
|
// NB: Name must be the last one here so that keywords valid for other
|
||||||
// longhands are not interpreted as names.
|
// longhands are not interpreted as names.
|
||||||
|
@ -199,58 +193,30 @@ macro_rules! try_parse_one {
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(SingleAnimation {
|
Ok(SingleAnimation {
|
||||||
animation_name:
|
% for prop in props:
|
||||||
name.unwrap_or_else(animation_name::single_value::get_initial_specified_value),
|
animation_${prop}: ${prop}.unwrap_or_else(animation_${prop}::single_value
|
||||||
animation_duration:
|
::get_initial_specified_value),
|
||||||
duration.unwrap_or_else(animation_duration::single_value::get_initial_value),
|
% endfor
|
||||||
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),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut names = vec![];
|
% for prop in props:
|
||||||
let mut durations = vec![];
|
let mut ${prop}s = vec![];
|
||||||
let mut timing_functions = vec![];
|
% endfor
|
||||||
let mut delays = vec![];
|
|
||||||
let mut iteration_counts = vec![];
|
|
||||||
let mut directions = vec![];
|
|
||||||
let mut fill_modes = vec![];
|
|
||||||
let mut play_states = vec![];
|
|
||||||
|
|
||||||
if input.try(|input| input.expect_ident_matching("none")).is_err() {
|
if input.try(|input| input.expect_ident_matching("none")).is_err() {
|
||||||
let results = try!(input.parse_comma_separated(|i| parse_one_animation(context, i)));
|
let results = try!(input.parse_comma_separated(|i| parse_one_animation(context, i)));
|
||||||
for result in results.into_iter() {
|
for result in results.into_iter() {
|
||||||
names.push(result.animation_name);
|
% for prop in props:
|
||||||
durations.push(result.animation_duration);
|
${prop}s.push(result.animation_${prop});
|
||||||
timing_functions.push(result.animation_timing_function);
|
% endfor
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Longhands {
|
Ok(Longhands {
|
||||||
animation_name: animation_name::SpecifiedValue(names),
|
% for prop in props:
|
||||||
animation_duration: animation_duration::SpecifiedValue(durations),
|
animation_${prop}: animation_${prop}::SpecifiedValue(${prop}s),
|
||||||
animation_timing_function: animation_timing_function::SpecifiedValue(timing_functions),
|
% endfor
|
||||||
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),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,14 +228,9 @@ macro_rules! try_parse_one {
|
||||||
return Ok(());
|
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
|
// If any value list length is differs then we don't do a shorthand serialization
|
||||||
// either.
|
// either.
|
||||||
% for name in subproperties:
|
% for name in props[1:]:
|
||||||
if len != self.animation_${name}.0.len() {
|
if len != self.animation_${name}.0.len() {
|
||||||
return Ok(())
|
return Ok(())
|
||||||
}
|
}
|
||||||
|
@ -280,7 +241,7 @@ macro_rules! try_parse_one {
|
||||||
try!(write!(dest, ", "));
|
try!(write!(dest, ", "));
|
||||||
}
|
}
|
||||||
|
|
||||||
% for name in subproperties:
|
% for name in props[1:]:
|
||||||
self.animation_${name}.0[i].to_css(dest)?;
|
self.animation_${name}.0[i].to_css(dest)?;
|
||||||
dest.write_str(" ")?;
|
dest.write_str(" ")?;
|
||||||
% endfor
|
% endfor
|
||||||
|
|
|
@ -983,7 +983,7 @@ mod shorthand_serialization {
|
||||||
|
|
||||||
let serialization = block.to_css_string();
|
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]
|
#[test]
|
||||||
|
@ -1001,8 +1001,8 @@ mod shorthand_serialization {
|
||||||
let serialization = block.to_css_string();
|
let serialization = block.to_css_string();
|
||||||
|
|
||||||
assert_eq!(serialization,
|
assert_eq!(serialization,
|
||||||
"animation: 1s ease-in 0s normal forwards infinite paused bounce, \
|
"animation: 1s ease-in 0s infinite normal forwards paused bounce, \
|
||||||
0.2s linear 1s reverse backwards 2 running roll;");
|
0.2s linear 1s 2 reverse backwards running roll;");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue