Skip to content

Commit a290247

Browse files
committed
More consistency
1 parent 1033292 commit a290247

File tree

8 files changed

+49
-149
lines changed

8 files changed

+49
-149
lines changed

src/AngleSharp.Css/BrowsingContextExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal static ICssProperty CreateShorthand(this IBrowsingContext context, Stri
6363
{
6464
var factory = context.GetFactory<IDeclarationFactory>();
6565
var info = factory.Create(name);
66-
var value = info.Combine(longhands);
66+
var value = info.Combine(factory, longhands);
6767
return new CssProperty(name, info.Converter, info.Flags, value, important);
6868
}
6969

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
namespace AngleSharp.Css.Declarations
22
{
33
using AngleSharp.Css.Dom;
4-
using AngleSharp.Css.Values;
5-
using AngleSharp.Text;
64
using System;
7-
using System.Linq;
85
using static ValueConverters;
96

107
static class BorderBottomDeclaration
118
{
129
public static String Name = PropertyNames.BorderBottom;
1310

14-
public static IValueConverter Converter = new BorderBottomAggregator();
11+
public static IValueConverter Converter = AggregateTuple(BorderSideConverter);
1512

1613
public static ICssValue InitialValue = null;
1714

@@ -28,30 +25,5 @@ static class BorderBottomDeclaration
2825
PropertyNames.BorderBottomStyle,
2926
PropertyNames.BorderBottomColor,
3027
};
31-
32-
sealed class BorderBottomAggregator : IValueAggregator, IValueConverter
33-
{
34-
public ICssValue Convert(StringSource source) => BorderSideConverter.Convert(source);
35-
36-
public ICssValue Merge(ICssValue[] values)
37-
{
38-
if (values.Length == 3)
39-
{
40-
return new CssTupleValue(values);
41-
}
42-
43-
return null;
44-
}
45-
46-
public ICssValue[] Split(ICssValue value)
47-
{
48-
if (value is CssTupleValue options)
49-
{
50-
return options.ToArray();
51-
}
52-
53-
return null;
54-
}
55-
}
5628
}
5729
}

src/AngleSharp.Css/Declarations/BorderDeclaration.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ sealed class BorderAggregator : IValueAggregator, IValueConverter
4141

4242
public ICssValue Convert(StringSource source) => converter.Convert(source);
4343

44-
public ICssValue Merge(ICssValue[] values) => new CssTupleValue(values);
44+
public ICssValue Merge(ICssValue[] values)
45+
{
46+
if (!values.OfType<CssPeriodicValue>().Any())
47+
{
48+
return new CssTupleValue(values);
49+
}
50+
51+
return null;
52+
}
4553

4654
public ICssValue[] Split(ICssValue value)
4755
{
Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
namespace AngleSharp.Css.Declarations
22
{
33
using AngleSharp.Css.Dom;
4-
using AngleSharp.Css.Values;
5-
using AngleSharp.Text;
64
using System;
75
using static ValueConverters;
86

@@ -15,7 +13,7 @@ static class BorderLeftDeclaration
1513
PropertyNames.Border,
1614
};
1715

18-
public static IValueConverter Converter = new BorderLeftAggregator();
16+
public static IValueConverter Converter = AggregateTuple(BorderSideConverter);
1917

2018
public static ICssValue InitialValue = null;
2119

@@ -27,41 +25,5 @@ static class BorderLeftDeclaration
2725
PropertyNames.BorderLeftStyle,
2826
PropertyNames.BorderLeftColor,
2927
};
30-
31-
sealed class BorderLeftAggregator : IValueAggregator, IValueConverter
32-
{
33-
public ICssValue Convert(StringSource source) => BorderSideConverter.Convert(source);
34-
35-
public ICssValue Merge(ICssValue[] values)
36-
{
37-
var width = values[0];
38-
var style = values[1];
39-
var color = values[2];
40-
41-
if (width != null || style != null || color != null)
42-
{
43-
return new CssTupleValue(new[] { width, style, color });
44-
}
45-
46-
return null;
47-
}
48-
49-
public ICssValue[] Split(ICssValue value)
50-
{
51-
var options = value as CssTupleValue;
52-
53-
if (options != null)
54-
{
55-
return new[]
56-
{
57-
options.Items[0],
58-
options.Items[1],
59-
options.Items[2],
60-
};
61-
}
62-
63-
return null;
64-
}
65-
}
6628
}
6729
}
Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
namespace AngleSharp.Css.Declarations
22
{
33
using AngleSharp.Css.Dom;
4-
using AngleSharp.Css.Values;
5-
using AngleSharp.Text;
64
using System;
75
using static ValueConverters;
86

@@ -15,7 +13,7 @@ static class BorderRightDeclaration
1513
PropertyNames.Border,
1614
};
1715

18-
public static IValueConverter Converter = new BorderRightAggregator();
16+
public static IValueConverter Converter = AggregateTuple(BorderSideConverter);
1917

2018
public static ICssValue InitialValue = null;
2119

@@ -27,41 +25,5 @@ static class BorderRightDeclaration
2725
PropertyNames.BorderRightStyle,
2826
PropertyNames.BorderRightColor,
2927
};
30-
31-
sealed class BorderRightAggregator : IValueAggregator, IValueConverter
32-
{
33-
public ICssValue Convert(StringSource source) => BorderSideConverter.Convert(source);
34-
35-
public ICssValue Merge(ICssValue[] values)
36-
{
37-
var width = values[0];
38-
var style = values[1];
39-
var color = values[2];
40-
41-
if (width != null || style != null || color != null)
42-
{
43-
return new CssTupleValue(new[] { width, style, color });
44-
}
45-
46-
return null;
47-
}
48-
49-
public ICssValue[] Split(ICssValue value)
50-
{
51-
var options = value as CssTupleValue;
52-
53-
if (options != null)
54-
{
55-
return new[]
56-
{
57-
options.Items[0],
58-
options.Items[1],
59-
options.Items[2],
60-
};
61-
}
62-
63-
return null;
64-
}
65-
}
6628
}
6729
}
Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
namespace AngleSharp.Css.Declarations
22
{
33
using AngleSharp.Css.Dom;
4-
using AngleSharp.Css.Values;
5-
using AngleSharp.Text;
64
using System;
75
using static ValueConverters;
86

@@ -15,7 +13,7 @@ static class BorderTopDeclaration
1513
PropertyNames.Border,
1614
};
1715

18-
public static IValueConverter Converter = new BorderTopAggregator();
16+
public static IValueConverter Converter = AggregateTuple(BorderSideConverter);
1917

2018
public static ICssValue InitialValue = null;
2119

@@ -27,41 +25,5 @@ static class BorderTopDeclaration
2725
PropertyNames.BorderTopStyle,
2826
PropertyNames.BorderTopColor,
2927
};
30-
31-
sealed class BorderTopAggregator : IValueAggregator, IValueConverter
32-
{
33-
public ICssValue Convert(StringSource source) => BorderSideConverter.Convert(source);
34-
35-
public ICssValue Merge(ICssValue[] values)
36-
{
37-
var width = values[0];
38-
var style = values[1];
39-
var color = values[2];
40-
41-
if (width != null || style != null || color != null)
42-
{
43-
return new CssTupleValue(new[] { width, style, color });
44-
}
45-
46-
return null;
47-
}
48-
49-
public ICssValue[] Split(ICssValue value)
50-
{
51-
var options = value as CssTupleValue;
52-
53-
if (options != null)
54-
{
55-
return new[]
56-
{
57-
options.Items[0],
58-
options.Items[1],
59-
options.Items[2],
60-
};
61-
}
62-
63-
return null;
64-
}
65-
}
6628
}
6729
}

src/AngleSharp.Css/Extensions/DeclarationInfoExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static class DeclarationInfoExtensions
1111
public static IEnumerable<String> GetMappings(this DeclarationInfo info) =>
1212
info.Longhands.Length > 0 ? info.Longhands : Enumerable.Repeat(info.Name, 1);
1313

14-
public static ICssValue Combine(this DeclarationInfo info, ICssValue[] longhands)
14+
public static ICssValue Combine(this DeclarationInfo info, IDeclarationFactory factory, ICssValue[] longhands)
1515
{
1616
var initial = true;
1717
var unset = true;

src/AngleSharp.Css/ValueConverters.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,8 @@ private static ICssFunctionValue ParseFormat(this StringSource source)
937937

938938
public static IValueConverter AggregatePeriodic(IValueConverter converter) => new PeriodicAggregator(converter);
939939

940+
public static IValueConverter AggregateTuple(IValueConverter converter) => new TupleAggregator(converter);
941+
940942
sealed class PeriodicAggregator : IValueAggregator, IValueConverter
941943
{
942944
private readonly IValueConverter _converter;
@@ -978,6 +980,38 @@ public ICssValue[] Split(ICssValue value)
978980
}
979981
}
980982

983+
sealed class TupleAggregator : IValueAggregator, IValueConverter
984+
{
985+
private readonly IValueConverter _converter;
986+
987+
public TupleAggregator(IValueConverter converter)
988+
{
989+
_converter = converter;
990+
}
991+
992+
public ICssValue Convert(StringSource source) => _converter.Convert(source);
993+
994+
public ICssValue Merge(ICssValue[] values)
995+
{
996+
if (values.Any(m => m != null))
997+
{
998+
return new CssTupleValue(values);
999+
}
1000+
1001+
return null;
1002+
}
1003+
1004+
public ICssValue[] Split(ICssValue value)
1005+
{
1006+
if (value is CssTupleValue options)
1007+
{
1008+
return options.ToArray();
1009+
}
1010+
1011+
return null;
1012+
}
1013+
}
1014+
9811015
#endregion
9821016
}
9831017
}

0 commit comments

Comments
 (0)