diff --git a/components/style/parser.rs b/components/style/parser.rs index df8551bff3b..04e9204c987 100644 --- a/components/style/parser.rs +++ b/components/style/parser.rs @@ -113,6 +113,12 @@ impl<'a> ParserContext<'a> { } } + /// Whether we're in a @page rule. + #[inline] + pub fn in_page_rule(&self) -> bool { + self.rule_type.map_or(false, |rule_type| rule_type == CssRuleType::Page) + } + /// Get the rule type, which assumes that one is available. pub fn rule_type(&self) -> CssRuleType { self.rule_type.expect("Rule type expected, but none was found.") diff --git a/components/style/values/specified/length.rs b/components/style/values/specified/length.rs index 85c400e6472..0db823acd45 100644 --- a/components/style/values/specified/length.rs +++ b/components/style/values/specified/length.rs @@ -16,7 +16,6 @@ use std::cmp; use std::ops::{Add, Mul}; use style_traits::{ParseError, StyleParseErrorKind}; use style_traits::values::specified::AllowedNumericType; -use stylesheets::CssRuleType; use super::{AllowQuirks, Number, ToComputedValue, Percentage}; use values::{Auto, CSSFloat, Either, None_, Normal}; use values::computed::{self, CSSPixelLength, Context, ExtremumLength}; @@ -422,9 +421,11 @@ impl Mul for NoCalcLength { impl NoCalcLength { /// Parse a given absolute or relative dimension. - pub fn parse_dimension(context: &ParserContext, value: CSSFloat, unit: &str) - -> Result { - let in_page_rule = context.rule_type.map_or(false, |rule_type| rule_type == CssRuleType::Page); + pub fn parse_dimension( + context: &ParserContext, + value: CSSFloat, + unit: &str, + ) -> Result { match_ignore_ascii_case! { unit, "px" => Ok(NoCalcLength::Absolute(AbsoluteLength::Px(value))), "in" => Ok(NoCalcLength::Absolute(AbsoluteLength::In(value))), @@ -440,25 +441,25 @@ impl NoCalcLength { "rem" => Ok(NoCalcLength::FontRelative(FontRelativeLength::Rem(value))), // viewport percentages "vw" => { - if in_page_rule { + if context.in_page_rule() { return Err(()) } Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vw(value))) }, "vh" => { - if in_page_rule { + if context.in_page_rule() { return Err(()) } Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vh(value))) }, "vmin" => { - if in_page_rule { + if context.in_page_rule() { return Err(()) } Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vmin(value))) }, "vmax" => { - if in_page_rule { + if context.in_page_rule() { return Err(()) } Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vmax(value))) @@ -566,25 +567,21 @@ impl Length { Length::NoCalc(NoCalcLength::zero()) } - /// Parse a given absolute or relative dimension. - pub fn parse_dimension(context: &ParserContext, value: CSSFloat, unit: &str) - -> Result { - NoCalcLength::parse_dimension(context, value, unit).map(Length::NoCalc) - } - #[inline] - fn parse_internal<'i, 't>(context: &ParserContext, - input: &mut Parser<'i, 't>, - num_context: AllowedNumericType, - allow_quirks: AllowQuirks) - -> Result> { + fn parse_internal<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + num_context: AllowedNumericType, + allow_quirks: AllowQuirks, + ) -> Result> { // FIXME: remove early returns when lifetimes are non-lexical { let location = input.current_source_location(); let token = input.next()?; match *token { Token::Dimension { value, ref unit, .. } if num_context.is_ok(context.parsing_mode, value) => { - return Length::parse_dimension(context, value, unit) + return NoCalcLength::parse_dimension(context, value, unit) + .map(Length::NoCalc) .map_err(|()| location.new_unexpected_token_error(token.clone())) } Token::Number { value, .. } if num_context.is_ok(context.parsing_mode, value) => { @@ -759,12 +756,12 @@ impl LengthOrPercentage { LengthOrPercentage::Length(NoCalcLength::zero()) } - fn parse_internal<'i, 't>(context: &ParserContext, - input: &mut Parser<'i, 't>, - num_context: AllowedNumericType, - allow_quirks: AllowQuirks) - -> Result> - { + fn parse_internal<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + num_context: AllowedNumericType, + allow_quirks: AllowQuirks, + ) -> Result> { // FIXME: remove early returns when lifetimes are non-lexical { let location = input.current_source_location(); @@ -861,11 +858,12 @@ impl From for LengthOrPercentageOrAuto { } impl LengthOrPercentageOrAuto { - fn parse_internal<'i, 't>(context: &ParserContext, - input: &mut Parser<'i, 't>, - num_context: AllowedNumericType, - allow_quirks: AllowQuirks) - -> Result> { + fn parse_internal<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + num_context: AllowedNumericType, + allow_quirks: AllowQuirks, + ) -> Result> { // FIXME: remove early returns when lifetimes are non-lexical { let location = input.current_source_location(); @@ -968,12 +966,12 @@ pub enum LengthOrPercentageOrNone { } impl LengthOrPercentageOrNone { - fn parse_internal<'i, 't>(context: &ParserContext, - input: &mut Parser<'i, 't>, - num_context: AllowedNumericType, - allow_quirks: AllowQuirks) - -> Result> - { + fn parse_internal<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + num_context: AllowedNumericType, + allow_quirks: AllowQuirks, + ) -> Result> { // FIXME: remove early returns when lifetimes are non-lexical { let location = input.current_source_location(); diff --git a/components/style_traits/lib.rs b/components/style_traits/lib.rs index 3988d2fa446..3b7304b1aa7 100644 --- a/components/style_traits/lib.rs +++ b/components/style_traits/lib.rs @@ -213,11 +213,13 @@ bitflags! { impl ParsingMode { /// Whether the parsing mode allows unitless lengths for non-zero values to be intpreted as px. + #[inline] pub fn allows_unitless_lengths(&self) -> bool { self.intersects(ParsingMode::ALLOW_UNITLESS_LENGTH) } /// Whether the parsing mode allows all numeric values. + #[inline] pub fn allows_all_numeric_values(&self) -> bool { self.intersects(ParsingMode::ALLOW_ALL_NUMERIC_VALUES) }