Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4c0f70

Browse files
committed
Make GetColorInt check if color name is valid to avoid ColorConverter throwing an exception
1 parent 9a92a74 commit f4c0f70

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Source/HtmlRenderer.WPF/Adapters/WpfAdapter.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
// "The Art of War"
1212

1313
using System;
14+
using System.Collections.Generic;
1415
using System.IO;
16+
using System.Reflection;
1517
using System.Windows;
1618
using System.Windows.Media;
1719
using System.Windows.Media.Imaging;
@@ -34,8 +36,22 @@ internal sealed class WpfAdapter : RAdapter
3436
/// </summary>
3537
private static readonly WpfAdapter _instance = new WpfAdapter();
3638

39+
/// <summary>
40+
/// List of valid predefined color names in lower-case
41+
/// </summary>
42+
private static readonly List<string> ValidColorNamesLc;
43+
3744
#endregion
3845

46+
static WpfAdapter()
47+
{
48+
ValidColorNamesLc = new List<string>();
49+
var colorList = new List<PropertyInfo>(typeof(Colors).GetProperties());
50+
foreach (var colorProp in colorList)
51+
{
52+
ValidColorNamesLc.Add(colorProp.Name.ToLower());
53+
}
54+
}
3955

4056
/// <summary>
4157
/// Init installed font families and set default font families mapping.
@@ -61,6 +77,10 @@ public static WpfAdapter Instance
6177

6278
protected override RColor GetColorInt(string colorName)
6379
{
80+
// check if color name is valid to avoid ColorConverter throwing an exception
81+
if (!ValidColorNamesLc.Contains(colorName.ToLower()))
82+
return RColor.Empty;
83+
6484
var convertFromString = ColorConverter.ConvertFromString(colorName) ?? Colors.Black;
6585
return Utils.Convert((Color)convertFromString);
6686
}

0 commit comments

Comments
 (0)