Skip to content

Commit

Permalink
Make background-position a shorthand instead of a longhand
Browse files Browse the repository at this point in the history
  • Loading branch information
canova committed Dec 14, 2016
1 parent b302642 commit ed806ef
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 104 deletions.
7 changes: 4 additions & 3 deletions components/layout/display_list_builder.rs
Expand Up @@ -755,11 +755,12 @@ impl FragmentDisplayListBuilding for Fragment {
}
};

let position = *get_cyclic(&background.background_position.0, index);
let horiz_position = *get_cyclic(&background.background_position_x.0, index);
let vert_position = *get_cyclic(&background.background_position_y.0, index);
// Use `background-position` to get the offset.
let horizontal_position = model::specified(position.horizontal,
let horizontal_position = model::specified(horiz_position.0,
bounds.size.width - image_size.width);
let vertical_position = model::specified(position.vertical,
let vertical_position = model::specified(vert_position.0,
bounds.size.height - image_size.height);

// The anchor position for this background, based on both the background-attachment
Expand Down
4 changes: 4 additions & 0 deletions components/script/dom/webidls/CSSStyleDeclaration.webidl
Expand Up @@ -38,6 +38,10 @@ partial interface CSSStyleDeclaration {
[SetterThrows, TreatNullAs=EmptyString] attribute DOMString background-color;
[SetterThrows, TreatNullAs=EmptyString] attribute DOMString backgroundPosition;
[SetterThrows, TreatNullAs=EmptyString] attribute DOMString background-position;
[SetterThrows, TreatNullAs=EmptyString] attribute DOMString backgroundPositionX;
[SetterThrows, TreatNullAs=EmptyString] attribute DOMString background-position-x;
[SetterThrows, TreatNullAs=EmptyString] attribute DOMString backgroundPositionY;
[SetterThrows, TreatNullAs=EmptyString] attribute DOMString background-position-y;
[SetterThrows, TreatNullAs=EmptyString] attribute DOMString backgroundRepeat;
[SetterThrows, TreatNullAs=EmptyString] attribute DOMString background-repeat;
[SetterThrows, TreatNullAs=EmptyString] attribute DOMString backgroundImage;
Expand Down
4 changes: 3 additions & 1 deletion components/style/properties/gecko.mako.rs
Expand Up @@ -1384,6 +1384,7 @@ fn static_assert() {
}
</%self:simple_image_array_property>

% if shorthand != "background":
pub fn copy_${shorthand}_position_from(&mut self, other: &Self) {
use gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType;

Expand Down Expand Up @@ -1419,7 +1420,7 @@ fn static_assert() {
pub fn clone_${shorthand}_position(&self)
-> longhands::${shorthand}_position::computed_value::T {
use values::computed::position::Position;
longhands::background_position::computed_value::T(
longhands::${shorthand}_position::computed_value::T(
self.gecko.${image_layers_field}.mLayers.iter()
.take(self.gecko.${image_layers_field}.mPositionXCount as usize)
.take(self.gecko.${image_layers_field}.mPositionYCount as usize)
Expand Down Expand Up @@ -1448,6 +1449,7 @@ fn static_assert() {
geckolayer.mPosition.mYPosition = servo.vertical.into();
}
}
% endif

<%self:simple_image_array_property name="size" shorthand="${shorthand}" field_name="mSize">
use gecko_bindings::structs::nsStyleImageLayers_Size_Dimension;
Expand Down
35 changes: 12 additions & 23 deletions components/style/properties/helpers/animated_properties.mako.rs
Expand Up @@ -7,7 +7,8 @@ use cssparser::{Color as CSSParserColor, Parser, RGBA};
use euclid::{Point2D, Size2D};
use properties::PropertyDeclaration;
use properties::longhands;
use properties::longhands::background_position::computed_value::T as BackgroundPosition;
use properties::longhands::background_position_x::computed_value::T as BackgroundPositionX;
use properties::longhands::background_position_y::computed_value::T as BackgroundPositionY;
use properties::longhands::background_size::computed_value::T as BackgroundSize;
use properties::longhands::font_weight::computed_value::T as FontWeight;
use properties::longhands::line_height::computed_value::T as LineHeight;
Expand Down Expand Up @@ -574,13 +575,6 @@ impl Interpolate for Position {

impl RepeatableListInterpolate for Position {}

impl Interpolate for BackgroundPosition {
#[inline]
fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
Ok(BackgroundPosition(try!(self.0.interpolate(&other.0, progress))))
}
}

/// https://drafts.csswg.org/css-transitions/#animtype-simple-list
impl Interpolate for HorizontalPosition {
#[inline]
Expand All @@ -601,24 +595,19 @@ impl Interpolate for VerticalPosition {

impl RepeatableListInterpolate for VerticalPosition {}

% if product == "gecko":
use properties::longhands::background_position_x::computed_value::T as BackgroundPositionX;
use properties::longhands::background_position_y::computed_value::T as BackgroundPositionY;

impl Interpolate for BackgroundPositionX {
#[inline]
fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
Ok(BackgroundPositionX(try!(self.0.interpolate(&other.0, progress))))
}
impl Interpolate for BackgroundPositionX {
#[inline]
fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
Ok(BackgroundPositionX(try!(self.0.interpolate(&other.0, progress))))
}
}

impl Interpolate for BackgroundPositionY {
#[inline]
fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
Ok(BackgroundPositionY(try!(self.0.interpolate(&other.0, progress))))
}
impl Interpolate for BackgroundPositionY {
#[inline]
fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
Ok(BackgroundPositionY(try!(self.0.interpolate(&other.0, progress))))
}
% endif
}

/// https://drafts.csswg.org/css-transitions/#animtype-shadow-list
impl Interpolate for TextShadow {
Expand Down
49 changes: 2 additions & 47 deletions components/style/properties/longhand/background.mako.rs
Expand Up @@ -86,52 +86,7 @@ ${helpers.predefined_type("background-color", "CSSColor",
}
</%helpers:vector_longhand>

<%helpers:vector_longhand name="background-position" animatable="True">
use std::fmt;
use style_traits::ToCss;
use values::HasViewportPercentage;
use values::specified::position::Position;

pub mod computed_value {
use values::computed::position::Position;
use properties::animated_properties::{Interpolate, RepeatableListInterpolate};

pub type T = Position;
}

pub type SpecifiedValue = Position;

#[inline]
pub fn get_initial_value() -> computed_value::T {
use values::computed::position::Position;
Position {
horizontal: computed::LengthOrPercentage::Percentage(0.0),
vertical: computed::LengthOrPercentage::Percentage(0.0),
}
}
#[inline]
pub fn get_initial_specified_value() -> SpecifiedValue {
use values::specified::Percentage;
use values::specified::position::{HorizontalPosition, VerticalPosition};
Position {
horizontal: HorizontalPosition {
keyword: None,
position: Some(specified::LengthOrPercentage::Percentage(Percentage(0.0))),
},
vertical: VerticalPosition {
keyword: None,
position: Some(specified::LengthOrPercentage::Percentage(Percentage(0.0))),
},
}
}

pub fn parse(context: &ParserContext, input: &mut Parser)
-> Result<SpecifiedValue, ()> {
Ok(try!(Position::parse(context, input)))
}
</%helpers:vector_longhand>

<%helpers:vector_longhand name="background-position-x" products="gecko" animatable="True">
<%helpers:vector_longhand name="background-position-x" animatable="True">
use std::fmt;
use style_traits::ToCss;
use values::HasViewportPercentage;
Expand Down Expand Up @@ -174,7 +129,7 @@ ${helpers.predefined_type("background-color", "CSSColor",
}
</%helpers:vector_longhand>

<%helpers:vector_longhand name="background-position-y" products="gecko" animatable="True">
<%helpers:vector_longhand name="background-position-y" animatable="True">
use std::fmt;
use style_traits::ToCss;
use values::HasViewportPercentage;
Expand Down
56 changes: 47 additions & 9 deletions components/style/properties/longhand/svg.mako.rs
Expand Up @@ -91,21 +91,59 @@ ${helpers.single_keyword("mask-repeat",
products="gecko",
animatable=False)}

<%helpers:longhand name="mask-position" products="gecko" animatable="True">
use properties::longhands::background_position;
pub use ::properties::longhands::background_position::SpecifiedValue;
pub use ::properties::longhands::background_position::single_value as single_value;
pub use ::properties::longhands::background_position::computed_value as computed_value;
<%helpers:vector_longhand name="mask-position" products="gecko" animatable="True">
use std::fmt;
use style_traits::ToCss;
use values::HasViewportPercentage;
use values::specified::position::Position;

pub mod computed_value {
use values::computed::position::Position;
use properties::animated_properties::{Interpolate, RepeatableListInterpolate};
use properties::longhands::mask_position::computed_value::T as MaskPosition;

pub type T = Position;

impl RepeatableListInterpolate for MaskPosition {}

impl Interpolate for MaskPosition {
fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
Ok(MaskPosition(try!(self.0.interpolate(&other.0, progress))))
}
}
}

pub type SpecifiedValue = Position;

#[inline]
pub fn get_initial_value() -> computed_value::T {
background_position::get_initial_value()
use values::computed::position::Position;
Position {
horizontal: computed::LengthOrPercentage::Percentage(0.0),
vertical: computed::LengthOrPercentage::Percentage(0.0),
}
}
#[inline]
pub fn get_initial_specified_value() -> SpecifiedValue {
use values::specified::Percentage;
use values::specified::position::{HorizontalPosition, VerticalPosition};
Position {
horizontal: HorizontalPosition {
keyword: None,
position: Some(specified::LengthOrPercentage::Percentage(Percentage(0.0))),
},
vertical: VerticalPosition {
keyword: None,
position: Some(specified::LengthOrPercentage::Percentage(Percentage(0.0))),
},
}
}

pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
background_position::parse(context, input)
pub fn parse(context: &ParserContext, input: &mut Parser)
-> Result<SpecifiedValue, ()> {
Position::parse(context, input)
}
</%helpers:longhand>
</%helpers:vector_longhand>

// missing: margin-box fill-box stroke-box view-box no-clip
// (gecko doesn't implement these)
Expand Down

0 comments on commit ed806ef

Please sign in to comment.