Refactor KeyframePercentage::parse. This adresses #20179

This commit is contained in:
Florian Wagner 2018-03-11 11:28:17 +01:00
parent e3c78808c5
commit 85bb94be9c

View file

@ -5,7 +5,7 @@
//! Keyframes: https://drafts.csswg.org/css-animations/#keyframes //! Keyframes: https://drafts.csswg.org/css-animations/#keyframes
use cssparser::{AtRuleParser, Parser, QualifiedRuleParser, RuleListParser, ParserInput, CowRcStr}; use cssparser::{AtRuleParser, Parser, QualifiedRuleParser, RuleListParser, ParserInput, CowRcStr};
use cssparser::{DeclarationListParser, DeclarationParser, parse_one_rule, SourceLocation}; use cssparser::{DeclarationListParser, DeclarationParser, parse_one_rule, SourceLocation, Token};
use error_reporting::{NullReporter, ContextualParseError, ParseErrorReporter}; use error_reporting::{NullReporter, ContextualParseError, ParseErrorReporter};
use parser::{ParserContext, ParserErrorContext}; use parser::{ParserContext, ParserErrorContext};
use properties::{DeclarationSource, Importance, PropertyDeclaration, PropertyDeclarationBlock, PropertyId}; use properties::{DeclarationSource, Importance, PropertyDeclaration, PropertyDeclarationBlock, PropertyId};
@ -126,20 +126,21 @@ impl KeyframePercentage {
} }
fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<KeyframePercentage, ParseError<'i>> { fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<KeyframePercentage, ParseError<'i>> {
let percentage = if input.try(|input| input.expect_ident_matching("from")).is_ok() { let token = input.next()?.clone();
KeyframePercentage::new(0.) match token {
} else if input.try(|input| input.expect_ident_matching("to")).is_ok() { Token::Ident(ref identifier) if identifier.as_ref().eq_ignore_ascii_case("from") => {
KeyframePercentage::new(1.) Ok(KeyframePercentage::new(0.))
} else { },
let percentage = input.expect_percentage()?; Token::Ident(ref identifier) if identifier.as_ref().eq_ignore_ascii_case("to") => {
if percentage >= 0. && percentage <= 1. { Ok(KeyframePercentage::new(1.))
KeyframePercentage::new(percentage) },
} else { Token::Percentage { unit_value: percentage, .. } if percentage >= 0. && percentage <= 1. => {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)); Ok(KeyframePercentage::new(percentage))
} },
}; _ => {
Err(input.new_unexpected_token_error(token))
Ok(percentage) },
}
} }
} }