style: Multiple style nits across parsing code.

This commit is contained in:
Emilio Cobos Álvarez 2017-04-06 13:24:38 +02:00
parent 726f918b46
commit 705ecb4557
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
5 changed files with 98 additions and 85 deletions

View file

@ -659,17 +659,19 @@ ${helpers.single_keyword("font-variant-caps",
/// <length> | <percentage> | <absolute-size> | <relative-size>
pub fn parse(_: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
if let Ok(lop) = input.try(specified::LengthOrPercentage::parse_non_negative) {
Ok(SpecifiedValue::Length(lop))
} else if let Ok(kw) = input.try(KeywordSize::parse) {
Ok(SpecifiedValue::Keyword(kw))
} else {
return Ok(SpecifiedValue::Length(lop))
}
if let Ok(kw) = input.try(KeywordSize::parse) {
return Ok(SpecifiedValue::Keyword(kw))
}
match_ignore_ascii_case! {&*input.expect_ident()?,
"smaller" => Ok(SpecifiedValue::Smaller),
"larger" => Ok(SpecifiedValue::Larger),
_ => Err(())
}
}
}
</%helpers:longhand>
<%helpers:longhand products="gecko" name="font-size-adjust" animatable="True"

View file

@ -45,18 +45,22 @@
}
}
/// normal | <number> | <length> | <percentage>
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
pub fn parse(_: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
use cssparser::Token;
use std::ascii::AsciiExt;
// We try to parse as a Number first because, for 'line-height', we want "0" to be
// parsed as a plain Number rather than a Length (0px); this matches the behaviour
// of all major browsers
input.try(specified::Number::parse_non_negative)
.map(SpecifiedValue::Number)
.or_else(|()| {
input.try(specified::LengthOrPercentage::parse_non_negative)
.map(SpecifiedValue::LengthOrPercentage)
.or_else(|()| {
// We try to parse as a Number first because, for 'line-height', we want
// "0" to be parsed as a plain Number rather than a Length (0px); this
// matches the behaviour of all major browsers
if let Ok(number) = input.try(specified::Number::parse_non_negative) {
return Ok(SpecifiedValue::Number(number))
}
if let Ok(lop) = input.try(specified::LengthOrPercentage::parse_non_negative) {
return Ok(SpecifiedValue::LengthOrPercentage(lop))
}
match try!(input.next()) {
Token::Ident(ref value) if value.eq_ignore_ascii_case("normal") => {
Ok(SpecifiedValue::Normal)
@ -68,8 +72,6 @@
% endif
_ => Err(()),
}
})
})
}
pub mod computed_value {
use app_units::Au;

View file

@ -56,7 +56,7 @@
use values::specified::LengthOrPercentageOrAutoOrContent;
% endif
pub fn parse_flexibility(input: &mut Parser)
fn parse_flexibility(input: &mut Parser)
-> Result<(Number, Option<Number>),()> {
let grow = try!(Number::parse_non_negative(input));
let shrink = input.try(Number::parse_non_negative).ok();

View file

@ -6,6 +6,7 @@
use cssparser::{Parser, Token};
use parser::{Parse, ParserContext};
use std::ascii::AsciiExt;
use std::fmt;
use style_traits::ToCss;
use values::{CSSFloat, HasViewportPercentage};
@ -130,25 +131,25 @@ pub enum TrackBreadth<L> {
/// Parse a single flexible length.
pub fn parse_flex(input: &mut Parser) -> Result<CSSFloat, ()> {
match try!(input.next()) {
Token::Dimension(ref value, ref unit) if unit.to_lowercase() == "fr" && value.value.is_sign_positive()
Token::Dimension(ref value, ref unit) if unit.eq_ignore_ascii_case("fr") && value.value.is_sign_positive()
=> Ok(value.value),
_ => Err(()),
}
}
impl Parse for TrackBreadth<LengthOrPercentage> {
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
fn parse(_: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
if let Ok(lop) = input.try(LengthOrPercentage::parse_non_negative) {
Ok(TrackBreadth::Breadth(lop))
} else {
return Ok(TrackBreadth::Breadth(lop))
}
if let Ok(f) = input.try(parse_flex) {
Ok(TrackBreadth::Flex(f))
} else {
return Ok(TrackBreadth::Flex(f))
}
TrackKeyword::parse(input).map(TrackBreadth::Keyword)
}
}
}
}
impl<L: ToCss> ToCss for TrackBreadth<L> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
@ -223,26 +224,30 @@ impl<L> Default for TrackSize<L> {
impl Parse for TrackSize<LengthOrPercentage> {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
if let Ok(b) = input.try(|i| TrackBreadth::parse(context, i)) {
Ok(TrackSize::Breadth(b))
} else {
return Ok(TrackSize::Breadth(b))
}
if input.try(|i| i.expect_function_matching("minmax")).is_ok() {
Ok(try!(input.parse_nested_block(|input| {
let inflexible_breadth = if let Ok(lop) = input.try(LengthOrPercentage::parse_non_negative) {
Ok(TrackBreadth::Breadth(lop))
} else {
TrackKeyword::parse(input).map(TrackBreadth::Keyword)
return input.parse_nested_block(|input| {
let inflexible_breadth =
match input.try(LengthOrPercentage::parse_non_negative) {
Ok(lop) => TrackBreadth::Breadth(lop),
Err(..) => {
let keyword = try!(TrackKeyword::parse(input));
TrackBreadth::Keyword(keyword)
}
};
try!(input.expect_comma());
Ok(TrackSize::MinMax(try!(inflexible_breadth), try!(TrackBreadth::parse(context, input))))
})))
} else {
Ok(TrackSize::MinMax(inflexible_breadth, try!(TrackBreadth::parse(context, input))))
});
}
try!(input.expect_function_matching("fit-content"));
// FIXME(emilio): This needs a parse_nested_block, doesn't it?
Ok(try!(LengthOrPercentage::parse(context, input).map(TrackSize::FitContent)))
}
}
}
}
impl<L: ToCss> ToCss for TrackSize<L> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {

View file

@ -442,7 +442,7 @@ impl Length {
/// Parse a non-negative length
#[inline]
pub fn parse_non_negative(input: &mut Parser) -> Result<Length, ()> {
Length::parse_internal(input, AllowedNumericType::NonNegative)
Self::parse_internal(input, AllowedNumericType::NonNegative)
}
/// Get an absolute length from a px value.
@ -462,7 +462,7 @@ impl Length {
impl Parse for Length {
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
Length::parse_internal(input, AllowedNumericType::All)
Self::parse_internal(input, AllowedNumericType::All)
}
}
@ -1049,7 +1049,7 @@ impl LengthOrPercentage {
/// Parse a non-negative length.
#[inline]
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentage, ()> {
LengthOrPercentage::parse_internal(input, AllowedNumericType::NonNegative)
Self::parse_internal(input, AllowedNumericType::NonNegative)
}
/// Parse a length, treating dimensionless numbers as pixels
@ -1057,20 +1057,25 @@ impl LengthOrPercentage {
/// https://www.w3.org/TR/SVG2/types.html#presentation-attribute-css-value
pub fn parse_numbers_are_pixels(input: &mut Parser) -> Result<LengthOrPercentage, ()> {
if let Ok(lop) = input.try(|i| Self::parse_internal(i, AllowedNumericType::All)) {
Ok(lop)
} else {
return Ok(lop)
}
// TODO(emilio): Probably should use Number::parse_non_negative to
// handle calc()?
let num = input.expect_number()?;
Ok(LengthOrPercentage::Length(NoCalcLength::Absolute(Au((AU_PER_PX * num) as i32))))
}
}
/// Parse a non-negative length, treating dimensionless numbers as pixels
///
/// This is nonstandard behavior used by Firefox for SVG
pub fn parse_numbers_are_pixels_non_negative(input: &mut Parser) -> Result<LengthOrPercentage, ()> {
if let Ok(lop) = input.try(|i| Self::parse_internal(i, AllowedNumericType::NonNegative)) {
Ok(lop)
} else {
return Ok(lop)
}
// TODO(emilio): Probably should use Number::parse_non_negative to
// handle calc()?
let num = input.expect_number()?;
if num >= 0. {
Ok(LengthOrPercentage::Length(NoCalcLength::Absolute(Au((AU_PER_PX * num) as i32))))
@ -1078,7 +1083,6 @@ impl LengthOrPercentage {
Err(())
}
}
}
/// Extract value from ref without a clone, replacing it with a 0 Au
///
@ -1092,7 +1096,7 @@ impl LengthOrPercentage {
impl Parse for LengthOrPercentage {
#[inline]
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
LengthOrPercentage::parse_internal(input, AllowedNumericType::All)
Self::parse_internal(input, AllowedNumericType::All)
}
}
@ -1168,7 +1172,7 @@ impl LengthOrPercentageOrAuto {
/// Parse a non-negative length, percentage, or auto.
#[inline]
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentageOrAuto, ()> {
LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::NonNegative)
Self::parse_internal(input, AllowedNumericType::NonNegative)
}
/// Parse a non-negative length, percentage, or auto.
@ -1183,7 +1187,7 @@ impl LengthOrPercentageOrAuto {
impl Parse for LengthOrPercentageOrAuto {
#[inline]
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::All)
Self::parse_internal(input, AllowedNumericType::All)
}
}
@ -1241,15 +1245,15 @@ impl LengthOrPercentageOrNone {
}
/// Parse a non-negative LengthOrPercentageOrNone.
#[inline]
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentageOrNone, ()> {
LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::NonNegative)
pub fn parse_non_negative(input: &mut Parser) -> Result<Self, ()> {
Self::parse_internal(input, AllowedNumericType::NonNegative)
}
}
impl Parse for LengthOrPercentageOrNone {
#[inline]
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::All)
Self::parse_internal(input, AllowedNumericType::All)
}
}
@ -1341,15 +1345,15 @@ pub type LengthOrNumber = Either<Length, Number>;
impl LengthOrNumber {
/// Parse a non-negative LengthOrNumber.
pub fn parse_non_negative(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
// We try to parse as a Number first because, for cases like LengthOrNumber,
// we want "0" to be parsed as a plain Number rather than a Length (0px); this
// matches the behaviour of all major browsers
pub fn parse_non_negative(_: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
// We try to parse as a Number first because, for cases like
// LengthOrNumber, we want "0" to be parsed as a plain Number rather
// than a Length (0px); this matches the behaviour of all major browsers
if let Ok(v) = input.try(Number::parse_non_negative) {
Ok(Either::Second(v))
} else {
Length::parse_non_negative(input).map(Either::First)
return Ok(Either::Second(v))
}
Length::parse_non_negative(input).map(Either::First)
}
}