From 9809faccb679d4f2867f3725cc90759d97ff9455 Mon Sep 17 00:00:00 2001 From: MarcinZiabek Date: Sun, 16 Apr 2023 23:49:47 +0200 Subject: [PATCH] Improvement (#528): added validation for color arguments --- Source/QuestPDF/Fluent/BorderExtensions.cs | 2 + Source/QuestPDF/Fluent/DebugExtensions.cs | 2 + Source/QuestPDF/Fluent/ElementExtensions.cs | 3 ++ Source/QuestPDF/Fluent/LineExtensions.cs | 2 + Source/QuestPDF/Fluent/PageExtensions.cs | 1 + .../Fluent/TextSpanDescriptorExtensions.cs | 3 ++ Source/QuestPDF/Fluent/TextStyleExtensions.cs | 3 ++ Source/QuestPDF/Helpers/ColorValidator.cs | 41 +++++++++++++++++++ 8 files changed, 57 insertions(+) create mode 100644 Source/QuestPDF/Helpers/ColorValidator.cs diff --git a/Source/QuestPDF/Fluent/BorderExtensions.cs b/Source/QuestPDF/Fluent/BorderExtensions.cs index 32e94f1..4676a42 100644 --- a/Source/QuestPDF/Fluent/BorderExtensions.cs +++ b/Source/QuestPDF/Fluent/BorderExtensions.cs @@ -1,5 +1,6 @@ using System; using QuestPDF.Elements; +using QuestPDF.Helpers; using QuestPDF.Infrastructure; namespace QuestPDF.Fluent @@ -57,6 +58,7 @@ namespace QuestPDF.Fluent public static IContainer BorderColor(this IContainer element, string color) { + ColorValidator.Validate(color); return element.Border(x => x.Color = color); } } diff --git a/Source/QuestPDF/Fluent/DebugExtensions.cs b/Source/QuestPDF/Fluent/DebugExtensions.cs index 5473a35..b5ce3e3 100644 --- a/Source/QuestPDF/Fluent/DebugExtensions.cs +++ b/Source/QuestPDF/Fluent/DebugExtensions.cs @@ -8,6 +8,8 @@ namespace QuestPDF.Fluent { public static IContainer DebugArea(this IContainer parent, string text, string color) { + ColorValidator.Validate(color); + var container = new Container(); parent.Component(new DebugArea diff --git a/Source/QuestPDF/Fluent/ElementExtensions.cs b/Source/QuestPDF/Fluent/ElementExtensions.cs index 804dc84..b890791 100644 --- a/Source/QuestPDF/Fluent/ElementExtensions.cs +++ b/Source/QuestPDF/Fluent/ElementExtensions.cs @@ -1,6 +1,7 @@ using System; using QuestPDF.Drawing.Exceptions; using QuestPDF.Elements; +using QuestPDF.Helpers; using QuestPDF.Infrastructure; namespace QuestPDF.Fluent @@ -52,6 +53,8 @@ namespace QuestPDF.Fluent public static IContainer Background(this IContainer element, string color) { + ColorValidator.Validate(color); + return element.Element(new Background { Color = color diff --git a/Source/QuestPDF/Fluent/LineExtensions.cs b/Source/QuestPDF/Fluent/LineExtensions.cs index 2dacdcf..9e94ec8 100644 --- a/Source/QuestPDF/Fluent/LineExtensions.cs +++ b/Source/QuestPDF/Fluent/LineExtensions.cs @@ -1,5 +1,6 @@ using System; using QuestPDF.Elements; +using QuestPDF.Helpers; using QuestPDF.Infrastructure; namespace QuestPDF.Fluent @@ -30,6 +31,7 @@ namespace QuestPDF.Fluent public static void LineColor(this ILine descriptor, string value) { + ColorValidator.Validate(value); (descriptor as Line).Color = value; } } diff --git a/Source/QuestPDF/Fluent/PageExtensions.cs b/Source/QuestPDF/Fluent/PageExtensions.cs index 42df2b8..9213f5c 100644 --- a/Source/QuestPDF/Fluent/PageExtensions.cs +++ b/Source/QuestPDF/Fluent/PageExtensions.cs @@ -110,6 +110,7 @@ namespace QuestPDF.Fluent public void PageColor(string color) { + ColorValidator.Validate(color); Page.BackgroundColor = color; } diff --git a/Source/QuestPDF/Fluent/TextSpanDescriptorExtensions.cs b/Source/QuestPDF/Fluent/TextSpanDescriptorExtensions.cs index 74222b5..203479f 100644 --- a/Source/QuestPDF/Fluent/TextSpanDescriptorExtensions.cs +++ b/Source/QuestPDF/Fluent/TextSpanDescriptorExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.CompilerServices; +using QuestPDF.Helpers; using QuestPDF.Infrastructure; namespace QuestPDF.Fluent @@ -28,12 +29,14 @@ namespace QuestPDF.Fluent public static T FontColor(this T descriptor, string value) where T : TextSpanDescriptor { + ColorValidator.Validate(value); descriptor.MutateTextStyle(x => x.FontColor(value)); return descriptor; } public static T BackgroundColor(this T descriptor, string value) where T : TextSpanDescriptor { + ColorValidator.Validate(value); descriptor.MutateTextStyle(x => x.BackgroundColor(value)); return descriptor; } diff --git a/Source/QuestPDF/Fluent/TextStyleExtensions.cs b/Source/QuestPDF/Fluent/TextStyleExtensions.cs index b6f3906..d63ef7c 100644 --- a/Source/QuestPDF/Fluent/TextStyleExtensions.cs +++ b/Source/QuestPDF/Fluent/TextStyleExtensions.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using QuestPDF.Helpers; using QuestPDF.Infrastructure; namespace QuestPDF.Fluent @@ -14,11 +15,13 @@ namespace QuestPDF.Fluent public static TextStyle FontColor(this TextStyle style, string value) { + ColorValidator.Validate(value); return style.Mutate(TextStyleProperty.Color, value); } public static TextStyle BackgroundColor(this TextStyle style, string value) { + ColorValidator.Validate(value); return style.Mutate(TextStyleProperty.BackgroundColor, value); } diff --git a/Source/QuestPDF/Helpers/ColorValidator.cs b/Source/QuestPDF/Helpers/ColorValidator.cs new file mode 100644 index 0000000..a375940 --- /dev/null +++ b/Source/QuestPDF/Helpers/ColorValidator.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Concurrent; +using SkiaSharp; + +namespace QuestPDF.Helpers +{ + internal static class ColorValidator + { + private static readonly ConcurrentDictionary Colors = new(); + + public static void Validate(string? color) + { + if (color == null) + throw new ArgumentException("Color value cannot be null"); + + var isValid = Colors.GetOrAdd(color, IsColorValid); + + if (isValid) + return; + + throw new ArgumentException( + $"The provided value '{color}' is not a valid hex color. " + + "The following formats are supported: #RGB, #ARGB, #RRGGBB, #AARRGGBB. " + + "The hash sign is optional so the following formats are also valid: RGB, ARGB, RRGGBB, AARRGGBB. " + + "For example #FF8800 is a solid orange color, while #20CF is a barely visible aqua color."); + + static bool IsColorValid(string color) + { + try + { + SKColor.Parse(color); + return true; + } + catch + { + return false; + } + } + } + } +} \ No newline at end of file