From bdedcb9eb43ea766eb573c4407978225a1dfbdbd Mon Sep 17 00:00:00 2001 From: Rohit Burra Date: Mon, 7 Nov 2016 09:38:03 +0530 Subject: [PATCH] Adds scroll-snap-type shorthand property, tests --- .../style/properties/shorthand/box.mako.rs | 34 +++++++++++++ tests/unit/style/properties/serialization.rs | 49 +++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/components/style/properties/shorthand/box.mako.rs b/components/style/properties/shorthand/box.mako.rs index 2465a91ae8a0..d21cba8195ba 100644 --- a/components/style/properties/shorthand/box.mako.rs +++ b/components/style/properties/shorthand/box.mako.rs @@ -297,3 +297,37 @@ macro_rules! try_parse_one { } } + +<%helpers:shorthand name="scroll-snap-type" products="gecko" + sub_properties="scroll-snap-type-x scroll-snap-type-y"> + use properties::longhands::scroll_snap_type_x; + + pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result { + let result = try!(scroll_snap_type_x::parse(context, input)); + Ok(Longhands { + scroll_snap_type_x: Some(result), + scroll_snap_type_y: Some(result), + }) + } + + impl<'a> LonghandsToSerialize<'a> { + // Serializes into the single keyword value if both scroll-snap-type and scroll-snap-type-y are same. + // Otherwise into an empty string. This is done to match Gecko's behaviour. + fn to_css_declared(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + let x_and_y_equal = match (self.scroll_snap_type_x, self.scroll_snap_type_y) { + (&DeclaredValue::Value(ref x_value), &DeclaredValue::Value(ref y_value)) => { + *x_value == *y_value + }, + (&DeclaredValue::Initial, &DeclaredValue::Initial) => true, + (&DeclaredValue::Inherit, &DeclaredValue::Inherit) => true, + (x, y) => { *x == *y }, + }; + + if x_and_y_equal { + self.scroll_snap_type_x.to_css(dest) + } else { + Ok(()) + } + } + } + diff --git a/tests/unit/style/properties/serialization.rs b/tests/unit/style/properties/serialization.rs index 746a45b252ef..1128f060fd9a 100644 --- a/tests/unit/style/properties/serialization.rs +++ b/tests/unit/style/properties/serialization.rs @@ -1006,4 +1006,53 @@ mod shorthand_serialization { ); } } + + mod scroll_snap_type { + pub use super::*; + use style::properties::longhands::scroll_snap_type_x::computed_value::T as ScrollSnapTypeXValue; + + #[test] + fn should_serialize_to_empty_string_if_sub_types_not_equal() { + let declarations = vec![ + (PropertyDeclaration::ScrollSnapTypeX(DeclaredValue::Value(ScrollSnapTypeXValue::mandatory)), + Importance::Normal), + (PropertyDeclaration::ScrollSnapTypeY(DeclaredValue::Value(ScrollSnapTypeXValue::none)), + Importance::Normal) + ]; + + let block = PropertyDeclarationBlock { + declarations: declarations, + important_count: 0 + }; + + let mut s = String::new(); + + let x = block.single_value_to_css("scroll-snap-type", &mut s); + + assert_eq!(x.is_ok(), true); + assert_eq!(s, "scroll-snap-type: ;"); + } + + #[test] + fn should_serialize_to_single_value_if_sub_types_are_equal() { + let declarations = vec![ + (PropertyDeclaration::ScrollSnapTypeX(DeclaredValue::Value(ScrollSnapTypeXValue::mandatory)), + Importance::Normal), + (PropertyDeclaration::ScrollSnapTypeY(DeclaredValue::Value(ScrollSnapTypeXValue::mandatory)), + Importance::Normal) + ]; + + let block = PropertyDeclarationBlock { + declarations: declarations, + important_count: 0 + }; + + let mut s = String::new(); + + let x = block.single_value_to_css("scroll-snap-type", &mut s); + + assert_eq!(x.is_ok(), true); + assert_eq!(s, "scroll-snap-type: mandatory;"); + } + } }