GradientConverter
using AngleSharp.Dom.Css;
using AngleSharp.Extensions;
using AngleSharp.Parser.Css;
using System.Collections.Generic;
namespace AngleSharp.Css.ValueConverters
{
internal abstract class GradientConverter : IValueConverter
{
private sealed class StopValue : IPropertyValue
{
private readonly IPropertyValue _color;
private readonly IPropertyValue _position;
private readonly CssValue _original;
public string CssText {
get {
if (_color == null && _position != null)
return _position.CssText;
if (_color != null && _position == null)
return _color.CssText;
return _color.CssText + " " + _position.CssText;
}
}
public CssValue Original => _original;
public StopValue(IPropertyValue color, IPropertyValue position, IEnumerable<CssToken> tokens)
{
_color = color;
_position = position;
_original = new CssValue(tokens);
}
public CssValue ExtractFor(string name)
{
return _original;
}
}
private sealed class GradientValue : IPropertyValue
{
private readonly bool _repeating;
private readonly IPropertyValue _initial;
private readonly IPropertyValue[] _stops;
private readonly CssValue _original;
public string CssText {
get {
int num = _stops.Length;
if (_initial != null)
num++;
string[] array = new string[num];
num = 0;
if (_initial != null)
array[num++] = _initial.CssText;
for (int i = 0; i < _stops.Length; i++) {
array[num++] = _stops[i].CssText;
}
return string.Join(", ", array);
}
}
public CssValue Original => _original;
public GradientValue(bool repeating, IPropertyValue initial, IPropertyValue[] stops, IEnumerable<CssToken> tokens)
{
_repeating = repeating;
_initial = initial;
_stops = stops;
_original = new CssValue(tokens);
}
public CssValue ExtractFor(string name)
{
return _original;
}
}
private readonly bool _repeating;
public GradientConverter(bool repeating)
{
_repeating = repeating;
}
private static IPropertyValue[] ToGradientStops(List<List<CssToken>> values, int offset)
{
IPropertyValue[] array = new IPropertyValue[values.Count - offset];
int num = offset;
int num2 = 0;
while (num < values.Count) {
array[num2] = ToGradientStop(values[num]);
if (array[num2] == null)
return null;
num++;
num2++;
}
return array;
}
private static IPropertyValue ToGradientStop(List<CssToken> value)
{
IPropertyValue propertyValue = null;
IPropertyValue propertyValue2 = null;
List<List<CssToken>> list = value.ToItems();
if (list.Count != 0) {
propertyValue2 = Converters.LengthOrPercentConverter.Convert(list[list.Count - 1]);
if (propertyValue2 != null)
list.RemoveAt(list.Count - 1);
}
if (list.Count != 0) {
propertyValue = Converters.ColorConverter.Convert(list[list.Count - 1]);
if (propertyValue != null)
list.RemoveAt(list.Count - 1);
}
if (list.Count != 0)
return null;
return new StopValue(propertyValue, propertyValue2, value);
}
public IPropertyValue Convert(IEnumerable<CssToken> value)
{
List<List<CssToken>> list = value.ToList();
IPropertyValue propertyValue = (list.Count != 0) ? ConvertFirstArgument(list[0]) : null;
int offset = (propertyValue != null) ? 1 : 0;
IPropertyValue[] array = ToGradientStops(list, offset);
if (array == null)
return null;
return new GradientValue(_repeating, propertyValue, array, value);
}
public IPropertyValue Construct(CssProperty[] properties)
{
return properties.Guard<GradientValue>();
}
protected abstract IPropertyValue ConvertFirstArgument(IEnumerable<CssToken> value);
}
}