Skip to content

Commit d856b31

Browse files
committed
Integrate percentage
1 parent 2a44071 commit d856b31

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

src/AngleSharp.Css/Extensions/StringExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static String CssColor(this String value)
3434
/// <returns>The indicator if the string was indeed an integer.</returns>
3535
public static Boolean CssInteger(this String value, out Int32 result)
3636
{
37-
return Int32.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out result);
37+
return Int32.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
3838
}
3939

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

5151
/// <summary>

src/AngleSharp.Css/Parser/Micro/CompoundParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static CssTupleValue ParseQuotes(this StringSource source)
4141
/// </summary>
4242
public static CssBorderImageSliceValue ParseBorderImageSlice(this StringSource source)
4343
{
44-
var lengths = new CssLengthValue[4];
44+
var lengths = new ICssValue[4];
4545
var filled = false;
4646
var completed = 0;
4747
var pos = 0;

src/AngleSharp.Css/Parser/Micro/UnitParser.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static ICssValue ParseAutoLength(this StringSource source)
5656
/// </summary>
5757
public static ICssValue ParseBorderWidth(this StringSource source) =>
5858
source.ParseLengthOrCalc() ??
59-
source.ParsePercentOrNumber() ??
59+
source.ParsePercentOrNumberForLength() ??
6060
source.ParseAutoLength();
6161

6262
/// <summary>
@@ -71,7 +71,7 @@ public static ICssValue ParseLineWidth(this StringSource source) =>
7171
/// </summary>
7272
public static ICssValue ParseLineHeight(this StringSource source) =>
7373
source.ParseLengthOrCalc() ??
74-
source.ParsePercentOrNumber() ??
74+
source.ParsePercentOrNumberForLength() ??
7575
source.ParseNormalLength();
7676

7777
/// <summary>
@@ -95,12 +95,12 @@ public static ICssValue ParseLineHeight(this StringSource source) =>
9595
/// <summary>
9696
/// Parses a percent or number value.
9797
/// </summary>
98-
public static CssLengthValue? ParsePercentOrNumber(this StringSource source)
98+
private static CssLengthValue? ParsePercentOrNumberForLength(this StringSource source)
9999
{
100100
var pos = source.Index;
101101
var test = source.ParseUnit();
102102

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

119+
/// <summary>
120+
/// Parses a percent or number value.
121+
/// </summary>
122+
public static CssPercentageValue? ParsePercentOrNumber(this StringSource source)
123+
{
124+
var pos = source.Index;
125+
var test = source.ParseUnit();
126+
127+
if (test is not null && test.Value.CssNumber(out var value))
128+
{
129+
if (test.Dimension == "%")
130+
{
131+
return new CssPercentageValue(value, CssPercentageValue.Unit.Percent);
132+
}
133+
else if (test.Dimension.Length == 0)
134+
{
135+
return new CssPercentageValue(value, CssPercentageValue.Unit.None);
136+
}
137+
}
138+
139+
source.BackTo(pos);
140+
return null;
141+
}
142+
119143
/// <summary>
120144
/// Parses an angle value.
121145
/// </summary>

src/AngleSharp.Css/Values/Composites/CssBorderImageSliceValue.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public sealed class CssBorderImageSliceValue : ICssCompositeValue, IEquatable<Cs
1111
{
1212
#region Fields
1313

14-
private readonly CssLengthValue _bottom;
15-
private readonly CssLengthValue _left;
16-
private readonly CssLengthValue _right;
17-
private readonly CssLengthValue _top;
14+
private readonly ICssValue _bottom;
15+
private readonly ICssValue _left;
16+
private readonly ICssValue _right;
17+
private readonly ICssValue _top;
1818
private readonly Boolean _filled;
1919

2020
#endregion
@@ -29,7 +29,7 @@ public sealed class CssBorderImageSliceValue : ICssCompositeValue, IEquatable<Cs
2929
/// <param name="bottom">The bottom length.</param>
3030
/// <param name="left">The left length.</param>
3131
/// <param name="filled">True if the filled flag is enabled, otherwise false.</param>
32-
public CssBorderImageSliceValue(CssLengthValue top, CssLengthValue right, CssLengthValue bottom, CssLengthValue left, Boolean filled)
32+
public CssBorderImageSliceValue(ICssValue top, ICssValue right, ICssValue bottom, ICssValue left, Boolean filled)
3333
{
3434
_top = top;
3535
_right = right;
@@ -45,22 +45,22 @@ public CssBorderImageSliceValue(CssLengthValue top, CssLengthValue right, CssLen
4545
/// <summary>
4646
/// Gets the bottom coordinate.
4747
/// </summary>
48-
public CssLengthValue Bottom => _bottom;
48+
public ICssValue Bottom => _bottom;
4949

5050
/// <summary>
5151
/// Gets the left coordinate.
5252
/// </summary>
53-
public CssLengthValue Left => _left;
53+
public ICssValue Left => _left;
5454

5555
/// <summary>
5656
/// Gets the top coordinate.
5757
/// </summary>
58-
public CssLengthValue Top => _top;
58+
public ICssValue Top => _top;
5959

6060
/// <summary>
6161
/// Gets the right coordinate.
6262
/// </summary>
63-
public CssLengthValue Right => _right;
63+
public ICssValue Right => _right;
6464

6565
/// <summary>
6666
/// Gets if the slice should be filled.

0 commit comments

Comments
 (0)