Remove uses of parse_four_sides and serialize_four_sides

This commit is contained in:
Anthony Ramine 2017-05-25 12:22:49 +02:00
parent ca8fae91da
commit 6f3c46ca61
6 changed files with 41 additions and 141 deletions

View file

@ -887,35 +887,34 @@
<%call expr="self.shorthand(name, sub_properties=sub_properties, **kwargs)"> <%call expr="self.shorthand(name, sub_properties=sub_properties, **kwargs)">
#[allow(unused_imports)] #[allow(unused_imports)]
use parser::Parse; use parser::Parse;
use super::parse_four_sides; use values::generics::rect::Rect;
use values::specified; use values::specified;
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> { pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let (top, right, bottom, left) = let rect = Rect::parse_with(context, input, |_c, i| {
% if allow_quirks: % if allow_quirks:
try!(parse_four_sides(input, |i| ${parser_function}_quirky(context, i, specified::AllowQuirks::Yes))); ${parser_function}_quirky(_c, i, specified::AllowQuirks::Yes)
% elif needs_context: % elif needs_context:
try!(parse_four_sides(input, |i| ${parser_function}(context, i))); ${parser_function}(_c, i)
% else: % else:
try!(parse_four_sides(input, ${parser_function})); ${parser_function}(i)
let _unused = context;
% endif % endif
})?;
Ok(expanded! { Ok(expanded! {
% for side in ["top", "right", "bottom", "left"]: % for side in ["top", "right", "bottom", "left"]:
${to_rust_ident(sub_property_pattern % side)}: ${side}, ${to_rust_ident(sub_property_pattern % side)}: rect.${side},
% endfor % endfor
}) })
} }
impl<'a> ToCss for LonghandsToSerialize<'a> { impl<'a> ToCss for LonghandsToSerialize<'a> {
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 {
super::serialize_four_sides( let rect = Rect::new(
dest, % for side in ["top", "right", "bottom", "left"]:
self.${to_rust_ident(sub_property_pattern % 'top')}, &self.${to_rust_ident(sub_property_pattern % side)},
self.${to_rust_ident(sub_property_pattern % 'right')}, % endfor
self.${to_rust_ident(sub_property_pattern % 'bottom')}, );
self.${to_rust_ident(sub_property_pattern % 'left')} rect.to_css(dest)
)
} }
} }
</%call> </%call>

View file

@ -134,55 +134,6 @@ pub mod shorthands {
use parser::{Parse, ParserContext}; use parser::{Parse, ParserContext};
use values::specified; use values::specified;
/// Parses a property for four different sides per CSS syntax.
///
/// * Zero or more than four values is invalid.
/// * One value sets them all
/// * Two values set (top, bottom) and (left, right)
/// * Three values set top, (left, right) and bottom
/// * Four values set them in order
///
/// returns the values in (top, right, bottom, left) order.
pub fn parse_four_sides<F, T>(input: &mut Parser, parse_one: F) -> Result<(T, T, T, T), ()>
where F: Fn(&mut Parser) -> Result<T, ()>,
T: Clone,
{
let top = try!(parse_one(input));
let right;
let bottom;
let left;
match input.try(|i| parse_one(i)) {
Err(()) => {
right = top.clone();
bottom = top.clone();
left = top.clone();
}
Ok(value) => {
right = value;
match input.try(|i| parse_one(i)) {
Err(()) => {
bottom = top.clone();
left = right.clone();
}
Ok(value) => {
bottom = value;
match input.try(|i| parse_one(i)) {
Err(()) => {
left = right.clone();
}
Ok(value) => {
left = value;
}
}
}
}
}
}
Ok((top, right, bottom, left))
}
<%include file="/shorthand/serialize.mako.rs" /> <%include file="/shorthand/serialize.mako.rs" />
<%include file="/shorthand/background.mako.rs" /> <%include file="/shorthand/background.mako.rs" />
<%include file="/shorthand/border.mako.rs" /> <%include file="/shorthand/border.mako.rs" />

View file

@ -17,27 +17,28 @@ ${helpers.four_sides_shorthand("border-style", "border-%s-style",
' '.join('border-%s-width' % side ' '.join('border-%s-width' % side
for side in PHYSICAL_SIDES)}" for side in PHYSICAL_SIDES)}"
spec="https://drafts.csswg.org/css-backgrounds/#border-width"> spec="https://drafts.csswg.org/css-backgrounds/#border-width">
use super::parse_four_sides; use values::generics::rect::Rect;
use values::specified::{AllowQuirks, BorderWidth}; use values::specified::{AllowQuirks, BorderWidth};
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> { pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let (top, right, bottom, left) = try!(parse_four_sides(input, |i| { let rect = Rect::parse_with(context, input, |_, i| {
BorderWidth::parse_quirky(context, i, AllowQuirks::Yes) BorderWidth::parse_quirky(context, i, AllowQuirks::Yes)
})); })?;
Ok(expanded! { Ok(expanded! {
% for side in PHYSICAL_SIDES: % for side in PHYSICAL_SIDES:
${to_rust_ident('border-%s-width' % side)}: ${side}, ${to_rust_ident('border-%s-width' % side)}: rect.${side},
% endfor % endfor
}) })
} }
impl<'a> ToCss for LonghandsToSerialize<'a> { impl<'a> ToCss for LonghandsToSerialize<'a> {
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 {
% for side in PHYSICAL_SIDES: let rect = Rect::new(
let ${side} = self.border_${side}_width.clone(); % for side in PHYSICAL_SIDES:
% endfor &self.border_${side}_width,
% endfor
super::serialize_four_sides(dest, &top, &right, &bottom, &left) );
rect.to_css(dest)
} }
} }
</%helpers:shorthand> </%helpers:shorthand>

View file

@ -6,57 +6,6 @@ use style_traits::ToCss;
use values::specified::{BorderStyle, Color, CSSColor}; use values::specified::{BorderStyle, Color, CSSColor};
use std::fmt; use std::fmt;
#[allow(missing_docs)]
pub fn serialize_four_sides<W, I>(dest: &mut W,
top: &I,
right: &I,
bottom: &I,
left: &I)
-> fmt::Result
where W: fmt::Write,
I: ToCss + PartialEq,
{
if left == right {
let horizontal_value = left;
if top == bottom {
let vertical_value = top;
if horizontal_value == vertical_value {
let single_value = horizontal_value;
try!(single_value.to_css(dest));
} else {
try!(vertical_value.to_css(dest));
try!(write!(dest, " "));
try!(horizontal_value.to_css(dest));
}
} else {
try!(top.to_css(dest));
try!(write!(dest, " "));
try!(horizontal_value.to_css(dest));
try!(write!(dest, " "));
try!(bottom.to_css(dest));
}
} else {
try!(top.to_css(dest));
try!(write!(dest, " "));
try!(right.to_css(dest));
try!(write!(dest, " "));
try!(bottom.to_css(dest));
try!(write!(dest, " "));
try!(left.to_css(dest));
}
Ok(())
}
fn serialize_directional_border<W, I,>(dest: &mut W, fn serialize_directional_border<W, I,>(dest: &mut W,
width: &I, width: &I,
style: &BorderStyle, style: &BorderStyle,

View file

@ -6,12 +6,12 @@
//! types that are generic over their `ToCss` implementations. //! types that are generic over their `ToCss` implementations.
use euclid::size::Size2D; use euclid::size::Size2D;
use properties::shorthands::serialize_four_sides;
use std::fmt; use std::fmt;
use style_traits::{HasViewportPercentage, ToCss}; use style_traits::{HasViewportPercentage, ToCss};
use values::computed::ComputedValueAsSpecified; use values::computed::ComputedValueAsSpecified;
use values::generics::BorderRadiusSize; use values::generics::BorderRadiusSize;
use values::generics::position::Position; use values::generics::position::Position;
use values::generics::rect::Rect;
use values::specified::url::SpecifiedUrl; use values::specified::url::SpecifiedUrl;
/// A clipping shape, for `clip-path`. /// A clipping shape, for `clip-path`.
@ -224,17 +224,17 @@ pub fn serialize_radius_values<L, W>(dest: &mut W, top_left: &Size2D<L>,
bottom_left: &Size2D<L>) -> fmt::Result bottom_left: &Size2D<L>) -> fmt::Result
where L: ToCss + PartialEq, W: fmt::Write where L: ToCss + PartialEq, W: fmt::Write
{ {
if top_left.width == top_left.height && top_right.width == top_right.height && Rect::new(&top_left.width, &top_right.width, &bottom_right.width, &bottom_left.width).to_css(dest)?;
bottom_right.width == bottom_right.height && bottom_left.width == bottom_left.height { if
serialize_four_sides(dest, &top_left.width, &top_right.width, top_left.width != top_left.height ||
&bottom_right.width, &bottom_left.width) top_right.width != top_right.height ||
} else { bottom_right.width != bottom_right.height ||
serialize_four_sides(dest, &top_left.width, &top_right.width, bottom_left.width != bottom_left.height
&bottom_right.width, &bottom_left.width)?; {
dest.write_str(" / ")?; dest.write_str(" / ")?;
serialize_four_sides(dest, &top_left.height, &top_right.height, Rect::new(&top_left.height, &top_right.height, &bottom_right.height, &bottom_left.height).to_css(dest)?;
&bottom_right.height, &bottom_left.height)
} }
Ok(())
} }
impl<L> Default for ShapeRadius<L> { impl<L> Default for ShapeRadius<L> {

View file

@ -9,7 +9,6 @@
use cssparser::Parser; use cssparser::Parser;
use parser::{Parse, ParserContext}; use parser::{Parse, ParserContext};
use properties::shorthands::parse_four_sides;
use std::borrow::Cow; use std::borrow::Cow;
use std::fmt; use std::fmt;
use style_traits::ToCss; use style_traits::ToCss;
@ -20,6 +19,7 @@ use values::generics::basic_shape::{FillRule, BasicShape as GenericBasicShape};
use values::generics::basic_shape::{FloatAreaShape as GenericFloatAreaShape, InsetRect as GenericInsetRect}; use values::generics::basic_shape::{FloatAreaShape as GenericFloatAreaShape, InsetRect as GenericInsetRect};
use values::generics::basic_shape::{GeometryBox, ShapeBox, ShapeSource}; use values::generics::basic_shape::{GeometryBox, ShapeBox, ShapeSource};
use values::generics::basic_shape::{Polygon as GenericPolygon, ShapeRadius as GenericShapeRadius}; use values::generics::basic_shape::{Polygon as GenericPolygon, ShapeRadius as GenericShapeRadius};
use values::generics::rect::Rect;
use values::specified::{LengthOrPercentage, Percentage}; use values::specified::{LengthOrPercentage, Percentage};
use values::specified::position::{HorizontalPosition, Position, PositionComponent, Side, VerticalPosition}; use values::specified::position::{HorizontalPosition, Position, PositionComponent, Side, VerticalPosition};
use values::specified::url::SpecifiedUrl; use values::specified::url::SpecifiedUrl;
@ -127,18 +127,18 @@ impl Parse for InsetRect {
impl InsetRect { impl InsetRect {
/// Parse the inner function arguments of `inset()` /// Parse the inner function arguments of `inset()`
pub fn parse_function_arguments(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> { pub fn parse_function_arguments(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
let (t, r, b, l) = parse_four_sides(input, |i| LengthOrPercentage::parse(context, i))?; let rect = Rect::parse_with(context, input, LengthOrPercentage::parse)?;
let rect = if input.try(|i| i.expect_ident_matching("round")).is_ok() { let round_rect = if input.try(|i| i.expect_ident_matching("round")).is_ok() {
Some(BorderRadius::parse(context, input)?) Some(BorderRadius::parse(context, input)?)
} else { } else {
None None
}; };
Ok(GenericInsetRect { Ok(GenericInsetRect {
top: t, top: rect.top,
right: r, right: rect.right,
bottom: b, bottom: rect.bottom,
left: l, left: rect.left,
round: rect, round: round_rect,
}) })
} }
} }