Skip to content

Commit e97935a

Browse files
committed
Added margin and padding decls
1 parent 06b15e3 commit e97935a

19 files changed

+731
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Released on tbd.
1616
- Added further compactification of CSS tuples (#89, #93)
1717
- Added support for CSS nesting in style rules (#148)
1818
- Added support for 8-digit hex color codes (#132)
19+
- Added support for `margin-block` and `margin-inline` declarations
20+
- Added support for `padding-block` and `padding-inline` declarations
1921
- Added more CSSOM possibilities and helpers (#6)
2022
- Added parts of recent color spec update such as `rgb` with spaces (#131)
2123
- Added now Color L4 parsing with `hsl`, `hwb`, `lab`, `lch`, `oklab`, and `oklch`

src/AngleSharp.Css/Constants/InitialValues.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,18 @@ static class InitialValues
9595
public static readonly ICssValue MinWidthDecl = new Constant<Length>(CssKeywords.Auto, Length.Auto);
9696
public static readonly ICssValue MaxHeightDecl = new Constant<Object>(CssKeywords.None, null);
9797
public static readonly ICssValue MaxWidthDecl = new Constant<Object>(CssKeywords.None, null);
98+
public static readonly ICssValue MarginBlockEndDecl = Length.Zero;
99+
public static readonly ICssValue MarginBlockStartDecl = Length.Zero;
100+
public static readonly ICssValue MarginInlineEndDecl = Length.Zero;
101+
public static readonly ICssValue MarginInlineStartDecl = Length.Zero;
98102
public static readonly ICssValue MarginLeftDecl = Length.Zero;
99103
public static readonly ICssValue MarginBottomDecl = Length.Zero;
100104
public static readonly ICssValue MarginRightDecl = Length.Zero;
101105
public static readonly ICssValue MarginTopDecl = Length.Zero;
106+
public static readonly ICssValue PaddingBlockEndDecl = Length.Zero;
107+
public static readonly ICssValue PaddingBlockStartDecl = Length.Zero;
108+
public static readonly ICssValue PaddingInlineEndDecl = Length.Zero;
109+
public static readonly ICssValue PaddingInlineStartDecl = Length.Zero;
102110
public static readonly ICssValue PaddingLeftDecl = Length.Zero;
103111
public static readonly ICssValue PaddingBottomDecl = Length.Zero;
104112
public static readonly ICssValue PaddingRightDecl = Length.Zero;

src/AngleSharp.Css/Constants/PropertyNames.cs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,36 @@ public static class PropertyNames
802802
/// </summary>
803803
public static readonly String ListStyle = "list-style";
804804

805+
/// <summary>
806+
/// The margin-block declaration.
807+
/// </summary>
808+
public static readonly String MarginBlock = "margin-block";
809+
810+
/// <summary>
811+
/// The margin-block-end declaration.
812+
/// </summary>
813+
public static readonly String MarginBlockEnd = "margin-block-end";
814+
815+
/// <summary>
816+
/// The margin-block-start declaration.
817+
/// </summary>
818+
public static readonly String MarginBlockStart = "margin-block-start";
819+
820+
/// <summary>
821+
/// The margin-inline declaration.
822+
/// </summary>
823+
public static readonly String MarginInline = "margin-inline";
824+
825+
/// <summary>
826+
/// The margin-inline-end declaration.
827+
/// </summary>
828+
public static readonly String MarginInlineEnd = "margin-inline-end";
829+
830+
/// <summary>
831+
/// The margin-inline-start declaration.
832+
/// </summary>
833+
public static readonly String MarginInlineStart = "margin-inline-start";
834+
805835
/// <summary>
806836
/// The margin-right declaration.
807837
/// </summary>
@@ -927,10 +957,40 @@ public static class PropertyNames
927957
/// </summary>
928958
public static readonly String OverflowWrap = "overflow-wrap";
929959

930-
/// <summary>
931-
/// The padding-top declaration.
932-
/// </summary>
933-
public static readonly String PaddingTop = "padding-top";
960+
/// <summary>
961+
/// The padding-block declaration.
962+
/// </summary>
963+
public static readonly String PaddingBlock = "padding-block";
964+
965+
/// <summary>
966+
/// The padding-block-end declaration.
967+
/// </summary>
968+
public static readonly String PaddingBlockEnd = "padding-block-end";
969+
970+
/// <summary>
971+
/// The padding-block-start declaration.
972+
/// </summary>
973+
public static readonly String PaddingBlockStart = "padding-block-start";
974+
975+
/// <summary>
976+
/// The padding-inline declaration.
977+
/// </summary>
978+
public static readonly String PaddingInline = "padding-inline";
979+
980+
/// <summary>
981+
/// The padding-inline-end declaration.
982+
/// </summary>
983+
public static readonly String PaddingInlineEnd = "padding-inline-end";
984+
985+
/// <summary>
986+
/// The padding-inline-start declaration.
987+
/// </summary>
988+
public static readonly String PaddingInlineStart = "padding-inline-start";
989+
990+
/// <summary>
991+
/// The padding-top declaration.
992+
/// </summary>
993+
public static readonly String PaddingTop = "padding-top";
934994

935995
/// <summary>
936996
/// The padding-right declaration.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace AngleSharp.Css.Converters
2+
{
3+
using AngleSharp.Css.Dom;
4+
using AngleSharp.Css.Parser;
5+
using AngleSharp.Css.Values;
6+
using AngleSharp.Text;
7+
using System;
8+
9+
sealed class FlowRelativeValueConverter : IValueConverter
10+
{
11+
private readonly IValueConverter _converter;
12+
13+
public FlowRelativeValueConverter(IValueConverter converter)
14+
{
15+
_converter = converter;
16+
}
17+
18+
public ICssValue Convert(StringSource source)
19+
{
20+
var options = new ICssValue[2];
21+
var length = 0;
22+
23+
for (var i = 0; i < options.Length; i++)
24+
{
25+
options[i] = _converter.Convert(source);
26+
source.SkipSpacesAndComments();
27+
28+
if (options[length] != null)
29+
{
30+
length++;
31+
}
32+
}
33+
34+
if (length > 0)
35+
{
36+
var values = new ICssValue[length];
37+
Array.Copy(options, values, length);
38+
return new CssFlowRelativeValue(values);
39+
}
40+
41+
return null;
42+
}
43+
}
44+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace AngleSharp.Css.Declarations
2+
{
3+
using AngleSharp.Css.Converters;
4+
using AngleSharp.Css.Dom;
5+
using AngleSharp.Css.Values;
6+
using AngleSharp.Text;
7+
using System;
8+
using static ValueConverters;
9+
10+
static class MarginBlockDeclaration
11+
{
12+
public static String Name = PropertyNames.MarginBlock;
13+
14+
public static IValueConverter Converter = new MarginBlockAggregator();
15+
16+
public static ICssValue InitialValue = null;
17+
18+
public static PropertyFlags Flags = PropertyFlags.Shorthand;
19+
20+
public static String[] Longhands = new[]
21+
{
22+
PropertyNames.MarginBlockStart,
23+
PropertyNames.MarginBlockEnd,
24+
};
25+
26+
sealed class MarginBlockAggregator : IValueAggregator, IValueConverter
27+
{
28+
private static readonly IValueConverter converter = Or(AutoLengthOrPercentConverter, AssignInitial(Length.Zero)).FlowRelative();
29+
30+
public ICssValue Convert(StringSource source) => converter.Convert(source);
31+
32+
public ICssValue Merge(ICssValue[] values)
33+
{
34+
var start = values[0];
35+
var end = values[1];
36+
37+
if (start != null && end != null)
38+
{
39+
return new CssFlowRelativeValue(new[] { start, end });
40+
}
41+
42+
return null;
43+
}
44+
45+
public ICssValue[] Split(ICssValue value)
46+
{
47+
if (value is CssFlowRelativeValue flowRelative)
48+
{
49+
return new[] { flowRelative.Start, flowRelative.End };
50+
}
51+
52+
return null;
53+
}
54+
}
55+
}
56+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace AngleSharp.Css.Declarations
2+
{
3+
using System;
4+
using Dom;
5+
using static ValueConverters;
6+
7+
static class MarginBlockEndDeclaration
8+
{
9+
public static String Name = PropertyNames.MarginBlockEnd;
10+
11+
public static String[] Shorthands = new[]
12+
{
13+
PropertyNames.MarginBlock,
14+
};
15+
16+
public static IValueConverter Converter = AutoLengthOrPercentConverter;
17+
18+
public static ICssValue InitialValue = InitialValues.MarginBlockEndDecl;
19+
20+
public static PropertyFlags Flags = PropertyFlags.Unitless | PropertyFlags.Animatable;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace AngleSharp.Css.Declarations
2+
{
3+
using System;
4+
using Dom;
5+
using static ValueConverters;
6+
7+
static class MarginBlockStartDeclaration
8+
{
9+
public static String Name = PropertyNames.MarginBlockStart;
10+
11+
public static String[] Shorthands = new[]
12+
{
13+
PropertyNames.MarginBlock,
14+
};
15+
16+
public static IValueConverter Converter = AutoLengthOrPercentConverter;
17+
18+
public static ICssValue InitialValue = InitialValues.MarginBlockStartDecl;
19+
20+
public static PropertyFlags Flags = PropertyFlags.Unitless | PropertyFlags.Animatable;
21+
}
22+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace AngleSharp.Css.Declarations
2+
{
3+
using AngleSharp.Css.Converters;
4+
using AngleSharp.Css.Dom;
5+
using AngleSharp.Css.Values;
6+
using AngleSharp.Text;
7+
using System;
8+
using static ValueConverters;
9+
10+
static class MarginInlineDeclaration
11+
{
12+
public static String Name = PropertyNames.MarginInline;
13+
14+
public static IValueConverter Converter = new MarginInlineAggregator();
15+
16+
public static ICssValue InitialValue = null;
17+
18+
public static PropertyFlags Flags = PropertyFlags.Shorthand;
19+
20+
public static String[] Longhands = new[]
21+
{
22+
PropertyNames.MarginInlineStart,
23+
PropertyNames.MarginInlineEnd,
24+
};
25+
26+
sealed class MarginInlineAggregator : IValueAggregator, IValueConverter
27+
{
28+
private static readonly IValueConverter converter = Or(AutoLengthOrPercentConverter, AssignInitial(Length.Zero)).FlowRelative();
29+
30+
public ICssValue Convert(StringSource source) => converter.Convert(source);
31+
32+
public ICssValue Merge(ICssValue[] values)
33+
{
34+
var start = values[0];
35+
var end = values[1];
36+
37+
if (start != null && end != null)
38+
{
39+
return new CssFlowRelativeValue(new[] { start, end });
40+
}
41+
42+
return null;
43+
}
44+
45+
public ICssValue[] Split(ICssValue value)
46+
{
47+
if (value is CssFlowRelativeValue flowRelative)
48+
{
49+
return new[] { flowRelative.Start, flowRelative.End };
50+
}
51+
52+
return null;
53+
}
54+
}
55+
}
56+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace AngleSharp.Css.Declarations
2+
{
3+
using Dom;
4+
using System;
5+
using static ValueConverters;
6+
7+
static class MarginInlineEndDeclaration
8+
{
9+
public static String Name = PropertyNames.MarginInlineEnd;
10+
11+
public static String[] Shorthands = new[]
12+
{
13+
PropertyNames.MarginInline,
14+
};
15+
16+
public static IValueConverter Converter = AutoLengthOrPercentConverter;
17+
18+
public static ICssValue InitialValue = InitialValues.MarginInlineEndDecl;
19+
20+
public static PropertyFlags Flags = PropertyFlags.Unitless | PropertyFlags.Animatable;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace AngleSharp.Css.Declarations
2+
{
3+
using System;
4+
using Dom;
5+
using static ValueConverters;
6+
7+
static class MarginInlineStartDeclaration
8+
{
9+
public static String Name = PropertyNames.MarginInlineStart;
10+
11+
public static String[] Shorthands = new[]
12+
{
13+
PropertyNames.MarginInline,
14+
};
15+
16+
public static IValueConverter Converter = AutoLengthOrPercentConverter;
17+
18+
public static ICssValue InitialValue = InitialValues.MarginInlineStartDecl;
19+
20+
public static PropertyFlags Flags = PropertyFlags.Unitless | PropertyFlags.Animatable;
21+
}
22+
}

0 commit comments

Comments
 (0)