Fix related to #14101

Add Parse trait implementation for some structures
This commit is contained in:
Artem Biryukov 2016-11-10 17:11:33 +03:00
parent 4b9693cf81
commit 9564673b5a
14 changed files with 149 additions and 114 deletions

View file

@ -8,6 +8,7 @@
use Atom; use Atom;
use cssparser::{Delimiter, Parser, SourcePosition, Token, TokenSerializationType}; use cssparser::{Delimiter, Parser, SourcePosition, Token, TokenSerializationType};
use parser::Parse;
use properties::DeclaredValue; use properties::DeclaredValue;
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use std::borrow::Cow; use std::borrow::Cow;
@ -111,7 +112,8 @@ impl ComputedValue {
} }
} }
pub fn parse(input: &mut Parser) -> Result<SpecifiedValue, ()> { impl Parse for SpecifiedValue {
fn parse(input: &mut Parser) -> Result<Self, ()> {
let mut references = Some(HashSet::new()); let mut references = Some(HashSet::new());
let (first, css, last) = try!(parse_self_contained_declaration_value(input, &mut references)); let (first, css, last) = try!(parse_self_contained_declaration_value(input, &mut references));
Ok(SpecifiedValue { Ok(SpecifiedValue {
@ -121,6 +123,7 @@ pub fn parse(input: &mut Parser) -> Result<SpecifiedValue, ()> {
references: references.unwrap(), references: references.unwrap(),
}) })
} }
}
/// Parse the value of a non-custom property that contains `var()` references. /// Parse the value of a non-custom property that contains `var()` references.
pub fn parse_non_custom_with_var<'i, 't> pub fn parse_non_custom_with_var<'i, 't>

View file

@ -67,7 +67,7 @@
pub mod single_value { pub mod single_value {
use cssparser::Parser; use cssparser::Parser;
use parser::{ParserContext, ParserContextExtraData}; use parser::{Parse, ParserContext, ParserContextExtraData};
use properties::{CSSWideKeyword, DeclaredValue, Shorthand}; use properties::{CSSWideKeyword, DeclaredValue, Shorthand};
use values::computed::{Context, ToComputedValue}; use values::computed::{Context, ToComputedValue};
use values::{computed, specified}; use values::{computed, specified};
@ -175,13 +175,12 @@
#![allow(unused_imports)] #![allow(unused_imports)]
% if not property.derived_from: % if not property.derived_from:
use cssparser::Parser; use cssparser::Parser;
use parser::{ParserContext, ParserContextExtraData}; use parser::{Parse, ParserContext, ParserContextExtraData};
use properties::{CSSWideKeyword, DeclaredValue, Shorthand}; use properties::{CSSWideKeyword, DeclaredValue, Shorthand};
% endif % endif
use values::{Auto, Either, None_, Normal}; use values::{Auto, Either, None_, Normal};
use cascade_info::CascadeInfo; use cascade_info::CascadeInfo;
use error_reporting::ParseErrorReporter; use error_reporting::ParseErrorReporter;
use parser::Parse;
use properties::longhands; use properties::longhands;
use properties::property_bit_field::PropertyBitField; use properties::property_bit_field::PropertyBitField;
use properties::{ComputedValues, PropertyDeclaration}; use properties::{ComputedValues, PropertyDeclaration};

View file

@ -15,7 +15,6 @@ ${helpers.predefined_type("opacity",
<%helpers:vector_longhand name="box-shadow" allow_empty="True" animatable="True"> <%helpers:vector_longhand name="box-shadow" allow_empty="True" animatable="True">
use cssparser; use cssparser;
use std::fmt; use std::fmt;
use parser::Parse;
use style_traits::ToCss; use style_traits::ToCss;
use values::HasViewportPercentage; use values::HasViewportPercentage;
@ -355,7 +354,6 @@ ${helpers.predefined_type("opacity",
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> { pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
use app_units::Au; use app_units::Au;
use parser::Parse;
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use values::specified::Length; use values::specified::Length;
@ -677,7 +675,6 @@ pub struct OriginParseResult {
} }
pub fn parse_origin(_: &ParserContext, input: &mut Parser) -> Result<OriginParseResult,()> { pub fn parse_origin(_: &ParserContext, input: &mut Parser) -> Result<OriginParseResult,()> {
use parser::Parse;
use values::specified::{LengthOrPercentage, Percentage}; use values::specified::{LengthOrPercentage, Percentage};
let (mut horizontal, mut vertical, mut depth) = (None, None, None); let (mut horizontal, mut vertical, mut depth) = (None, None, None);
loop { loop {

View file

@ -496,6 +496,7 @@ ${helpers.single_keyword("font-variant-position",
pub mod computed_value { pub mod computed_value {
use cssparser::Parser; use cssparser::Parser;
use parser::Parse;
use std::fmt; use std::fmt;
use style_traits::ToCss; use style_traits::ToCss;
@ -542,10 +543,10 @@ ${helpers.single_keyword("font-variant-position",
} }
} }
impl FeatureTagValue { impl Parse for FeatureTagValue {
/// https://www.w3.org/TR/css-fonts-3/#propdef-font-feature-settings /// https://www.w3.org/TR/css-fonts-3/#propdef-font-feature-settings
/// <string> [ on | off | <integer> ] /// <string> [ on | off | <integer> ]
pub fn parse(input: &mut Parser) -> Result<FeatureTagValue, ()> { fn parse(input: &mut Parser) -> Result<Self, ()> {
let tag = try!(input.expect_string()); let tag = try!(input.expect_string());
// allowed strings of length 4 containing chars: <U+20, U+7E> // allowed strings of length 4 containing chars: <U+20, U+7E>

View file

@ -27,7 +27,7 @@ use euclid::size::Size2D;
use computed_values; use computed_values;
#[cfg(feature = "servo")] use logical_geometry::{LogicalMargin, PhysicalSide}; #[cfg(feature = "servo")] use logical_geometry::{LogicalMargin, PhysicalSide};
use logical_geometry::WritingMode; use logical_geometry::WritingMode;
use parser::{ParserContext, ParserContextExtraData}; use parser::{Parse, ParserContext, ParserContextExtraData};
use style_traits::ToCss; use style_traits::ToCss;
use stylesheets::Origin; use stylesheets::Origin;
#[cfg(feature = "servo")] use values::Either; #[cfg(feature = "servo")] use values::Either;
@ -49,7 +49,7 @@ pub mod declaration_block;
pub mod longhands { pub mod longhands {
use cssparser::Parser; use cssparser::Parser;
use parser::ParserContext; use parser::{Parse, ParserContext};
use values::specified; use values::specified;
<%include file="/longhand/background.mako.rs" /> <%include file="/longhand/background.mako.rs" />
@ -79,7 +79,7 @@ pub mod longhands {
pub mod shorthands { pub mod shorthands {
use cssparser::Parser; use cssparser::Parser;
use parser::ParserContext; use parser::{Parse, ParserContext};
use values::specified; use values::specified;
pub fn parse_four_sides<F, T>(input: &mut Parser, parse_one: F) -> Result<(T, T, T, T), ()> pub fn parse_four_sides<F, T>(input: &mut Parser, parse_one: F) -> Result<(T, T, T, T), ()>
@ -344,8 +344,8 @@ pub enum CSSWideKeyword {
UnsetKeyword, UnsetKeyword,
} }
impl CSSWideKeyword { impl Parse for CSSWideKeyword {
pub fn parse(input: &mut Parser) -> Result<CSSWideKeyword, ()> { fn parse(input: &mut Parser) -> Result<Self, ()> {
match_ignore_ascii_case! { try!(input.expect_ident()), match_ignore_ascii_case! { try!(input.expect_ident()),
"initial" => Ok(CSSWideKeyword::InitialKeyword), "initial" => Ok(CSSWideKeyword::InitialKeyword),
"inherit" => Ok(CSSWideKeyword::InheritKeyword), "inherit" => Ok(CSSWideKeyword::InheritKeyword),
@ -736,7 +736,7 @@ impl PropertyDeclaration {
Ok(CSSWideKeyword::UnsetKeyword) | // Custom properties are alawys inherited Ok(CSSWideKeyword::UnsetKeyword) | // Custom properties are alawys inherited
Ok(CSSWideKeyword::InheritKeyword) => DeclaredValue::Inherit, Ok(CSSWideKeyword::InheritKeyword) => DeclaredValue::Inherit,
Ok(CSSWideKeyword::InitialKeyword) => DeclaredValue::Initial, Ok(CSSWideKeyword::InitialKeyword) => DeclaredValue::Initial,
Err(()) => match ::custom_properties::parse(input) { Err(()) => match ::custom_properties::SpecifiedValue::parse(input) {
Ok(value) => DeclaredValue::Value(value), Ok(value) => DeclaredValue::Value(value),
Err(()) => return PropertyDeclarationParseResult::InvalidValue, Err(()) => return PropertyDeclarationParseResult::InvalidValue,
} }

View file

@ -149,6 +149,7 @@ pub fn parse_border(context: &ParserContext, input: &mut Parser)
for corner in ['top-left', 'top-right', 'bottom-right', 'bottom-left'] for corner in ['top-left', 'top-right', 'bottom-right', 'bottom-left']
)}"> )}">
use values::specified::basic_shape::BorderRadius; use values::specified::basic_shape::BorderRadius;
use parser::Parse;
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> { pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let _ignored = context; let _ignored = context;

View file

@ -7,6 +7,7 @@
<%helpers:shorthand name="outline" sub_properties="outline-color outline-style outline-width"> <%helpers:shorthand name="outline" sub_properties="outline-color outline-style outline-width">
use properties::longhands::outline_width; use properties::longhands::outline_width;
use values::specified; use values::specified;
use parser::Parse;
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> { pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let _unused = context; let _unused = context;

View file

@ -56,6 +56,7 @@
// https://drafts.csswg.org/css-flexbox/#flex-property // https://drafts.csswg.org/css-flexbox/#flex-property
<%helpers:shorthand name="flex" sub_properties="flex-grow flex-shrink flex-basis"> <%helpers:shorthand name="flex" sub_properties="flex-grow flex-shrink flex-basis">
use parser::Parse;
use app_units::Au; use app_units::Au;
use values::specified::{Number, Length, LengthOrPercentageOrAutoOrContent}; use values::specified::{Number, Length, LengthOrPercentageOrAutoOrContent};

View file

@ -7,7 +7,6 @@
//! [values]: https://drafts.csswg.org/css-values/ //! [values]: https://drafts.csswg.org/css-values/
pub use cssparser::{RGBA, Parser}; pub use cssparser::{RGBA, Parser};
use parser::Parse; use parser::Parse;
use std::fmt::{self, Debug}; use std::fmt::{self, Debug};
use style_traits::ToCss; use style_traits::ToCss;

View file

@ -213,14 +213,6 @@ pub struct InsetRect {
} }
impl InsetRect { impl InsetRect {
pub fn parse(input: &mut Parser) -> Result<InsetRect, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"inset" => {
Ok(try!(input.parse_nested_block(InsetRect::parse_function_arguments)))
},
_ => Err(())
}
}
pub fn parse_function_arguments(input: &mut Parser) -> Result<InsetRect, ()> { pub fn parse_function_arguments(input: &mut Parser) -> Result<InsetRect, ()> {
let (t, r, b, l) = try!(parse_four_sides(input, LengthOrPercentage::parse)); let (t, r, b, l) = try!(parse_four_sides(input, LengthOrPercentage::parse));
let mut rect = InsetRect { let mut rect = InsetRect {
@ -237,6 +229,17 @@ impl InsetRect {
} }
} }
impl Parse for InsetRect {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"inset" => {
Ok(try!(input.parse_nested_block(InsetRect::parse_function_arguments)))
},
_ => Err(())
}
}
}
impl ToCss for InsetRect { impl ToCss for InsetRect {
// XXXManishearth again, we should try to reduce the number of values printed here // XXXManishearth again, we should try to reduce the number of values printed here
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
@ -374,14 +377,6 @@ pub struct Circle {
} }
impl Circle { impl Circle {
pub fn parse(input: &mut Parser) -> Result<Circle, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"circle" => {
Ok(try!(input.parse_nested_block(Circle::parse_function_arguments)))
},
_ => Err(())
}
}
pub fn parse_function_arguments(input: &mut Parser) -> Result<Circle, ()> { pub fn parse_function_arguments(input: &mut Parser) -> Result<Circle, ()> {
let radius = input.try(ShapeRadius::parse).ok().unwrap_or_else(Default::default); let radius = input.try(ShapeRadius::parse).ok().unwrap_or_else(Default::default);
let position = if let Ok(_) = input.try(|input| input.expect_ident_matching("at")) { let position = if let Ok(_) = input.try(|input| input.expect_ident_matching("at")) {
@ -402,6 +397,17 @@ impl Circle {
} }
} }
impl Parse for Circle {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"circle" => {
Ok(try!(input.parse_nested_block(Circle::parse_function_arguments)))
},
_ => Err(())
}
}
}
impl ToCss for Circle { impl ToCss for Circle {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("circle(")); try!(dest.write_str("circle("));
@ -446,14 +452,6 @@ pub struct Ellipse {
impl Ellipse { impl Ellipse {
pub fn parse(input: &mut Parser) -> Result<Ellipse, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"ellipse" => {
Ok(try!(input.parse_nested_block(Ellipse::parse_function_arguments)))
},
_ => Err(())
}
}
pub fn parse_function_arguments(input: &mut Parser) -> Result<Ellipse, ()> { pub fn parse_function_arguments(input: &mut Parser) -> Result<Ellipse, ()> {
let (a, b) = input.try(|input| -> Result<_, ()> { let (a, b) = input.try(|input| -> Result<_, ()> {
Ok((try!(ShapeRadius::parse(input)), try!(ShapeRadius::parse(input)))) Ok((try!(ShapeRadius::parse(input)), try!(ShapeRadius::parse(input))))
@ -477,6 +475,17 @@ impl Ellipse {
} }
} }
impl Parse for Ellipse {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"ellipse" => {
Ok(try!(input.parse_nested_block(Ellipse::parse_function_arguments)))
},
_ => Err(())
}
}
}
impl ToCss for Ellipse { impl ToCss for Ellipse {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("ellipse(")); try!(dest.write_str("ellipse("));
@ -524,14 +533,6 @@ pub struct Polygon {
} }
impl Polygon { impl Polygon {
pub fn parse(input: &mut Parser) -> Result<Polygon, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"polygon" => {
Ok(try!(input.parse_nested_block(Polygon::parse_function_arguments)))
},
_ => Err(())
}
}
pub fn parse_function_arguments(input: &mut Parser) -> Result<Polygon, ()> { pub fn parse_function_arguments(input: &mut Parser) -> Result<Polygon, ()> {
let fill = input.try(|input| { let fill = input.try(|input| {
let fill = FillRule::parse(input); let fill = FillRule::parse(input);
@ -550,6 +551,17 @@ impl Polygon {
} }
} }
impl Parse for Polygon {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match_ignore_ascii_case! { try!(input.expect_function()),
"polygon" => {
Ok(try!(input.parse_nested_block(Polygon::parse_function_arguments)))
},
_ => Err(())
}
}
}
impl ToCss for Polygon { impl ToCss for Polygon {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("polygon(")); try!(dest.write_str("polygon("));
@ -616,8 +628,8 @@ impl Default for ShapeRadius {
} }
} }
impl ShapeRadius { impl Parse for ShapeRadius {
pub fn parse(input: &mut Parser) -> Result<ShapeRadius, ()> { fn parse(input: &mut Parser) -> Result<Self, ()> {
input.try(LengthOrPercentage::parse).map(ShapeRadius::Length) input.try(LengthOrPercentage::parse).map(ShapeRadius::Length)
.or_else(|_| { .or_else(|_| {
match_ignore_ascii_case! { try!(input.expect_ident()), match_ignore_ascii_case! { try!(input.expect_ident()),
@ -703,8 +715,8 @@ impl ToCss for BorderRadius {
} }
} }
impl BorderRadius { impl Parse for BorderRadius {
pub fn parse(input: &mut Parser) -> Result<BorderRadius, ()> { fn parse(input: &mut Parser) -> Result<Self, ()> {
let widths = try!(parse_one_set_of_border_values(input)); let widths = try!(parse_one_set_of_border_values(input));
let heights = if input.try(|input| input.expect_delim('/')).is_ok() { let heights = if input.try(|input| input.expect_delim('/')).is_ok() {
try!(parse_one_set_of_border_values(input)) try!(parse_one_set_of_border_values(input))
@ -781,8 +793,8 @@ pub enum FillRule {
impl ComputedValueAsSpecified for FillRule {} impl ComputedValueAsSpecified for FillRule {}
impl FillRule { impl Parse for FillRule {
pub fn parse(input: &mut Parser) -> Result<FillRule, ()> { fn parse(input: &mut Parser) -> Result<FillRule, ()> {
match_ignore_ascii_case! { try!(input.expect_ident()), match_ignore_ascii_case! { try!(input.expect_ident()),
"nonzero" => Ok(FillRule::NonZero), "nonzero" => Ok(FillRule::NonZero),
"evenodd" => Ok(FillRule::EvenOdd), "evenodd" => Ok(FillRule::EvenOdd),

View file

@ -840,15 +840,18 @@ impl LengthOrPercentageOrAuto {
} }
} }
#[inline] #[inline]
pub fn parse(input: &mut Parser) -> Result<LengthOrPercentageOrAuto, ()> {
LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::All)
}
#[inline]
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentageOrAuto, ()> { pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentageOrAuto, ()> {
LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::NonNegative) LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::NonNegative)
} }
} }
impl Parse for LengthOrPercentageOrAuto {
#[inline]
fn parse(input: &mut Parser) -> Result<Self, ()> {
LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::All)
}
}
#[derive(Clone, PartialEq, Copy, Debug)] #[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum LengthOrPercentageOrNone { pub enum LengthOrPercentageOrNone {
@ -899,15 +902,18 @@ impl LengthOrPercentageOrNone {
} }
} }
#[inline] #[inline]
pub fn parse(input: &mut Parser) -> Result<LengthOrPercentageOrNone, ()> {
LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::All)
}
#[inline]
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentageOrNone, ()> { pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentageOrNone, ()> {
LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::NonNegative) LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::NonNegative)
} }
} }
impl Parse for LengthOrPercentageOrNone {
#[inline]
fn parse(input: &mut Parser) -> Result<Self, ()> {
LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::All)
}
}
pub type LengthOrNone = Either<Length, None_>; pub type LengthOrNone = Either<Length, None_>;
impl LengthOrNone { impl LengthOrNone {
@ -949,8 +955,8 @@ impl ToCss for LengthOrPercentageOrAutoOrContent {
} }
} }
impl LengthOrPercentageOrAutoOrContent { impl Parse for LengthOrPercentageOrAutoOrContent {
pub fn parse(input: &mut Parser) -> Result<LengthOrPercentageOrAutoOrContent, ()> { fn parse(input: &mut Parser) -> Result<Self, ()> {
let context = AllowedNumericType::NonNegative; let context = AllowedNumericType::NonNegative;
match try!(input.next()) { match try!(input.next()) {
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) => Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>

View file

@ -5,7 +5,7 @@
use app_units::Au; use app_units::Au;
use cssparser::{self, Parser, Token}; use cssparser::{self, Parser, Token};
use euclid::size::Size2D; use euclid::size::Size2D;
use parser::ParserContext; use parser::{Parse, ParserContext};
use self::url::SpecifiedUrl; use self::url::SpecifiedUrl;
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use std::f32::consts::PI; use std::f32::consts::PI;
@ -36,8 +36,9 @@ pub struct CSSColor {
pub parsed: cssparser::Color, pub parsed: cssparser::Color,
pub authored: Option<String>, pub authored: Option<String>,
} }
impl CSSColor {
pub fn parse(input: &mut Parser) -> Result<CSSColor, ()> { impl Parse for CSSColor {
fn parse(input: &mut Parser) -> Result<Self, ()> {
let start_position = input.position(); let start_position = input.position();
let authored = match input.next() { let authored = match input.next() {
Ok(Token::Ident(s)) => Some(s.into_owned()), Ok(Token::Ident(s)) => Some(s.into_owned()),
@ -192,9 +193,11 @@ impl BorderRadiusSize {
pub fn circle(radius: LengthOrPercentage) -> BorderRadiusSize { pub fn circle(radius: LengthOrPercentage) -> BorderRadiusSize {
BorderRadiusSize(Size2D::new(radius, radius)) BorderRadiusSize(Size2D::new(radius, radius))
} }
}
impl Parse for BorderRadiusSize {
#[inline] #[inline]
pub fn parse(input: &mut Parser) -> Result<BorderRadiusSize, ()> { fn parse(input: &mut Parser) -> Result<Self, ()> {
let first = try!(LengthOrPercentage::parse_non_negative(input)); let first = try!(LengthOrPercentage::parse_non_negative(input));
let second = input.try(LengthOrPercentage::parse_non_negative).unwrap_or(first); let second = input.try(LengthOrPercentage::parse_non_negative).unwrap_or(first);
Ok(BorderRadiusSize(Size2D::new(first, second))) Ok(BorderRadiusSize(Size2D::new(first, second)))
@ -236,9 +239,9 @@ const RAD_PER_DEG: CSSFloat = PI / 180.0;
const RAD_PER_GRAD: CSSFloat = PI / 200.0; const RAD_PER_GRAD: CSSFloat = PI / 200.0;
const RAD_PER_TURN: CSSFloat = PI * 2.0; const RAD_PER_TURN: CSSFloat = PI * 2.0;
impl Angle { impl Parse for Angle {
/// Parses an angle according to CSS-VALUES § 6.1. /// Parses an angle according to CSS-VALUES § 6.1.
pub fn parse(input: &mut Parser) -> Result<Angle, ()> { fn parse(input: &mut Parser) -> Result<Self, ()> {
match try!(input.next()) { match try!(input.next()) {
Token::Dimension(ref value, ref unit) => Angle::parse_dimension(value.value, unit), Token::Dimension(ref value, ref unit) => Angle::parse_dimension(value.value, unit),
Token::Number(ref value) if value.value == 0. => Ok(Angle(0.)), Token::Number(ref value) if value.value == 0. => Ok(Angle(0.)),
@ -248,7 +251,9 @@ impl Angle {
_ => Err(()) _ => Err(())
} }
} }
}
impl Angle {
pub fn parse_dimension(value: CSSFloat, unit: &str) -> Result<Angle, ()> { pub fn parse_dimension(value: CSSFloat, unit: &str) -> Result<Angle, ()> {
match_ignore_ascii_case! { unit, match_ignore_ascii_case! { unit,
"deg" => Ok(Angle(value * RAD_PER_DEG)), "deg" => Ok(Angle(value * RAD_PER_DEG)),
@ -399,8 +404,12 @@ impl Time {
Err(()) Err(())
} }
} }
}
pub fn parse(input: &mut Parser) -> Result<Time, ()> { impl ComputedValueAsSpecified for Time {}
impl Parse for Time {
fn parse(input: &mut Parser) -> Result<Self, ()> {
match input.next() { match input.next() {
Ok(Token::Dimension(ref value, ref unit)) => { Ok(Token::Dimension(ref value, ref unit)) => {
Time::parse_dimension(value.value, &unit) Time::parse_dimension(value.value, &unit)
@ -413,8 +422,6 @@ impl Time {
} }
} }
impl ComputedValueAsSpecified for Time {}
impl ToCss for Time { impl ToCss for Time {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
write!(dest, "{}s", self.0) write!(dest, "{}s", self.0)
@ -427,11 +434,13 @@ pub struct Number(pub CSSFloat);
impl NoViewportPercentage for Number {} impl NoViewportPercentage for Number {}
impl Number { impl Parse for Number {
pub fn parse(input: &mut Parser) -> Result<Number, ()> { fn parse(input: &mut Parser) -> Result<Self, ()> {
parse_number(input).map(Number) parse_number(input).map(Number)
} }
}
impl Number {
fn parse_with_minimum(input: &mut Parser, min: CSSFloat) -> Result<Number, ()> { fn parse_with_minimum(input: &mut Parser, min: CSSFloat) -> Result<Number, ()> {
match parse_number(input) { match parse_number(input) {
Ok(value) if value < min => Err(()), Ok(value) if value < min => Err(()),
@ -472,8 +481,8 @@ pub struct Opacity(pub CSSFloat);
impl NoViewportPercentage for Opacity {} impl NoViewportPercentage for Opacity {}
impl Opacity { impl Parse for Opacity {
pub fn parse(input: &mut Parser) -> Result<Opacity, ()> { fn parse(input: &mut Parser) -> Result<Self, ()> {
parse_number(input).map(Opacity) parse_number(input).map(Opacity)
} }
} }

View file

@ -169,7 +169,18 @@ impl Position {
}) })
} }
pub fn parse(input: &mut Parser) -> Result<Position, ()> { pub fn center() -> Position {
Position {
horiz_keyword: Some(Keyword::Center),
horiz_position: None,
vert_keyword: Some(Keyword::Center),
vert_position: None,
}
}
}
impl Parse for Position {
fn parse(input: &mut Parser) -> Result<Self, ()> {
let first = try!(PositionComponent::parse(input)); let first = try!(PositionComponent::parse(input));
let second = input.try(PositionComponent::parse) let second = input.try(PositionComponent::parse)
.unwrap_or(PositionComponent::Keyword(Keyword::Center)); .unwrap_or(PositionComponent::Keyword(Keyword::Center));
@ -216,15 +227,6 @@ impl Position {
} }
} }
} }
pub fn center() -> Position {
Position {
horiz_keyword: Some(Keyword::Center),
horiz_position: None,
vert_keyword: Some(Keyword::Center),
vert_position: None,
}
}
} }
impl Keyword { impl Keyword {
@ -362,7 +364,17 @@ impl HasViewportPercentage for PositionComponent {
} }
impl PositionComponent { impl PositionComponent {
pub fn parse(input: &mut Parser) -> Result<PositionComponent, ()> { #[inline]
pub fn to_length_or_percentage(self) -> LengthOrPercentage {
match self {
PositionComponent::Length(value) => value,
PositionComponent::Keyword(keyword) => keyword.to_length_or_percentage(),
}
}
}
impl Parse for PositionComponent {
fn parse(input: &mut Parser) -> Result<Self, ()> {
input.try(LengthOrPercentage::parse) input.try(LengthOrPercentage::parse)
.map(PositionComponent::Length) .map(PositionComponent::Length)
.or_else(|()| { .or_else(|()| {
@ -381,11 +393,4 @@ impl PositionComponent {
} }
}) })
} }
#[inline]
pub fn to_length_or_percentage(self) -> LengthOrPercentage {
match self {
PositionComponent::Length(value) => value,
PositionComponent::Keyword(keyword) => keyword.to_length_or_percentage(),
}
}
} }

View file

@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use parsing::parse; use parsing::parse;
use style::parser::Parse;
use style::values::specified::position::*; use style::values::specified::position::*;
use style_traits::ToCss; use style_traits::ToCss;