Extended model for css integer
This commit is contained in:
Родитель
e8dab2666e
Коммит
2a44071575
|
@ -157,8 +157,8 @@ namespace AngleSharp.Css
|
|||
{ CssKeywords.EaseOut, new CssCubicBezierValue(0.0, 0.0, 0.58, 1.0) },
|
||||
{ CssKeywords.EaseInOut, new CssCubicBezierValue(0.42, 0.0, 0.58, 1.0) },
|
||||
{ CssKeywords.Linear, new CssCubicBezierValue(0.0, 0.0, 1.0, 1.0) },
|
||||
{ CssKeywords.StepStart, new CssStepsValue(1, true) },
|
||||
{ CssKeywords.StepEnd, new CssStepsValue(1, false) },
|
||||
{ CssKeywords.StepStart, new CssStepsValue(CssIntegerValue.One, true) },
|
||||
{ CssKeywords.StepEnd, new CssStepsValue(CssIntegerValue.One, false) },
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -11,9 +11,9 @@ namespace AngleSharp.Css.Converters
|
|||
{
|
||||
private static readonly CssCounterValue[] NoneValue = Array.Empty<CssCounterValue>();
|
||||
|
||||
private readonly Int32 _defaultValue;
|
||||
private readonly ICssValue _defaultValue;
|
||||
|
||||
public CounterValueConverter(Int32 defaultValue)
|
||||
public CounterValueConverter(ICssValue defaultValue)
|
||||
{
|
||||
_defaultValue = defaultValue;
|
||||
}
|
||||
|
|
|
@ -2,13 +2,14 @@ namespace AngleSharp.Css.Declarations
|
|||
{
|
||||
using AngleSharp.Css.Converters;
|
||||
using AngleSharp.Css.Dom;
|
||||
using AngleSharp.Css.Values;
|
||||
using System;
|
||||
|
||||
static class CounterIncrementDeclaration
|
||||
{
|
||||
public static String Name = PropertyNames.CounterIncrement;
|
||||
|
||||
public static IValueConverter Converter = new CounterValueConverter(1);
|
||||
public static IValueConverter Converter = new CounterValueConverter(CssIntegerValue.One);
|
||||
|
||||
public static ICssValue InitialValue = InitialValues.CounterIncrementDecl;
|
||||
|
||||
|
|
|
@ -2,13 +2,14 @@ namespace AngleSharp.Css.Declarations
|
|||
{
|
||||
using AngleSharp.Css.Converters;
|
||||
using AngleSharp.Css.Dom;
|
||||
using AngleSharp.Css.Values;
|
||||
using System;
|
||||
|
||||
static class CounterResetDeclaration
|
||||
{
|
||||
public static String Name = PropertyNames.CounterReset;
|
||||
|
||||
public static IValueConverter Converter = new CounterValueConverter(0);
|
||||
public static IValueConverter Converter = new CounterValueConverter(CssIntegerValue.Zero);
|
||||
|
||||
public static ICssValue InitialValue = InitialValues.CounterResetDecl;
|
||||
|
||||
|
|
|
@ -266,17 +266,7 @@ namespace AngleSharp.Css.Parser
|
|||
return null;
|
||||
}
|
||||
|
||||
private static ICssValue ParseIntegerCount(this StringSource source)
|
||||
{
|
||||
var arg = source.ParsePositiveInteger();
|
||||
|
||||
if (arg.HasValue)
|
||||
{
|
||||
return new CssLengthValue(arg.Value, CssLengthValue.Unit.None);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
private static ICssValue ParseIntegerCount(StringSource source) => source.ParsePositiveInteger();
|
||||
|
||||
private static ICssValue ParseRepeat(this StringSource source, Func<StringSource, ICssValue> parseCount, Func<StringSource, ICssValue> parseValue)
|
||||
{
|
||||
|
|
|
@ -84,12 +84,12 @@ namespace AngleSharp.Css.Parser
|
|||
/// <summary>
|
||||
/// Parses the natural integer (int) value.
|
||||
/// </summary>
|
||||
public static Int32? ParseNaturalInteger(this StringSource source)
|
||||
public static CssIntegerValue? ParseNaturalInteger(this StringSource source)
|
||||
{
|
||||
var pos = source.Index;
|
||||
var element = source.ParseInteger();
|
||||
|
||||
if (element.HasValue && element.Value >= 0)
|
||||
if (element.HasValue && element.Value.IntValue >= 0)
|
||||
{
|
||||
return element;
|
||||
}
|
||||
|
@ -101,12 +101,12 @@ namespace AngleSharp.Css.Parser
|
|||
/// <summary>
|
||||
/// Parses the positive integer (int) value.
|
||||
/// </summary>
|
||||
public static Int32? ParsePositiveInteger(this StringSource source)
|
||||
public static CssIntegerValue? ParsePositiveInteger(this StringSource source)
|
||||
{
|
||||
var pos = source.Index;
|
||||
var element = source.ParseInteger();
|
||||
|
||||
if (element.HasValue && element.Value > 0)
|
||||
if (element.HasValue && element.Value.IntValue > 0)
|
||||
{
|
||||
return element;
|
||||
}
|
||||
|
@ -118,12 +118,12 @@ namespace AngleSharp.Css.Parser
|
|||
/// <summary>
|
||||
/// Parses the weight (int) value.
|
||||
/// </summary>
|
||||
public static Int32? ParseWeightInteger(this StringSource source)
|
||||
public static CssIntegerValue? ParseWeightInteger(this StringSource source)
|
||||
{
|
||||
var pos = source.Index;
|
||||
var element = source.ParsePositiveInteger();
|
||||
|
||||
if (element.HasValue && IsWeight(element.Value))
|
||||
if (element.HasValue && IsWeight(element.Value.IntValue))
|
||||
{
|
||||
return element;
|
||||
}
|
||||
|
@ -135,14 +135,19 @@ namespace AngleSharp.Css.Parser
|
|||
/// <summary>
|
||||
/// Parses the binary (int) value.
|
||||
/// </summary>
|
||||
public static Int32? ParseBinary(this StringSource source)
|
||||
public static CssIntegerValue? ParseBinary(this StringSource source)
|
||||
{
|
||||
var pos = source.Index;
|
||||
var element = source.ParseInteger();
|
||||
|
||||
if (element.HasValue && (element.Value == 0 || element.Value == 1))
|
||||
if (element.HasValue)
|
||||
{
|
||||
return element;
|
||||
var val = element.Value.IntValue;
|
||||
|
||||
if (val == 0 || val == 1)
|
||||
{
|
||||
return element;
|
||||
}
|
||||
}
|
||||
|
||||
source.BackTo(pos);
|
||||
|
@ -152,7 +157,7 @@ namespace AngleSharp.Css.Parser
|
|||
/// <summary>
|
||||
/// Parses the integer (int) value.
|
||||
/// </summary>
|
||||
public static Int32? ParseInteger(this StringSource source)
|
||||
public static CssIntegerValue? ParseInteger(this StringSource source)
|
||||
{
|
||||
var unit = source.ParseUnit();
|
||||
|
||||
|
@ -162,7 +167,7 @@ namespace AngleSharp.Css.Parser
|
|||
|
||||
if (Int32.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result))
|
||||
{
|
||||
return result;
|
||||
return new CssIntegerValue(result);
|
||||
}
|
||||
|
||||
var negative = value.StartsWith("-");
|
||||
|
@ -170,7 +175,7 @@ namespace AngleSharp.Css.Parser
|
|||
|
||||
if (allNumbers)
|
||||
{
|
||||
return negative ? Int32.MinValue : Int32.MaxValue;
|
||||
return new CssIntegerValue(negative ? Int32.MinValue : Int32.MaxValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -141,27 +141,27 @@ namespace AngleSharp.Css
|
|||
/// Represents an integer object.
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/CSS/integer
|
||||
/// </summary>
|
||||
public static readonly IValueConverter OnlyIntegerConverter = new StructValueConverter<CssLengthValue>(FromInteger(NumberParser.ParseInteger));
|
||||
public static readonly IValueConverter OnlyIntegerConverter = new StructValueConverter<CssIntegerValue>(NumberParser.ParseInteger);
|
||||
|
||||
/// <summary>
|
||||
/// Represents an integer object that is zero or greater.
|
||||
/// </summary>
|
||||
public static readonly IValueConverter NaturalIntegerConverter = new StructValueConverter<CssLengthValue>(FromInteger(NumberParser.ParseNaturalInteger));
|
||||
public static readonly IValueConverter NaturalIntegerConverter = new StructValueConverter<CssIntegerValue>(NumberParser.ParseNaturalInteger);
|
||||
|
||||
/// <summary>
|
||||
/// Represents an integer object that only allows values \in { 100, 200, ..., 900 }.
|
||||
/// </summary>
|
||||
public static readonly IValueConverter WeightIntegerConverter = new StructValueConverter<CssLengthValue>(FromInteger(NumberParser.ParseWeightInteger));
|
||||
public static readonly IValueConverter WeightIntegerConverter = new StructValueConverter<CssIntegerValue>(NumberParser.ParseWeightInteger);
|
||||
|
||||
/// <summary>
|
||||
/// Represents an integer object that is greater tha zero.
|
||||
/// </summary>
|
||||
public static readonly IValueConverter PositiveIntegerConverter = new StructValueConverter<CssLengthValue>(FromInteger(NumberParser.ParsePositiveInteger));
|
||||
public static readonly IValueConverter PositiveIntegerConverter = new StructValueConverter<CssIntegerValue>(NumberParser.ParsePositiveInteger);
|
||||
|
||||
/// <summary>
|
||||
/// Represents an integer object with 0 or 1.
|
||||
/// </summary>
|
||||
public static readonly IValueConverter BinaryConverter = new StructValueConverter<CssLengthValue>(FromInteger(NumberParser.ParseBinary));
|
||||
public static readonly IValueConverter BinaryConverter = new StructValueConverter<CssIntegerValue>(NumberParser.ParseBinary);
|
||||
|
||||
/// <summary>
|
||||
/// Represents a number object.
|
||||
|
|
|
@ -14,7 +14,7 @@ namespace AngleSharp.Css.Values
|
|||
{
|
||||
#region Fields
|
||||
|
||||
private readonly Int32 _intervals;
|
||||
private readonly ICssValue _intervals;
|
||||
private readonly Boolean _start;
|
||||
|
||||
#endregion
|
||||
|
@ -28,9 +28,9 @@ namespace AngleSharp.Css.Values
|
|||
/// </summary>
|
||||
/// <param name="intervals">It must be a positive integer (greater than 0).</param>
|
||||
/// <param name="start">Optional: If not specified then the change occurs at the end.</param>
|
||||
public CssStepsValue(Int32 intervals, Boolean start = false)
|
||||
public CssStepsValue(ICssValue intervals, Boolean start = false)
|
||||
{
|
||||
_intervals = Math.Max(1, intervals);
|
||||
_intervals = intervals;
|
||||
_start = start;
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@ namespace AngleSharp.Css.Values
|
|||
{
|
||||
var args = new List<ICssValue>
|
||||
{
|
||||
new CssLengthValue(_intervals, CssLengthValue.Unit.None),
|
||||
_intervals
|
||||
};
|
||||
|
||||
if (_start)
|
||||
|
@ -71,9 +71,8 @@ namespace AngleSharp.Css.Values
|
|||
{
|
||||
get
|
||||
{
|
||||
if (_intervals != 1)
|
||||
if (!_intervals.Equals(CssIntegerValue.One))
|
||||
{
|
||||
var fn = FunctionNames.Steps;
|
||||
return Name.CssFunction(Arguments.Join(", "));
|
||||
}
|
||||
else if (_start)
|
||||
|
@ -90,7 +89,7 @@ namespace AngleSharp.Css.Values
|
|||
/// <summary>
|
||||
/// Gets the numbers of intervals.
|
||||
/// </summary>
|
||||
public Int32 Intervals => _intervals;
|
||||
public ICssValue Intervals => _intervals;
|
||||
|
||||
/// <summary>
|
||||
/// Gets if the steps should occur in the beginning.
|
||||
|
@ -106,17 +105,11 @@ namespace AngleSharp.Css.Values
|
|||
/// </summary>
|
||||
/// <param name="other">The value to check against.</param>
|
||||
/// <returns>True if both are equal, otherwise false.</returns>
|
||||
public Boolean Equals(CssStepsValue other)
|
||||
{
|
||||
return _start == other._start && _intervals == other._intervals;
|
||||
}
|
||||
public Boolean Equals(CssStepsValue other) => _start == other._start && _intervals.Equals(other._intervals);
|
||||
|
||||
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssStepsValue value && Equals(value);
|
||||
|
||||
ICssValue ICssValue.Compute(ICssComputeContext context)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
ICssValue ICssValue.Compute(ICssComputeContext context) => this;
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace AngleSharp.Css.Values
|
|||
#region Fields
|
||||
|
||||
private readonly String _name;
|
||||
private readonly Int32 _value;
|
||||
private readonly ICssValue _value;
|
||||
|
||||
#endregion
|
||||
|
||||
|
@ -23,7 +23,7 @@ namespace AngleSharp.Css.Values
|
|||
/// </summary>
|
||||
/// <param name="name">The name of the referenced counter.</param>
|
||||
/// <param name="value">The new value of the counter.</param>
|
||||
public CssCounterValue(String name, Int32 value)
|
||||
public CssCounterValue(String name, ICssValue value)
|
||||
{
|
||||
_name = name;
|
||||
_value = value;
|
||||
|
@ -36,7 +36,7 @@ namespace AngleSharp.Css.Values
|
|||
/// <summary>
|
||||
/// Gets the CSS text representation.
|
||||
/// </summary>
|
||||
public String CssText => String.Concat(_name, " ", _value.ToString());
|
||||
public String CssText => String.Concat(_name, " ", _value.CssText);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the identifier of the counter.
|
||||
|
@ -46,7 +46,7 @@ namespace AngleSharp.Css.Values
|
|||
/// <summary>
|
||||
/// Gets the value of the counter.
|
||||
/// </summary>
|
||||
public Int32 Value => _value;
|
||||
public ICssValue Value => _value;
|
||||
|
||||
#endregion
|
||||
|
||||
|
@ -57,7 +57,7 @@ namespace AngleSharp.Css.Values
|
|||
/// </summary>
|
||||
/// <param name="other">The other counter to check against.</param>
|
||||
/// <returns>True if both are equal, otherwise false.</returns>
|
||||
public Boolean Equals(CssCounterValue other) => Name.Is(other.Name) && Value == other.Value;
|
||||
public Boolean Equals(CssCounterValue other) => Name.Is(other.Name) && _value.Equals(other._value);
|
||||
|
||||
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssCounterValue value && Equals(value);
|
||||
|
||||
|
|
|
@ -8,6 +8,20 @@ namespace AngleSharp.Css.Values
|
|||
/// </summary>
|
||||
public readonly struct CssIntegerValue : IEquatable<CssIntegerValue>, IComparable<CssIntegerValue>, ICssMetricValue
|
||||
{
|
||||
#region Basic lengths
|
||||
|
||||
/// <summary>
|
||||
/// Gets the 0.0.
|
||||
/// </summary>
|
||||
public static readonly CssIntegerValue Zero = new(0);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the 1.0.
|
||||
/// </summary>
|
||||
public static readonly CssIntegerValue One = new(1);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
private readonly Int32 _value;
|
||||
|
|
|
@ -18,7 +18,7 @@ namespace AngleSharp.Css.Values
|
|||
/// <summary>
|
||||
/// Gets the 1.0.
|
||||
/// </summary>
|
||||
public static readonly CssNumberValue One = new(0.0);
|
||||
public static readonly CssNumberValue One = new(1.0);
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче