Consolidation and type improvements
This commit is contained in:
Родитель
bafe780f44
Коммит
fd3d6536f2
|
@ -9,6 +9,8 @@ namespace AngleSharp.Css.Converters
|
|||
|
||||
sealed class CounterValueConverter : IValueConverter
|
||||
{
|
||||
private static readonly CounterValue[] NoneValue = new CounterValue[0];
|
||||
|
||||
private readonly Int32 _defaultValue;
|
||||
|
||||
public CounterValueConverter(Int32 defaultValue)
|
||||
|
@ -18,7 +20,7 @@ namespace AngleSharp.Css.Converters
|
|||
|
||||
public ICssValue Convert(StringSource source)
|
||||
{
|
||||
var counters = new List<ICssValue>();
|
||||
var counters = new List<CounterValue>();
|
||||
|
||||
if (!source.IsIdentifier(CssKeywords.None))
|
||||
{
|
||||
|
@ -37,10 +39,10 @@ namespace AngleSharp.Css.Converters
|
|||
counters.Add(new CounterValue(name, value));
|
||||
}
|
||||
|
||||
return new CssCountersValue(counters.ToArray());
|
||||
return new CssTupleValue<CounterValue>(counters.ToArray());
|
||||
}
|
||||
|
||||
return new CssCountersValue();
|
||||
return new Constant<CounterValue[]>(CssKeywords.None, NoneValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,25 +27,17 @@ namespace AngleSharp.Css.Converters
|
|||
return varRefs;
|
||||
}
|
||||
|
||||
public static IValueConverter Many(this IValueConverter converter, Int32 min = 1, Int32 max = UInt16.MaxValue, String separator = null)
|
||||
{
|
||||
return new OneOrMoreValueConverter(converter, min, max, separator);
|
||||
}
|
||||
public static IValueConverter Many(this IValueConverter converter, Int32 min = 1, Int32 max = UInt16.MaxValue, String separator = null) =>
|
||||
new OneOrMoreValueConverter(converter, min, max, separator);
|
||||
|
||||
public static IValueConverter FromList(this IValueConverter converter)
|
||||
{
|
||||
return new ListValueConverter(converter);
|
||||
}
|
||||
public static IValueConverter FromList(this IValueConverter converter) =>
|
||||
new ListValueConverter(converter);
|
||||
|
||||
public static IValueConverter ToConverter<T>(this Dictionary<String, T> values)
|
||||
{
|
||||
return new DictionaryValueConverter<T>(values);
|
||||
}
|
||||
public static IValueConverter ToConverter<T>(this Dictionary<String, T> values) =>
|
||||
new DictionaryValueConverter<T>(values);
|
||||
|
||||
public static IValueConverter Periodic(this IValueConverter converter)
|
||||
{
|
||||
return new PeriodicValueConverter(converter);
|
||||
}
|
||||
public static IValueConverter Periodic(this IValueConverter converter) =>
|
||||
new PeriodicValueConverter(converter);
|
||||
|
||||
public static IValueConverter Exclusive(this IValueConverter converter)
|
||||
{
|
||||
|
@ -65,17 +57,14 @@ namespace AngleSharp.Css.Converters
|
|||
});
|
||||
}
|
||||
|
||||
public static IValueConverter Option(this IValueConverter converter)
|
||||
{
|
||||
return new OptionValueConverter<Object>(converter, null);
|
||||
}
|
||||
public static IValueConverter Option(this IValueConverter converter) =>
|
||||
new OptionValueConverter<Object>(converter, null);
|
||||
|
||||
public static IValueConverter Option<T>(this IValueConverter converter, T defaultValue)
|
||||
{
|
||||
return new OptionValueConverter<T>(converter, defaultValue);
|
||||
}
|
||||
public static IValueConverter Option<T>(this IValueConverter converter, T defaultValue) =>
|
||||
new OptionValueConverter<T>(converter, defaultValue);
|
||||
|
||||
public static String Join(this ICssValue[] values, String separator)
|
||||
public static String Join<T>(this T[] values, String separator)
|
||||
where T : ICssValue
|
||||
{
|
||||
var buffer = StringBuilderPool.Obtain();
|
||||
var previous = false;
|
||||
|
@ -98,29 +87,5 @@ namespace AngleSharp.Css.Converters
|
|||
|
||||
return buffer.ToPool();
|
||||
}
|
||||
|
||||
public static String Join<T>(this T[] values, String separator)
|
||||
{
|
||||
var buffer = StringBuilderPool.Obtain();
|
||||
var previous = false;
|
||||
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
var str = values[i].ToString();
|
||||
|
||||
if (!String.IsNullOrEmpty(str))
|
||||
{
|
||||
if (previous)
|
||||
{
|
||||
buffer.Append(separator);
|
||||
}
|
||||
|
||||
buffer.Append(str);
|
||||
previous = true;
|
||||
}
|
||||
}
|
||||
|
||||
return buffer.ToPool();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
namespace AngleSharp.Css.Values
|
||||
{
|
||||
using AngleSharp.Css.Converters;
|
||||
using AngleSharp.Css.Dom;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the CSS counters definition.
|
||||
/// </summary>
|
||||
class CssCountersValue : ICssMultipleValue
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private readonly ICssValue[] _counters;
|
||||
|
||||
#endregion
|
||||
|
||||
#region ctor
|
||||
|
||||
public CssCountersValue()
|
||||
{
|
||||
_counters = null;
|
||||
}
|
||||
|
||||
public CssCountersValue(ICssValue[] counters)
|
||||
{
|
||||
_counters = counters;
|
||||
}
|
||||
|
||||
public ICssValue this[Int32 index] => _counters[index];
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public ICssValue[] Values => _counters;
|
||||
|
||||
public String CssText => _counters != null ? _counters.Join(" ") : CssKeywords.None;
|
||||
|
||||
public Int32 Count => _counters.Length;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
IEnumerator<ICssValue> IEnumerable<ICssValue>.GetEnumerator() =>
|
||||
_counters.AsEnumerable().GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => _counters.GetEnumerator();
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -10,17 +10,18 @@ namespace AngleSharp.Css.Values
|
|||
/// <summary>
|
||||
/// Represents a CSS value list.
|
||||
/// </summary>
|
||||
class CssListValue : ICssMultipleValue
|
||||
class CssListValue<T> : ICssMultipleValue
|
||||
where T : ICssValue
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private readonly ICssValue[] _items;
|
||||
private readonly T[] _items;
|
||||
|
||||
#endregion
|
||||
|
||||
#region ctor
|
||||
|
||||
public CssListValue(ICssValue[] items)
|
||||
public CssListValue(T[] items)
|
||||
{
|
||||
_items = items;
|
||||
}
|
||||
|
@ -31,8 +32,8 @@ namespace AngleSharp.Css.Values
|
|||
|
||||
public ICssValue this[Int32 index] => _items[index];
|
||||
|
||||
public ICssValue[] Items => _items;
|
||||
|
||||
public T[] Items => _items;
|
||||
|
||||
public String CssText => _items.Join(", ");
|
||||
|
||||
public Int32 Count => _items.Length;
|
||||
|
@ -42,10 +43,25 @@ namespace AngleSharp.Css.Values
|
|||
#region Methods
|
||||
|
||||
IEnumerator<ICssValue> IEnumerable<ICssValue>.GetEnumerator() =>
|
||||
_items.AsEnumerable().GetEnumerator();
|
||||
_items.OfType<ICssValue>().GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => _items.GetEnumerator();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a CSS value list.
|
||||
/// </summary>
|
||||
class CssListValue : CssListValue<ICssValue>
|
||||
{
|
||||
#region ctor
|
||||
|
||||
public CssListValue(ICssValue[] items)
|
||||
: base(items)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,20 +10,21 @@ namespace AngleSharp.Css.Values
|
|||
/// <summary>
|
||||
/// Represents a tuple of CSS values.
|
||||
/// </summary>
|
||||
class CssTupleValue : ICssMultipleValue
|
||||
class CssTupleValue<T> : ICssMultipleValue
|
||||
where T : ICssValue
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private readonly ICssValue[] _items;
|
||||
private readonly T[] _items;
|
||||
private readonly String _separator;
|
||||
|
||||
#endregion
|
||||
|
||||
#region ctor
|
||||
|
||||
public CssTupleValue(ICssValue[] items, String separator = null)
|
||||
|
||||
public CssTupleValue(T[] items = null, String separator = null)
|
||||
{
|
||||
_items = items;
|
||||
_items = items ?? new T[0];
|
||||
_separator = separator ?? " ";
|
||||
}
|
||||
|
||||
|
@ -33,7 +34,7 @@ namespace AngleSharp.Css.Values
|
|||
|
||||
public ICssValue this[Int32 index] => _items[index];
|
||||
|
||||
public ICssValue[] Items => _items;
|
||||
public T[] Items => _items;
|
||||
|
||||
public String Separator => _separator;
|
||||
|
||||
|
@ -46,10 +47,25 @@ namespace AngleSharp.Css.Values
|
|||
#region Methods
|
||||
|
||||
IEnumerator<ICssValue> IEnumerable<ICssValue>.GetEnumerator() =>
|
||||
_items.AsEnumerable().GetEnumerator();
|
||||
_items.OfType<ICssValue>().GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => _items.GetEnumerator();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a tuple of CSS values.
|
||||
/// </summary>
|
||||
class CssTupleValue : CssTupleValue<ICssValue>
|
||||
{
|
||||
#region ctor
|
||||
|
||||
public CssTupleValue(ICssValue[] items = null, String separator = null)
|
||||
: base(items, separator)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче