@@ -42,34 +42,28 @@ public static ICssValue ParseAutoLength(this StringSource source)
42
42
return null ;
43
43
}
44
44
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 ( ) ;
51
49
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 ) ;
57
53
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 ( ) ;
64
58
65
59
public static Double ? ParsePercent ( this StringSource source )
66
60
{
67
61
var pos = source . Index ;
68
62
var test = source . ParseUnit ( ) ;
69
63
70
- if ( test != null && test . Dimension == "%" )
64
+ if ( test ? . Dimension == "%" &&
65
+ Double . TryParse ( test . Value , NumberStyles . Float , CultureInfo . InvariantCulture , out var value ) )
71
66
{
72
- var value = Double . Parse ( test . Value , CultureInfo . InvariantCulture ) ;
73
67
return value * 0.01 ;
74
68
}
75
69
@@ -82,10 +76,8 @@ public static ICssValue ParseLineHeight(this StringSource source)
82
76
var pos = source . Index ;
83
77
var test = source . ParseUnit ( ) ;
84
78
85
- if ( test != null )
79
+ if ( test != null && Double . TryParse ( test . Value , NumberStyles . Float , CultureInfo . InvariantCulture , out var value ) )
86
80
{
87
- var value = Double . Parse ( test . Value , CultureInfo . InvariantCulture ) ;
88
-
89
81
if ( test . Dimension == "%" )
90
82
{
91
83
return new Length ( value , Length . Unit . Percent ) ;
@@ -109,9 +101,9 @@ public static ICssValue ParseLineHeight(this StringSource source)
109
101
{
110
102
var unit = Angle . GetUnit ( test . Dimension ) ;
111
103
112
- if ( unit != Angle . Unit . None )
104
+ if ( unit != Angle . Unit . None &&
105
+ Double . TryParse ( test . Value , NumberStyles . Float , CultureInfo . InvariantCulture , out var value ) )
113
106
{
114
- var value = Double . Parse ( test . Value , CultureInfo . InvariantCulture ) ;
115
107
return new Angle ( value , unit ) ;
116
108
}
117
109
@@ -121,10 +113,8 @@ public static ICssValue ParseLineHeight(this StringSource source)
121
113
return null ;
122
114
}
123
115
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 ) ;
128
118
129
119
public static Frequency ? ParseFrequency ( this StringSource source )
130
120
{
@@ -135,9 +125,9 @@ public static ICssValue ParseAngleOrCalc(this StringSource source)
135
125
{
136
126
var unit = Frequency . GetUnit ( test . Dimension ) ;
137
127
138
- if ( unit != Frequency . Unit . None )
128
+ if ( unit != Frequency . Unit . None &&
129
+ Double . TryParse ( test . Value , NumberStyles . Float , CultureInfo . InvariantCulture , out var value ) )
139
130
{
140
- var value = Double . Parse ( test . Value , CultureInfo . InvariantCulture ) ;
141
131
return new Frequency ( value , unit ) ;
142
132
}
143
133
@@ -147,11 +137,9 @@ public static ICssValue ParseAngleOrCalc(this StringSource source)
147
137
return null ;
148
138
}
149
139
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 ) ;
155
143
156
144
public static ICssValue ParseTrackBreadth ( this StringSource source , Boolean flexible = true )
157
145
{
@@ -167,9 +155,9 @@ public static ICssValue ParseTrackBreadth(this StringSource source, Boolean flex
167
155
{
168
156
var unit = Fraction . GetUnit ( test . Dimension ) ;
169
157
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 ) )
171
160
{
172
- var value = Double . Parse ( test . Value , CultureInfo . InvariantCulture ) ;
173
161
return new Fraction ( value , unit ) ;
174
162
}
175
163
}
@@ -212,10 +200,8 @@ public static ICssValue ParseTrackBreadth(this StringSource source, Boolean flex
212
200
return length ;
213
201
}
214
202
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 ) ;
219
205
220
206
public static Length ? ParseLength ( this StringSource source )
221
207
{
@@ -232,10 +218,8 @@ public static ICssValue ParseDistanceOrCalc(this StringSource source)
232
218
return length ;
233
219
}
234
220
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 ) ;
239
223
240
224
public static Resolution ? ParseResolution ( this StringSource source )
241
225
{
@@ -246,9 +230,9 @@ public static ICssValue ParseLengthOrCalc(this StringSource source)
246
230
{
247
231
var unit = Resolution . GetUnit ( test . Dimension ) ;
248
232
249
- if ( unit != Resolution . Unit . None )
233
+ if ( unit != Resolution . Unit . None &&
234
+ Double . TryParse ( test . Value , NumberStyles . Float , CultureInfo . InvariantCulture , out var value ) )
250
235
{
251
- var value = Double . Parse ( test . Value , CultureInfo . InvariantCulture ) ;
252
236
return new Resolution ( value , unit ) ;
253
237
}
254
238
@@ -267,9 +251,9 @@ public static ICssValue ParseLengthOrCalc(this StringSource source)
267
251
{
268
252
var unit = Time . GetUnit ( test . Dimension ) ;
269
253
270
- if ( unit != Time . Unit . None )
254
+ if ( unit != Time . Unit . None &&
255
+ Double . TryParse ( test . Value , NumberStyles . Float , CultureInfo . InvariantCulture , out var value ) )
271
256
{
272
- var value = Double . Parse ( test . Value , CultureInfo . InvariantCulture ) ;
273
257
return new Time ( value , unit ) ;
274
258
}
275
259
@@ -286,10 +270,10 @@ public static ICssValue ParseTimeOrCalc(this StringSource source)
286
270
287
271
private static Length ? GetLength ( Unit test )
288
272
{
289
- if ( test != null )
273
+ if ( test != null &&
274
+ Double . TryParse ( test . Value , NumberStyles . Float , CultureInfo . InvariantCulture , out var value ) )
290
275
{
291
276
var unit = Length . Unit . Px ;
292
- var value = Double . Parse ( test . Value , CultureInfo . InvariantCulture ) ;
293
277
294
278
if ( ( test . Dimension == String . Empty && test . Value == "0" ) ||
295
279
( unit = Length . GetUnit ( test . Dimension ) ) != Length . Unit . None )
0 commit comments