Skip to content

Commit

Permalink
Adds scroll-snap-type shorthand property, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohit Burra committed Nov 14, 2016
1 parent 75d3524 commit bdedcb9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
34 changes: 34 additions & 0 deletions components/style/properties/shorthand/box.mako.rs
Expand Up @@ -297,3 +297,37 @@ macro_rules! try_parse_one {
}
}
</%helpers:shorthand>

<%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<Longhands, ()> {
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<W>(&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(())
}
}
}
</%helpers:shorthand>
49 changes: 49 additions & 0 deletions tests/unit/style/properties/serialization.rs
Expand Up @@ -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;");
}
}
}

0 comments on commit bdedcb9

Please sign in to comment.