Skip to content

Commit 0d8e2fe

Browse files
committed
Use TryParse to mitigate #33
1 parent d39ec7c commit 0d8e2fe

File tree

4 files changed

+67
-112
lines changed

4 files changed

+67
-112
lines changed

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

+20-40
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ static class ColorParser
4545
{
4646
if (source.Current == Symbols.RoundBracketOpen)
4747
{
48-
var function = default(Func<StringSource, Color?>);
49-
50-
if (ColorFunctions.TryGetValue(ident, out function))
48+
if (ColorFunctions.TryGetValue(ident, out var function))
5149
{
5250
source.SkipCurrentAndSpaces();
5351
return function.Invoke(source);
@@ -68,7 +66,6 @@ static class ColorParser
6866
private static Color? Literal(StringSource source)
6967
{
7068
var current = source.Next();
71-
var result = default(Color);
7269
var buffer = StringBuilderPool.Obtain();
7370

7471
while (current.IsHex())
@@ -77,7 +74,7 @@ static class ColorParser
7774
current = source.Next();
7875
}
7976

80-
if (Color.TryFromHex(buffer.ToPool(), out result))
77+
if (Color.TryFromHex(buffer.ToPool(), out var result))
8178
{
8279
return result;
8380
}
@@ -120,9 +117,9 @@ static class ColorParser
120117
{
121118
var h = ParseAngle(source);
122119
var c1 = source.SkipGetSkip();
123-
var s = ParsePercent(source);
120+
var s = source.ParsePercent();
124121
var c2 = source.SkipGetSkip();
125-
var l = ParsePercent(source);
122+
var l = source.ParsePercent();
126123
var c3 = source.SkipGetSkip();
127124

128125
if (h != null && s != null && l != null)
@@ -172,9 +169,9 @@ static class ColorParser
172169
{
173170
var h = ParseAngle(source);
174171
var c1 = source.SkipGetSkip();
175-
var s = ParsePercent(source);
172+
var s = source.ParsePercent();
176173
var c2 = source.SkipGetSkip();
177-
var l = ParsePercent(source);
174+
var l = source.ParsePercent();
178175
var c3 = source.SkipGetSkip();
179176

180177
if (h != null && s != null && l != null)
@@ -199,26 +196,13 @@ static class ColorParser
199196
return null;
200197
}
201198

202-
private static Double? ParsePercent(StringSource source)
203-
{
204-
var unit = source.ParseUnit();
205-
206-
if (unit != null && unit.Dimension == "%")
207-
{
208-
return Double.Parse(unit.Value, CultureInfo.InvariantCulture) * 0.01f;
209-
}
210-
211-
return null;
212-
}
213-
214199
private static Byte? ParseRgbComponent(StringSource source)
215200
{
216201
var unit = source.ParseUnit();
217202

218-
if (unit != null)
203+
if (unit != null &&
204+
Int32.TryParse(unit.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value))
219205
{
220-
var value = Int32.Parse(unit.Value, CultureInfo.InvariantCulture);
221-
222206
if (unit.Dimension == "%")
223207
{
224208
return (Byte)(255f / 100f * value);
@@ -236,17 +220,16 @@ static class ColorParser
236220
{
237221
var unit = source.ParseUnit();
238222

239-
if (unit != null)
223+
if (unit != null &&
224+
Double.TryParse(unit.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
240225
{
241-
var value = Double.Parse(unit.Value, CultureInfo.InvariantCulture);
242-
243226
if (unit.Dimension == "%")
244227
{
245-
return value * 0.01f;
228+
return value * 0.01;
246229
}
247230
else if (unit.Dimension == String.Empty)
248231
{
249-
return Math.Max(Math.Min(value, 1f), 0f);
232+
return Math.Max(Math.Min(value, 1.0), 0.0);
250233
}
251234
}
252235

@@ -257,13 +240,12 @@ static class ColorParser
257240
{
258241
var unit = source.ParseUnit();
259242

260-
if (unit != null)
243+
if (unit != null &&
244+
Double.TryParse(unit.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
261245
{
262-
var value = Double.Parse(unit.Value, CultureInfo.InvariantCulture);
263246
var dim = Angle.Unit.Deg;
264247

265-
if (unit.Dimension == String.Empty ||
266-
(dim = Angle.GetUnit(unit.Dimension)) != Angle.Unit.None)
248+
if (unit.Dimension == String.Empty || (dim = Angle.GetUnit(unit.Dimension)) != Angle.Unit.None)
267249
{
268250
var angle = new Angle(value, dim);
269251
return angle.ToTurns();
@@ -273,12 +255,10 @@ static class ColorParser
273255
return null;
274256
}
275257

276-
private static Boolean Check(Char closingBracket, Char firstComma = Symbols.Comma, Char secondComma = Symbols.Comma, Char thirdComma = Symbols.Comma)
277-
{
278-
return closingBracket == Symbols.RoundBracketClose &&
279-
firstComma == Symbols.Comma &&
280-
secondComma == Symbols.Comma &&
281-
thirdComma == Symbols.Comma;
282-
}
258+
private static Boolean Check(Char closingBracket, Char firstComma = Symbols.Comma, Char secondComma = Symbols.Comma, Char thirdComma = Symbols.Comma) =>
259+
closingBracket == Symbols.RoundBracketClose &&
260+
firstComma == Symbols.Comma &&
261+
secondComma == Symbols.Comma &&
262+
thirdComma == Symbols.Comma;
283263
}
284264
}

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ static class NumberParser
1010
public static Double? ParseNumber(this StringSource source)
1111
{
1212
var unit = source.ParseUnit();
13-
var result = default(Double);
1413

15-
if (unit != null && unit.Dimension == String.Empty &&
16-
Double.TryParse(unit.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out result))
14+
if (unit != null &&
15+
unit.Dimension == String.Empty &&
16+
Double.TryParse(unit.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var result))
1717
{
1818
return result;
1919
}
@@ -124,13 +124,12 @@ static class NumberParser
124124
public static Int32? ParseInteger(this StringSource source)
125125
{
126126
var unit = source.ParseUnit();
127-
var result = default(Int32);
128127

129128
if (unit != null && unit.Dimension == String.Empty)
130129
{
131130
var value = unit.Value;
132131

133-
if (Int32.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
132+
if (Int32.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result))
134133
{
135134
return result;
136135
}

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

+8-16
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ namespace AngleSharp.Css.Parser
77

88
static class PointParser
99
{
10-
public static ICssValue ParsePointX(this StringSource source)
11-
{
12-
return source.ParsePointDir(IsHorizontal);
13-
}
10+
public static ICssValue ParsePointX(this StringSource source) =>
11+
source.ParsePointDir(IsHorizontal);
1412

15-
public static ICssValue ParsePointY(this StringSource source)
16-
{
17-
return source.ParsePointDir(IsVertical);
18-
}
13+
public static ICssValue ParsePointY(this StringSource source) =>
14+
source.ParsePointDir(IsVertical);
1915

2016
private static ICssValue ParsePointDir(this StringSource source, Predicate<String> checkKeyword)
2117
{
@@ -167,15 +163,11 @@ public static CssBackgroundSizeValue ParseSize(this StringSource source)
167163
}
168164
}
169165

170-
private static Boolean IsHorizontal(String str)
171-
{
172-
return str.Isi(CssKeywords.Left) || str.Isi(CssKeywords.Right) || str.Isi(CssKeywords.Center);
173-
}
166+
private static Boolean IsHorizontal(String str) =>
167+
str.Isi(CssKeywords.Left) || str.Isi(CssKeywords.Right) || str.Isi(CssKeywords.Center);
174168

175-
private static Boolean IsVertical(String str)
176-
{
177-
return str.Isi(CssKeywords.Top) || str.Isi(CssKeywords.Bottom) || str.Isi(CssKeywords.Center);
178-
}
169+
private static Boolean IsVertical(String str) =>
170+
str.Isi(CssKeywords.Top) || str.Isi(CssKeywords.Bottom) || str.Isi(CssKeywords.Center);
179171

180172
private static Length? KeywordToLength(String keyword)
181173
{

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

+35-51
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,28 @@ public static ICssValue ParseAutoLength(this StringSource source)
4242
return null;
4343
}
4444

45-
public static ICssValue ParseBorderWidth(this StringSource source)
46-
{
47-
return source.ParseLengthOrCalc() ??
48-
source.ParsePercentOrNumber() ??
49-
source.ParseAutoLength();
50-
}
45+
public static ICssValue ParseBorderWidth(this StringSource source) =>
46+
source.ParseLengthOrCalc() ??
47+
source.ParsePercentOrNumber() ??
48+
source.ParseAutoLength();
5149

52-
public static ICssValue ParseLineWidth(this StringSource source)
53-
{
54-
return source.ParseLengthOrCalc() ??
55-
source.ParseConstant(Map.BorderWidths);
56-
}
50+
public static ICssValue ParseLineWidth(this StringSource source) =>
51+
source.ParseLengthOrCalc() ??
52+
source.ParseConstant(Map.BorderWidths);
5753

58-
public static ICssValue ParseLineHeight(this StringSource source)
59-
{
60-
return source.ParseLengthOrCalc() ??
61-
source.ParsePercentOrNumber() ??
62-
source.ParseNormalLength();
63-
}
54+
public static ICssValue ParseLineHeight(this StringSource source) =>
55+
source.ParseLengthOrCalc() ??
56+
source.ParsePercentOrNumber() ??
57+
source.ParseNormalLength();
6458

6559
public static Double? ParsePercent(this StringSource source)
6660
{
6761
var pos = source.Index;
6862
var test = source.ParseUnit();
6963

70-
if (test != null && test.Dimension == "%")
64+
if (test?.Dimension == "%" &&
65+
Double.TryParse(test.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
7166
{
72-
var value = Double.Parse(test.Value, CultureInfo.InvariantCulture);
7367
return value * 0.01;
7468
}
7569

@@ -82,10 +76,8 @@ public static ICssValue ParseLineHeight(this StringSource source)
8276
var pos = source.Index;
8377
var test = source.ParseUnit();
8478

85-
if (test != null)
79+
if (test != null && Double.TryParse(test.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
8680
{
87-
var value = Double.Parse(test.Value, CultureInfo.InvariantCulture);
88-
8981
if (test.Dimension == "%")
9082
{
9183
return new Length(value, Length.Unit.Percent);
@@ -109,9 +101,9 @@ public static ICssValue ParseLineHeight(this StringSource source)
109101
{
110102
var unit = Angle.GetUnit(test.Dimension);
111103

112-
if (unit != Angle.Unit.None)
104+
if (unit != Angle.Unit.None &&
105+
Double.TryParse(test.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
113106
{
114-
var value = Double.Parse(test.Value, CultureInfo.InvariantCulture);
115107
return new Angle(value, unit);
116108
}
117109

@@ -121,10 +113,8 @@ public static ICssValue ParseLineHeight(this StringSource source)
121113
return null;
122114
}
123115

124-
public static ICssValue ParseAngleOrCalc(this StringSource source)
125-
{
126-
return source.ParseAngle().OrCalc(source);
127-
}
116+
public static ICssValue ParseAngleOrCalc(this StringSource source) =>
117+
source.ParseAngle().OrCalc(source);
128118

129119
public static Frequency? ParseFrequency(this StringSource source)
130120
{
@@ -135,9 +125,9 @@ public static ICssValue ParseAngleOrCalc(this StringSource source)
135125
{
136126
var unit = Frequency.GetUnit(test.Dimension);
137127

138-
if (unit != Frequency.Unit.None)
128+
if (unit != Frequency.Unit.None &&
129+
Double.TryParse(test.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
139130
{
140-
var value = Double.Parse(test.Value, CultureInfo.InvariantCulture);
141131
return new Frequency(value, unit);
142132
}
143133

@@ -147,11 +137,9 @@ public static ICssValue ParseAngleOrCalc(this StringSource source)
147137
return null;
148138
}
149139

150-
public static ICssValue ParseFontSize(this StringSource source)
151-
{
152-
return source.ParseDistanceOrCalc() ??
153-
source.ParseConstant(Map.FontSizes);
154-
}
140+
public static ICssValue ParseFontSize(this StringSource source) =>
141+
source.ParseDistanceOrCalc() ??
142+
source.ParseConstant(Map.FontSizes);
155143

156144
public static ICssValue ParseTrackBreadth(this StringSource source, Boolean flexible = true)
157145
{
@@ -167,9 +155,9 @@ public static ICssValue ParseTrackBreadth(this StringSource source, Boolean flex
167155
{
168156
var unit = Fraction.GetUnit(test.Dimension);
169157

170-
if (flexible && unit != Fraction.Unit.None)
158+
if (flexible && unit != Fraction.Unit.None &&
159+
Double.TryParse(test.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
171160
{
172-
var value = Double.Parse(test.Value, CultureInfo.InvariantCulture);
173161
return new Fraction(value, unit);
174162
}
175163
}
@@ -212,10 +200,8 @@ public static ICssValue ParseTrackBreadth(this StringSource source, Boolean flex
212200
return length;
213201
}
214202

215-
public static ICssValue ParseDistanceOrCalc(this StringSource source)
216-
{
217-
return source.ParseDistance().OrCalc(source);
218-
}
203+
public static ICssValue ParseDistanceOrCalc(this StringSource source) =>
204+
source.ParseDistance().OrCalc(source);
219205

220206
public static Length? ParseLength(this StringSource source)
221207
{
@@ -232,10 +218,8 @@ public static ICssValue ParseDistanceOrCalc(this StringSource source)
232218
return length;
233219
}
234220

235-
public static ICssValue ParseLengthOrCalc(this StringSource source)
236-
{
237-
return source.ParseLength().OrCalc(source);
238-
}
221+
public static ICssValue ParseLengthOrCalc(this StringSource source) =>
222+
source.ParseLength().OrCalc(source);
239223

240224
public static Resolution? ParseResolution(this StringSource source)
241225
{
@@ -246,9 +230,9 @@ public static ICssValue ParseLengthOrCalc(this StringSource source)
246230
{
247231
var unit = Resolution.GetUnit(test.Dimension);
248232

249-
if (unit != Resolution.Unit.None)
233+
if (unit != Resolution.Unit.None &&
234+
Double.TryParse(test.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
250235
{
251-
var value = Double.Parse(test.Value, CultureInfo.InvariantCulture);
252236
return new Resolution(value, unit);
253237
}
254238

@@ -267,9 +251,9 @@ public static ICssValue ParseLengthOrCalc(this StringSource source)
267251
{
268252
var unit = Time.GetUnit(test.Dimension);
269253

270-
if (unit != Time.Unit.None)
254+
if (unit != Time.Unit.None &&
255+
Double.TryParse(test.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
271256
{
272-
var value = Double.Parse(test.Value, CultureInfo.InvariantCulture);
273257
return new Time(value, unit);
274258
}
275259

@@ -286,10 +270,10 @@ public static ICssValue ParseTimeOrCalc(this StringSource source)
286270

287271
private static Length? GetLength(Unit test)
288272
{
289-
if (test != null)
273+
if (test != null &&
274+
Double.TryParse(test.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
290275
{
291276
var unit = Length.Unit.Px;
292-
var value = Double.Parse(test.Value, CultureInfo.InvariantCulture);
293277

294278
if ((test.Dimension == String.Empty && test.Value == "0") ||
295279
(unit = Length.GetUnit(test.Dimension)) != Length.Unit.None)

0 commit comments

Comments
 (0)