Skip to content

Commit

Permalink
style: Switch to the new scroll-snap-type syntax for the old scroll s…
Browse files Browse the repository at this point in the history
…nap implementation and drop the scroll-snap-type-{x,y} longhands.

Now scroll-snap-type is a longhand property.

Differential Revision: https://phabricator.services.mozilla.com/D21622
  • Loading branch information
Hiroyuki Ikezoe authored and emilio committed Apr 12, 2019
1 parent 2f457ed commit 21481e3
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 47 deletions.
2 changes: 2 additions & 0 deletions components/style/properties/data.py
Expand Up @@ -341,7 +341,9 @@ def specified_is_copy(self):
"SVGOpacity",
"SVGPaintOrder",
"ScrollSnapAlign",
"ScrollSnapAxis",
"ScrollSnapStrictness",
"ScrollSnapType",
"TextAlign",
"TextDecorationLine",
"TextEmphasisPosition",
Expand Down
21 changes: 9 additions & 12 deletions components/style/properties/longhands/box.mako.rs
Expand Up @@ -427,18 +427,15 @@ ${helpers.predefined_type(
animation_value_type="discrete",
)}

% for axis in ["x", "y"]:
${helpers.predefined_type(
"scroll-snap-type-" + axis,
"ScrollSnapStrictness",
"computed::ScrollSnapStrictness::None",
products="gecko",
needs_context=False,
gecko_pref="layout.css.scroll-snap.enabled",
spec="Nonstandard (https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type-x)",
animation_value_type="discrete",
)}
% endfor
${helpers.predefined_type(
"scroll-snap-type",
"ScrollSnapType",
"computed::ScrollSnapType::none()",
products="gecko",
gecko_pref="layout.css.scroll-snap.enabled",
spec="https://drafts.csswg.org/css-scroll-snap-1/#scroll-snap-type",
animation_value_type="discrete",
)}

% for axis in ["x", "y"]:
${helpers.predefined_type(
Expand Down
30 changes: 0 additions & 30 deletions components/style/properties/shorthands/box.mako.rs
Expand Up @@ -303,36 +303,6 @@ macro_rules! try_parse_one {
}
</%helpers:shorthand>

<%helpers:shorthand name="scroll-snap-type" products="gecko"
gecko_pref="layout.css.scroll-snap.enabled"
sub_properties="scroll-snap-type-x scroll-snap-type-y"
spec="https://drafts.csswg.org/css-scroll-snap/#propdef-scroll-snap-type">
use crate::properties::longhands::scroll_snap_type_x;

pub fn parse_value<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Longhands, ParseError<'i>> {
let result = scroll_snap_type_x::parse(context, input)?;
Ok(expanded! {
scroll_snap_type_x: result,
scroll_snap_type_y: result,
})
}

impl<'a> ToCss for LonghandsToSerialize<'a> {
// Serializes into the single keyword value if both scroll-snap-type-x and scroll-snap-type-y are same.
// Otherwise into an empty string. This is done to match Gecko's behaviour.
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
if self.scroll_snap_type_x == self.scroll_snap_type_y {
self.scroll_snap_type_x.to_css(dest)
} else {
Ok(())
}
}
}
</%helpers:shorthand>

${helpers.two_properties_shorthand(
"overscroll-behavior",
"overscroll-behavior-x",
Expand Down
4 changes: 2 additions & 2 deletions components/style/values/computed/box.rs
Expand Up @@ -14,8 +14,8 @@ use crate::values::specified::box_ as specified;
pub use crate::values::specified::box_::{AnimationName, Appearance, BreakBetween, BreakWithin};
pub use crate::values::specified::box_::{Clear as SpecifiedClear, Float as SpecifiedFloat};
pub use crate::values::specified::box_::{Contain, Display, Overflow};
pub use crate::values::specified::box_::{OverflowAnchor, OverflowClipBox};
pub use crate::values::specified::box_::{OverscrollBehavior, ScrollSnapAlign, ScrollSnapStrictness};
pub use crate::values::specified::box_::{OverflowAnchor, OverflowClipBox, OverscrollBehavior};
pub use crate::values::specified::box_::{ScrollSnapAlign, ScrollSnapAxis, ScrollSnapStrictness, ScrollSnapType};
pub use crate::values::specified::box_::{TouchAction, TransitionProperty, WillChange};

/// A computed value for the `vertical-align` property.
Expand Down
3 changes: 2 additions & 1 deletion components/style/values/computed/mod.rs
Expand Up @@ -42,7 +42,8 @@ pub use self::box_::{AnimationIterationCount, AnimationName, Contain};
pub use self::box_::{Appearance, BreakBetween, BreakWithin, Clear, Float};
pub use self::box_::{Display, Overflow, OverflowAnchor, TransitionProperty};
pub use self::box_::{OverflowClipBox, OverscrollBehavior, Perspective, Resize};
pub use self::box_::{ScrollSnapAlign, ScrollSnapStrictness, TouchAction, VerticalAlign, WillChange};
pub use self::box_::{ScrollSnapAlign, ScrollSnapAxis, ScrollSnapStrictness, ScrollSnapType};
pub use self::box_::{TouchAction, VerticalAlign, WillChange};
pub use self::color::{Color, ColorOrAuto, ColorPropertyValue};
pub use self::column::ColumnCount;
pub use self::counters::{Content, ContentItem, CounterIncrement, CounterSetOrReset};
Expand Down
94 changes: 93 additions & 1 deletion components/style/values/specified/box.rs
Expand Up @@ -379,6 +379,32 @@ impl Parse for AnimationName {
}
}

/// https://drafts.csswg.org/css-scroll-snap-1/#snap-axis
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(
Clone,
Copy,
Debug,
Eq,
MallocSizeOf,
Parse,
PartialEq,
SpecifiedValueInfo,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[repr(u8)]
pub enum ScrollSnapAxis {
X,
Y,
Block,
Inline,
Both,
}

/// https://drafts.csswg.org/css-scroll-snap-1/#snap-strictness
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
Expand All @@ -398,11 +424,77 @@ impl Parse for AnimationName {
)]
#[repr(u8)]
pub enum ScrollSnapStrictness {
None,
#[css(skip)]
None, // Used to represent scroll-snap-type: none. It's not parsed.
Mandatory,
Proximity,
}

/// https://drafts.csswg.org/css-scroll-snap-1/#scroll-snap-type
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(
Clone,
Copy,
Debug,
Eq,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToComputedValue,
ToResolvedValue,
ToShmem,
)]
#[repr(C)]
pub struct ScrollSnapType {
axis: ScrollSnapAxis,
strictness: ScrollSnapStrictness,
}

impl ScrollSnapType {
/// Returns `none`.
#[inline]
pub fn none() -> Self {
Self {
axis: ScrollSnapAxis::Both,
strictness: ScrollSnapStrictness::None,
}
}
}

impl Parse for ScrollSnapType {
/// none | [ x | y | block | inline | both ] [ mandatory | proximity ]?
fn parse<'i, 't>(
_context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(ScrollSnapType::none());
}

let axis = ScrollSnapAxis::parse(input)?;
let strictness = input.try(ScrollSnapStrictness::parse).unwrap_or(ScrollSnapStrictness::Proximity);
Ok(Self { axis, strictness })
}
}

impl ToCss for ScrollSnapType {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
if self.strictness == ScrollSnapStrictness::None {
return dest.write_str("none");
}
self.axis.to_css(dest)?;
if self.strictness != ScrollSnapStrictness::Proximity {
dest.write_str(" ")?;
self.strictness.to_css(dest)?;
}
Ok(())
}
}

/// Specified value of scroll-snap-align keyword value.
#[allow(missing_docs)]
#[derive(
Expand Down
2 changes: 1 addition & 1 deletion components/style/values/specified/mod.rs
Expand Up @@ -40,7 +40,7 @@ pub use self::box_::{AnimationIterationCount, AnimationName, Contain, Display};
pub use self::box_::{Appearance, BreakBetween, BreakWithin};
pub use self::box_::{Clear, Float, Overflow, OverflowAnchor};
pub use self::box_::{OverflowClipBox, OverscrollBehavior, Perspective, Resize};
pub use self::box_::{ScrollSnapAlign, ScrollSnapStrictness};
pub use self::box_::{ScrollSnapAlign, ScrollSnapAxis, ScrollSnapStrictness, ScrollSnapType};
pub use self::box_::{TouchAction, TransitionProperty, VerticalAlign, WillChange};
pub use self::color::{Color, ColorOrAuto, ColorPropertyValue};
pub use self::column::ColumnCount;
Expand Down

0 comments on commit 21481e3

Please sign in to comment.