Move Position into its own values module

This commit is contained in:
Manish Goregaokar 2016-08-01 15:22:04 +05:30
parent 88c1a67d89
commit 44e33bcdc5
No known key found for this signature in database
GPG key ID: 3BBF4D3E2EF79F98
8 changed files with 215 additions and 134 deletions

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>