Skip to content

Commit

Permalink
Integrate percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jan 19, 2024
1 parent 2a44071 commit d856b31
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/AngleSharp.Css/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static String CssColor(this String value)
/// <returns>The indicator if the string was indeed an integer.</returns>
public static Boolean CssInteger(this String value, out Int32 result)
{
return Int32.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out result);
return Int32.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
}

/// <summary>
Expand All @@ -45,7 +45,7 @@ public static Boolean CssInteger(this String value, out Int32 result)
/// <returns>The indicator if the string was indeed a number.</returns>
public static Boolean CssNumber(this String value, out Double result)
{
return Double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out result);
return Double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/AngleSharp.Css/Parser/Micro/CompoundParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static CssTupleValue ParseQuotes(this StringSource source)
/// </summary>
public static CssBorderImageSliceValue ParseBorderImageSlice(this StringSource source)
{
var lengths = new CssLengthValue[4];
var lengths = new ICssValue[4];
var filled = false;
var completed = 0;
var pos = 0;
Expand Down
32 changes: 28 additions & 4 deletions src/AngleSharp.Css/Parser/Micro/UnitParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static ICssValue ParseAutoLength(this StringSource source)
/// </summary>
public static ICssValue ParseBorderWidth(this StringSource source) =>
source.ParseLengthOrCalc() ??
source.ParsePercentOrNumber() ??
source.ParsePercentOrNumberForLength() ??
source.ParseAutoLength();

/// <summary>
Expand All @@ -71,7 +71,7 @@ public static ICssValue ParseLineWidth(this StringSource source) =>
/// </summary>
public static ICssValue ParseLineHeight(this StringSource source) =>
source.ParseLengthOrCalc() ??
source.ParsePercentOrNumber() ??
source.ParsePercentOrNumberForLength() ??
source.ParseNormalLength();

/// <summary>
Expand All @@ -95,12 +95,12 @@ public static ICssValue ParseLineHeight(this StringSource source) =>
/// <summary>
/// Parses a percent or number value.
/// </summary>
public static CssLengthValue? ParsePercentOrNumber(this StringSource source)
private static CssLengthValue? ParsePercentOrNumberForLength(this StringSource source)
{
var pos = source.Index;
var test = source.ParseUnit();

if (test != null && Double.TryParse(test.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
if (test is not null && test.Value.CssNumber(out var value))
{
if (test.Dimension == "%")
{
Expand All @@ -116,6 +116,30 @@ public static ICssValue ParseLineHeight(this StringSource source) =>
return null;
}

/// <summary>
/// Parses a percent or number value.
/// </summary>
public static CssPercentageValue? ParsePercentOrNumber(this StringSource source)
{
var pos = source.Index;
var test = source.ParseUnit();

if (test is not null && test.Value.CssNumber(out var value))
{
if (test.Dimension == "%")
{
return new CssPercentageValue(value, CssPercentageValue.Unit.Percent);
}
else if (test.Dimension.Length == 0)
{
return new CssPercentageValue(value, CssPercentageValue.Unit.None);
}
}

source.BackTo(pos);
return null;
}

/// <summary>
/// Parses an angle value.
/// </summary>
Expand Down
18 changes: 9 additions & 9 deletions src/AngleSharp.Css/Values/Composites/CssBorderImageSliceValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public sealed class CssBorderImageSliceValue : ICssCompositeValue, IEquatable<Cs
{
#region Fields

private readonly CssLengthValue _bottom;
private readonly CssLengthValue _left;
private readonly CssLengthValue _right;
private readonly CssLengthValue _top;
private readonly ICssValue _bottom;
private readonly ICssValue _left;
private readonly ICssValue _right;
private readonly ICssValue _top;
private readonly Boolean _filled;

#endregion
Expand All @@ -29,7 +29,7 @@ public sealed class CssBorderImageSliceValue : ICssCompositeValue, IEquatable<Cs
/// <param name="bottom">The bottom length.</param>
/// <param name="left">The left length.</param>
/// <param name="filled">True if the filled flag is enabled, otherwise false.</param>
public CssBorderImageSliceValue(CssLengthValue top, CssLengthValue right, CssLengthValue bottom, CssLengthValue left, Boolean filled)
public CssBorderImageSliceValue(ICssValue top, ICssValue right, ICssValue bottom, ICssValue left, Boolean filled)
{
_top = top;
_right = right;
Expand All @@ -45,22 +45,22 @@ public CssBorderImageSliceValue(CssLengthValue top, CssLengthValue right, CssLen
/// <summary>
/// Gets the bottom coordinate.
/// </summary>
public CssLengthValue Bottom => _bottom;
public ICssValue Bottom => _bottom;

/// <summary>
/// Gets the left coordinate.
/// </summary>
public CssLengthValue Left => _left;
public ICssValue Left => _left;

/// <summary>
/// Gets the top coordinate.
/// </summary>
public CssLengthValue Top => _top;
public ICssValue Top => _top;

/// <summary>
/// Gets the right coordinate.
/// </summary>
public CssLengthValue Right => _right;
public ICssValue Right => _right;

/// <summary>
/// Gets if the slice should be filled.
Expand Down

0 comments on commit d856b31

Please sign in to comment.