mirror of
https://github.com/servo/servo.git
synced 2025-08-08 15:05:35 +01:00
Add from_computed_value() function for converting computed values to specified
This commit is contained in:
parent
153ec64c15
commit
d81c6af59c
17 changed files with 555 additions and 1 deletions
|
@ -41,6 +41,13 @@ pub trait ToComputedValue {
|
|||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, _context: &Context) -> Self::ComputedValue;
|
||||
|
||||
#[inline]
|
||||
/// Convert a computed value to specified value form.
|
||||
///
|
||||
/// This will be used for recascading during animation.
|
||||
/// Such from_computed_valued values should recompute to the same value.
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self;
|
||||
}
|
||||
|
||||
pub trait ComputedValueAsSpecified {}
|
||||
|
@ -52,6 +59,11 @@ impl<T> ToComputedValue for T where T: ComputedValueAsSpecified + Clone {
|
|||
fn to_computed_value(&self, _context: &Context) -> T {
|
||||
self.clone()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &T) -> Self {
|
||||
computed.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToComputedValue for specified::CSSColor {
|
||||
|
@ -61,6 +73,14 @@ impl ToComputedValue for specified::CSSColor {
|
|||
fn to_computed_value(&self, _context: &Context) -> CSSColor {
|
||||
self.parsed
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &CSSColor) -> Self {
|
||||
specified::CSSColor {
|
||||
parsed: *computed,
|
||||
authored: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ComputedValueAsSpecified for specified::BorderStyle {}
|
||||
|
@ -82,6 +102,11 @@ impl ToComputedValue for specified::Length {
|
|||
length.to_computed_value(context.style().get_font().clone_font_size())
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Au) -> Self {
|
||||
specified::Length::Absolute(*computed)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Copy, Debug)]
|
||||
|
@ -170,6 +195,15 @@ impl ToComputedValue for specified::CalcLengthOrPercentage {
|
|||
context.style().root_font_size())
|
||||
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &CalcLengthOrPercentage) -> Self {
|
||||
specified::CalcLengthOrPercentage {
|
||||
absolute: computed.length,
|
||||
percentage: computed.percentage.map(specified::Percentage),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -192,6 +226,13 @@ impl ToComputedValue for specified::BorderRadiusSize {
|
|||
let h = self.0.height.to_computed_value(context);
|
||||
BorderRadiusSize(Size2D::new(w, h))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &BorderRadiusSize) -> Self {
|
||||
let w = ToComputedValue::from_computed_value(&computed.0.width);
|
||||
let h = ToComputedValue::from_computed_value(&computed.0.height);
|
||||
specified::BorderRadiusSize(Size2D::new(w, h))
|
||||
}
|
||||
}
|
||||
|
||||
impl ::cssparser::ToCss for BorderRadiusSize {
|
||||
|
@ -264,6 +305,24 @@ impl ToComputedValue for specified::LengthOrPercentage {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn from_computed_value(computed: &LengthOrPercentage) -> Self {
|
||||
match *computed {
|
||||
LengthOrPercentage::Length(value) => {
|
||||
specified::LengthOrPercentage::Length(
|
||||
ToComputedValue::from_computed_value(&value)
|
||||
)
|
||||
}
|
||||
LengthOrPercentage::Percentage(value) => {
|
||||
specified::LengthOrPercentage::Percentage(specified::Percentage(value))
|
||||
}
|
||||
LengthOrPercentage::Calc(calc) => {
|
||||
specified::LengthOrPercentage::Calc(
|
||||
ToComputedValue::from_computed_value(&calc)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::cssparser::ToCss for LengthOrPercentage {
|
||||
|
@ -331,6 +390,26 @@ impl ToComputedValue for specified::LengthOrPercentageOrAuto {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &LengthOrPercentageOrAuto) -> Self {
|
||||
match *computed {
|
||||
LengthOrPercentageOrAuto::Auto => specified::LengthOrPercentageOrAuto::Auto,
|
||||
LengthOrPercentageOrAuto::Length(value) => {
|
||||
specified::LengthOrPercentageOrAuto::Length(
|
||||
ToComputedValue::from_computed_value(&value)
|
||||
)
|
||||
}
|
||||
LengthOrPercentageOrAuto::Percentage(value) => {
|
||||
specified::LengthOrPercentageOrAuto::Percentage(specified::Percentage(value))
|
||||
}
|
||||
LengthOrPercentageOrAuto::Calc(calc) => {
|
||||
specified::LengthOrPercentageOrAuto::Calc(
|
||||
ToComputedValue::from_computed_value(&calc)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::cssparser::ToCss for LengthOrPercentageOrAuto {
|
||||
|
@ -390,6 +469,32 @@ impl ToComputedValue for specified::LengthOrPercentageOrAutoOrContent {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &LengthOrPercentageOrAutoOrContent) -> Self {
|
||||
match *computed {
|
||||
LengthOrPercentageOrAutoOrContent::Auto => {
|
||||
specified::LengthOrPercentageOrAutoOrContent::Auto
|
||||
}
|
||||
LengthOrPercentageOrAutoOrContent::Content => {
|
||||
specified::LengthOrPercentageOrAutoOrContent::Content
|
||||
}
|
||||
LengthOrPercentageOrAutoOrContent::Length(value) => {
|
||||
specified::LengthOrPercentageOrAutoOrContent::Length(
|
||||
ToComputedValue::from_computed_value(&value)
|
||||
)
|
||||
}
|
||||
LengthOrPercentageOrAutoOrContent::Percentage(value) => {
|
||||
specified::LengthOrPercentageOrAutoOrContent::Percentage(specified::Percentage(value))
|
||||
}
|
||||
LengthOrPercentageOrAutoOrContent::Calc(calc) => {
|
||||
specified::LengthOrPercentageOrAutoOrContent::Calc(
|
||||
ToComputedValue::from_computed_value(&calc)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::cssparser::ToCss for LengthOrPercentageOrAutoOrContent {
|
||||
|
@ -445,6 +550,26 @@ impl ToComputedValue for specified::LengthOrPercentageOrNone {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &LengthOrPercentageOrNone) -> Self {
|
||||
match *computed {
|
||||
LengthOrPercentageOrNone::None => specified::LengthOrPercentageOrNone::None,
|
||||
LengthOrPercentageOrNone::Length(value) => {
|
||||
specified::LengthOrPercentageOrNone::Length(
|
||||
ToComputedValue::from_computed_value(&value)
|
||||
)
|
||||
}
|
||||
LengthOrPercentageOrNone::Percentage(value) => {
|
||||
specified::LengthOrPercentageOrNone::Percentage(specified::Percentage(value))
|
||||
}
|
||||
LengthOrPercentageOrNone::Calc(calc) => {
|
||||
specified::LengthOrPercentageOrNone::Calc(
|
||||
ToComputedValue::from_computed_value(&calc)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::cssparser::ToCss for LengthOrPercentageOrNone {
|
||||
|
@ -492,6 +617,18 @@ impl ToComputedValue for specified::LengthOrNone {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &LengthOrNone) -> Self {
|
||||
match *computed {
|
||||
LengthOrNone::Length(au) => {
|
||||
specified::LengthOrNone::Length(ToComputedValue::from_computed_value(&au))
|
||||
}
|
||||
LengthOrNone::None => {
|
||||
specified::LengthOrNone::None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::cssparser::ToCss for LengthOrNone {
|
||||
|
@ -517,6 +654,20 @@ impl ToComputedValue for specified::Image {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Image) -> Self {
|
||||
match *computed {
|
||||
Image::Url(ref url, ref extra_data) => {
|
||||
specified::Image::Url(url.clone(), extra_data.clone())
|
||||
},
|
||||
Image::LinearGradient(ref linear_gradient) => {
|
||||
specified::Image::LinearGradient(
|
||||
ToComputedValue::from_computed_value(linear_gradient)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -638,6 +789,25 @@ impl ToComputedValue for specified::LinearGradient {
|
|||
}).collect()
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &LinearGradient) -> Self {
|
||||
let LinearGradient {
|
||||
angle_or_corner,
|
||||
ref stops
|
||||
} = *computed;
|
||||
specified::LinearGradient {
|
||||
angle_or_corner: angle_or_corner,
|
||||
stops: stops.iter().map(|stop| {
|
||||
specified::ColorStop {
|
||||
color: ToComputedValue::from_computed_value(&stop.color),
|
||||
position: match stop.position {
|
||||
None => None,
|
||||
Some(value) => Some(ToComputedValue::from_computed_value(&value)),
|
||||
},
|
||||
}
|
||||
}).collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
pub type Length = Au;
|
||||
pub type Number = CSSFloat;
|
||||
|
|
|
@ -94,6 +94,7 @@ impl<T: Parse + PartialEq + Copy> ShapeSource<T> {
|
|||
|
||||
impl<T: ToComputedValue> ToComputedValue for ShapeSource<T> {
|
||||
type ComputedValue = computed_basic_shape::ShapeSource<T::ComputedValue>;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, cx: &Context) -> Self::ComputedValue {
|
||||
match *self {
|
||||
|
@ -111,6 +112,24 @@ impl<T: ToComputedValue> ToComputedValue for ShapeSource<T> {
|
|||
ShapeSource::None => computed_basic_shape::ShapeSource::None,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
match *computed {
|
||||
computed_basic_shape::ShapeSource::Url(ref url, ref data) => {
|
||||
ShapeSource::Url(url.clone(), data.clone())
|
||||
}
|
||||
computed_basic_shape::ShapeSource::Shape(ref shape, ref reference) => {
|
||||
ShapeSource::Shape(
|
||||
ToComputedValue::from_computed_value(shape),
|
||||
reference.as_ref().map(|r| ToComputedValue::from_computed_value(r)))
|
||||
}
|
||||
computed_basic_shape::ShapeSource::Box(ref reference) => {
|
||||
ShapeSource::Box(ToComputedValue::from_computed_value(reference))
|
||||
}
|
||||
computed_basic_shape::ShapeSource::None => ShapeSource::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
|
@ -169,6 +188,23 @@ impl ToComputedValue for BasicShape {
|
|||
BasicShape::Polygon(ref poly) => computed_basic_shape::BasicShape::Polygon(poly.to_computed_value(cx)),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
match *computed {
|
||||
computed_basic_shape::BasicShape::Inset(ref rect) => {
|
||||
BasicShape::Inset(ToComputedValue::from_computed_value(rect))
|
||||
}
|
||||
computed_basic_shape::BasicShape::Circle(ref circle) => {
|
||||
BasicShape::Circle(ToComputedValue::from_computed_value(circle))
|
||||
}
|
||||
computed_basic_shape::BasicShape::Ellipse(ref e) => {
|
||||
BasicShape::Ellipse(ToComputedValue::from_computed_value(e))
|
||||
}
|
||||
computed_basic_shape::BasicShape::Polygon(ref poly) => {
|
||||
BasicShape::Polygon(ToComputedValue::from_computed_value(poly))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Copy, Debug)]
|
||||
|
@ -239,6 +275,17 @@ impl ToComputedValue for InsetRect {
|
|||
round: self.round.map(|r| r.to_computed_value(cx)),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
InsetRect {
|
||||
top: ToComputedValue::from_computed_value(&computed.top),
|
||||
right: ToComputedValue::from_computed_value(&computed.right),
|
||||
bottom: ToComputedValue::from_computed_value(&computed.bottom),
|
||||
left: ToComputedValue::from_computed_value(&computed.left),
|
||||
round: computed.round.map(|ref r| ToComputedValue::from_computed_value(r)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-shapes/#basic-shape-serialization
|
||||
|
@ -384,6 +431,14 @@ impl ToComputedValue for Circle {
|
|||
position: self.position.to_computed_value(cx),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
Circle {
|
||||
radius: ToComputedValue::from_computed_value(&computed.radius),
|
||||
position: ToComputedValue::from_computed_value(&computed.position),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Copy, Debug)]
|
||||
|
@ -454,6 +509,15 @@ impl ToComputedValue for Ellipse {
|
|||
position: self.position.to_computed_value(cx),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
Ellipse {
|
||||
semiaxis_x: ToComputedValue::from_computed_value(&computed.semiaxis_x),
|
||||
semiaxis_y: ToComputedValue::from_computed_value(&computed.semiaxis_y),
|
||||
position: ToComputedValue::from_computed_value(&computed.position),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -528,6 +592,19 @@ impl ToComputedValue for Polygon {
|
|||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
Polygon {
|
||||
fill: ToComputedValue::from_computed_value(&computed.fill),
|
||||
coordinates: computed.coordinates.iter()
|
||||
.map(|c| {
|
||||
(ToComputedValue::from_computed_value(&c.0),
|
||||
ToComputedValue::from_computed_value(&c.1))
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-shapes/#typedef-shape-radius
|
||||
|
@ -582,6 +659,17 @@ impl ToComputedValue for ShapeRadius {
|
|||
ShapeRadius::FarthestSide => computed_basic_shape::ShapeRadius::FarthestSide,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
match *computed {
|
||||
computed_basic_shape::ShapeRadius::Length(ref lop) => {
|
||||
ShapeRadius::Length(ToComputedValue::from_computed_value(lop))
|
||||
}
|
||||
computed_basic_shape::ShapeRadius::ClosestSide => ShapeRadius::ClosestSide,
|
||||
computed_basic_shape::ShapeRadius::FarthestSide => ShapeRadius::FarthestSide,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-backgrounds-3/#border-radius
|
||||
|
@ -674,6 +762,16 @@ impl ToComputedValue for BorderRadius {
|
|||
bottom_left: self.bottom_left.to_computed_value(cx),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
BorderRadius {
|
||||
top_left: ToComputedValue::from_computed_value(&computed.top_left),
|
||||
top_right: ToComputedValue::from_computed_value(&computed.top_right),
|
||||
bottom_right: ToComputedValue::from_computed_value(&computed.bottom_right),
|
||||
bottom_left: ToComputedValue::from_computed_value(&computed.bottom_left),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-shapes/#typedef-fill-rule
|
||||
|
|
|
@ -456,7 +456,7 @@ enum CalcUnit {
|
|||
Time,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Copy, Debug)]
|
||||
#[derive(Clone, PartialEq, Copy, Debug, Default)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
pub struct CalcLengthOrPercentage {
|
||||
pub absolute: Option<Au>,
|
||||
|
@ -1565,6 +1565,11 @@ impl ToComputedValue for Number {
|
|||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, _: &Context) -> CSSFloat { self.0 }
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &CSSFloat) -> Self {
|
||||
Number(*computed)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToCss for Number {
|
||||
|
@ -1598,6 +1603,11 @@ impl ToComputedValue for Opacity {
|
|||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &CSSFloat) -> Self {
|
||||
Opacity(*computed)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToCss for Opacity {
|
||||
|
|
|
@ -329,6 +329,16 @@ impl ToComputedValue for Position {
|
|||
vertical: vertical,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &computed_position::Position) -> Position {
|
||||
Position {
|
||||
horiz_keyword: None,
|
||||
horiz_position: Some(ToComputedValue::from_computed_value(&computed.horizontal)),
|
||||
vert_keyword: None,
|
||||
vert_position: Some(ToComputedValue::from_computed_value(&computed.vertical)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HasViewportPercentage for PositionComponent {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue