Add serialize_four_sides, use for serializing BorderRadius

This commit is contained in:
Manish Goregaokar 2016-08-02 15:43:52 +05:30
parent c6feae3c5c
commit d1e45f78af
No known key found for this signature in database
GPG key ID: 3BBF4D3E2EF79F98
5 changed files with 110 additions and 27 deletions

View file

@ -73,7 +73,8 @@ pub mod longhands {
}
pub mod shorthands {
use cssparser::Parser;
use cssparser::{Parser, ToCss};
use std::fmt;
use parser::ParserContext;
use values::specified;
@ -120,6 +121,33 @@ pub mod shorthands {
Ok((top, right, bottom, left))
}
/// Serialize a set of top,left,bottom,right values, in <margin>-shorthand style,
/// attempting to minimize the output
pub fn serialize_four_sides<T, W>(sides: (&T, &T, &T, &T), dest: &mut W) -> fmt::Result
where W: fmt::Write, T: ToCss+PartialEq {
if sides.0 == sides.1 && sides.0 == sides.2 && sides.0 == sides.3 {
sides.0.to_css(dest)
} else if sides.0 == sides.2 && sides.1 == sides.3 {
try!(sides.0.to_css(dest));
try!(dest.write_str(" "));
sides.1.to_css(dest)
} else if sides.1 == sides.3 {
try!(sides.0.to_css(dest));
try!(dest.write_str(" "));
try!(sides.1.to_css(dest));
try!(dest.write_str(" "));
sides.2.to_css(dest)
} else {
try!(sides.0.to_css(dest));
try!(dest.write_str(" "));
try!(sides.1.to_css(dest));
try!(dest.write_str(" "));
try!(sides.2.to_css(dest));
try!(dest.write_str(" "));
sides.3.to_css(dest)
}
}
<%include file="/shorthand/background.mako.rs" />
<%include file="/shorthand/border.mako.rs" />
<%include file="/shorthand/box.mako.rs" />

View file

@ -9,7 +9,7 @@
use app_units::Au;
use cssparser::{Parser, ToCss};
use properties::shorthands::parse_four_sides;
use properties::shorthands::{parse_four_sides, serialize_four_sides};
use std::fmt;
use values::computed::basic_shape as computed_basic_shape;
use values::computed::{Context, ToComputedValue, ComputedValueAsSpecified};
@ -408,21 +408,28 @@ impl ToCss for BorderRadius {
// a helper function somewhere, for all the parse_four_sides-like
// values
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.top_left.0.width.to_css(dest));
try!(dest.write_str(" "));
try!(self.top_right.0.width.to_css(dest));
try!(dest.write_str(" "));
try!(self.bottom_left.0.width.to_css(dest));
try!(dest.write_str(" "));
try!(self.bottom_right.0.width.to_css(dest));
try!(dest.write_str(" / "));
try!(self.top_left.0.height.to_css(dest));
try!(dest.write_str(" "));
try!(self.top_right.0.height.to_css(dest));
try!(dest.write_str(" "));
try!(self.bottom_left.0.height.to_css(dest));
try!(dest.write_str(" "));
self.bottom_right.0.height.to_css(dest)
if self.top_left.0.width == self.top_left.0.height &&
self.top_right.0.width == self.top_right.0.height &&
self.bottom_left.0.width == self.bottom_left.0.height &&
self.bottom_right.0.width == self.bottom_right.0.height {
serialize_four_sides((&self.top_left.0.width,
&self.top_right.0.width,
&self.bottom_left.0.width,
&self.bottom_right.0.width),
dest)
} else {
try!(serialize_four_sides((&self.top_left.0.width,
&self.top_right.0.width,
&self.bottom_left.0.width,
&self.bottom_right.0.width),
dest));
try!(dest.write_str(" / "));
serialize_four_sides((&self.top_left.0.height,
&self.top_right.0.height,
&self.bottom_left.0.height,
&self.bottom_right.0.height),
dest)
}
}
}