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;
private readonly string[] _labels;
public string[] Values {
get {
string cssText = _top.CssText;
string cssText2 = _right.CssText;
string cssText3 = _bottom.CssText;
string cssText4 = _left.CssText;
if (cssText2.Is(cssText4)) {
if (cssText.Is(cssText3)) {
if (cssText2.Is(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, string[] labels)
{
_top = options[0];
_right = (options[1] ?? _top);
_bottom = (options[2] ?? _top);
_left = (options[3] ?? _right);
_original = new CssValue(tokens);
_labels = labels;
}
public CssValue ExtractFor(string name)
{
if (name.Is(_labels[0]))
return _top.Original;
if (name.Is(_labels[1]))
return _right.Original;
if (name.Is(_labels[2]))
return _bottom.Original;
if (name.Is(_labels[3]))
return _left.Original;
return null;
}
}
private readonly IValueConverter _converter;
private readonly string[] _labels;
public PeriodicValueConverter(IValueConverter converter, string[] labels)
{
_converter = converter;
_labels = ((labels.Length == 4) ? labels : Enumerable.Repeat(string.Empty, 4).ToArray());
}
public IPropertyValue Convert(IEnumerable<CssToken> value)
{
List<CssToken> list = new List<CssToken>(value);
IPropertyValue[] array = new IPropertyValue[4];
if (list.Count == 0)
return null;
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, _labels);
}
public IPropertyValue Construct(CssProperty[] properties)
{
if (properties.Length == 4) {
IPropertyValue[] array = new IPropertyValue[4] {
_converter.Construct((from m in properties
where m.Name == _labels[0]
select m).ToArray()),
_converter.Construct((from m in properties
where m.Name == _labels[1]
select m).ToArray()),
_converter.Construct((from m in properties
where m.Name == _labels[2]
select m).ToArray()),
_converter.Construct((from m in properties
where m.Name == _labels[3]
select m).ToArray())
};
if (array[0] == null || array[1] == null || array[2] == null || array[3] == null)
return null;
return new PeriodicValue(array, Enumerable.Empty<CssToken>(), _labels);
}
return null;
}
}
}