OneOrMoreValueConverter
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 OneOrMoreValueConverter : IValueConverter
{
private sealed class MultipleValue : IPropertyValue
{
private readonly IPropertyValue[] _values;
private readonly CssValue _value;
public string CssText => string.Join(" ", from m in _values
where !string.IsNullOrEmpty(m.CssText)
select m.CssText);
public CssValue Original => _value;
public MultipleValue(IPropertyValue[] values, IEnumerable<CssToken> tokens)
{
_values = values;
_value = new CssValue(tokens);
}
public CssValue ExtractFor(string name)
{
List<CssToken> list = new List<CssToken>();
IPropertyValue[] values = _values;
for (int i = 0; i < values.Length; i++) {
CssValue cssValue = values[i].ExtractFor(name);
if (cssValue != null) {
if (list.Count > 0)
list.Add(CssToken.Whitespace);
list.AddRange(cssValue);
}
}
return new CssValue(list);
}
}
private readonly IValueConverter _converter;
private readonly int _minimum;
private readonly int _maximum;
public OneOrMoreValueConverter(IValueConverter converter, int minimum, int maximum)
{
_converter = converter;
_minimum = minimum;
_maximum = maximum;
}
public IPropertyValue Convert(IEnumerable<CssToken> value)
{
List<List<CssToken>> list = value.ToItems();
int count = list.Count;
if (count >= _minimum && count <= _maximum) {
IPropertyValue[] array = new IPropertyValue[list.Count];
for (int i = 0; i < count; i++) {
array[i] = _converter.Convert(list[i]);
if (array[i] == null)
return null;
}
return new MultipleValue(array, value);
}
return null;
}
public IPropertyValue Construct(CssProperty[] properties)
{
IPropertyValue propertyValue = properties.Guard<MultipleValue>();
if (propertyValue == null) {
IPropertyValue[] array = new IPropertyValue[properties.Length];
for (int i = 0; i < properties.Length; i++) {
IPropertyValue propertyValue2 = _converter.Construct(new CssProperty[1] {
properties[i]
});
if (propertyValue2 == null)
return null;
array[i] = propertyValue2;
}
propertyValue = new MultipleValue(array, Enumerable.Empty<CssToken>());
}
return propertyValue;
}
}
}