Skip to content

Commit 062d85e

Browse files
committed
Rewire shorthand resolution
1 parent 6d6af09 commit 062d85e

File tree

8 files changed

+106
-180
lines changed

8 files changed

+106
-180
lines changed

src/AngleSharp.Css/BrowsingContextExtensions.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ internal static DeclarationInfo GetDeclarationInfo(this IBrowsingContext context
5959
return factory.Create(propertyName);
6060
}
6161

62+
internal static ICssProperty CreateShorthand(this IBrowsingContext context, String name, ICssValue[] longhands, Boolean important)
63+
{
64+
var factory = context.GetFactory<IDeclarationFactory>();
65+
var info = factory.Create(name);
66+
var value = info.Combine(longhands);
67+
return new CssProperty(name, info.Converter, info.Flags, value, important);
68+
}
69+
70+
internal static ICssProperty[] CreateLonghands(this IBrowsingContext context, ICssProperty shorthand)
71+
{
72+
var factory = context.GetFactory<IDeclarationFactory>();
73+
var info = factory.Create(shorthand.Name);
74+
var values = info.Seperate(factory, shorthand.RawValue);
75+
return factory.CreateProperties(info.Longhands, values, shorthand.IsImportant);
76+
}
77+
6278
internal static CssProperty CreateProperty(this IBrowsingContext context, String propertyName)
6379
{
6480
var info = context.GetDeclarationInfo(propertyName);
@@ -75,7 +91,25 @@ internal static CssProperty CreateProperty(this IBrowsingContext context, String
7591
private static Boolean IsAllowingUnknownDeclarations(this IBrowsingContext context)
7692
{
7793
var parser = context.GetProvider<CssParser>();
78-
return parser != null ? parser.Options.IsIncludingUnknownDeclarations : true;
94+
return parser?.Options.IsIncludingUnknownDeclarations ?? true;
95+
}
96+
97+
private static ICssProperty[] CreateProperties(this IDeclarationFactory factory, String[] names, ICssValue[] values, Boolean important)
98+
{
99+
if (values != null && values.Length == names.Length)
100+
{
101+
var properties = new ICssProperty[names.Length];
102+
103+
for (var i = 0; i < names.Length; i++)
104+
{
105+
var info = factory.Create(names[i]);
106+
properties[i] = new CssProperty(names[i], info.Converter, info.Flags, values[i], important);
107+
}
108+
109+
return properties;
110+
}
111+
112+
return null;
79113
}
80114
}
81115
}

src/AngleSharp.Css/DeclarationInfo.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public class DeclarationInfo
2121
public DeclarationInfo(String name, IValueConverter converter, PropertyFlags flags = PropertyFlags.None, ICssValue initialValue = null, String[] shorthands = null, String[] longhands = null)
2222
{
2323
Name = name;
24-
Converter = Or(converter, AssignInitial(initialValue));
24+
Converter = initialValue != null ? Or(converter, AssignInitial(initialValue)) : converter;
25+
Aggregator = converter as IValueAggregator;
2526
Flags = flags;
2627
InitialValue = initialValue;
2728
Shorthands = shorthands ?? Array.Empty<String>();
@@ -43,6 +44,11 @@ public DeclarationInfo(String name, IValueConverter converter, PropertyFlags fla
4344
/// </summary>
4445
public IValueConverter Converter { get; }
4546

47+
/// <summary>
48+
/// Gets the value aggregator, if any.
49+
/// </summary>
50+
public IValueAggregator Aggregator { get; }
51+
4652
/// <summary>
4753
/// Gets the flags of the declaration.
4854
/// </summary>

src/AngleSharp.Css/Declarations/AnimationDeclaration.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static class AnimationDeclaration
3030
PropertyNames.AnimationName,
3131
};
3232

33-
sealed class AnimationConverter : IValueConverter
33+
sealed class AnimationAggregator : IValueAggregator, IValueConverter
3434
{
3535
private static readonly IValueConverter ListConverter = WithAny(
3636
TimeConverter.Option(),
@@ -43,13 +43,6 @@ sealed class AnimationConverter : IValueConverter
4343
IdentifierConverter.Option()).FromList();
4444

4545
public ICssValue Convert(StringSource source) => ListConverter.Convert(source);
46-
}
47-
48-
sealed class AnimationAggregator : IValueAggregator, IValueConverter
49-
{
50-
private static readonly IValueConverter converter = new AnimationConverter();
51-
52-
public ICssValue Convert(StringSource source) => converter.Convert(source);
5346

5447
public ICssValue Merge(ICssValue[] values)
5548
{
@@ -72,9 +65,7 @@ public ICssValue Merge(ICssValue[] values)
7265

7366
public ICssValue[] Split(ICssValue value)
7467
{
75-
var list = value as CssListValue;
76-
77-
if (list != null)
68+
if (value is CssListValue list)
7869
{
7970
return new[]
8071
{

src/AngleSharp.Css/Declarations/BackgroundDeclaration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private static Int32 GetCount(params CssListValue[] lists)
221221

222222
foreach (var list in lists)
223223
{
224-
count = Math.Max(count, list.Count);
224+
count = Math.Max(count, list?.Count ?? 0);
225225
}
226226

227227
return count;

src/AngleSharp.Css/Declarations/BorderColorDeclaration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ sealed class BorderColorAggregator : IValueAggregator, IValueConverter
3939

4040
public ICssValue Merge(ICssValue[] values)
4141
{
42-
if (values.Length > 0)
42+
if (values[0] != null)
4343
{
4444
return new CssPeriodicValue(values);
4545
}

src/AngleSharp.Css/Declarations/BorderWidthDeclaration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ sealed class BorderWidthAggregator : IValueAggregator, IValueConverter
3939

4040
public ICssValue Merge(ICssValue[] values)
4141
{
42-
if (values.Length > 0)
42+
if (values[0] != null)
4343
{
4444
return new CssPeriodicValue(values);
4545
}

0 commit comments

Comments
 (0)