Auto merge of #12680 - Manishearth:basic-shape, r=SimonSapin

style: Add support for parsing and serialization of <basic-shape>s

Still WIP: I still need to use this somewhere and make serialization minimal. I'm not sure if I should do either in this PR.
The only other browser that handles basic shapes doesn't serialize correctly either (https://bugzilla.mozilla.org/show_bug.cgi?id=1290864), so that's not something we need to get done now.

As far as using this somewhere, I have the following options:

 - Merge this now, work on using it in stylo in a followup.
 - Just write extensive unit tests for parsing/serialization for now (stylo in a followup)
 - Use this for clip-path in stylo only (which I intend to do anyway, just not sure if I should do it in this PR)
 - Use this for clip-path in Servo (I'd rather not do this; this would be a huge change requiring a lot more layout knowledge than I currently have)

Thoughts? Review? @SimonSapin @bholley

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12680)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-08-05 06:27:24 -05:00 committed by GitHub
commit 0fc0db67c6
16 changed files with 1188 additions and 181 deletions

View file

@ -1042,17 +1042,18 @@ fn static_assert() {
}
pub fn clone_background_position(&self) -> longhands::background_position::computed_value::T {
use values::computed::position::Position;
let position = &self.gecko.mImage.mLayers.mFirstElement.mPosition;
longhands::background_position::computed_value::T {
longhands::background_position::computed_value::T(Position {
horizontal: position.mXPosition.into(),
vertical: position.mYPosition.into(),
}
})
}
pub fn set_background_position(&mut self, v: longhands::background_position::computed_value::T) {
let position = &mut self.gecko.mImage.mLayers.mFirstElement.mPosition;
position.mXPosition = v.horizontal.into();
position.mYPosition = v.vertical.into();
position.mXPosition = v.0.horizontal.into();
position.mYPosition = v.0.vertical.into();
self.gecko.mImage.mPositionXCount = 1;
self.gecko.mImage.mPositionYCount = 1;
}

View file

@ -30,6 +30,7 @@ use super::ComputedValues;
use values::computed::{Angle, LengthOrPercentageOrAuto, LengthOrPercentageOrNone};
use values::computed::{BorderRadiusSize, LengthOrNone};
use values::computed::{CalcLengthOrPercentage, LengthOrPercentage};
use values::computed::position::Position;
// NB: This needs to be here because it needs all the longhands generated
// beforehand.
@ -469,16 +470,23 @@ impl Interpolate for ClipRect {
}
/// https://drafts.csswg.org/css-transitions/#animtype-simple-list
impl Interpolate for BackgroundPosition {
impl Interpolate for Position {
#[inline]
fn interpolate(&self, other: &Self, time: f64) -> Result<Self, ()> {
Ok(BackgroundPosition {
Ok(Position {
horizontal: try!(self.horizontal.interpolate(&other.horizontal, time)),
vertical: try!(self.vertical.interpolate(&other.vertical, time)),
})
}
}
impl Interpolate for BackgroundPosition {
#[inline]
fn interpolate(&self, other: &Self, time: f64) -> Result<Self, ()> {
Ok(BackgroundPosition(try!(self.0.interpolate(&other.0, time))))
}
}
impl Interpolate for BackgroundSize {
fn interpolate(&self, other: &Self, time: f64) -> Result<Self, ()> {
use properties::longhands::background_size::computed_value::ExplicitSize;

View file

@ -80,91 +80,35 @@ ${helpers.predefined_type("background-color", "CSSColor",
use std::fmt;
use values::LocalToCss;
use values::HasViewportPercentage;
use values::specified::position::Position;
pub mod computed_value {
use values::computed::LengthOrPercentage;
use values::computed::position::Position;
#[derive(PartialEq, Copy, Clone, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct T {
pub horizontal: LengthOrPercentage,
pub vertical: LengthOrPercentage,
}
pub struct T(pub Position);
}
impl HasViewportPercentage for SpecifiedValue {
fn has_viewport_percentage(&self) -> bool {
return self.horizontal.has_viewport_percentage() || self.vertical.has_viewport_percentage();
self.0.has_viewport_percentage()
}
}
#[derive(Debug, Clone, PartialEq, Copy)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct SpecifiedValue {
pub horizontal: specified::LengthOrPercentage,
pub vertical: specified::LengthOrPercentage,
}
pub struct SpecifiedValue(pub Position);
impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.horizontal.to_css(dest));
try!(dest.write_str(" "));
try!(self.vertical.to_css(dest));
Ok(())
self.0.to_css(dest)
}
}
impl ToCss for computed_value::T {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.horizontal.to_css(dest));
try!(dest.write_str(" "));
try!(self.vertical.to_css(dest));
Ok(())
}
}
impl SpecifiedValue {
fn new(first: specified::PositionComponent, second: specified::PositionComponent)
-> Result<SpecifiedValue, ()> {
let (horiz, vert) = match (category(first), category(second)) {
// Don't allow two vertical keywords or two horizontal keywords.
(PositionCategory::HorizontalKeyword, PositionCategory::HorizontalKeyword) |
(PositionCategory::VerticalKeyword, PositionCategory::VerticalKeyword) => return Err(()),
// Swap if both are keywords and vertical precedes horizontal.
(PositionCategory::VerticalKeyword, PositionCategory::HorizontalKeyword) |
(PositionCategory::VerticalKeyword, PositionCategory::OtherKeyword) |
(PositionCategory::OtherKeyword, PositionCategory::HorizontalKeyword) => (second, first),
// By default, horizontal is first.
_ => (first, second),
};
Ok(SpecifiedValue {
horizontal: horiz.to_length_or_percentage(),
vertical: vert.to_length_or_percentage(),
})
}
}
// Collapse `Position` into a few categories to simplify the above `match` expression.
enum PositionCategory {
HorizontalKeyword,
VerticalKeyword,
OtherKeyword,
LengthOrPercentage,
}
fn category(p: specified::PositionComponent) -> PositionCategory {
match p {
specified::PositionComponent::Left |
specified::PositionComponent::Right =>
PositionCategory::HorizontalKeyword,
specified::PositionComponent::Top |
specified::PositionComponent::Bottom =>
PositionCategory::VerticalKeyword,
specified::PositionComponent::Center =>
PositionCategory::OtherKeyword,
specified::PositionComponent::LengthOrPercentage(_) =>
PositionCategory::LengthOrPercentage,
self.0.to_css(dest)
}
}
@ -173,27 +117,22 @@ ${helpers.predefined_type("background-color", "CSSColor",
#[inline]
fn to_computed_value(&self, context: &Context) -> computed_value::T {
computed_value::T {
horizontal: self.horizontal.to_computed_value(context),
vertical: self.vertical.to_computed_value(context),
}
computed_value::T(self.0.to_computed_value(context))
}
}
#[inline]
pub fn get_initial_value() -> computed_value::T {
computed_value::T {
use values::computed::position::Position;
computed_value::T(Position {
horizontal: computed::LengthOrPercentage::Percentage(0.0),
vertical: computed::LengthOrPercentage::Percentage(0.0),
}
})
}
pub fn parse(_context: &ParserContext, input: &mut Parser)
-> Result<SpecifiedValue, ()> {
let first = try!(specified::PositionComponent::parse(input));
let second = input.try(specified::PositionComponent::parse)
.unwrap_or(specified::PositionComponent::Center);
SpecifiedValue::new(first, second)
Ok(SpecifiedValue(try!(Position::parse(input))))
}
</%helpers:longhand>

View file

@ -73,11 +73,12 @@ pub mod longhands {
}
pub mod shorthands {
use cssparser::Parser;
use cssparser::{Parser, ToCss};
use std::fmt;
use parser::ParserContext;
use values::specified;
fn parse_four_sides<F, T>(input: &mut Parser, parse_one: F) -> Result<(T, T, T, T), ()>
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, ()>, F: Copy, T: Clone {
// zero or more than four values is invalid.
// one value sets them all
@ -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

@ -97,53 +97,15 @@ pub fn parse_border(context: &ParserContext, input: &mut Parser)
'border-%s-radius' % (corner)
for corner in ['top-left', 'top-right', 'bottom-right', 'bottom-left']
)}">
use app_units::Au;
use values::specified::{Length, LengthOrPercentage};
use values::specified::BorderRadiusSize;
use values::specified::basic_shape::BorderRadius;
let _ignored = context;
fn parse_one_set_of_border_values(mut input: &mut Parser)
-> Result<[LengthOrPercentage; 4], ()> {
let mut count = 0;
let mut values = [LengthOrPercentage::Length(Length::Absolute(Au(0))); 4];
while count < 4 {
if let Ok(value) = input.try(LengthOrPercentage::parse) {
values[count] = value;
count += 1;
} else {
break
}
}
match count {
1 => Ok([values[0], values[0], values[0], values[0]]),
2 => Ok([values[0], values[1], values[0], values[1]]),
3 => Ok([values[0], values[1], values[2], values[1]]),
4 => Ok([values[0], values[1], values[2], values[3]]),
_ => Err(()),
}
}
fn parse_one_set_of_border_radii(mut input: &mut Parser)
-> Result<[BorderRadiusSize; 4], ()> {
let widths = try!(parse_one_set_of_border_values(input));
let mut heights = widths.clone();
let mut radii_values = [BorderRadiusSize::zero(); 4];
if input.try(|input| input.expect_delim('/')).is_ok() {
heights = try!(parse_one_set_of_border_values(input));
}
for i in 0..radii_values.len() {
radii_values[i] = BorderRadiusSize::new(widths[i], heights[i]);
}
Ok(radii_values)
}
let radii = try!(parse_one_set_of_border_radii(input));
let radii = try!(BorderRadius::parse(input));
Ok(Longhands {
border_top_left_radius: Some(radii[0]),
border_top_right_radius: Some(radii[1]),
border_bottom_right_radius: Some(radii[2]),
border_bottom_left_radius: Some(radii[3]),
border_top_left_radius: Some(radii.top_left),
border_top_right_radius: Some(radii.top_right),
border_bottom_right_radius: Some(radii.bottom_right),
border_bottom_left_radius: Some(radii.bottom_left),
})
</%helpers:shorthand>