Skip to content

Commit

Permalink
Generate valid form for {background|mask}-position from their longhands.
Browse files Browse the repository at this point in the history
If there is any longhand consisting of both keyword and position, we
should attach both keyword and position for both longhands when serialize
them.

1. To generate a serialized result with keyword, we add a macro function
`to_css_with_keyword` accepted HorizontalPosition and VerticalPosition as
the parameter to finish that. For the longhands which missing keyword, we
use the default keyword.
(`left` for `*-position-x` and `top` for `*-position-y`)

2. Update `Position::to_css` to generate the valid format by calling
`to_css_with_keyword` with HorizontalPosition and VerticalPosition.

3. Update `to_css` to use new `Position::to_css` in background,
background-position, mask and mask-position.

MozReview-Commit-ID: 5Bnhdsi5yeM
  • Loading branch information
kuoe0 committed Apr 14, 2017
1 parent f537fbd commit 2358e46
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
16 changes: 12 additions & 4 deletions components/style/properties/shorthand/background.mako.rs
Expand Up @@ -159,11 +159,17 @@
}

try!(image.to_css(dest));
% for name in "repeat attachment position_x position_y".split():
% for name in "repeat attachment".split():
try!(write!(dest, " "));
try!(${name}.to_css(dest));
% endfor

try!(write!(dest, " "));
Position {
horizontal: position_x.clone(),
vertical: position_y.clone()
}.to_css(dest)?;

if *size != background_size::single_value::get_initial_specified_value() {
try!(write!(dest, " / "));
try!(size.to_css(dest));
Expand Down Expand Up @@ -225,9 +231,11 @@
return Ok(());
}
for i in 0..len {
self.background_position_x.0[i].to_css(dest)?;
dest.write_str(" ")?;
self.background_position_y.0[i].to_css(dest)?;
Position {
horizontal: self.background_position_x.0[i].clone(),
vertical: self.background_position_y.0[i].clone()
}.to_css(dest)?;

if i < len - 1 {
dest.write_str(", ")?;
}
Expand Down
17 changes: 11 additions & 6 deletions components/style/properties/shorthand/mask.mako.rs
Expand Up @@ -148,9 +148,12 @@
dest.write_str(" ")?;
mode.to_css(dest)?;
dest.write_str(" ")?;
position_x.to_css(dest)?;
dest.write_str(" ")?;
position_y.to_css(dest)?;

Position {
horizontal: position_x.clone(),
vertical: position_y.clone()
}.to_css(dest)?;

if *size != mask_size::single_value::get_initial_specified_value() {
dest.write_str(" / ")?;
size.to_css(dest)?;
Expand Down Expand Up @@ -218,9 +221,11 @@
}

for i in 0..len {
self.mask_position_x.0[i].to_css(dest)?;
dest.write_str(" ")?;
self.mask_position_y.0[i].to_css(dest)?;
Position {
horizontal: self.mask_position_x.0[i].clone(),
vertical: self.mask_position_y.0[i].clone()
}.to_css(dest)?;

if i < len - 1 {
dest.write_str(", ")?;
}
Expand Down
52 changes: 31 additions & 21 deletions components/style/values/specified/position.rs
Expand Up @@ -32,27 +32,36 @@ pub struct Position {

impl ToCss for Position {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
let mut space_present = false;
if let Some(horiz_key) = self.horizontal.keyword {
try!(horiz_key.to_css(dest));
try!(dest.write_str(" "));
space_present = true;
};
if let Some(ref horiz_pos) = self.horizontal.position {
try!(horiz_pos.to_css(dest));
try!(dest.write_str(" "));
space_present = true;
};
if let Some(vert_key) = self.vertical.keyword {
try!(vert_key.to_css(dest));
space_present = false;
};
if let Some(ref vert_pos) = self.vertical.position {
if space_present == false {
try!(dest.write_str(" "));
}
try!(vert_pos.to_css(dest));
};
macro_rules! to_css_with_keyword {
($pos:expr, $default_keyword:expr) => {
$pos.keyword.unwrap_or($default_keyword).to_css(dest)?;

if let Some(ref position) = $pos.position {
dest.write_str(" ")?;
position.to_css(dest)?;
};
};
}

let mut is_keyword_needed = false;
let position_x = &self.horizontal;
let position_y = &self.vertical;

if (position_x.keyword.is_some() && position_x.position.is_some()) ||
(position_y.keyword.is_some() && position_y.position.is_some()) {
is_keyword_needed = true;
}

if is_keyword_needed {
to_css_with_keyword!(position_x, Keyword::Left);
dest.write_str(" ")?;
to_css_with_keyword!(position_y, Keyword::Top);
} else {
position_x.to_css(dest)?;
dest.write_str(" ")?;
position_y.to_css(dest)?;
}

Ok(())
}
}
Expand Down Expand Up @@ -403,6 +412,7 @@ impl ToCss for VerticalPosition {
};
Ok(())
}

}

impl Parse for VerticalPosition {
Expand Down

0 comments on commit 2358e46

Please sign in to comment.