style: Trivially cleanup length parsing.

Mostly formatting signatures properly, but also removing useless functions and
stuff.
This commit is contained in:
Emilio Cobos Álvarez 2018-02-16 15:58:13 +01:00
parent a6113af873
commit fdc8405330
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
3 changed files with 43 additions and 37 deletions

View file

@ -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. /// Get the rule type, which assumes that one is available.
pub fn rule_type(&self) -> CssRuleType { pub fn rule_type(&self) -> CssRuleType {
self.rule_type.expect("Rule type expected, but none was found.") self.rule_type.expect("Rule type expected, but none was found.")

View file

@ -16,7 +16,6 @@ use std::cmp;
use std::ops::{Add, Mul}; use std::ops::{Add, Mul};
use style_traits::{ParseError, StyleParseErrorKind}; use style_traits::{ParseError, StyleParseErrorKind};
use style_traits::values::specified::AllowedNumericType; use style_traits::values::specified::AllowedNumericType;
use stylesheets::CssRuleType;
use super::{AllowQuirks, Number, ToComputedValue, Percentage}; use super::{AllowQuirks, Number, ToComputedValue, Percentage};
use values::{Auto, CSSFloat, Either, None_, Normal}; use values::{Auto, CSSFloat, Either, None_, Normal};
use values::computed::{self, CSSPixelLength, Context, ExtremumLength}; use values::computed::{self, CSSPixelLength, Context, ExtremumLength};
@ -422,9 +421,11 @@ impl Mul<CSSFloat> for NoCalcLength {
impl NoCalcLength { impl NoCalcLength {
/// Parse a given absolute or relative dimension. /// Parse a given absolute or relative dimension.
pub fn parse_dimension(context: &ParserContext, value: CSSFloat, unit: &str) pub fn parse_dimension(
-> Result<NoCalcLength, ()> { context: &ParserContext,
let in_page_rule = context.rule_type.map_or(false, |rule_type| rule_type == CssRuleType::Page); value: CSSFloat,
unit: &str,
) -> Result<Self, ()> {
match_ignore_ascii_case! { unit, match_ignore_ascii_case! { unit,
"px" => Ok(NoCalcLength::Absolute(AbsoluteLength::Px(value))), "px" => Ok(NoCalcLength::Absolute(AbsoluteLength::Px(value))),
"in" => Ok(NoCalcLength::Absolute(AbsoluteLength::In(value))), "in" => Ok(NoCalcLength::Absolute(AbsoluteLength::In(value))),
@ -440,25 +441,25 @@ impl NoCalcLength {
"rem" => Ok(NoCalcLength::FontRelative(FontRelativeLength::Rem(value))), "rem" => Ok(NoCalcLength::FontRelative(FontRelativeLength::Rem(value))),
// viewport percentages // viewport percentages
"vw" => { "vw" => {
if in_page_rule { if context.in_page_rule() {
return Err(()) return Err(())
} }
Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vw(value))) Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vw(value)))
}, },
"vh" => { "vh" => {
if in_page_rule { if context.in_page_rule() {
return Err(()) return Err(())
} }
Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vh(value))) Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vh(value)))
}, },
"vmin" => { "vmin" => {
if in_page_rule { if context.in_page_rule() {
return Err(()) return Err(())
} }
Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vmin(value))) Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vmin(value)))
}, },
"vmax" => { "vmax" => {
if in_page_rule { if context.in_page_rule() {
return Err(()) return Err(())
} }
Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vmax(value))) Ok(NoCalcLength::ViewportPercentage(ViewportPercentageLength::Vmax(value)))
@ -566,25 +567,21 @@ impl Length {
Length::NoCalc(NoCalcLength::zero()) Length::NoCalc(NoCalcLength::zero())
} }
/// Parse a given absolute or relative dimension.
pub fn parse_dimension(context: &ParserContext, value: CSSFloat, unit: &str)
-> Result<Length, ()> {
NoCalcLength::parse_dimension(context, value, unit).map(Length::NoCalc)
}
#[inline] #[inline]
fn parse_internal<'i, 't>(context: &ParserContext, fn parse_internal<'i, 't>(
input: &mut Parser<'i, 't>, context: &ParserContext,
num_context: AllowedNumericType, input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) num_context: AllowedNumericType,
-> Result<Length, ParseError<'i>> { allow_quirks: AllowQuirks,
) -> Result<Self, ParseError<'i>> {
// FIXME: remove early returns when lifetimes are non-lexical // FIXME: remove early returns when lifetimes are non-lexical
{ {
let location = input.current_source_location(); let location = input.current_source_location();
let token = input.next()?; let token = input.next()?;
match *token { match *token {
Token::Dimension { value, ref unit, .. } if num_context.is_ok(context.parsing_mode, value) => { 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())) .map_err(|()| location.new_unexpected_token_error(token.clone()))
} }
Token::Number { value, .. } if num_context.is_ok(context.parsing_mode, value) => { Token::Number { value, .. } if num_context.is_ok(context.parsing_mode, value) => {
@ -759,12 +756,12 @@ impl LengthOrPercentage {
LengthOrPercentage::Length(NoCalcLength::zero()) LengthOrPercentage::Length(NoCalcLength::zero())
} }
fn parse_internal<'i, 't>(context: &ParserContext, fn parse_internal<'i, 't>(
input: &mut Parser<'i, 't>, context: &ParserContext,
num_context: AllowedNumericType, input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) num_context: AllowedNumericType,
-> Result<LengthOrPercentage, ParseError<'i>> allow_quirks: AllowQuirks,
{ ) -> Result<Self, ParseError<'i>> {
// FIXME: remove early returns when lifetimes are non-lexical // FIXME: remove early returns when lifetimes are non-lexical
{ {
let location = input.current_source_location(); let location = input.current_source_location();
@ -861,11 +858,12 @@ impl From<computed::Percentage> for LengthOrPercentageOrAuto {
} }
impl LengthOrPercentageOrAuto { impl LengthOrPercentageOrAuto {
fn parse_internal<'i, 't>(context: &ParserContext, fn parse_internal<'i, 't>(
input: &mut Parser<'i, 't>, context: &ParserContext,
num_context: AllowedNumericType, input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) num_context: AllowedNumericType,
-> Result<Self, ParseError<'i>> { allow_quirks: AllowQuirks,
) -> Result<Self, ParseError<'i>> {
// FIXME: remove early returns when lifetimes are non-lexical // FIXME: remove early returns when lifetimes are non-lexical
{ {
let location = input.current_source_location(); let location = input.current_source_location();
@ -968,12 +966,12 @@ pub enum LengthOrPercentageOrNone {
} }
impl LengthOrPercentageOrNone { impl LengthOrPercentageOrNone {
fn parse_internal<'i, 't>(context: &ParserContext, fn parse_internal<'i, 't>(
input: &mut Parser<'i, 't>, context: &ParserContext,
num_context: AllowedNumericType, input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks) num_context: AllowedNumericType,
-> Result<LengthOrPercentageOrNone, ParseError<'i>> allow_quirks: AllowQuirks,
{ ) -> Result<Self, ParseError<'i>> {
// FIXME: remove early returns when lifetimes are non-lexical // FIXME: remove early returns when lifetimes are non-lexical
{ {
let location = input.current_source_location(); let location = input.current_source_location();

View file

@ -213,11 +213,13 @@ bitflags! {
impl ParsingMode { impl ParsingMode {
/// Whether the parsing mode allows unitless lengths for non-zero values to be intpreted as px. /// Whether the parsing mode allows unitless lengths for non-zero values to be intpreted as px.
#[inline]
pub fn allows_unitless_lengths(&self) -> bool { pub fn allows_unitless_lengths(&self) -> bool {
self.intersects(ParsingMode::ALLOW_UNITLESS_LENGTH) self.intersects(ParsingMode::ALLOW_UNITLESS_LENGTH)
} }
/// Whether the parsing mode allows all numeric values. /// Whether the parsing mode allows all numeric values.
#[inline]
pub fn allows_all_numeric_values(&self) -> bool { pub fn allows_all_numeric_values(&self) -> bool {
self.intersects(ParsingMode::ALLOW_ALL_NUMERIC_VALUES) self.intersects(ParsingMode::ALLOW_ALL_NUMERIC_VALUES)
} }