Skip to content

Commit

Permalink
style: Refactor function parsing branches for `specified::easing::Tim…
Browse files Browse the repository at this point in the history
…ingFunction`

Differential Revision: https://phabricator.services.mozilla.com/D149756
  • Loading branch information
dshin-moz authored and Loirooriol committed Sep 25, 2023
1 parent b7c1958 commit 8f08127
Showing 1 changed file with 74 additions and 60 deletions.
134 changes: 74 additions & 60 deletions components/style/values/specified/easing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,71 +53,85 @@ impl Parse for TimingFunction {
let location = input.current_source_location();
let function = input.expect_function()?.clone();
input.parse_nested_block(move |i| {
(match_ignore_ascii_case! { &function,
"cubic-bezier" => {
let x1 = Number::parse(context, i)?;
i.expect_comma()?;
let y1 = Number::parse(context, i)?;
i.expect_comma()?;
let x2 = Number::parse(context, i)?;
i.expect_comma()?;
let y2 = Number::parse(context, i)?;
match_ignore_ascii_case! { &function,
"cubic-bezier" => Self::parse_cubic_bezier(context, i),
"steps" => Self::parse_steps(context, i),
"linear" => Self::parse_linear_function(context, i),
_ => Err(location.new_custom_error(StyleParseErrorKind::UnexpectedFunction(function.clone()))),
}
})
}
}

if x1.get() < 0.0 || x1.get() > 1.0 || x2.get() < 0.0 || x2.get() > 1.0 {
return Err(i.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}
impl TimingFunction {
fn parse_cubic_bezier<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let x1 = Number::parse(context, input)?;
input.expect_comma()?;
let y1 = Number::parse(context, input)?;
input.expect_comma()?;
let x2 = Number::parse(context, input)?;
input.expect_comma()?;
let y2 = Number::parse(context, input)?;

Ok(GenericTimingFunction::CubicBezier { x1, y1, x2, y2 })
},
"steps" => {
let steps = Integer::parse_positive(context, i)?;
let position = i.try_parse(|i| {
i.expect_comma()?;
StepPosition::parse(context, i)
}).unwrap_or(StepPosition::End);
if x1.get() < 0.0 || x1.get() > 1.0 || x2.get() < 0.0 || x2.get() > 1.0 {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}

// jump-none accepts a positive integer greater than 1.
// FIXME(emilio): The spec asks us to avoid rejecting it at parse
// time except until computed value time.
//
// It's not totally clear it's worth it though, and no other browser
// does this.
if position == StepPosition::JumpNone && 2 > steps.value() {
return Err(i.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}
Ok(GenericTimingFunction::Steps(steps, position))
},
"linear" => {
if !linear_timing_function_enabled() {
return Err(i.new_custom_error(StyleParseErrorKind::ExperimentalProperty));
}
if i.is_exhausted() {
return Ok(GenericTimingFunction::LinearFunction(crate::OwnedSlice::default()))
}
let entries = i.parse_comma_separated(|i| {
let mut input_start = i.try_parse(|i| Percentage::parse(context, i)).ok();
let mut input_end = i.try_parse(|i| Percentage::parse(context, i)).ok();
Ok(GenericTimingFunction::CubicBezier { x1, y1, x2, y2 })
}

let output = Number::parse(context, i)?;
if input_start.is_none() {
debug_assert!(input_end.is_none(), "Input end parsed without input start?");
input_start = i.try_parse(|i| Percentage::parse(context, i)).ok();
input_end = i.try_parse(|i| Percentage::parse(context, i)).ok();
}
Ok(GenericLinearStop {
output,
input_start: input_start.into(),
input_end: input_end.into()
})
})?;
Ok(GenericTimingFunction::LinearFunction(crate::OwnedSlice::from(entries)))
},
_ => Err(()),
})
.map_err(|()| {
location.new_custom_error(StyleParseErrorKind::UnexpectedFunction(function.clone()))
fn parse_steps<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let steps = Integer::parse_positive(context, input)?;
let position = input.try_parse(|i| {
i.expect_comma()?;
StepPosition::parse(context, i)
}).unwrap_or(StepPosition::End);

// jump-none accepts a positive integer greater than 1.
// FIXME(emilio): The spec asks us to avoid rejecting it at parse
// time except until computed value time.
//
// It's not totally clear it's worth it though, and no other browser
// does this.
if position == StepPosition::JumpNone && steps.value() <= 1 {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}
Ok(GenericTimingFunction::Steps(steps, position))
}

fn parse_linear_function<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
if !linear_timing_function_enabled() {
return Err(input.new_custom_error(StyleParseErrorKind::ExperimentalProperty));
}
if input.is_exhausted() {
return Ok(GenericTimingFunction::LinearFunction(crate::OwnedSlice::default()))
}
let entries = input.parse_comma_separated(|i| {
let mut input_start = i.try_parse(|i| Percentage::parse(context, i)).ok();
let mut input_end = i.try_parse(|i| Percentage::parse(context, i)).ok();

let output = Number::parse(context, i)?;
if input_start.is_none() {
debug_assert!(input_end.is_none(), "Input end parsed without input start?");
input_start = i.try_parse(|i| Percentage::parse(context, i)).ok();
input_end = i.try_parse(|i| Percentage::parse(context, i)).ok();
}
Ok(GenericLinearStop {
output,
input_start: input_start.into(),
input_end: input_end.into()
})
})
})?;
Ok(GenericTimingFunction::LinearFunction(crate::OwnedSlice::from(entries)))
}
}

Expand Down

0 comments on commit 8f08127

Please sign in to comment.