Skip to content

Commit bbfba7d

Browse files
committed
Implemented scroll-snap-type
1 parent bd83c45 commit bbfba7d

12 files changed

+237
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Released on Tuesday, May 31 2022.
1111
- Added support for `@counter-style` (#102)
1212
- Added support for `@font-feature-values` (#102)
1313
- Added support for `conic-gradient` (#101)
14+
- Added support for `scroll-snap-type` declaration (#91)
1415

1516
# 0.16.4
1617

src/AngleSharp.Css/Constants/CssKeywords.cs

+20
Original file line numberDiff line numberDiff line change
@@ -1576,5 +1576,25 @@ public static class CssKeywords
15761576
/// The fit-content keyword.
15771577
/// </summary>
15781578
public static readonly String FitContent = "fit-content";
1579+
1580+
/// <summary>
1581+
/// The x keyword.
1582+
/// </summary>
1583+
public static readonly String X = "x";
1584+
1585+
/// <summary>
1586+
/// The y keyword.
1587+
/// </summary>
1588+
public static readonly String Y = "y";
1589+
1590+
/// <summary>
1591+
/// The proximity keyword.
1592+
/// </summary>
1593+
public static readonly String Proximity = "proximity";
1594+
1595+
/// <summary>
1596+
/// The mandatory keyword.
1597+
/// </summary>
1598+
public static readonly String Mandatory = "mandatory";
15791599
}
15801600
}

src/AngleSharp.Css/Constants/InitialValues.cs

+3
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,8 @@ static class InitialValues
206206
public static readonly ICssValue Scrollbar3dLightColorDecl = Color.White;
207207
public static readonly ICssValue LetterSpacingDecl = Length.Normal;
208208
public static readonly ICssValue FontSizeAdjustDecl = new Length(1.0, Length.Unit.Em);
209+
public static readonly ICssValue ScrollSnapTypeDecl = new Constant<Object>(CssKeywords.None, null);
210+
public static readonly ICssValue ScrollMarginDecl = Length.Zero;
211+
public static readonly ICssValue ScrollSnapAlignDecl = new Constant<ScrollSnapAlign>(CssKeywords.None, ScrollSnapAlign.None);
209212
}
210213
}

src/AngleSharp.Css/Constants/Map.cs

+32
Original file line numberDiff line numberDiff line change
@@ -904,5 +904,37 @@ static class Map
904904
{ CssKeywords.Hidden, Visibility.Hidden },
905905
{ CssKeywords.Collapse, Visibility.Collapse },
906906
};
907+
908+
/// <summary>
909+
/// Contains the scroll-snap-axis mapping.
910+
/// </summary>
911+
public static readonly Dictionary<String, ScrollSnapAxis> ScrollSnapAxises = new Dictionary<String, ScrollSnapAxis>(StringComparer.OrdinalIgnoreCase)
912+
{
913+
{ CssKeywords.X, ScrollSnapAxis.X },
914+
{ CssKeywords.Y, ScrollSnapAxis.Y },
915+
{ CssKeywords.Block, ScrollSnapAxis.Block },
916+
{ CssKeywords.Inline, ScrollSnapAxis.Inline },
917+
{ CssKeywords.Both, ScrollSnapAxis.Both },
918+
};
919+
920+
/// <summary>
921+
/// Contains the scroll-snap-strictness mapping.
922+
/// </summary>
923+
public static readonly Dictionary<String, ScrollSnapStrictness> ScrollSnapStrictnesses = new Dictionary<String, ScrollSnapStrictness>(StringComparer.OrdinalIgnoreCase)
924+
{
925+
{ CssKeywords.Proximity, ScrollSnapStrictness.Proximity},
926+
{ CssKeywords.Mandatory, ScrollSnapStrictness.Mandatory },
927+
};
928+
929+
/// <summary>
930+
/// Contains the scroll-snap-align mapping.
931+
/// </summary>
932+
public static readonly Dictionary<String, ScrollSnapAlign> ScrollSnapAlignments = new Dictionary<String, ScrollSnapAlign>(StringComparer.OrdinalIgnoreCase)
933+
{
934+
{ CssKeywords.None, ScrollSnapAlign.None },
935+
{ CssKeywords.Start, ScrollSnapAlign.Start},
936+
{ CssKeywords.End, ScrollSnapAlign.End },
937+
{ CssKeywords.Center, ScrollSnapAlign.Center },
938+
};
907939
}
908940
}

src/AngleSharp.Css/Constants/PropertyNames.cs

+15
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,21 @@ public static class PropertyNames
10571057
/// </summary>
10581058
public static readonly String ScrollbarTrackColor = "scrollbar-track-color";
10591059

1060+
/// <summary>
1061+
/// The scroll-snap-type declaration.
1062+
/// </summary>
1063+
public static readonly String ScrollSnapType = "scroll-snap-type";
1064+
1065+
/// <summary>
1066+
/// The scroll-margin declaration.
1067+
/// </summary>
1068+
public static readonly String ScrollMargin = "scroll-margin";
1069+
1070+
/// <summary>
1071+
/// The scroll-snap-align declaration.
1072+
/// </summary>
1073+
public static readonly String ScrollSnapAlign = "scroll-snap-aling";
1074+
10601075
/// <summary>
10611076
/// The stroke declaration.
10621077
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace AngleSharp.Css.Converters
2+
{
3+
using AngleSharp.Css.Dom;
4+
using AngleSharp.Css.Parser;
5+
using AngleSharp.Text;
6+
using System;
7+
8+
sealed class SeparatedEnumsConverter : IValueConverter
9+
{
10+
private readonly IValueConverter[] _converters;
11+
private readonly Char _seperator;
12+
13+
public SeparatedEnumsConverter(IValueConverter[] converters, Char seperator)
14+
{
15+
_converters = converters;
16+
_seperator = seperator;
17+
}
18+
public ICssValue Convert(StringSource source)
19+
{
20+
var value = _converters[0].Convert(source);
21+
22+
if (value != null)
23+
{
24+
var c = source.SkipSpacesAndComments();
25+
26+
if (!source.IsDone)
27+
{
28+
if (c == _seperator)
29+
{
30+
source.SkipCurrentAndSpaces();
31+
}
32+
else
33+
{
34+
value = null;
35+
}
36+
}
37+
}
38+
39+
return value;
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace AngleSharp.Css.Declarations
2+
{
3+
using AngleSharp.Css.Converters;
4+
using AngleSharp.Css.Dom;
5+
using AngleSharp.Css.Values;
6+
using System;
7+
using static ValueConverters;
8+
9+
/// <summary>
10+
/// For more information, see:
11+
/// https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type
12+
/// </summary>
13+
static class ScrollSnapTypeDeclaration
14+
{
15+
private static readonly ICssValue defaultStrictness = new Constant<ScrollSnapStrictness>(CssKeywords.Proximity, ScrollSnapStrictness.Proximity);
16+
17+
public static String Name = PropertyNames.ScrollSnapType;
18+
19+
public static IValueConverter Converter = Or(None, WithOrder(ScrollSnapAxisConverter, ScrollSnapStrictnessConverter.Option(defaultStrictness)));
20+
21+
public static ICssValue InitialValue = InitialValues.ScrollSnapTypeDecl;
22+
23+
public static PropertyFlags Flags = PropertyFlags.None;
24+
}
25+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace AngleSharp.Css.Dom
2+
{
3+
/// <summary>
4+
/// An enumeration with all possible scroll snap align options.
5+
/// </summary>
6+
public enum ScrollSnapAlign : byte
7+
{
8+
/// <summary>
9+
/// This box does not define a snap position in the specified axis
10+
/// </summary>
11+
None,
12+
/// <summary>
13+
/// Start alignment of this box’s scroll snap area within the scroll container’s snapport is a snap position in the specified axis
14+
/// </summary>
15+
Start,
16+
/// <summary>
17+
/// End alignment of this box’s scroll snap area within the scroll container’s snapport is a snap position in the specified axis
18+
/// </summary>
19+
End,
20+
/// <summary>
21+
/// Center alignment of this box’s scroll snap area within the scroll container’s snapport is a snap position in the specified axis
22+
/// </summary>
23+
Center,
24+
}
25+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace AngleSharp.Css.Dom
2+
{
3+
/// <summary>
4+
/// An enumeration with all possible scroll snap axis options.
5+
/// </summary>
6+
public enum ScrollSnapAxis : byte
7+
{
8+
/// <summary>
9+
/// The scroll container snaps to snap positions in its horizontal axis only
10+
/// </summary>
11+
X,
12+
/// <summary>
13+
/// The scroll container snaps to snap positions in its horizontal axis only
14+
/// </summary>
15+
Y,
16+
/// <summary>
17+
/// The scroll container snaps to snap positions in its block axis only
18+
/// </summary>
19+
Block,
20+
/// <summary>
21+
/// The scroll container snaps to snap positions in its inline axis only
22+
/// </summary>
23+
Inline,
24+
/// <summary>
25+
/// The scroll container snaps to snap positions in its inline axis only
26+
/// </summary>
27+
Both,
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace AngleSharp.Css.Dom
2+
{
3+
/// <summary>
4+
/// An enumeration with all possible scroll snap strictness options.
5+
/// </summary>
6+
public enum ScrollSnapStrictness : byte
7+
{
8+
/// <summary>
9+
/// If specified on a scroll container, the scroll container is required to be snapped
10+
/// to a snap position when there are no active scrolling operations.
11+
/// If a valid snap position exists then the scroll container must snap
12+
/// at the termination of a scroll (if none exist then no snapping occurs)
13+
/// </summary>
14+
Mandatory,
15+
/// <summary>
16+
/// If specified on a scroll container, the scroll container may snap to a snap position
17+
/// at the termination of a scroll, at the discretion of the UA given the parameters of the scroll
18+
/// </summary>
19+
Proximity,
20+
}
21+
}

src/AngleSharp.Css/Factories/DefaultDeclarationFactory.cs

+7
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,13 @@ public class DefaultDeclarationFactory : IDeclarationFactory
17491749
initialValue: FillDeclaration.InitialValue,
17501750
flags: FillDeclaration.Flags)
17511751
},
1752+
{
1753+
ScrollSnapTypeDeclaration.Name, new DeclarationInfo(
1754+
name: ScrollSnapTypeDeclaration.Name,
1755+
converter: ScrollSnapTypeDeclaration.Converter,
1756+
initialValue: ScrollSnapTypeDeclaration.InitialValue,
1757+
flags: ScrollSnapTypeDeclaration.Flags)
1758+
},
17521759
};
17531760

17541761
/// <summary>

src/AngleSharp.Css/ValueConverters.cs

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ static class ValueConverters
2222

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

25+
public static IValueConverter SpaceSeparated(IValueConverter converter) => new SeparatorConverter(converter, Symbols.Space);
26+
2527
/// <summary>
2628
/// Creates a converter for the initial keyword with the given value.
2729
/// </summary>
@@ -565,6 +567,21 @@ static class ValueConverters
565567
/// </summary>
566568
public static readonly IValueConverter ContentVisibilityConverter = Map.Visibilities.ToConverter();
567569

570+
/// <summary>
571+
/// Represents a converter for the ScrollSnapAlignment enumeration.
572+
/// </summary>
573+
public static readonly IValueConverter ScrollSnapAlignmentConverter = Map.ScrollSnapAlignments.ToConverter();
574+
575+
/// <summary>
576+
/// Represents a converter for the ScrollSnapAxis enumeration.
577+
/// </summary>
578+
public static readonly IValueConverter ScrollSnapAxisConverter = Map.ScrollSnapAxises.ToConverter();
579+
580+
/// <summary>
581+
/// Represents a converter for the ScrollSnapStrictness enumeration.
582+
/// </summary>
583+
public static readonly IValueConverter ScrollSnapStrictnessConverter = Map.ScrollSnapStrictnesses.ToConverter();
584+
568585
#endregion
569586

570587
#region Specific

0 commit comments

Comments
 (0)