mirror of
https://github.com/servo/servo.git
synced 2025-08-07 14:35:33 +01:00
Fix related to #14101
Add Parse trait implementation for some structures
This commit is contained in:
parent
4b9693cf81
commit
9564673b5a
14 changed files with 149 additions and 114 deletions
|
@ -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;
|
||||
|
|
|
@ -213,14 +213,6 @@ pub struct 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, ()> {
|
||||
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<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 {
|
||||
// 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 {
|
||||
|
@ -374,14 +377,6 @@ pub struct 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, ()> {
|
||||
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<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 {
|
||||
fn to_css<W>(&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<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, ()> {
|
||||
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<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 {
|
||||
fn to_css<W>(&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<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, ()> {
|
||||
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<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 {
|
||||
fn to_css<W>(&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<ShapeRadius, ()> {
|
||||
impl Parse for ShapeRadius {
|
||||
fn parse(input: &mut Parser) -> Result<Self, ()> {
|
||||
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<BorderRadius, ()> {
|
||||
impl Parse for BorderRadius {
|
||||
fn parse(input: &mut Parser) -> Result<Self, ()> {
|
||||
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<FillRule, ()> {
|
||||
impl Parse for FillRule {
|
||||
fn parse(input: &mut Parser) -> Result<FillRule, ()> {
|
||||
match_ignore_ascii_case! { try!(input.expect_ident()),
|
||||
"nonzero" => Ok(FillRule::NonZero),
|
||||
"evenodd" => Ok(FillRule::EvenOdd),
|
||||
|
|
|
@ -840,15 +840,18 @@ impl LengthOrPercentageOrAuto {
|
|||
}
|
||||
}
|
||||
#[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, ()> {
|
||||
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)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
pub enum LengthOrPercentageOrNone {
|
||||
|
@ -899,15 +902,18 @@ impl LengthOrPercentageOrNone {
|
|||
}
|
||||
}
|
||||
#[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, ()> {
|
||||
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_>;
|
||||
|
||||
impl LengthOrNone {
|
||||
|
@ -949,8 +955,8 @@ impl ToCss for LengthOrPercentageOrAutoOrContent {
|
|||
}
|
||||
}
|
||||
|
||||
impl LengthOrPercentageOrAutoOrContent {
|
||||
pub fn parse(input: &mut Parser) -> Result<LengthOrPercentageOrAutoOrContent, ()> {
|
||||
impl Parse for LengthOrPercentageOrAutoOrContent {
|
||||
fn parse(input: &mut Parser) -> Result<Self, ()> {
|
||||
let context = AllowedNumericType::NonNegative;
|
||||
match try!(input.next()) {
|
||||
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
|
||||
|
|
|
@ -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<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 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<BorderRadiusSize, ()> {
|
||||
fn parse(input: &mut Parser) -> Result<Self, ()> {
|
||||
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<Angle, ()> {
|
||||
fn parse(input: &mut Parser) -> Result<Self, ()> {
|
||||
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<Angle, ()> {
|
||||
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<Time, ()> {
|
||||
impl ComputedValueAsSpecified for Time {}
|
||||
|
||||
impl Parse for Time {
|
||||
fn parse(input: &mut Parser) -> Result<Self, ()> {
|
||||
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<W>(&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<Number, ()> {
|
||||
impl Parse for Number {
|
||||
fn parse(input: &mut Parser) -> Result<Self, ()> {
|
||||
parse_number(input).map(Number)
|
||||
}
|
||||
}
|
||||
|
||||
impl Number {
|
||||
fn parse_with_minimum(input: &mut Parser, min: CSSFloat) -> Result<Number, ()> {
|
||||
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<Opacity, ()> {
|
||||
impl Parse for Opacity {
|
||||
fn parse(input: &mut Parser) -> Result<Self, ()> {
|
||||
parse_number(input).map(Opacity)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 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<PositionComponent, ()> {
|
||||
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<Self, ()> {
|
||||
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(())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue