ConstraintValueConverter
using AngleSharp.Dom.Css;
using AngleSharp.Parser.Css;
using System.Collections.Generic;
using System.Linq;
namespace AngleSharp.Css.ValueConverters
{
internal sealed class ConstraintValueConverter : IValueConverter
{
private sealed class TransformationValueConverter : IPropertyValue
{
private readonly IPropertyValue _value;
private readonly string[] _labels;
public string CssText => _value.CssText;
public CssValue Original => _value.Original;
public TransformationValueConverter(IPropertyValue value, string[] labels)
{
_value = value;
_labels = labels;
}
public CssValue ExtractFor(string name)
{
if (!_labels.Contains(name))
return null;
return _value.ExtractFor(name);
}
}
private readonly IValueConverter _converter;
private readonly string[] _labels;
public ConstraintValueConverter(IValueConverter converter, string[] labels)
{
_converter = converter;
_labels = labels;
}
public IPropertyValue Convert(IEnumerable<CssToken> value)
{
IPropertyValue propertyValue = _converter.Convert(value);
if (propertyValue == null)
return null;
return new TransformationValueConverter(propertyValue, _labels);
}
public IPropertyValue Construct(CssProperty[] properties)
{
IEnumerable<CssProperty> enumerable = from m in properties
where _labels.Contains(m.Name)
select m;
string text = null;
foreach (CssProperty item in enumerable) {
string value = item.Value;
if (text != null && value != text)
return null;
text = value;
}
IPropertyValue propertyValue = _converter.Construct(enumerable.Take(1).ToArray());
if (propertyValue == null)
return null;
return new TransformationValueConverter(propertyValue, _labels);
}
}
}