Skip to content

Commit 2c06be0

Browse files
committed
Optimized the border radius converter and initial values
1 parent 0dc9599 commit 2c06be0

23 files changed

+256
-257
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 0.12.0
22

3-
Released on Thursday, May 5 2019.
3+
Released on Sunday, May 12 2019.
44

55
- Reference latest AngleSharp
66
- Fixed empty value when removing properties (#14)
@@ -10,6 +10,7 @@ Released on Thursday, May 5 2019.
1010
- Fixed exception when not providing an `IRenderDevice` (#20)
1111
- Fixed missing `CssStylingService.Default` in cascade (#21)
1212
- Added extension helper `SetStyle` to modify all styles of many elements (#22)
13+
- Fixed expansion of shorthand properties to longhand (#23)
1314
- Opened CSSOM, e.g., declared `ICssFunctionValue` `public` (#24)
1415
- Introduced special converter for the `src` declaration (#25)
1516
- Fixed bug regarding CSS grid serialization (#27)

src/AngleSharp.Css.Tests/Declarations/CssBorderRadiusProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void CssBorderBottomLeftRadiusPxPxLegal()
1515
Assert.IsFalse(property.IsImportant);
1616
Assert.IsFalse(property.IsInherited);
1717
Assert.IsTrue(property.HasValue);
18-
Assert.AreEqual("40px 40px", property.Value);
18+
Assert.AreEqual("40px", property.Value);
1919
}
2020

2121
[Test]

src/AngleSharp.Css/Constants/InitialValues.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ static class InitialValues
2929
public static readonly ICssValue FontStretchDecl = new Constant<FontStretch>(CssKeywords.Normal, FontStretch.Normal);
3030
public static readonly ICssValue FontSizeDecl = new Constant<Length>(CssKeywords.Medium, Length.Medium);
3131
public static readonly ICssValue FontFamilyDecl = new Label("Times New Roman");
32+
public static readonly ICssValue BorderWidthDecl = new Constant<Length>(CssKeywords.Medium, Length.Medium);
33+
public static readonly ICssValue BorderStyleDecl = new Constant<LineStyle>(CssKeywords.None, LineStyle.None);
34+
public static readonly ICssValue BorderColorDecl = new Constant<Color>(CssKeywords.CurrentColor, Color.CurrentColor);
3235
public static readonly ICssValue LineHeightDecl = new Constant<Length>(CssKeywords.Normal, Length.Normal);
3336
public static readonly ICssValue BorderTopWidthDecl = new Constant<Length>(CssKeywords.Medium, Length.Medium);
3437
public static readonly ICssValue BorderRightWidthDecl = new Constant<Length>(CssKeywords.Medium, Length.Medium);

src/AngleSharp.Css/Converters/OptionValueConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ namespace AngleSharp.Css.Converters
44
using AngleSharp.Css.Values;
55
using AngleSharp.Text;
66

7-
sealed class OptionValueConverter<T> : IValueConverter
7+
sealed class OptionValueConverter : IValueConverter
88
{
99
private readonly IValueConverter _converter;
10-
private readonly T _defaultValue;
10+
private readonly ICssValue _defaultValue;
1111

12-
public OptionValueConverter(IValueConverter converter, T defaultValue)
12+
public OptionValueConverter(IValueConverter converter, ICssValue defaultValue)
1313
{
1414
_converter = converter;
1515
_defaultValue = defaultValue;
@@ -19,7 +19,7 @@ public ICssValue Convert(StringSource source)
1919
{
2020
if (source.IsDone || source.Current == Symbols.Comma)
2121
{
22-
return new CssDefaultValue<T>(_defaultValue);
22+
return new CssDefaultValue(_defaultValue);
2323
}
2424

2525
return _converter.Convert(source);
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 RadiusValueConverter : IValueConverter
10+
{
11+
private readonly IValueConverter _converter;
12+
13+
public RadiusValueConverter(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 CssRadiusValue(values);
39+
}
40+
41+
return null;
42+
}
43+
}
44+
}

src/AngleSharp.Css/Converters/ValueConverterExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public static IValueConverter ToConverter<T>(this Dictionary<String, T> values)
3939
public static IValueConverter Periodic(this IValueConverter converter) =>
4040
new PeriodicValueConverter(converter);
4141

42+
public static IValueConverter Radius(this IValueConverter converter) =>
43+
new RadiusValueConverter(converter);
44+
4245
public static IValueConverter Exclusive(this IValueConverter converter)
4346
{
4447
return new ClassValueConverter<ICssValue>(source =>
@@ -57,11 +60,8 @@ public static IValueConverter Exclusive(this IValueConverter converter)
5760
});
5861
}
5962

60-
public static IValueConverter Option(this IValueConverter converter) =>
61-
new OptionValueConverter<Object>(converter, null);
62-
63-
public static IValueConverter Option<T>(this IValueConverter converter, T defaultValue) =>
64-
new OptionValueConverter<T>(converter, defaultValue);
63+
public static IValueConverter Option(this IValueConverter converter, ICssValue defaultValue) =>
64+
new OptionValueConverter(converter, defaultValue);
6565

6666
public static String Join<T>(this T[] values, String separator)
6767
where T : ICssValue

src/AngleSharp.Css/Declarations/AnimationDeclaration.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ static class AnimationDeclaration
3333
sealed class AnimationAggregator : IValueAggregator, IValueConverter
3434
{
3535
private static readonly IValueConverter ListConverter = WithAny(
36-
TimeConverter.Option(),
37-
TransitionConverter.Option(),
38-
TimeConverter.Option(),
39-
PositiveOrInfiniteNumberConverter.Option(),
40-
AnimationDirectionConverter.Option(),
41-
AnimationFillStyleConverter.Option(),
42-
PlayStateConverter.Option(),
43-
IdentifierConverter.Option()).FromList();
36+
TimeConverter.Option(InitialValues.AnimationDurationDecl),
37+
TransitionConverter.Option(InitialValues.AnimationTimingFunctionDecl),
38+
TimeConverter.Option(InitialValues.AnimationDelayDecl),
39+
PositiveOrInfiniteNumberConverter.Option(InitialValues.AnimationIterationCountDecl),
40+
AnimationDirectionConverter.Option(InitialValues.AnimationDirectionDecl),
41+
AnimationFillStyleConverter.Option(InitialValues.AnimationFillModeDecl),
42+
PlayStateConverter.Option(InitialValues.AnimationPlayStateDecl),
43+
IdentifierConverter.Option(InitialValues.AnimationNameDecl)).FromList();
4444

4545
public ICssValue Convert(StringSource source) => ListConverter.Convert(source);
4646

src/AngleSharp.Css/Declarations/BorderBottomDeclaration.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ static class BorderBottomDeclaration
88
{
99
public static String Name = PropertyNames.BorderBottom;
1010

11-
public static IValueConverter Converter = AggregateTuple(BorderSideConverter);
12-
13-
public static ICssValue InitialValue = null;
14-
15-
public static PropertyFlags Flags = PropertyFlags.Animatable | PropertyFlags.Shorthand;
16-
1711
public static String[] Shorthands = new[]
1812
{
1913
PropertyNames.Border,
2014
};
2115

16+
public static IValueConverter Converter = WithBorderSide(
17+
InitialValues.BorderBottomWidthDecl,
18+
InitialValues.BorderBottomStyleDecl,
19+
InitialValues.BorderBottomColorDecl);
20+
21+
public static ICssValue InitialValue = null;
22+
23+
public static PropertyFlags Flags = PropertyFlags.Animatable | PropertyFlags.Shorthand;
24+
2225
public static String[] Longhands = new[]
2326
{
2427
PropertyNames.BorderBottomWidth,

src/AngleSharp.Css/Declarations/BorderDeclaration.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,12 @@ static class BorderDeclaration
2525
PropertyNames.BorderColor,
2626
};
2727

28-
sealed class BorderValueConverter : IValueConverter
29-
{
30-
private static readonly IValueConverter converter = WithAny(
31-
LineWidthConverter.Option(),
32-
LineStyleConverter.Option(),
33-
CurrentColorConverter.Option());
34-
35-
public ICssValue Convert(StringSource source) => converter.Convert(source);
36-
}
37-
3828
sealed class BorderAggregator : IValueAggregator, IValueConverter
3929
{
40-
private static readonly IValueConverter converter = new BorderValueConverter();
30+
private static readonly IValueConverter converter = WithAny(
31+
LineWidthConverter.Option(InitialValues.BorderWidthDecl),
32+
LineStyleConverter.Option(InitialValues.BorderStyleDecl),
33+
CurrentColorConverter.Option(InitialValues.BorderColorDecl));
4134

4235
public ICssValue Convert(StringSource source) => converter.Convert(source);
4336

src/AngleSharp.Css/Declarations/BorderLeftDeclaration.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ static class BorderLeftDeclaration
1313
PropertyNames.Border,
1414
};
1515

16-
public static IValueConverter Converter = AggregateTuple(BorderSideConverter);
16+
public static IValueConverter Converter = WithBorderSide(
17+
InitialValues.BorderLeftWidthDecl,
18+
InitialValues.BorderLeftStyleDecl,
19+
InitialValues.BorderLeftColorDecl);
1720

1821
public static ICssValue InitialValue = null;
1922

src/AngleSharp.Css/Declarations/BorderRadiusDeclaration.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static class BorderRadiusDeclaration
2626
PropertyNames.BorderBottomLeftRadius,
2727
};
2828

29-
sealed class BorderRadiusValueConverter : IValueConverter
29+
sealed class BorderRadiusAggregator : IValueAggregator, IValueConverter
3030
{
3131
private readonly IValueConverter _converter = LengthOrPercentConverter.Periodic();
3232

@@ -45,33 +45,20 @@ public ICssValue Convert(StringSource source)
4545

4646
return vertical != null ? new CssBorderRadiusValue(horizontal, vertical) : null;
4747
}
48-
}
49-
50-
sealed class BorderRadiusAggregator : IValueAggregator, IValueConverter
51-
{
52-
private static readonly IValueConverter converter = new BorderRadiusValueConverter();
5348

54-
public ICssValue Convert(StringSource source)
55-
{
56-
return converter.Convert(source);
57-
}
58-
59-
private static ICssValue Both(ICssValue horizontal, ICssValue vertical)
60-
{
61-
return new CssTupleValue(new[] { horizontal, vertical });
62-
}
49+
private static ICssValue Both(params ICssValue[] values) => new CssRadiusValue(values);
6350

6451
public ICssValue Merge(ICssValue[] values)
6552
{
66-
var topLeft = values[0] as CssTupleValue;
67-
var topRight = values[1] as CssTupleValue;
68-
var bottomRight = values[2] as CssTupleValue;
69-
var bottomLeft = values[3] as CssTupleValue;
53+
var topLeft = values[0] as CssRadiusValue;
54+
var topRight = values[1] as CssRadiusValue;
55+
var bottomRight = values[2] as CssRadiusValue;
56+
var bottomLeft = values[3] as CssRadiusValue;
7057

7158
if (topLeft != null && topRight != null && bottomRight != null && bottomLeft != null)
7259
{
73-
var horizontal = new CssPeriodicValue(new[] { topLeft.Items[0], topRight.Items[0], bottomRight.Items[0], bottomLeft.Items[0] });
74-
var vertical = new CssPeriodicValue(new[] { topLeft.Items[1], topRight.Items[1], bottomRight.Items[1], bottomLeft.Items[1] });
60+
var horizontal = new CssPeriodicValue(new[] { topLeft.Width, topRight.Width, bottomRight.Width, bottomLeft.Width });
61+
var vertical = new CssPeriodicValue(new[] { topLeft.Height, topRight.Height, bottomRight.Height, bottomLeft.Height });
7562
return new CssBorderRadiusValue(horizontal, vertical);
7663
}
7764

src/AngleSharp.Css/Declarations/BorderRightDeclaration.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ static class BorderRightDeclaration
1313
PropertyNames.Border,
1414
};
1515

16-
public static IValueConverter Converter = AggregateTuple(BorderSideConverter);
16+
public static IValueConverter Converter = WithBorderSide(
17+
InitialValues.BorderRightWidthDecl,
18+
InitialValues.BorderRightStyleDecl,
19+
InitialValues.BorderRightColorDecl);
1720

1821
public static ICssValue InitialValue = null;
1922

src/AngleSharp.Css/Declarations/BorderTopDeclaration.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ static class BorderTopDeclaration
1313
PropertyNames.Border,
1414
};
1515

16-
public static IValueConverter Converter = AggregateTuple(BorderSideConverter);
16+
public static IValueConverter Converter = WithBorderSide(
17+
InitialValues.BorderTopWidthDecl,
18+
InitialValues.BorderTopStyleDecl,
19+
InitialValues.BorderTopColorDecl);
1720

1821
public static ICssValue InitialValue = null;
1922

src/AngleSharp.Css/Declarations/ListStyleDeclaration.cs

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ namespace AngleSharp.Css.Declarations
22
{
33
using AngleSharp.Css.Converters;
44
using AngleSharp.Css.Dom;
5-
using AngleSharp.Css.Values;
6-
using AngleSharp.Text;
75
using System;
86
using static ValueConverters;
97

108
static class ListStyleDeclaration
119
{
1210
public static String Name = PropertyNames.ListStyle;
1311

14-
public static IValueConverter Converter = new ListStyleAggregator();
12+
public static IValueConverter Converter = AggregateTuple(
13+
WithAny(
14+
ListStyleConverter.Option(InitialValues.ListStyleTypeDecl),
15+
ListPositionConverter.Option(InitialValues.ListStylePositionDecl),
16+
OptionalImageSourceConverter.Option(InitialValues.ListStyleImageDecl)));
1517

1618
public static ICssValue InitialValue = null;
1719

@@ -23,59 +25,5 @@ static class ListStyleDeclaration
2325
PropertyNames.ListStylePosition,
2426
PropertyNames.ListStyleImage,
2527
};
26-
27-
sealed class ListStyleValueConverter : IValueConverter
28-
{
29-
private static readonly IValueConverter converter = WithAny(
30-
ListStyleConverter.Option(),
31-
ListPositionConverter.Option(),
32-
OptionalImageSourceConverter.Option());
33-
34-
public ICssValue Convert(StringSource source)
35-
{
36-
return converter.Convert(source);
37-
}
38-
}
39-
40-
sealed class ListStyleAggregator : IValueAggregator, IValueConverter
41-
{
42-
private static readonly IValueConverter converter = new ListStyleValueConverter();
43-
44-
public ICssValue Convert(StringSource source)
45-
{
46-
return converter.Convert(source);
47-
}
48-
49-
public ICssValue Merge(ICssValue[] values)
50-
{
51-
var type = values[0];
52-
var position = values[1];
53-
var image = values[2];
54-
55-
if (type != null || position != null || image != null)
56-
{
57-
return new CssTupleValue(new[] { type, position, image });
58-
}
59-
60-
return null;
61-
}
62-
63-
public ICssValue[] Split(ICssValue value)
64-
{
65-
var options = value as CssTupleValue;
66-
67-
if (options != null)
68-
{
69-
return new[]
70-
{
71-
options.Items[0],
72-
options.Items[1],
73-
options.Items[2],
74-
};
75-
}
76-
77-
return null;
78-
}
79-
}
8028
}
8129
}

0 commit comments

Comments
 (0)