mirror of
https://github.com/servo/servo.git
synced 2025-08-09 15:35:34 +01:00
style: Refactor function parsing branches for specified::easing::TimingFunction
Differential Revision: https://phabricator.services.mozilla.com/D149756
This commit is contained in:
parent
3d02b4ef90
commit
ca6ad97159
1 changed files with 78 additions and 64 deletions
|
@ -53,25 +53,42 @@ impl Parse for TimingFunction {
|
||||||
let location = input.current_source_location();
|
let location = input.current_source_location();
|
||||||
let function = input.expect_function()?.clone();
|
let function = input.expect_function()?.clone();
|
||||||
input.parse_nested_block(move |i| {
|
input.parse_nested_block(move |i| {
|
||||||
(match_ignore_ascii_case! { &function,
|
match_ignore_ascii_case! { &function,
|
||||||
"cubic-bezier" => {
|
"cubic-bezier" => Self::parse_cubic_bezier(context, i),
|
||||||
let x1 = Number::parse(context, i)?;
|
"steps" => Self::parse_steps(context, i),
|
||||||
i.expect_comma()?;
|
"linear" => Self::parse_linear_function(context, i),
|
||||||
let y1 = Number::parse(context, i)?;
|
_ => Err(location.new_custom_error(StyleParseErrorKind::UnexpectedFunction(function.clone()))),
|
||||||
i.expect_comma()?;
|
}
|
||||||
let x2 = Number::parse(context, i)?;
|
})
|
||||||
i.expect_comma()?;
|
}
|
||||||
let y2 = Number::parse(context, i)?;
|
}
|
||||||
|
|
||||||
|
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)?;
|
||||||
|
|
||||||
if x1.get() < 0.0 || x1.get() > 1.0 || x2.get() < 0.0 || x2.get() > 1.0 {
|
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));
|
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(GenericTimingFunction::CubicBezier { x1, y1, x2, y2 })
|
Ok(GenericTimingFunction::CubicBezier { x1, y1, x2, y2 })
|
||||||
},
|
}
|
||||||
"steps" => {
|
|
||||||
let steps = Integer::parse_positive(context, i)?;
|
fn parse_steps<'i, 't>(
|
||||||
let position = i.try_parse(|i| {
|
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()?;
|
i.expect_comma()?;
|
||||||
StepPosition::parse(context, i)
|
StepPosition::parse(context, i)
|
||||||
}).unwrap_or(StepPosition::End);
|
}).unwrap_or(StepPosition::End);
|
||||||
|
@ -82,19 +99,23 @@ impl Parse for TimingFunction {
|
||||||
//
|
//
|
||||||
// It's not totally clear it's worth it though, and no other browser
|
// It's not totally clear it's worth it though, and no other browser
|
||||||
// does this.
|
// does this.
|
||||||
if position == StepPosition::JumpNone && 2 > steps.value() {
|
if position == StepPosition::JumpNone && steps.value() <= 1 {
|
||||||
return Err(i.new_custom_error(StyleParseErrorKind::UnspecifiedError));
|
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
|
||||||
}
|
}
|
||||||
Ok(GenericTimingFunction::Steps(steps, position))
|
Ok(GenericTimingFunction::Steps(steps, position))
|
||||||
},
|
|
||||||
"linear" => {
|
|
||||||
if !linear_timing_function_enabled() {
|
|
||||||
return Err(i.new_custom_error(StyleParseErrorKind::ExperimentalProperty));
|
|
||||||
}
|
}
|
||||||
if i.is_exhausted() {
|
|
||||||
|
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()))
|
return Ok(GenericTimingFunction::LinearFunction(crate::OwnedSlice::default()))
|
||||||
}
|
}
|
||||||
let entries = i.parse_comma_separated(|i| {
|
let entries = input.parse_comma_separated(|i| {
|
||||||
let mut input_start = i.try_parse(|i| Percentage::parse(context, i)).ok();
|
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 mut input_end = i.try_parse(|i| Percentage::parse(context, i)).ok();
|
||||||
|
|
||||||
|
@ -111,13 +132,6 @@ impl Parse for TimingFunction {
|
||||||
})
|
})
|
||||||
})?;
|
})?;
|
||||||
Ok(GenericTimingFunction::LinearFunction(crate::OwnedSlice::from(entries)))
|
Ok(GenericTimingFunction::LinearFunction(crate::OwnedSlice::from(entries)))
|
||||||
},
|
|
||||||
_ => Err(()),
|
|
||||||
})
|
|
||||||
.map_err(|()| {
|
|
||||||
location.new_custom_error(StyleParseErrorKind::UnexpectedFunction(function.clone()))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue