PeriodicValueConverter
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 PeriodicValueConverter : IValueConverter
{
private sealed class PeriodicValue : IPropertyValue
{
private readonly IPropertyValue _top;
private readonly IPropertyValue _right;
private readonly IPropertyValue _bottom;
private readonly IPropertyValue _left;
private readonly CssValue _original;
public string[] Values {
get {
string cssText = _top.CssText;
string cssText2 = _right.CssText;
string cssText3 = _bottom.CssText;
string cssText4 = _left.CssText;
if (cssText2 == cssText4) {
if (cssText == cssText3) {
if (cssText2 == cssText)
return new string[1] {
cssText
};
return new string[2] {
cssText,
cssText2
};
}
return new string[3] {
cssText,
cssText2,
cssText3
};
}
return new string[4] {
cssText,
cssText2,
cssText3,
cssText4
};
}
}
public string CssText => string.Join(" ", Values);
public CssValue Original => _original;
public PeriodicValue(IPropertyValue[] options, IEnumerable<CssToken> tokens)
{
_top = options[0];
_right = (options[1] ?? _top);
_bottom = (options[2] ?? _top);
_left = (options[3] ?? _right);
_original = new CssValue(tokens);
}
public CssValue ExtractFor(string name)
{
if (name.Contains("top"))
return _top.Original;
if (name.Contains("left"))
return _left.Original;
if (name.Contains("right"))
return _right.Original;
if (name.Contains("bottom"))
return _bottom.Original;
return null;
}
}
private readonly IValueConverter _converter;
public PeriodicValueConverter(IValueConverter converter)
{
_converter = converter;
}
public IPropertyValue Convert(IEnumerable<CssToken> value)
{
List<CssToken> list = new List<CssToken>(value);
IPropertyValue[] array = new IPropertyValue[4];
for (int i = 0; i < array.Length; i++) {
if (list.Count == 0)
break;
array[i] = _converter.VaryStart(list);
if (array[i] == null)
return null;
}
if (list.Count != 0)
return null;
return new PeriodicValue(array, value);
}
public IPropertyValue Construct(CssProperty[] properties)
{
if (properties.Length == 4) {
IPropertyValue[] array = new IPropertyValue[4] {
_converter.Construct(new CssProperty[1] {
properties[0]
}),
_converter.Construct(new CssProperty[1] {
properties[1]
}),
_converter.Construct(new CssProperty[1] {
properties[2]
}),
_converter.Construct(new CssProperty[1] {
properties[3]
})
};
if (array[0] == null || array[1] == null || array[2] == null || array[3] == null)
return null;
return new PeriodicValue(array, Enumerable.Empty<CssToken>());
}
return null;
}
}
}