mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
style: Multiple style nits across parsing code.
This commit is contained in:
parent
726f918b46
commit
705ecb4557
5 changed files with 98 additions and 85 deletions
|
@ -659,15 +659,17 @@ ${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 {
|
||||
match_ignore_ascii_case! {&*input.expect_ident()?,
|
||||
"smaller" => Ok(SpecifiedValue::Smaller),
|
||||
"larger" => Ok(SpecifiedValue::Larger),
|
||||
_ => Err(())
|
||||
}
|
||||
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>
|
||||
|
|
|
@ -45,31 +45,33 @@
|
|||
}
|
||||
}
|
||||
/// 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(|()| {
|
||||
match try!(input.next()) {
|
||||
Token::Ident(ref value) if value.eq_ignore_ascii_case("normal") => {
|
||||
Ok(SpecifiedValue::Normal)
|
||||
}
|
||||
% if product == "gecko":
|
||||
Token::Ident(ref value) if value.eq_ignore_ascii_case("-moz-block-height") => {
|
||||
Ok(SpecifiedValue::MozBlockHeight)
|
||||
}
|
||||
% endif
|
||||
_ => Err(()),
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// 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)
|
||||
}
|
||||
% if product == "gecko":
|
||||
Token::Ident(ref value) if value.eq_ignore_ascii_case("-moz-block-height") => {
|
||||
Ok(SpecifiedValue::MozBlockHeight)
|
||||
}
|
||||
% endif
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
pub mod computed_value {
|
||||
use app_units::Au;
|
||||
|
|
|
@ -56,8 +56,8 @@
|
|||
use values::specified::LengthOrPercentageOrAutoOrContent;
|
||||
% endif
|
||||
|
||||
pub fn parse_flexibility(input: &mut Parser)
|
||||
-> Result<(Number, Option<Number>),()> {
|
||||
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();
|
||||
Ok((grow, shrink))
|
||||
|
|
|
@ -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,23 +131,23 @@ 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 {
|
||||
if let Ok(f) = input.try(parse_flex) {
|
||||
Ok(TrackBreadth::Flex(f))
|
||||
} else {
|
||||
TrackKeyword::parse(input).map(TrackBreadth::Keyword)
|
||||
}
|
||||
return Ok(TrackBreadth::Breadth(lop))
|
||||
}
|
||||
|
||||
if let Ok(f) = input.try(parse_flex) {
|
||||
return Ok(TrackBreadth::Flex(f))
|
||||
}
|
||||
|
||||
TrackKeyword::parse(input).map(TrackBreadth::Keyword)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -223,24 +224,28 @@ 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 {
|
||||
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 Ok(TrackSize::Breadth(b))
|
||||
}
|
||||
|
||||
if input.try(|i| i.expect_function_matching("minmax")).is_ok() {
|
||||
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 {
|
||||
try!(input.expect_function_matching("fit-content"));
|
||||
Ok(try!(LengthOrPercentage::parse(context, input).map(TrackSize::FitContent)))
|
||||
}
|
||||
try!(input.expect_comma());
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,11 +1057,13 @@ 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 {
|
||||
let num = input.expect_number()?;
|
||||
Ok(LengthOrPercentage::Length(NoCalcLength::Absolute(Au((AU_PER_PX * num) as i32))))
|
||||
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
|
||||
|
@ -1069,14 +1071,16 @@ impl LengthOrPercentage {
|
|||
/// 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)
|
||||
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))))
|
||||
} else {
|
||||
let num = input.expect_number()?;
|
||||
if num >= 0. {
|
||||
Ok(LengthOrPercentage::Length(NoCalcLength::Absolute(Au((AU_PER_PX * num) as i32))))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue