AllowedNumericType.is_ok() takes ParingMode as well.

And it returns true if ParsingMode.allows_all_numeric_values is true regardless
of AllowedNumericType itself.
This commit is contained in:
Hiroyuki Ikezoe 2017-06-14 09:52:05 +09:00
parent 7341574b66
commit 6c5915068a
3 changed files with 27 additions and 22 deletions

View file

@ -715,11 +715,12 @@ impl ToCss for Percentage {
impl Percentage { impl Percentage {
/// Parse a specific kind of percentage. /// Parse a specific kind of percentage.
pub fn parse_with_clamping_mode<'i, 't>(input: &mut Parser<'i, 't>, pub fn parse_with_clamping_mode<'i, 't>(context: &ParserContext,
context: AllowedNumericType) input: &mut Parser<'i, 't>,
num_context: AllowedNumericType)
-> Result<Self, ParseError<'i>> { -> Result<Self, ParseError<'i>> {
match try!(input.next()) { match try!(input.next()) {
Token::Percentage(ref value) if context.is_ok(value.unit_value) => { Token::Percentage(ref value) if num_context.is_ok(context.parsing_mode, value.unit_value) => {
Ok(Percentage(value.unit_value)) Ok(Percentage(value.unit_value))
} }
t => Err(BasicParseError::UnexpectedToken(t).into()) t => Err(BasicParseError::UnexpectedToken(t).into())
@ -727,8 +728,10 @@ impl Percentage {
} }
/// Parses a percentage token, but rejects it if it's negative. /// Parses a percentage token, but rejects it if it's negative.
pub fn parse_non_negative<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> { pub fn parse_non_negative<'i, 't>(context: &ParserContext,
Self::parse_with_clamping_mode(input, AllowedNumericType::NonNegative) input: &mut Parser<'i, 't>)
-> Result<Self, ParseError<'i>> {
Self::parse_with_clamping_mode(context, input, AllowedNumericType::NonNegative)
} }
/// 0% /// 0%
@ -746,8 +749,8 @@ impl Percentage {
impl Parse for Percentage { impl Parse for Percentage {
#[inline] #[inline]
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> { fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
Self::parse_with_clamping_mode(input, AllowedNumericType::All) Self::parse_with_clamping_mode(context, input, AllowedNumericType::All)
} }
} }

View file

@ -120,7 +120,7 @@ pub fn parse_number_with_clamping_mode<'i, 't>(context: &ParserContext,
clamping_mode: AllowedNumericType) clamping_mode: AllowedNumericType)
-> Result<Number, ParseError<'i>> { -> Result<Number, ParseError<'i>> {
match try!(input.next()) { match try!(input.next()) {
Token::Number(ref value) if clamping_mode.is_ok(value.value) => { Token::Number(ref value) if clamping_mode.is_ok(context.parsing_mode, value.value) => {
Ok(Number { Ok(Number {
value: value.value.min(f32::MAX).max(f32::MIN), value: value.value.min(f32::MAX).max(f32::MIN),
calc_clamping_mode: None, calc_clamping_mode: None,
@ -364,14 +364,21 @@ impl Time {
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
clamping_mode: AllowedNumericType) clamping_mode: AllowedNumericType)
-> Result<Self, ParseError<'i>> { -> Result<Self, ParseError<'i>> {
use style_traits::PARSING_MODE_DEFAULT;
match input.next() { match input.next() {
Ok(Token::Dimension(ref value, ref unit)) if clamping_mode.is_ok(value.value) => { // Note that we generally pass ParserContext to is_ok() to check
// that the ParserMode of the ParserContext allows all numeric
// values for SMIL regardless of clamping_mode, but in this Time
// value case, the value does not animate for SMIL at all, so we use
// PARSING_MODE_DEFAULT directly.
Ok(Token::Dimension(ref value, ref unit)) if clamping_mode.is_ok(PARSING_MODE_DEFAULT, value.value) => {
Time::parse_dimension(value.value, &unit, /* from_calc = */ false) Time::parse_dimension(value.value, &unit, /* from_calc = */ false)
.map_err(|()| StyleParseError::UnspecifiedError.into()) .map_err(|()| StyleParseError::UnspecifiedError.into())
} }
Ok(Token::Function(ref name)) if name.eq_ignore_ascii_case("calc") => { Ok(Token::Function(ref name)) if name.eq_ignore_ascii_case("calc") => {
match input.parse_nested_block(|i| CalcNode::parse_time(context, i)) { match input.parse_nested_block(|i| CalcNode::parse_time(context, i)) {
Ok(time) if clamping_mode.is_ok(time.seconds) => Ok(time), Ok(time) if clamping_mode.is_ok(PARSING_MODE_DEFAULT, time.seconds) => Ok(time),
_ => Err(StyleParseError::UnspecifiedError.into()), _ => Err(StyleParseError::UnspecifiedError.into()),
} }
} }
@ -457,21 +464,13 @@ impl Number {
#[allow(missing_docs)] #[allow(missing_docs)]
pub fn parse_non_negative<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) pub fn parse_non_negative<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Number, ParseError<'i>> { -> Result<Number, ParseError<'i>> {
if context.parsing_mode.allows_all_numeric_values() { parse_number_with_clamping_mode(context, input, AllowedNumericType::NonNegative)
parse_number(context, input)
} else {
parse_number_with_clamping_mode(context, input, AllowedNumericType::NonNegative)
}
} }
#[allow(missing_docs)] #[allow(missing_docs)]
pub fn parse_at_least_one<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) pub fn parse_at_least_one<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Number, ParseError<'i>> { -> Result<Number, ParseError<'i>> {
if context.parsing_mode.allows_all_numeric_values() { parse_number_with_clamping_mode(context, input, AllowedNumericType::AtLeastOne)
parse_number(context, input)
} else {
parse_number_with_clamping_mode(context, input, AllowedNumericType::AtLeastOne)
}
} }
} }
@ -522,7 +521,7 @@ impl NumberOrPercentage {
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
type_: AllowedNumericType) type_: AllowedNumericType)
-> Result<Self, ParseError<'i>> { -> Result<Self, ParseError<'i>> {
if let Ok(per) = input.try(|i| Percentage::parse_with_clamping_mode(i, type_)) { if let Ok(per) = input.try(|i| Percentage::parse_with_clamping_mode(context, i, type_)) {
return Ok(NumberOrPercentage::Percentage(per)); return Ok(NumberOrPercentage::Percentage(per));
} }

View file

@ -265,7 +265,10 @@ pub mod specified {
impl AllowedNumericType { impl AllowedNumericType {
/// Whether the value fits the rules of this numeric type. /// Whether the value fits the rules of this numeric type.
#[inline] #[inline]
pub fn is_ok(&self, val: f32) -> bool { pub fn is_ok(&self, parsing_mode: ParsingMode, val: f32) -> bool {
if parsing_mode.allows_all_numeric_values() {
return true;
}
match *self { match *self {
AllowedNumericType::All => true, AllowedNumericType::All => true,
AllowedNumericType::NonNegative => val >= 0.0, AllowedNumericType::NonNegative => val >= 0.0,