From 773848315b31dbc4dc2878d9a3783af246cae6f0 Mon Sep 17 00:00:00 2001 From: Gertjan van Heertum Date: Thu, 20 Aug 2015 15:26:39 +0200 Subject: [PATCH] Extended the Marker start/mid/end getters with a function to return null if the property was set to none. This will prevent nullpointer issues when having none as one of the items. --- Source/Basic Shapes/SvgLine.cs | 7 ++++--- Source/Basic Shapes/SvgPolygon.cs | 7 ++++--- Source/Basic Shapes/SvgPolyline.cs | 7 ++++--- Source/ExtensionMethods/UriExtensions.cs | 16 ++++++++++++++++ Source/Paths/SvgPath.cs | 7 ++++--- Source/Svg.csproj | 1 + 6 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 Source/ExtensionMethods/UriExtensions.cs diff --git a/Source/Basic Shapes/SvgLine.cs b/Source/Basic Shapes/SvgLine.cs index 348c8a0..1ece3ee 100644 --- a/Source/Basic Shapes/SvgLine.cs +++ b/Source/Basic Shapes/SvgLine.cs @@ -4,6 +4,7 @@ using System.Text; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; +using Svg.ExtensionMethods; namespace Svg { @@ -85,7 +86,7 @@ namespace Svg [SvgAttribute("marker-end")] public Uri MarkerEnd { - get { return this.Attributes.GetAttribute("marker-end"); } + get { return this.Attributes.GetAttribute("marker-end").ReplaceWithNullIfNone(); } set { this.Attributes["marker-end"] = value; } } @@ -96,7 +97,7 @@ namespace Svg [SvgAttribute("marker-mid")] public Uri MarkerMid { - get { return this.Attributes.GetAttribute("marker-mid"); } + get { return this.Attributes.GetAttribute("marker-mid").ReplaceWithNullIfNone(); } set { this.Attributes["marker-mid"] = value; } } @@ -107,7 +108,7 @@ namespace Svg [SvgAttribute("marker-start")] public Uri MarkerStart { - get { return this.Attributes.GetAttribute("marker-start"); } + get { return this.Attributes.GetAttribute("marker-start").ReplaceWithNullIfNone(); } set { this.Attributes["marker-start"] = value; } } diff --git a/Source/Basic Shapes/SvgPolygon.cs b/Source/Basic Shapes/SvgPolygon.cs index a59a328..7eb427b 100644 --- a/Source/Basic Shapes/SvgPolygon.cs +++ b/Source/Basic Shapes/SvgPolygon.cs @@ -4,6 +4,7 @@ using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Diagnostics; +using Svg.ExtensionMethods; using Svg.Pathing; namespace Svg @@ -32,7 +33,7 @@ namespace Svg [SvgAttribute("marker-end")] public Uri MarkerEnd { - get { return this.Attributes.GetAttribute("marker-end"); } + get { return this.Attributes.GetAttribute("marker-end").ReplaceWithNullIfNone(); } set { this.Attributes["marker-end"] = value; } } @@ -43,7 +44,7 @@ namespace Svg [SvgAttribute("marker-mid")] public Uri MarkerMid { - get { return this.Attributes.GetAttribute("marker-mid"); } + get { return this.Attributes.GetAttribute("marker-mid").ReplaceWithNullIfNone(); } set { this.Attributes["marker-mid"] = value; } } @@ -54,7 +55,7 @@ namespace Svg [SvgAttribute("marker-start")] public Uri MarkerStart { - get { return this.Attributes.GetAttribute("marker-start"); } + get { return this.Attributes.GetAttribute("marker-start").ReplaceWithNullIfNone(); } set { this.Attributes["marker-start"] = value; } } diff --git a/Source/Basic Shapes/SvgPolyline.cs b/Source/Basic Shapes/SvgPolyline.cs index 0aa6924..650b8fa 100644 --- a/Source/Basic Shapes/SvgPolyline.cs +++ b/Source/Basic Shapes/SvgPolyline.cs @@ -4,6 +4,7 @@ using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Diagnostics; +using Svg.ExtensionMethods; namespace Svg { @@ -19,7 +20,7 @@ namespace Svg [SvgAttribute("marker-end")] public Uri MarkerEnd { - get { return this.Attributes.GetAttribute("marker-end"); } + get { return this.Attributes.GetAttribute("marker-end").ReplaceWithNullIfNone(); } set { this.Attributes["marker-end"] = value; } } @@ -30,7 +31,7 @@ namespace Svg [SvgAttribute("marker-mid")] public Uri MarkerMid { - get { return this.Attributes.GetAttribute("marker-mid"); } + get { return this.Attributes.GetAttribute("marker-mid").ReplaceWithNullIfNone(); } set { this.Attributes["marker-mid"] = value; } } @@ -41,7 +42,7 @@ namespace Svg [SvgAttribute("marker-start")] public Uri MarkerStart { - get { return this.Attributes.GetAttribute("marker-start"); } + get { return this.Attributes.GetAttribute("marker-start").ReplaceWithNullIfNone(); } set { this.Attributes["marker-start"] = value; } } diff --git a/Source/ExtensionMethods/UriExtensions.cs b/Source/ExtensionMethods/UriExtensions.cs new file mode 100644 index 0000000..aa300f9 --- /dev/null +++ b/Source/ExtensionMethods/UriExtensions.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Svg.ExtensionMethods +{ + public static class UriExtensions + { + public static Uri ReplaceWithNullIfNone(this Uri uri) + { + if(uri == null) { return null; } + return string.Equals(uri.ToString(), "none", StringComparison.OrdinalIgnoreCase) ? null : uri; + } + } +} diff --git a/Source/Paths/SvgPath.cs b/Source/Paths/SvgPath.cs index 9d5c2ea..ce9e322 100644 --- a/Source/Paths/SvgPath.cs +++ b/Source/Paths/SvgPath.cs @@ -6,6 +6,7 @@ using System.Drawing.Drawing2D; using System.Xml.Serialization; using System.Xml; using System.Diagnostics; +using Svg.ExtensionMethods; using Svg.Pathing; using Svg.Transforms; @@ -51,7 +52,7 @@ namespace Svg [SvgAttribute("marker-end", true)] public Uri MarkerEnd { - get { return this.Attributes.GetAttribute("marker-end"); } + get { return this.Attributes.GetAttribute("marker-end").ReplaceWithNullIfNone(); } set { this.Attributes["marker-end"] = value; } } @@ -62,7 +63,7 @@ namespace Svg [SvgAttribute("marker-mid", true)] public Uri MarkerMid { - get { return this.Attributes.GetAttribute("marker-mid"); } + get { return this.Attributes.GetAttribute("marker-mid").ReplaceWithNullIfNone(); } set { this.Attributes["marker-mid"] = value; } } @@ -73,7 +74,7 @@ namespace Svg [SvgAttribute("marker-start", true)] public Uri MarkerStart { - get { return this.Attributes.GetAttribute("marker-start"); } + get { return this.Attributes.GetAttribute("marker-start").ReplaceWithNullIfNone(); } set { this.Attributes["marker-start"] = value; } } diff --git a/Source/Svg.csproj b/Source/Svg.csproj index ea132a1..36f894f 100644 --- a/Source/Svg.csproj +++ b/Source/Svg.csproj @@ -107,6 +107,7 @@ + -- GitLab