BorderRadiusConverter
using AngleSharp.Dom.Css;
using AngleSharp.Extensions;
using AngleSharp.Parser.Css;
using System.Collections.Generic;
using System.Linq;
namespace AngleSharp.Css.ValueConverters
{
internal sealed class BorderRadiusConverter : IValueConverter
{
private sealed class BorderRadiusValue : IPropertyValue
{
private readonly IPropertyValue _horizontal;
private readonly IPropertyValue _vertical;
private readonly CssValue _original;
public string CssText {
get {
string cssText = _horizontal.CssText;
if (_vertical != null) {
string cssText2 = _vertical.CssText;
if (cssText != cssText2)
return cssText + " / " + cssText2;
}
return cssText;
}
}
public CssValue Original => _original;
public BorderRadiusValue(IPropertyValue horizontal, IPropertyValue vertical, CssValue original)
{
_horizontal = horizontal;
_vertical = vertical;
_original = original;
}
public CssValue ExtractFor(string name)
{
CssValue items = _horizontal.ExtractFor(name);
return new CssValue(Enumerable.Concat(second: _vertical.ExtractFor(name), first: items.Concat(CssToken.Whitespace)));
}
}
private readonly IValueConverter _converter = Converters.LengthOrPercentConverter.Periodic(PropertyNames.BorderTopLeftRadius, PropertyNames.BorderTopRightRadius, PropertyNames.BorderBottomRightRadius, PropertyNames.BorderBottomLeftRadius);
public IPropertyValue Convert(IEnumerable<CssToken> value)
{
List<CssToken> list = new List<CssToken>();
List<CssToken> list2 = new List<CssToken>();
List<CssToken> list3 = list;
foreach (CssToken item in value) {
if (item.Type == CssTokenType.Delim && item.Data.Is("/")) {
if (list3 == list2)
return null;
list3 = list2;
} else
list3.Add(item);
}
IPropertyValue propertyValue = _converter.Convert(list);
if (propertyValue == null)
return null;
IPropertyValue propertyValue2 = (list3 == list2) ? _converter.Convert(list2) : propertyValue;
if (propertyValue2 == null)
return null;
return new BorderRadiusValue(propertyValue, propertyValue2, new CssValue(value));
}
public IPropertyValue Construct(CssProperty[] properties)
{
if (properties.Length == 4) {
List<CssToken> list = new List<CssToken>();
List<CssToken> list2 = new List<CssToken>();
List<CssProperty> list3 = new List<CssProperty>();
list3.Add(properties.First((CssProperty m) => m.Name.Is(PropertyNames.BorderTopLeftRadius)));
list3.Add(properties.First((CssProperty m) => m.Name.Is(PropertyNames.BorderTopRightRadius)));
list3.Add(properties.First((CssProperty m) => m.Name.Is(PropertyNames.BorderBottomRightRadius)));
list3.Add(properties.First((CssProperty m) => m.Name.Is(PropertyNames.BorderBottomLeftRadius)));
for (int i = 0; i < list3.Count; i++) {
IEnumerable<IPropertyValue> enumerable = list3[i].DeclaredValue as IEnumerable<IPropertyValue>;
if (enumerable == null)
return null;
CssValue original = enumerable.First().Original;
CssValue original2 = enumerable.Last().Original;
if (i != 0) {
list.Add(CssToken.Whitespace);
list2.Add(CssToken.Whitespace);
}
list.AddRange(original);
list2.AddRange(original2);
}
IPropertyValue horizontal = _converter.Convert(list);
IPropertyValue vertical = _converter.Convert(list2);
IEnumerable<CssToken> tokens = list.Concat(new CssToken(CssTokenType.Delim, "/", TextPosition.Empty)).Concat(list2);
return new BorderRadiusValue(horizontal, vertical, new CssValue(tokens));
}
return null;
}
}
}