Skip to content

Commit b764ad1

Browse files
author
Arthur
committed
Merge pull request #19 from mikkelxn/upstream
ColorConverter exception prevention
2 parents 97a0d5c + 91ebb92 commit b764ad1

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

Source/Demo/Common/Resources/Tooltip.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<b>HtmlPnael control showing <u>HTML Renderer</u> capabilities</b>
1+
<b>HtmlPanel control showing <u>HTML Renderer</u> capabilities</b>
22
<table border="0" style="margin: 10px 20px;">
33
<tr>
44
<td width="32" style="padding: 2px 5px 0 0">

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)