Auto merge of #14509 - canaltinova:position, r=Manishearth

Implement background-position-x/y

<!-- Please describe your changes on the following line: -->
This is a WIP PR. Just HorizontalPosition / VerticalPosition implementations are complete. I would like to get early feedbacks about this architecture. Here's some architectural topics to consider:

- I created `HorizontalPosition` and `VerticalPosition` structs for this and used them in `Position` as well. We have decided to split `Keyword` enum, but we need them as unified for `PositionComponent` enum. So I didn't split but I can split it if we prefer to change PositionComponent as well.
- If we prefer Keyword enum like this, we can create a SubPosition(or something like this) instead of HorizontalPosition/VerticalPosition enums since only difference is 2 lines in `parse` functions. We can create a `parse_horizontal` and `parse_vertical` instead and a lot of code duplication can be cleared.
- I couldn't find a good way to use HorizontalPosition/VerticalPosition's parse functions in `Position`'s parse function. It is a bit more complicated. I'm open to suggestions :)
- I don't know much about logical keywords so do I need to do something different? I placed some comments where logical keywords are processing.

Any advice about these?

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #14458 (github issue number if applicable).

<!-- Either: -->
- [X] There are tests for these changes

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- 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/14509)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-12-15 12:15:06 -08:00 committed by GitHub
commit 5357f05ff7
17 changed files with 883 additions and 254 deletions

View file

@ -7,7 +7,8 @@ use media_queries::CSSErrorReporterTest;
use servo_url::ServoUrl;
use style::parser::ParserContext;
use style::properties::longhands::{background_attachment, background_clip, background_color, background_image};
use style::properties::longhands::{background_origin, background_position, background_repeat, background_size};
use style::properties::longhands::{background_origin, background_position_x, background_position_y, background_repeat};
use style::properties::longhands::background_size;
use style::properties::shorthands::background;
use style::stylesheets::Origin;
@ -20,7 +21,8 @@ fn background_shorthand_should_parse_all_available_properties_when_specified() {
let result = background::parse_value(&context, &mut parser).unwrap();
assert_eq!(result.background_image.unwrap(), parse_longhand!(background_image, "url(\"http://servo/test.png\")"));
assert_eq!(result.background_position.unwrap(), parse_longhand!(background_position, "top center"));
assert_eq!(result.background_position_x.unwrap(), parse_longhand!(background_position_x, "center"));
assert_eq!(result.background_position_y.unwrap(), parse_longhand!(background_position_y, "top"));
assert_eq!(result.background_size.unwrap(), parse_longhand!(background_size, "200px 200px"));
assert_eq!(result.background_repeat.unwrap(), parse_longhand!(background_repeat, "repeat-x"));
assert_eq!(result.background_attachment.unwrap(), parse_longhand!(background_attachment, "fixed"));
@ -36,7 +38,8 @@ fn background_shorthand_should_parse_when_some_fields_set() {
let mut parser = Parser::new("14px 40px repeat-y");
let result = background::parse_value(&context, &mut parser).unwrap();
assert_eq!(result.background_position.unwrap(), parse_longhand!(background_position, "14px 40px"));
assert_eq!(result.background_position_x.unwrap(), parse_longhand!(background_position_x, "14px"));
assert_eq!(result.background_position_y.unwrap(), parse_longhand!(background_position_y, "40px"));
assert_eq!(result.background_repeat.unwrap(), parse_longhand!(background_repeat, "repeat-y"));
let mut parser = Parser::new("url(\"http://servo/test.png\") repeat blue");
@ -68,8 +71,8 @@ fn background_shorthand_should_parse_comma_separated_declarations() {
assert_eq!(result.background_image.unwrap(), parse_longhand!(background_image, "url(\"http://servo/test.png\"), \
url(\"http://servo/test.png\"), none"));
assert_eq!(result.background_position.unwrap(), parse_longhand!(background_position, "left top, center center, \
0% 0%"));
assert_eq!(result.background_position_x.unwrap(), parse_longhand!(background_position_x, "left, center, 0%"));
assert_eq!(result.background_position_y.unwrap(), parse_longhand!(background_position_y, "top, center, 0%"));
assert_eq!(result.background_repeat.unwrap(), parse_longhand!(background_repeat, "no-repeat, no-repeat, repeat"));
assert_eq!(result.background_clip.unwrap(), parse_longhand!(background_clip, "border-box, border-box, border-box"));
assert_eq!(result.background_origin.unwrap(), parse_longhand!(background_origin, "padding-box, padding-box, \
@ -86,12 +89,14 @@ fn background_shorthand_should_parse_position_and_size_correctly() {
let mut parser = Parser::new("7px 4px");
let result = background::parse_value(&context, &mut parser).unwrap();
assert_eq!(result.background_position.unwrap(), parse_longhand!(background_position, "7px 4px"));
assert_eq!(result.background_position_x.unwrap(), parse_longhand!(background_position_x, "7px"));
assert_eq!(result.background_position_y.unwrap(), parse_longhand!(background_position_y, "4px"));
let mut parser = Parser::new("7px 4px / 30px 20px");
let result = background::parse_value(&context, &mut parser).unwrap();
assert_eq!(result.background_position.unwrap(), parse_longhand!(background_position, "7px 4px"));
assert_eq!(result.background_position_x.unwrap(), parse_longhand!(background_position_x, "7px"));
assert_eq!(result.background_position_y.unwrap(), parse_longhand!(background_position_y, "4px"));
assert_eq!(result.background_size.unwrap(), parse_longhand!(background_size, "30px 20px"));
let mut parser = Parser::new("/ 30px 20px");

View file

@ -61,4 +61,79 @@ fn test_position() {
assert!(parse(Position::parse, "top 10px bottom").is_err());
assert!(parse(Position::parse, "top 10px bottom 15%").is_err());
// Logical keywords are not supported in Position yet
assert!(parse(Position::parse, "x-start").is_err());
assert!(parse(Position::parse, "y-end").is_err());
assert!(parse(Position::parse, "x-start y-end").is_err());
assert!(parse(Position::parse, "x-end 10px").is_err());
assert!(parse(Position::parse, "y-start 20px").is_err());
assert!(parse(Position::parse, "x-start bottom 10%").is_err());
assert!(parse(Position::parse, "left y-start 10%").is_err());
assert!(parse(Position::parse, "x-start 20px y-end 10%").is_err());
}
#[test]
fn test_horizontal_position() {
// One value serializations.
assert_roundtrip_with_context!(HorizontalPosition::parse, "20px", "20px");
assert_roundtrip_with_context!(HorizontalPosition::parse, "25%", "25%");
assert_roundtrip_with_context!(HorizontalPosition::parse, "center", "center");
assert_roundtrip_with_context!(HorizontalPosition::parse, "left", "left");
assert_roundtrip_with_context!(HorizontalPosition::parse, "right", "right");
assert_roundtrip_with_context!(HorizontalPosition::parse, "x-start", "x-start");
assert_roundtrip_with_context!(HorizontalPosition::parse, "x-end", "x-end");
// Two value serializations.
assert_roundtrip_with_context!(HorizontalPosition::parse, "right 10px", "right 10px");
assert_roundtrip_with_context!(HorizontalPosition::parse, "10px left", "left 10px");
assert_roundtrip_with_context!(HorizontalPosition::parse, "x-end 20%", "x-end 20%");
assert_roundtrip_with_context!(HorizontalPosition::parse, "20px x-start", "x-start 20px");
// Invalid horizontal positions.
assert!(parse(HorizontalPosition::parse, "top").is_err());
assert!(parse(HorizontalPosition::parse, "bottom").is_err());
assert!(parse(HorizontalPosition::parse, "y-start").is_err());
assert!(parse(HorizontalPosition::parse, "y-end").is_err());
assert!(parse(HorizontalPosition::parse, "20px y-end").is_err());
assert!(parse(HorizontalPosition::parse, "y-end 20px ").is_err());
assert!(parse(HorizontalPosition::parse, "bottom 20px").is_err());
assert!(parse(HorizontalPosition::parse, "20px top").is_err());
assert!(parse(HorizontalPosition::parse, "left center").is_err());
assert!(parse(HorizontalPosition::parse, "bottom top").is_err());
assert!(parse(HorizontalPosition::parse, "left top").is_err());
assert!(parse(HorizontalPosition::parse, "left right").is_err());
assert!(parse(HorizontalPosition::parse, "20px 30px").is_err());
}
#[test]
fn test_vertical_position() {
// One value serializations.
assert_roundtrip_with_context!(VerticalPosition::parse, "20px", "20px");
assert_roundtrip_with_context!(VerticalPosition::parse, "25%", "25%");
assert_roundtrip_with_context!(VerticalPosition::parse, "center", "center");
assert_roundtrip_with_context!(VerticalPosition::parse, "top", "top");
assert_roundtrip_with_context!(VerticalPosition::parse, "bottom", "bottom");
assert_roundtrip_with_context!(VerticalPosition::parse, "y-start", "y-start");
assert_roundtrip_with_context!(VerticalPosition::parse, "y-end", "y-end");
// Two value serializations.
assert_roundtrip_with_context!(VerticalPosition::parse, "bottom 10px", "bottom 10px");
assert_roundtrip_with_context!(VerticalPosition::parse, "10px top", "top 10px");
assert_roundtrip_with_context!(VerticalPosition::parse, "y-end 20%", "y-end 20%");
assert_roundtrip_with_context!(VerticalPosition::parse, "20px y-start", "y-start 20px");
// Invalid vertical positions.
assert!(parse(VerticalPosition::parse, "left").is_err());
assert!(parse(VerticalPosition::parse, "right").is_err());
assert!(parse(VerticalPosition::parse, "x-start").is_err());
assert!(parse(VerticalPosition::parse, "x-end").is_err());
assert!(parse(VerticalPosition::parse, "20px x-end").is_err());
assert!(parse(VerticalPosition::parse, "x-end 20px ").is_err());
assert!(parse(VerticalPosition::parse, "left 20px").is_err());
assert!(parse(VerticalPosition::parse, "20px right").is_err());
assert!(parse(VerticalPosition::parse, "left center").is_err());
assert!(parse(VerticalPosition::parse, "bottom top").is_err());
assert!(parse(VerticalPosition::parse, "left top").is_err());
assert!(parse(VerticalPosition::parse, "left right").is_err());
assert!(parse(VerticalPosition::parse, "20px 30px").is_err());
}

View file

@ -688,11 +688,12 @@ mod shorthand_serialization {
use style::properties::longhands::background_clip as clip;
use style::properties::longhands::background_image as image;
use style::properties::longhands::background_origin as origin;
use style::properties::longhands::background_position as position;
use style::properties::longhands::background_position_x as position_x;
use style::properties::longhands::background_position_y as position_y;
use style::properties::longhands::background_repeat as repeat;
use style::properties::longhands::background_size as size;
use style::values::specified::Image;
use style::values::specified::position::Position;
use style::values::specified::position::{HorizontalPosition, VerticalPosition};
use super::*;
macro_rules! single_vec_value_typedef {
($name:ident, $path:expr) => {
@ -731,12 +732,17 @@ mod shorthand_serialization {
authored: None
});
let position = single_vec_value_typedef!(position,
Position {
horiz_keyword: None,
horiz_position: Some(LengthOrPercentage::Length(Length::from_px(7f32))),
vert_keyword: None,
vert_position: Some(LengthOrPercentage::Length(Length::from_px(4f32)))
let position_x = single_vec_value_typedef!(position_x,
HorizontalPosition {
keyword: None,
position: Some(LengthOrPercentage::Length(Length::from_px(7f32))),
}
);
let position_y = single_vec_value_typedef!(position_y,
VerticalPosition {
keyword: None,
position: Some(LengthOrPercentage::Length(Length::from_px(4f32))),
}
);
@ -759,7 +765,8 @@ mod shorthand_serialization {
let clip = single_vec_keyword_value!(clip, padding_box);
properties.push(PropertyDeclaration::BackgroundColor(color));
properties.push(PropertyDeclaration::BackgroundPosition(position));
properties.push(PropertyDeclaration::BackgroundPositionX(position_x));
properties.push(PropertyDeclaration::BackgroundPositionY(position_y));
properties.push(PropertyDeclaration::BackgroundRepeat(repeat));
properties.push(PropertyDeclaration::BackgroundAttachment(attachment));
properties.push(PropertyDeclaration::BackgroundImage(image));
@ -785,12 +792,17 @@ mod shorthand_serialization {
authored: None
});
let position = single_vec_value_typedef!(position,
Position {
horiz_keyword: None,
horiz_position: Some(LengthOrPercentage::Length(Length::from_px(7f32))),
vert_keyword: None,
vert_position: Some(LengthOrPercentage::Length(Length::from_px(4f32)))
let position_x = single_vec_value_typedef!(position_x,
HorizontalPosition {
keyword: None,
position: Some(LengthOrPercentage::Length(Length::from_px(7f32))),
}
);
let position_y = single_vec_value_typedef!(position_y,
VerticalPosition {
keyword: None,
position: Some(LengthOrPercentage::Length(Length::from_px(4f32))),
}
);
@ -813,7 +825,8 @@ mod shorthand_serialization {
let clip = single_vec_keyword_value!(clip, padding_box);
properties.push(PropertyDeclaration::BackgroundColor(color));
properties.push(PropertyDeclaration::BackgroundPosition(position));
properties.push(PropertyDeclaration::BackgroundPositionX(position_x));
properties.push(PropertyDeclaration::BackgroundPositionY(position_y));
properties.push(PropertyDeclaration::BackgroundRepeat(repeat));
properties.push(PropertyDeclaration::BackgroundAttachment(attachment));
properties.push(PropertyDeclaration::BackgroundImage(image));
@ -838,12 +851,17 @@ mod shorthand_serialization {
authored: None
});
let position = single_vec_value_typedef!(position,
Position {
horiz_keyword: None,
horiz_position: Some(LengthOrPercentage::Length(Length::from_px(0f32))),
vert_keyword: None,
vert_position: Some(LengthOrPercentage::Length(Length::from_px(0f32)))
let position_x = single_vec_value_typedef!(position_x,
HorizontalPosition {
keyword: None,
position: Some(LengthOrPercentage::Length(Length::from_px(0f32))),
}
);
let position_y = single_vec_value_typedef!(position_y,
VerticalPosition {
keyword: None,
position: Some(LengthOrPercentage::Length(Length::from_px(0f32))),
}
);
@ -858,7 +876,8 @@ mod shorthand_serialization {
let clip = DeclaredValue::Initial;
properties.push(PropertyDeclaration::BackgroundColor(color));
properties.push(PropertyDeclaration::BackgroundPosition(position));
properties.push(PropertyDeclaration::BackgroundPositionX(position_x));
properties.push(PropertyDeclaration::BackgroundPositionY(position_y));
properties.push(PropertyDeclaration::BackgroundRepeat(repeat));
properties.push(PropertyDeclaration::BackgroundAttachment(attachment));
properties.push(PropertyDeclaration::BackgroundImage(image));
@ -881,7 +900,7 @@ mod shorthand_serialization {
use style::properties::longhands::mask_repeat as repeat;
use style::properties::longhands::mask_size as size;
use style::values::specified::Image;
use style::values::specified::position::Position;
use style::values::specified::position::{HorizontalPosition, Position, VerticalPosition};
use super::*;
macro_rules! single_vec_value_typedef {
@ -918,10 +937,14 @@ mod shorthand_serialization {
let position = single_vec_value_typedef!(position,
Position {
horiz_keyword: None,
horiz_position: Some(LengthOrPercentage::Length(Length::from_px(7f32))),
vert_keyword: None,
vert_position: Some(LengthOrPercentage::Length(Length::from_px(4f32)))
horizontal: HorizontalPosition {
keyword: None,
position: Some(LengthOrPercentage::Length(Length::from_px(7f32))),
},
vertical: VerticalPosition {
keyword: None,
position: Some(LengthOrPercentage::Length(Length::from_px(4f32))),
},
}
);
@ -968,10 +991,14 @@ mod shorthand_serialization {
let position = single_vec_value_typedef!(position,
Position {
horiz_keyword: None,
horiz_position: Some(LengthOrPercentage::Length(Length::from_px(7f32))),
vert_keyword: None,
vert_position: Some(LengthOrPercentage::Length(Length::from_px(4f32)))
horizontal: HorizontalPosition {
keyword: None,
position: Some(LengthOrPercentage::Length(Length::from_px(7f32))),
},
vertical: VerticalPosition {
keyword: None,
position: Some(LengthOrPercentage::Length(Length::from_px(4f32))),
},
}
);

View file

@ -190,10 +190,15 @@ fn test_parse_stylesheet() {
}
)),
Importance::Normal),
(PropertyDeclaration::BackgroundPosition(DeclaredValue::Value(
longhands::background_position::SpecifiedValue(
vec![longhands::background_position::single_value
::get_initial_specified_value()]))),
(PropertyDeclaration::BackgroundPositionX(DeclaredValue::Value(
longhands::background_position_x::SpecifiedValue(
vec![longhands::background_position_x::single_value
::get_initial_position_value()]))),
Importance::Normal),
(PropertyDeclaration::BackgroundPositionY(DeclaredValue::Value(
longhands::background_position_y::SpecifiedValue(
vec![longhands::background_position_y::single_value
::get_initial_position_value()]))),
Importance::Normal),
(PropertyDeclaration::BackgroundRepeat(DeclaredValue::Value(
longhands::background_repeat::SpecifiedValue(

View file

@ -6,3 +6,6 @@
[background_initial_color]
expected: FAIL
[background_initial_position]
expected: FAIL

View file

@ -6,3 +6,6 @@
[background_specified_color_color]
expected: FAIL
[background_specified_color_position]
expected: FAIL