mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
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:
parent
7341574b66
commit
6c5915068a
3 changed files with 27 additions and 22 deletions
|
@ -715,11 +715,12 @@ impl ToCss for Percentage {
|
|||
|
||||
impl Percentage {
|
||||
/// Parse a specific kind of percentage.
|
||||
pub fn parse_with_clamping_mode<'i, 't>(input: &mut Parser<'i, 't>,
|
||||
context: AllowedNumericType)
|
||||
pub fn parse_with_clamping_mode<'i, 't>(context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
num_context: AllowedNumericType)
|
||||
-> Result<Self, ParseError<'i>> {
|
||||
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))
|
||||
}
|
||||
t => Err(BasicParseError::UnexpectedToken(t).into())
|
||||
|
@ -727,8 +728,10 @@ impl Percentage {
|
|||
}
|
||||
|
||||
/// 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>> {
|
||||
Self::parse_with_clamping_mode(input, AllowedNumericType::NonNegative)
|
||||
pub fn parse_non_negative<'i, 't>(context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>)
|
||||
-> Result<Self, ParseError<'i>> {
|
||||
Self::parse_with_clamping_mode(context, input, AllowedNumericType::NonNegative)
|
||||
}
|
||||
|
||||
/// 0%
|
||||
|
@ -746,8 +749,8 @@ impl Percentage {
|
|||
|
||||
impl Parse for Percentage {
|
||||
#[inline]
|
||||
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
|
||||
Self::parse_with_clamping_mode(input, AllowedNumericType::All)
|
||||
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
|
||||
Self::parse_with_clamping_mode(context, input, AllowedNumericType::All)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -120,7 +120,7 @@ pub fn parse_number_with_clamping_mode<'i, 't>(context: &ParserContext,
|
|||
clamping_mode: AllowedNumericType)
|
||||
-> Result<Number, ParseError<'i>> {
|
||||
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 {
|
||||
value: value.value.min(f32::MAX).max(f32::MIN),
|
||||
calc_clamping_mode: None,
|
||||
|
@ -364,14 +364,21 @@ impl Time {
|
|||
input: &mut Parser<'i, 't>,
|
||||
clamping_mode: AllowedNumericType)
|
||||
-> Result<Self, ParseError<'i>> {
|
||||
use style_traits::PARSING_MODE_DEFAULT;
|
||||
|
||||
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)
|
||||
.map_err(|()| StyleParseError::UnspecifiedError.into())
|
||||
}
|
||||
Ok(Token::Function(ref name)) if name.eq_ignore_ascii_case("calc") => {
|
||||
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()),
|
||||
}
|
||||
}
|
||||
|
@ -457,23 +464,15 @@ impl Number {
|
|||
#[allow(missing_docs)]
|
||||
pub fn parse_non_negative<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
|
||||
-> Result<Number, ParseError<'i>> {
|
||||
if context.parsing_mode.allows_all_numeric_values() {
|
||||
parse_number(context, input)
|
||||
} else {
|
||||
parse_number_with_clamping_mode(context, input, AllowedNumericType::NonNegative)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub fn parse_at_least_one<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
|
||||
-> Result<Number, ParseError<'i>> {
|
||||
if context.parsing_mode.allows_all_numeric_values() {
|
||||
parse_number(context, input)
|
||||
} else {
|
||||
parse_number_with_clamping_mode(context, input, AllowedNumericType::AtLeastOne)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToComputedValue for Number {
|
||||
type ComputedValue = CSSFloat;
|
||||
|
@ -522,7 +521,7 @@ impl NumberOrPercentage {
|
|||
input: &mut Parser<'i, 't>,
|
||||
type_: AllowedNumericType)
|
||||
-> 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));
|
||||
}
|
||||
|
||||
|
|
|
@ -265,7 +265,10 @@ pub mod specified {
|
|||
impl AllowedNumericType {
|
||||
/// Whether the value fits the rules of this numeric type.
|
||||
#[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 {
|
||||
AllowedNumericType::All => true,
|
||||
AllowedNumericType::NonNegative => val >= 0.0,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue