diff --git a/servo/components/style/custom_properties.rs b/servo/components/style/custom_properties.rs index 0f12f841420e..0e8877814043 100644 --- a/servo/components/style/custom_properties.rs +++ b/servo/components/style/custom_properties.rs @@ -8,6 +8,7 @@ use Atom; use cssparser::{Delimiter, Parser, SourcePosition, Token, TokenSerializationType}; +use parser::Parse; use properties::DeclaredValue; use std::ascii::AsciiExt; use std::borrow::Cow; @@ -111,15 +112,17 @@ impl ComputedValue { } } -pub fn parse(input: &mut Parser) -> Result { - let mut references = Some(HashSet::new()); - let (first, css, last) = try!(parse_self_contained_declaration_value(input, &mut references)); - Ok(SpecifiedValue { - css: css.into_owned(), - first_token_type: first, - last_token_type: last, - references: references.unwrap(), - }) +impl Parse for SpecifiedValue { + fn parse(input: &mut Parser) -> Result { + let mut references = Some(HashSet::new()); + let (first, css, last) = try!(parse_self_contained_declaration_value(input, &mut references)); + Ok(SpecifiedValue { + css: css.into_owned(), + first_token_type: first, + last_token_type: last, + references: references.unwrap(), + }) + } } /// Parse the value of a non-custom property that contains `var()` references. diff --git a/servo/components/style/properties/helpers.mako.rs b/servo/components/style/properties/helpers.mako.rs index 6eae58418d7b..b4e4d59cbed6 100644 --- a/servo/components/style/properties/helpers.mako.rs +++ b/servo/components/style/properties/helpers.mako.rs @@ -67,7 +67,7 @@ pub mod single_value { use cssparser::Parser; - use parser::{ParserContext, ParserContextExtraData}; + use parser::{Parse, ParserContext, ParserContextExtraData}; use properties::{CSSWideKeyword, DeclaredValue, Shorthand}; use values::computed::{Context, ToComputedValue}; use values::{computed, specified}; @@ -175,13 +175,12 @@ #![allow(unused_imports)] % if not property.derived_from: use cssparser::Parser; - use parser::{ParserContext, ParserContextExtraData}; + use parser::{Parse, ParserContext, ParserContextExtraData}; use properties::{CSSWideKeyword, DeclaredValue, Shorthand}; % endif use values::{Auto, Either, None_, Normal}; use cascade_info::CascadeInfo; use error_reporting::ParseErrorReporter; - use parser::Parse; use properties::longhands; use properties::property_bit_field::PropertyBitField; use properties::{ComputedValues, PropertyDeclaration}; diff --git a/servo/components/style/properties/longhand/effects.mako.rs b/servo/components/style/properties/longhand/effects.mako.rs index 643c3f96ca85..4619f07a4d41 100644 --- a/servo/components/style/properties/longhand/effects.mako.rs +++ b/servo/components/style/properties/longhand/effects.mako.rs @@ -15,7 +15,6 @@ ${helpers.predefined_type("opacity", <%helpers:vector_longhand name="box-shadow" allow_empty="True" animatable="True"> use cssparser; use std::fmt; - use parser::Parse; use style_traits::ToCss; use values::HasViewportPercentage; @@ -355,7 +354,6 @@ ${helpers.predefined_type("opacity", pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result { use app_units::Au; - use parser::Parse; use std::ascii::AsciiExt; use values::specified::Length; @@ -677,7 +675,6 @@ pub struct OriginParseResult { } pub fn parse_origin(_: &ParserContext, input: &mut Parser) -> Result { - use parser::Parse; use values::specified::{LengthOrPercentage, Percentage}; let (mut horizontal, mut vertical, mut depth) = (None, None, None); loop { diff --git a/servo/components/style/properties/longhand/font.mako.rs b/servo/components/style/properties/longhand/font.mako.rs index 4df0b9de90c7..0b1e21266e28 100644 --- a/servo/components/style/properties/longhand/font.mako.rs +++ b/servo/components/style/properties/longhand/font.mako.rs @@ -496,6 +496,7 @@ ${helpers.single_keyword("font-variant-position", pub mod computed_value { use cssparser::Parser; + use parser::Parse; use std::fmt; 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 /// [ on | off | ] - pub fn parse(input: &mut Parser) -> Result { + fn parse(input: &mut Parser) -> Result { let tag = try!(input.expect_string()); // allowed strings of length 4 containing chars: diff --git a/servo/components/style/properties/properties.mako.rs b/servo/components/style/properties/properties.mako.rs index ec6eef352c7b..743859962100 100644 --- a/servo/components/style/properties/properties.mako.rs +++ b/servo/components/style/properties/properties.mako.rs @@ -27,7 +27,7 @@ use euclid::size::Size2D; use computed_values; #[cfg(feature = "servo")] use logical_geometry::{LogicalMargin, PhysicalSide}; use logical_geometry::WritingMode; -use parser::{ParserContext, ParserContextExtraData}; +use parser::{Parse, ParserContext, ParserContextExtraData}; use style_traits::ToCss; use stylesheets::Origin; #[cfg(feature = "servo")] use values::Either; @@ -49,7 +49,7 @@ pub mod declaration_block; pub mod longhands { use cssparser::Parser; - use parser::ParserContext; + use parser::{Parse, ParserContext}; use values::specified; <%include file="/longhand/background.mako.rs" /> @@ -79,7 +79,7 @@ pub mod longhands { pub mod shorthands { use cssparser::Parser; - use parser::ParserContext; + use parser::{Parse, ParserContext}; use values::specified; pub fn parse_four_sides(input: &mut Parser, parse_one: F) -> Result<(T, T, T, T), ()> @@ -344,8 +344,8 @@ pub enum CSSWideKeyword { UnsetKeyword, } -impl CSSWideKeyword { - pub fn parse(input: &mut Parser) -> Result { +impl Parse for CSSWideKeyword { + fn parse(input: &mut Parser) -> Result { match_ignore_ascii_case! { try!(input.expect_ident()), "initial" => Ok(CSSWideKeyword::InitialKeyword), "inherit" => Ok(CSSWideKeyword::InheritKeyword), @@ -736,7 +736,7 @@ impl PropertyDeclaration { Ok(CSSWideKeyword::UnsetKeyword) | // Custom properties are alawys inherited Ok(CSSWideKeyword::InheritKeyword) => DeclaredValue::Inherit, Ok(CSSWideKeyword::InitialKeyword) => DeclaredValue::Initial, - Err(()) => match ::custom_properties::parse(input) { + Err(()) => match ::custom_properties::SpecifiedValue::parse(input) { Ok(value) => DeclaredValue::Value(value), Err(()) => return PropertyDeclarationParseResult::InvalidValue, } diff --git a/servo/components/style/properties/shorthand/border.mako.rs b/servo/components/style/properties/shorthand/border.mako.rs index 9d65f533e4aa..73201487f48b 100644 --- a/servo/components/style/properties/shorthand/border.mako.rs +++ b/servo/components/style/properties/shorthand/border.mako.rs @@ -149,6 +149,7 @@ pub fn parse_border(context: &ParserContext, input: &mut Parser) for corner in ['top-left', 'top-right', 'bottom-right', 'bottom-left'] )}"> use values::specified::basic_shape::BorderRadius; + use parser::Parse; pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result { let _ignored = context; diff --git a/servo/components/style/properties/shorthand/outline.mako.rs b/servo/components/style/properties/shorthand/outline.mako.rs index 852fce2b2928..cb175b9c9c82 100644 --- a/servo/components/style/properties/shorthand/outline.mako.rs +++ b/servo/components/style/properties/shorthand/outline.mako.rs @@ -7,6 +7,7 @@ <%helpers:shorthand name="outline" sub_properties="outline-color outline-style outline-width"> use properties::longhands::outline_width; use values::specified; + use parser::Parse; pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result { let _unused = context; diff --git a/servo/components/style/properties/shorthand/position.mako.rs b/servo/components/style/properties/shorthand/position.mako.rs index 7eeebd9c1ab7..1b2d511642fc 100644 --- a/servo/components/style/properties/shorthand/position.mako.rs +++ b/servo/components/style/properties/shorthand/position.mako.rs @@ -56,6 +56,7 @@ // https://drafts.csswg.org/css-flexbox/#flex-property <%helpers:shorthand name="flex" sub_properties="flex-grow flex-shrink flex-basis"> + use parser::Parse; use app_units::Au; use values::specified::{Number, Length, LengthOrPercentageOrAutoOrContent}; diff --git a/servo/components/style/values/mod.rs b/servo/components/style/values/mod.rs index 485b46c82a2c..84c581170079 100644 --- a/servo/components/style/values/mod.rs +++ b/servo/components/style/values/mod.rs @@ -7,7 +7,6 @@ //! [values]: https://drafts.csswg.org/css-values/ pub use cssparser::{RGBA, Parser}; - use parser::Parse; use std::fmt::{self, Debug}; use style_traits::ToCss; diff --git a/servo/components/style/values/specified/basic_shape.rs b/servo/components/style/values/specified/basic_shape.rs index 88123ea1db11..15b630c67610 100644 --- a/servo/components/style/values/specified/basic_shape.rs +++ b/servo/components/style/values/specified/basic_shape.rs @@ -213,14 +213,6 @@ pub struct InsetRect { } impl InsetRect { - pub fn parse(input: &mut Parser) -> Result { - 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 { let (t, r, b, l) = try!(parse_four_sides(input, LengthOrPercentage::parse)); let mut rect = InsetRect { @@ -237,6 +229,17 @@ impl InsetRect { } } +impl Parse for InsetRect { + fn parse(input: &mut Parser) -> Result { + match_ignore_ascii_case! { try!(input.expect_function()), + "inset" => { + Ok(try!(input.parse_nested_block(InsetRect::parse_function_arguments))) + }, + _ => Err(()) + } + } +} + impl ToCss for InsetRect { // XXXManishearth again, we should try to reduce the number of values printed here fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { @@ -374,14 +377,6 @@ pub struct Circle { } impl Circle { - pub fn parse(input: &mut Parser) -> Result { - 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 { 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")) { @@ -402,6 +397,17 @@ impl Circle { } } +impl Parse for Circle { + fn parse(input: &mut Parser) -> Result { + match_ignore_ascii_case! { try!(input.expect_function()), + "circle" => { + Ok(try!(input.parse_nested_block(Circle::parse_function_arguments))) + }, + _ => Err(()) + } + } +} + impl ToCss for Circle { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { try!(dest.write_str("circle(")); @@ -446,14 +452,6 @@ pub struct Ellipse { impl Ellipse { - pub fn parse(input: &mut Parser) -> Result { - 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 { let (a, b) = input.try(|input| -> Result<_, ()> { 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 { + match_ignore_ascii_case! { try!(input.expect_function()), + "ellipse" => { + Ok(try!(input.parse_nested_block(Ellipse::parse_function_arguments))) + }, + _ => Err(()) + } + } +} + impl ToCss for Ellipse { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { try!(dest.write_str("ellipse(")); @@ -524,14 +533,6 @@ pub struct Polygon { } impl Polygon { - pub fn parse(input: &mut Parser) -> Result { - 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 { let fill = input.try(|input| { let fill = FillRule::parse(input); @@ -550,6 +551,17 @@ impl Polygon { } } +impl Parse for Polygon { + fn parse(input: &mut Parser) -> Result { + match_ignore_ascii_case! { try!(input.expect_function()), + "polygon" => { + Ok(try!(input.parse_nested_block(Polygon::parse_function_arguments))) + }, + _ => Err(()) + } + } +} + impl ToCss for Polygon { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { try!(dest.write_str("polygon(")); @@ -616,8 +628,8 @@ impl Default for ShapeRadius { } } -impl ShapeRadius { - pub fn parse(input: &mut Parser) -> Result { +impl Parse for ShapeRadius { + fn parse(input: &mut Parser) -> Result { input.try(LengthOrPercentage::parse).map(ShapeRadius::Length) .or_else(|_| { match_ignore_ascii_case! { try!(input.expect_ident()), @@ -703,8 +715,8 @@ impl ToCss for BorderRadius { } } -impl BorderRadius { - pub fn parse(input: &mut Parser) -> Result { +impl Parse for BorderRadius { + fn parse(input: &mut Parser) -> Result { let widths = try!(parse_one_set_of_border_values(input)); let heights = if input.try(|input| input.expect_delim('/')).is_ok() { try!(parse_one_set_of_border_values(input)) @@ -781,8 +793,8 @@ pub enum FillRule { impl ComputedValueAsSpecified for FillRule {} -impl FillRule { - pub fn parse(input: &mut Parser) -> Result { +impl Parse for FillRule { + fn parse(input: &mut Parser) -> Result { match_ignore_ascii_case! { try!(input.expect_ident()), "nonzero" => Ok(FillRule::NonZero), "evenodd" => Ok(FillRule::EvenOdd), diff --git a/servo/components/style/values/specified/length.rs b/servo/components/style/values/specified/length.rs index a8b33cb041e6..6befb97c747b 100644 --- a/servo/components/style/values/specified/length.rs +++ b/servo/components/style/values/specified/length.rs @@ -840,15 +840,18 @@ impl LengthOrPercentageOrAuto { } } #[inline] - pub fn parse(input: &mut Parser) -> Result { - LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::All) - } - #[inline] pub fn parse_non_negative(input: &mut Parser) -> Result { LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::NonNegative) } } +impl Parse for LengthOrPercentageOrAuto { + #[inline] + fn parse(input: &mut Parser) -> Result { + LengthOrPercentageOrAuto::parse_internal(input, AllowedNumericType::All) + } +} + #[derive(Clone, PartialEq, Copy, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub enum LengthOrPercentageOrNone { @@ -899,15 +902,18 @@ impl LengthOrPercentageOrNone { } } #[inline] - pub fn parse(input: &mut Parser) -> Result { - LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::All) - } - #[inline] pub fn parse_non_negative(input: &mut Parser) -> Result { LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::NonNegative) } } +impl Parse for LengthOrPercentageOrNone { + #[inline] + fn parse(input: &mut Parser) -> Result { + LengthOrPercentageOrNone::parse_internal(input, AllowedNumericType::All) + } +} + pub type LengthOrNone = Either; impl LengthOrNone { @@ -949,8 +955,8 @@ impl ToCss for LengthOrPercentageOrAutoOrContent { } } -impl LengthOrPercentageOrAutoOrContent { - pub fn parse(input: &mut Parser) -> Result { +impl Parse for LengthOrPercentageOrAutoOrContent { + fn parse(input: &mut Parser) -> Result { let context = AllowedNumericType::NonNegative; match try!(input.next()) { Token::Dimension(ref value, ref unit) if context.is_ok(value.value) => diff --git a/servo/components/style/values/specified/mod.rs b/servo/components/style/values/specified/mod.rs index eeed4139cd38..ca96150a2a32 100644 --- a/servo/components/style/values/specified/mod.rs +++ b/servo/components/style/values/specified/mod.rs @@ -5,7 +5,7 @@ use app_units::Au; use cssparser::{self, Parser, Token}; use euclid::size::Size2D; -use parser::ParserContext; +use parser::{Parse, ParserContext}; use self::url::SpecifiedUrl; use std::ascii::AsciiExt; use std::f32::consts::PI; @@ -36,8 +36,9 @@ pub struct CSSColor { pub parsed: cssparser::Color, pub authored: Option, } -impl CSSColor { - pub fn parse(input: &mut Parser) -> Result { + +impl Parse for CSSColor { + fn parse(input: &mut Parser) -> Result { let start_position = input.position(); let authored = match input.next() { Ok(Token::Ident(s)) => Some(s.into_owned()), @@ -192,9 +193,11 @@ impl BorderRadiusSize { pub fn circle(radius: LengthOrPercentage) -> BorderRadiusSize { BorderRadiusSize(Size2D::new(radius, radius)) } +} +impl Parse for BorderRadiusSize { #[inline] - pub fn parse(input: &mut Parser) -> Result { + fn parse(input: &mut Parser) -> Result { let first = try!(LengthOrPercentage::parse_non_negative(input)); let second = input.try(LengthOrPercentage::parse_non_negative).unwrap_or(first); 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_TURN: CSSFloat = PI * 2.0; -impl Angle { +impl Parse for Angle { /// Parses an angle according to CSS-VALUES ยง 6.1. - pub fn parse(input: &mut Parser) -> Result { + fn parse(input: &mut Parser) -> Result { match try!(input.next()) { Token::Dimension(ref value, ref unit) => Angle::parse_dimension(value.value, unit), Token::Number(ref value) if value.value == 0. => Ok(Angle(0.)), @@ -248,7 +251,9 @@ impl Angle { _ => Err(()) } } +} +impl Angle { pub fn parse_dimension(value: CSSFloat, unit: &str) -> Result { match_ignore_ascii_case! { unit, "deg" => Ok(Angle(value * RAD_PER_DEG)), @@ -399,8 +404,12 @@ impl Time { Err(()) } } +} - pub fn parse(input: &mut Parser) -> Result { +impl ComputedValueAsSpecified for Time {} + +impl Parse for Time { + fn parse(input: &mut Parser) -> Result { match input.next() { Ok(Token::Dimension(ref value, ref unit)) => { Time::parse_dimension(value.value, &unit) @@ -413,8 +422,6 @@ impl Time { } } -impl ComputedValueAsSpecified for Time {} - impl ToCss for Time { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { write!(dest, "{}s", self.0) @@ -427,11 +434,13 @@ pub struct Number(pub CSSFloat); impl NoViewportPercentage for Number {} -impl Number { - pub fn parse(input: &mut Parser) -> Result { +impl Parse for Number { + fn parse(input: &mut Parser) -> Result { parse_number(input).map(Number) } +} +impl Number { fn parse_with_minimum(input: &mut Parser, min: CSSFloat) -> Result { match parse_number(input) { Ok(value) if value < min => Err(()), @@ -472,8 +481,8 @@ pub struct Opacity(pub CSSFloat); impl NoViewportPercentage for Opacity {} -impl Opacity { - pub fn parse(input: &mut Parser) -> Result { +impl Parse for Opacity { + fn parse(input: &mut Parser) -> Result { parse_number(input).map(Opacity) } } diff --git a/servo/components/style/values/specified/position.rs b/servo/components/style/values/specified/position.rs index c856a002214d..e2cb6fae1d0a 100644 --- a/servo/components/style/values/specified/position.rs +++ b/servo/components/style/values/specified/position.rs @@ -169,7 +169,18 @@ impl Position { }) } - pub fn parse(input: &mut Parser) -> Result { + 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 { let first = try!(PositionComponent::parse(input)); let second = input.try(PositionComponent::parse) .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 { @@ -362,25 +364,6 @@ impl HasViewportPercentage for PositionComponent { } impl PositionComponent { - pub fn parse(input: &mut Parser) -> Result { - input.try(LengthOrPercentage::parse) - .map(PositionComponent::Length) - .or_else(|()| { - match try!(input.next()) { - Token::Ident(value) => { - match_ignore_ascii_case! { value, - "center" => Ok(PositionComponent::Keyword(Keyword::Center)), - "left" => Ok(PositionComponent::Keyword(Keyword::Left)), - "right" => Ok(PositionComponent::Keyword(Keyword::Right)), - "top" => Ok(PositionComponent::Keyword(Keyword::Top)), - "bottom" => Ok(PositionComponent::Keyword(Keyword::Bottom)), - _ => Err(()) - } - }, - _ => Err(()) - } - }) - } #[inline] pub fn to_length_or_percentage(self) -> LengthOrPercentage { match self { @@ -389,3 +372,25 @@ impl PositionComponent { } } } + +impl Parse for PositionComponent { + fn parse(input: &mut Parser) -> Result { + input.try(LengthOrPercentage::parse) + .map(PositionComponent::Length) + .or_else(|()| { + match try!(input.next()) { + Token::Ident(value) => { + match_ignore_ascii_case! { value, + "center" => Ok(PositionComponent::Keyword(Keyword::Center)), + "left" => Ok(PositionComponent::Keyword(Keyword::Left)), + "right" => Ok(PositionComponent::Keyword(Keyword::Right)), + "top" => Ok(PositionComponent::Keyword(Keyword::Top)), + "bottom" => Ok(PositionComponent::Keyword(Keyword::Bottom)), + _ => Err(()) + } + }, + _ => Err(()) + } + }) + } +} diff --git a/servo/tests/unit/style/parsing/position.rs b/servo/tests/unit/style/parsing/position.rs index 17a9e92679d7..e610e9cee2e7 100644 --- a/servo/tests/unit/style/parsing/position.rs +++ b/servo/tests/unit/style/parsing/position.rs @@ -3,6 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use parsing::parse; +use style::parser::Parse; use style::values::specified::position::*; use style_traits::ToCss;