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)">
#[allow(unused_imports)]
use parser::Parse;
use super::parse_four_sides;
use values::generics::rect::Rect;
use values::specified;
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:
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:
try!(parse_four_sides(input, |i| ${parser_function}(context, i)));
${parser_function}(_c, i)
% else:
try!(parse_four_sides(input, ${parser_function}));
let _unused = context;
${parser_function}(i)
% endif
})?;
Ok(expanded! {
% 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
})
}
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
super::serialize_four_sides(
dest,
self.${to_rust_ident(sub_property_pattern % 'top')},
self.${to_rust_ident(sub_property_pattern % 'right')},
self.${to_rust_ident(sub_property_pattern % 'bottom')},
self.${to_rust_ident(sub_property_pattern % 'left')}
)
let rect = Rect::new(
% for side in ["top", "right", "bottom", "left"]:
&self.${to_rust_ident(sub_property_pattern % side)},
% endfor
);
rect.to_css(dest)
}
}
</%call>

View file

@ -134,55 +134,6 @@ pub mod shorthands {
use parser::{Parse, ParserContext};
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/background.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
for side in PHYSICAL_SIDES)}"
spec="https://drafts.csswg.org/css-backgrounds/#border-width">
use super::parse_four_sides;
use values::generics::rect::Rect;
use values::specified::{AllowQuirks, BorderWidth};
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)
}));
})?;
Ok(expanded! {
% for side in PHYSICAL_SIDES:
${to_rust_ident('border-%s-width' % side)}: ${side},
${to_rust_ident('border-%s-width' % side)}: rect.${side},
% endfor
})
}
impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
% for side in PHYSICAL_SIDES:
let ${side} = self.border_${side}_width.clone();
% endfor
super::serialize_four_sides(dest, &top, &right, &bottom, &left)
let rect = Rect::new(
% for side in PHYSICAL_SIDES:
&self.border_${side}_width,
% endfor
);
rect.to_css(dest)
}
}
</%helpers:shorthand>

View file

@ -6,57 +6,6 @@ use style_traits::ToCss;
use values::specified::{BorderStyle, Color, CSSColor};
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,
width: &I,
style: &BorderStyle,

View file

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

View file

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