Skip to content

Commit

Permalink
Implemented scroll-snap-type
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jun 2, 2022
1 parent bd83c45 commit bbfba7d
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Released on Tuesday, May 31 2022.
- Added support for `@counter-style` (#102)
- Added support for `@font-feature-values` (#102)
- Added support for `conic-gradient` (#101)
- Added support for `scroll-snap-type` declaration (#91)

# 0.16.4

Expand Down
20 changes: 20 additions & 0 deletions src/AngleSharp.Css/Constants/CssKeywords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1576,5 +1576,25 @@ public static class CssKeywords
/// The fit-content keyword.
/// </summary>
public static readonly String FitContent = "fit-content";

/// <summary>
/// The x keyword.
/// </summary>
public static readonly String X = "x";

/// <summary>
/// The y keyword.
/// </summary>
public static readonly String Y = "y";

/// <summary>
/// The proximity keyword.
/// </summary>
public static readonly String Proximity = "proximity";

/// <summary>
/// The mandatory keyword.
/// </summary>
public static readonly String Mandatory = "mandatory";
}
}
3 changes: 3 additions & 0 deletions src/AngleSharp.Css/Constants/InitialValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,8 @@ static class InitialValues
public static readonly ICssValue Scrollbar3dLightColorDecl = Color.White;
public static readonly ICssValue LetterSpacingDecl = Length.Normal;
public static readonly ICssValue FontSizeAdjustDecl = new Length(1.0, Length.Unit.Em);
public static readonly ICssValue ScrollSnapTypeDecl = new Constant<Object>(CssKeywords.None, null);
public static readonly ICssValue ScrollMarginDecl = Length.Zero;
public static readonly ICssValue ScrollSnapAlignDecl = new Constant<ScrollSnapAlign>(CssKeywords.None, ScrollSnapAlign.None);
}
}
32 changes: 32 additions & 0 deletions src/AngleSharp.Css/Constants/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -904,5 +904,37 @@ static class Map
{ CssKeywords.Hidden, Visibility.Hidden },
{ CssKeywords.Collapse, Visibility.Collapse },
};

/// <summary>
/// Contains the scroll-snap-axis mapping.
/// </summary>
public static readonly Dictionary<String, ScrollSnapAxis> ScrollSnapAxises = new Dictionary<String, ScrollSnapAxis>(StringComparer.OrdinalIgnoreCase)
{
{ CssKeywords.X, ScrollSnapAxis.X },
{ CssKeywords.Y, ScrollSnapAxis.Y },
{ CssKeywords.Block, ScrollSnapAxis.Block },
{ CssKeywords.Inline, ScrollSnapAxis.Inline },
{ CssKeywords.Both, ScrollSnapAxis.Both },
};

/// <summary>
/// Contains the scroll-snap-strictness mapping.
/// </summary>
public static readonly Dictionary<String, ScrollSnapStrictness> ScrollSnapStrictnesses = new Dictionary<String, ScrollSnapStrictness>(StringComparer.OrdinalIgnoreCase)
{
{ CssKeywords.Proximity, ScrollSnapStrictness.Proximity},
{ CssKeywords.Mandatory, ScrollSnapStrictness.Mandatory },
};

/// <summary>
/// Contains the scroll-snap-align mapping.
/// </summary>
public static readonly Dictionary<String, ScrollSnapAlign> ScrollSnapAlignments = new Dictionary<String, ScrollSnapAlign>(StringComparer.OrdinalIgnoreCase)
{
{ CssKeywords.None, ScrollSnapAlign.None },
{ CssKeywords.Start, ScrollSnapAlign.Start},
{ CssKeywords.End, ScrollSnapAlign.End },
{ CssKeywords.Center, ScrollSnapAlign.Center },
};
}
}
15 changes: 15 additions & 0 deletions src/AngleSharp.Css/Constants/PropertyNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,21 @@ public static class PropertyNames
/// </summary>
public static readonly String ScrollbarTrackColor = "scrollbar-track-color";

/// <summary>
/// The scroll-snap-type declaration.
/// </summary>
public static readonly String ScrollSnapType = "scroll-snap-type";

/// <summary>
/// The scroll-margin declaration.
/// </summary>
public static readonly String ScrollMargin = "scroll-margin";

/// <summary>
/// The scroll-snap-align declaration.
/// </summary>
public static readonly String ScrollSnapAlign = "scroll-snap-aling";

/// <summary>
/// The stroke declaration.
/// </summary>
Expand Down
42 changes: 42 additions & 0 deletions src/AngleSharp.Css/Converters/SeparatedEnumsConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace AngleSharp.Css.Converters
{
using AngleSharp.Css.Dom;
using AngleSharp.Css.Parser;
using AngleSharp.Text;
using System;

sealed class SeparatedEnumsConverter : IValueConverter
{
private readonly IValueConverter[] _converters;
private readonly Char _seperator;

public SeparatedEnumsConverter(IValueConverter[] converters, Char seperator)
{
_converters = converters;
_seperator = seperator;
}
public ICssValue Convert(StringSource source)
{
var value = _converters[0].Convert(source);

if (value != null)
{
var c = source.SkipSpacesAndComments();

if (!source.IsDone)
{
if (c == _seperator)
{
source.SkipCurrentAndSpaces();
}
else
{
value = null;
}
}
}

return value;
}
}
}
25 changes: 25 additions & 0 deletions src/AngleSharp.Css/Declarations/ScrollSnapTypeDeclaration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace AngleSharp.Css.Declarations
{
using AngleSharp.Css.Converters;
using AngleSharp.Css.Dom;
using AngleSharp.Css.Values;
using System;
using static ValueConverters;

/// <summary>
/// For more information, see:
/// https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type
/// </summary>
static class ScrollSnapTypeDeclaration
{
private static readonly ICssValue defaultStrictness = new Constant<ScrollSnapStrictness>(CssKeywords.Proximity, ScrollSnapStrictness.Proximity);

public static String Name = PropertyNames.ScrollSnapType;

public static IValueConverter Converter = Or(None, WithOrder(ScrollSnapAxisConverter, ScrollSnapStrictnessConverter.Option(defaultStrictness)));

public static ICssValue InitialValue = InitialValues.ScrollSnapTypeDecl;

public static PropertyFlags Flags = PropertyFlags.None;
}
}
25 changes: 25 additions & 0 deletions src/AngleSharp.Css/Dom/ScrollSnapAlign.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace AngleSharp.Css.Dom
{
/// <summary>
/// An enumeration with all possible scroll snap align options.
/// </summary>
public enum ScrollSnapAlign : byte
{
/// <summary>
/// This box does not define a snap position in the specified axis
/// </summary>
None,
/// <summary>
/// Start alignment of this box’s scroll snap area within the scroll container’s snapport is a snap position in the specified axis
/// </summary>
Start,
/// <summary>
/// End alignment of this box’s scroll snap area within the scroll container’s snapport is a snap position in the specified axis
/// </summary>
End,
/// <summary>
/// Center alignment of this box’s scroll snap area within the scroll container’s snapport is a snap position in the specified axis
/// </summary>
Center,
}
}
29 changes: 29 additions & 0 deletions src/AngleSharp.Css/Dom/ScrollSnapAxis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace AngleSharp.Css.Dom
{
/// <summary>
/// An enumeration with all possible scroll snap axis options.
/// </summary>
public enum ScrollSnapAxis : byte
{
/// <summary>
/// The scroll container snaps to snap positions in its horizontal axis only
/// </summary>
X,
/// <summary>
/// The scroll container snaps to snap positions in its horizontal axis only
/// </summary>
Y,
/// <summary>
/// The scroll container snaps to snap positions in its block axis only
/// </summary>
Block,
/// <summary>
/// The scroll container snaps to snap positions in its inline axis only
/// </summary>
Inline,
/// <summary>
/// The scroll container snaps to snap positions in its inline axis only
/// </summary>
Both,
}
}
21 changes: 21 additions & 0 deletions src/AngleSharp.Css/Dom/ScrollSnapStrictness.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace AngleSharp.Css.Dom
{
/// <summary>
/// An enumeration with all possible scroll snap strictness options.
/// </summary>
public enum ScrollSnapStrictness : byte
{
/// <summary>
/// If specified on a scroll container, the scroll container is required to be snapped
/// to a snap position when there are no active scrolling operations.
/// If a valid snap position exists then the scroll container must snap
/// at the termination of a scroll (if none exist then no snapping occurs)
/// </summary>
Mandatory,
/// <summary>
/// If specified on a scroll container, the scroll container may snap to a snap position
/// at the termination of a scroll, at the discretion of the UA given the parameters of the scroll
/// </summary>
Proximity,
}
}
7 changes: 7 additions & 0 deletions src/AngleSharp.Css/Factories/DefaultDeclarationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,13 @@ public class DefaultDeclarationFactory : IDeclarationFactory
initialValue: FillDeclaration.InitialValue,
flags: FillDeclaration.Flags)
},
{
ScrollSnapTypeDeclaration.Name, new DeclarationInfo(
name: ScrollSnapTypeDeclaration.Name,
converter: ScrollSnapTypeDeclaration.Converter,
initialValue: ScrollSnapTypeDeclaration.InitialValue,
flags: ScrollSnapTypeDeclaration.Flags)
},
};

/// <summary>
Expand Down
17 changes: 17 additions & 0 deletions src/AngleSharp.Css/ValueConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ static class ValueConverters

public static IValueConverter SlashSeparated(IValueConverter converter) => new SeparatorConverter(converter, Symbols.Solidus);

public static IValueConverter SpaceSeparated(IValueConverter converter) => new SeparatorConverter(converter, Symbols.Space);

/// <summary>
/// Creates a converter for the initial keyword with the given value.
/// </summary>
Expand Down Expand Up @@ -565,6 +567,21 @@ static class ValueConverters
/// </summary>
public static readonly IValueConverter ContentVisibilityConverter = Map.Visibilities.ToConverter();

/// <summary>
/// Represents a converter for the ScrollSnapAlignment enumeration.
/// </summary>
public static readonly IValueConverter ScrollSnapAlignmentConverter = Map.ScrollSnapAlignments.ToConverter();

/// <summary>
/// Represents a converter for the ScrollSnapAxis enumeration.
/// </summary>
public static readonly IValueConverter ScrollSnapAxisConverter = Map.ScrollSnapAxises.ToConverter();

/// <summary>
/// Represents a converter for the ScrollSnapStrictness enumeration.
/// </summary>
public static readonly IValueConverter ScrollSnapStrictnessConverter = Map.ScrollSnapStrictnesses.ToConverter();

#endregion

#region Specific
Expand Down

0 comments on commit bbfba7d

Please sign in to comment.