Browse Source

Add argument validation for Fluent API

Marcin Ziąbek 8 months ago
parent
commit
f7c7521149

+ 14 - 1
Source/QuestPDF/Fluent/BorderExtensions.cs

@@ -1,4 +1,5 @@
-using QuestPDF.Elements;
+using System;
+using QuestPDF.Elements;
 using QuestPDF.Helpers;
 using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
 using QuestPDF.Infrastructure;
 
 
@@ -10,6 +11,18 @@ namespace QuestPDF.Fluent
         {
         {
             var border = element as Border ?? new Border();
             var border = element as Border ?? new Border();
 
 
+            if (top < 0)
+                throw new ArgumentOutOfRangeException(nameof(top), "The top border cannot be negative.");
+            
+            if (bottom < 0)
+                throw new ArgumentOutOfRangeException(nameof(bottom), "The bottom border cannot be negative.");
+            
+            if (left < 0)
+                throw new ArgumentOutOfRangeException(nameof(left), "The left border cannot be negative.");
+            
+            if (right < 0)
+                throw new ArgumentOutOfRangeException(nameof(right), "The right border cannot be negative.");
+            
             border.Top += top;
             border.Top += top;
             border.Bottom += bottom;
             border.Bottom += bottom;
             border.Left += left;
             border.Left += left;

+ 3 - 0
Source/QuestPDF/Fluent/ColumnExtensions.cs

@@ -13,6 +13,9 @@ namespace QuestPDF.Fluent
         /// </summary>
         /// </summary>
         public void Spacing(float value, Unit unit = Unit.Point)
         public void Spacing(float value, Unit unit = Unit.Point)
         {
         {
+            if (value < 0)
+                throw new ArgumentOutOfRangeException(nameof(value), "The column spacing cannot be negative.");
+            
             Column.Spacing = value.ToPoints(unit);
             Column.Spacing = value.ToPoints(unit);
         }
         }
         
         

+ 21 - 2
Source/QuestPDF/Fluent/ConstrainedExtensions.cs

@@ -1,4 +1,5 @@
-using QuestPDF.Elements;
+using System;
+using QuestPDF.Elements;
 using QuestPDF.Infrastructure;
 using QuestPDF.Infrastructure;
 
 
 namespace QuestPDF.Fluent
 namespace QuestPDF.Fluent
@@ -11,6 +12,15 @@ namespace QuestPDF.Fluent
         {
         {
             var constrained = element as Constrained ?? new Constrained();
             var constrained = element as Constrained ?? new Constrained();
 
 
+            if (min < 0)
+                throw new ArgumentOutOfRangeException(nameof(min), "The minimum width cannot be negative.");
+            
+            if (max < 0)
+                throw new ArgumentOutOfRangeException(nameof(max), "The maximum width cannot be negative.");
+            
+            if (min > max)
+                throw new ArgumentOutOfRangeException(nameof(min), "The minimum width cannot be greater than the maximum width.");
+            
             if (min.HasValue)
             if (min.HasValue)
                 constrained.MinWidth = min;
                 constrained.MinWidth = min;
             
             
@@ -60,7 +70,16 @@ namespace QuestPDF.Fluent
         private static IContainer ConstrainedHeight(this IContainer element, float? min = null, float? max = null)
         private static IContainer ConstrainedHeight(this IContainer element, float? min = null, float? max = null)
         {
         {
             var constrained = element as Constrained ?? new Constrained();
             var constrained = element as Constrained ?? new Constrained();
-
+            
+            if (min < 0)
+                throw new ArgumentOutOfRangeException(nameof(min), "The minimum height cannot be negative.");
+            
+            if (max < 0)
+                throw new ArgumentOutOfRangeException(nameof(max), "The maximum height cannot be negative.");
+            
+            if (min > max)
+                throw new ArgumentOutOfRangeException(nameof(min), "The minimum height cannot be greater than the maximum height.");
+            
             if (min.HasValue) 
             if (min.HasValue) 
                 constrained.MinHeight = min;
                 constrained.MinHeight = min;
             
             

+ 6 - 0
Source/QuestPDF/Fluent/ElementExtensions.cs

@@ -119,6 +119,9 @@ namespace QuestPDF.Fluent
         /// <param name="option">Determines the approach the component should adopt when maintaining the specified aspect ratio.</param>
         /// <param name="option">Determines the approach the component should adopt when maintaining the specified aspect ratio.</param>
         public static IContainer AspectRatio(this IContainer element, float ratio, AspectRatioOption option = AspectRatioOption.FitWidth)
         public static IContainer AspectRatio(this IContainer element, float ratio, AspectRatioOption option = AspectRatioOption.FitWidth)
         {
         {
+            if (ratio <= 0)
+                throw new ArgumentOutOfRangeException(nameof(ratio), "The aspect ratio must be greater than zero.");
+            
             return element.Element(new AspectRatio
             return element.Element(new AspectRatio
             {
             {
                 Ratio = ratio,
                 Ratio = ratio,
@@ -234,6 +237,9 @@ namespace QuestPDF.Fluent
         /// <param name="minHeight">The minimum height, in points, that the element should occupy before a page break.</param>
         /// <param name="minHeight">The minimum height, in points, that the element should occupy before a page break.</param>
         public static IContainer EnsureSpace(this IContainer element, float minHeight = Elements.EnsureSpace.DefaultMinHeight)
         public static IContainer EnsureSpace(this IContainer element, float minHeight = Elements.EnsureSpace.DefaultMinHeight)
         {
         {
+            if (minHeight < 0)
+                throw new ArgumentOutOfRangeException(nameof(minHeight), "The EnsureSpace minimum height cannot be negative.");
+            
             return element.Element(new EnsureSpace
             return element.Element(new EnsureSpace
             {
             {
                 MinHeight = minHeight
                 MinHeight = minHeight

+ 12 - 0
Source/QuestPDF/Fluent/GridExtensions.cs

@@ -16,16 +16,25 @@ namespace QuestPDF.Fluent
         
         
         public void VerticalSpacing(float value, Unit unit = Unit.Point)
         public void VerticalSpacing(float value, Unit unit = Unit.Point)
         {
         {
+            if (value < 0)
+                throw new ArgumentOutOfRangeException(nameof(value), "The Grid vertical spacing cannot be negative.");
+            
             Grid.VerticalSpacing = value.ToPoints(unit);
             Grid.VerticalSpacing = value.ToPoints(unit);
         }
         }
          
          
         public void HorizontalSpacing(float value, Unit unit = Unit.Point)
         public void HorizontalSpacing(float value, Unit unit = Unit.Point)
         {
         {
+            if (value < 0)
+                throw new ArgumentOutOfRangeException(nameof(value), "The Grid horizontal spacing cannot be negative.");
+            
             Grid.HorizontalSpacing = value.ToPoints(unit);
             Grid.HorizontalSpacing = value.ToPoints(unit);
         }
         }
         
         
         public void Columns(int value = Grid.DefaultColumnsCount)
         public void Columns(int value = Grid.DefaultColumnsCount)
         {
         {
+            if (value < 1)
+                throw new ArgumentOutOfRangeException(nameof(value), "The Grid columns count cannot be less than 1.");
+            
             Grid.ColumnsCount = value;
             Grid.ColumnsCount = value;
         }
         }
         
         
@@ -40,6 +49,9 @@ namespace QuestPDF.Fluent
         
         
         public IContainer Item(int columns = 1)
         public IContainer Item(int columns = 1)
         {
         {
+            if (columns < 1)
+                throw new ArgumentOutOfRangeException(nameof(columns), "The Grid item cannot span less than 1 column.");
+            
             var container = new Container();
             var container = new Container();
             
             
             var element = new GridElement
             var element = new GridElement

+ 6 - 0
Source/QuestPDF/Fluent/ImageExtensions.cs

@@ -25,6 +25,9 @@ namespace QuestPDF.Fluent
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="image.rasterDPI"]/*' />
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="image.rasterDPI"]/*' />
         public DynamicImageDescriptor WithRasterDpi(int dpi)
         public DynamicImageDescriptor WithRasterDpi(int dpi)
         {
         {
+            if (dpi <= 0)
+                throw new DocumentComposeException("DPI value must be greater than 0.");
+            
             ImageElement.TargetDpi = dpi;
             ImageElement.TargetDpi = dpi;
             return this;
             return this;
         }
         }
@@ -62,6 +65,9 @@ namespace QuestPDF.Fluent
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="image.rasterDPI"]/*' />
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="image.rasterDPI"]/*' />
         public ImageDescriptor WithRasterDpi(int dpi)
         public ImageDescriptor WithRasterDpi(int dpi)
         {
         {
+            if (dpi <= 0)
+                throw new DocumentComposeException("DPI value must be greater than 0.");
+            
             ImageElement.TargetDpi = dpi;
             ImageElement.TargetDpi = dpi;
             return this;
             return this;
         }
         }

+ 6 - 0
Source/QuestPDF/Fluent/InlinedExtensions.cs

@@ -24,6 +24,9 @@ namespace QuestPDF.Fluent
         /// </summary>
         /// </summary>
         public void VerticalSpacing(float value, Unit unit = Unit.Point)
         public void VerticalSpacing(float value, Unit unit = Unit.Point)
         {
         {
+            if (value < 0)
+                throw new ArgumentOutOfRangeException(nameof(value), "The Inlined vertical spacing cannot be negative.");
+            
             Inlined.VerticalSpacing = value.ToPoints(unit);
             Inlined.VerticalSpacing = value.ToPoints(unit);
         }
         }
 
 
@@ -32,6 +35,9 @@ namespace QuestPDF.Fluent
         /// </summary>
         /// </summary>
         public void HorizontalSpacing(float value, Unit unit = Unit.Point)
         public void HorizontalSpacing(float value, Unit unit = Unit.Point)
         {
         {
+            if (value < 0)
+                throw new ArgumentOutOfRangeException(nameof(value), "The Inlined horizontal spacing cannot be negative.");
+            
             Inlined.HorizontalSpacing = value.ToPoints(unit);
             Inlined.HorizontalSpacing = value.ToPoints(unit);
         }
         }
         
         

+ 5 - 1
Source/QuestPDF/Fluent/LineExtensions.cs

@@ -1,4 +1,5 @@
-using QuestPDF.Elements;
+using System;
+using QuestPDF.Elements;
 using QuestPDF.Helpers;
 using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
 using QuestPDF.Infrastructure;
 
 
@@ -8,6 +9,9 @@ namespace QuestPDF.Fluent
     {
     {
         private static ILine Line(this IContainer element, LineType type, float thickness)
         private static ILine Line(this IContainer element, LineType type, float thickness)
         {
         {
+            if (thickness < 0)
+                throw new ArgumentOutOfRangeException(nameof(thickness), "The Line thickness cannot be negative.");
+            
             var line = new Line
             var line = new Line
             {
             {
                 Thickness = thickness,
                 Thickness = thickness,

+ 1 - 1
Source/QuestPDF/Fluent/MultiColumnExtensions.cs

@@ -30,7 +30,7 @@ public sealed class MultiColumnDescriptor
     /// </remarks>
     /// </remarks>
     public void Columns(int value = 2)
     public void Columns(int value = 2)
     {
     {
-        if (value <= 1)
+        if (value < 2)
             throw new DocumentComposeException("The 'MultiColumn.Columns' value should be higher than 1.");
             throw new DocumentComposeException("The 'MultiColumn.Columns' value should be higher than 1.");
         
         
         MultiColumn.ColumnCount = value;
         MultiColumn.ColumnCount = value;

+ 9 - 0
Source/QuestPDF/Fluent/RowExtensions.cs

@@ -14,6 +14,9 @@ namespace QuestPDF.Fluent
         /// </summary>
         /// </summary>
         public void Spacing(float value, Unit unit = Unit.Point)
         public void Spacing(float value, Unit unit = Unit.Point)
         {
         {
+            if (value < 0)
+                throw new ArgumentOutOfRangeException(nameof(value), "The row spacing cannot be negative.");
+            
             Row.Spacing = value.ToPoints(unit);
             Row.Spacing = value.ToPoints(unit);
         }
         }
 
 
@@ -51,6 +54,9 @@ namespace QuestPDF.Fluent
         /// <returns>The container of the newly added item.</returns>
         /// <returns>The container of the newly added item.</returns>
         public IContainer RelativeItem(float size = 1)
         public IContainer RelativeItem(float size = 1)
         {
         {
+            if (size < 0)
+                throw new ArgumentOutOfRangeException(nameof(size), "The relative item size cannot be negative.");
+            
             return Item(RowItemType.Relative, size);
             return Item(RowItemType.Relative, size);
         }
         }
         
         
@@ -60,6 +66,9 @@ namespace QuestPDF.Fluent
         /// <returns>The container of the newly created item.</returns>
         /// <returns>The container of the newly created item.</returns>
         public IContainer ConstantItem(float size, Unit unit = Unit.Point)
         public IContainer ConstantItem(float size, Unit unit = Unit.Point)
         {
         {
+            if (size < 0)
+                throw new ArgumentOutOfRangeException(nameof(size), "The constant item size cannot be negative.");
+            
             return Item(RowItemType.Constant, size.ToPoints(unit));
             return Item(RowItemType.Constant, size.ToPoints(unit));
         }
         }
 
 

+ 6 - 0
Source/QuestPDF/Fluent/TableExtensions.cs

@@ -18,6 +18,9 @@ namespace QuestPDF.Fluent
         /// <returns>The container of the newly created column.</returns>
         /// <returns>The container of the newly created column.</returns>
         public void ConstantColumn(float width, Unit unit = Unit.Point)
         public void ConstantColumn(float width, Unit unit = Unit.Point)
         {
         {
+            if (width <= 0)
+                throw new ArgumentOutOfRangeException(nameof(width), "Constant column width must be greater than zero.");
+            
             ComplexColumn(constantWidth: width.ToPoints(unit));
             ComplexColumn(constantWidth: width.ToPoints(unit));
         }
         }
         
         
@@ -31,6 +34,9 @@ namespace QuestPDF.Fluent
         /// <returns>The container for the newly defined column.</returns>
         /// <returns>The container for the newly defined column.</returns>
         public void RelativeColumn(float width = 1)
         public void RelativeColumn(float width = 1)
         {
         {
+            if (width <= 0)
+                throw new ArgumentOutOfRangeException(nameof(width), "Relative column width must be greater than zero.");
+            
             ComplexColumn(relativeWidth: width);
             ComplexColumn(relativeWidth: width);
         }
         }
         
         

+ 4 - 1
Source/QuestPDF/Fluent/TextExtensions.cs

@@ -113,8 +113,11 @@ namespace QuestPDF.Fluent
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.clampLines"]/*' />
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.clampLines"]/*' />
         public TextBlockDescriptor ClampLines(int maxLines, string ellipsis = TextDescriptor.DefaultLineClampEllipsis)
         public TextBlockDescriptor ClampLines(int maxLines, string ellipsis = TextDescriptor.DefaultLineClampEllipsis)
         {
         {
+            if (maxLines < 0)
+                throw new ArgumentException("Line clamp must be greater or equal to zero", nameof(maxLines));
+            
             TextBlock.LineClamp = maxLines;
             TextBlock.LineClamp = maxLines;
-            TextBlock.LineClampEllipsis = ellipsis;
+            TextBlock.LineClampEllipsis = ellipsis ?? TextDescriptor.DefaultLineClampEllipsis;
             return this;
             return this;
         }
         }
         
         

+ 6 - 2
Source/QuestPDF/Fluent/TextStyleExtensions.cs

@@ -39,6 +39,9 @@ namespace QuestPDF.Fluent
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.fontFamily"]/*' />
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.fontFamily"]/*' />
         public static TextStyle FontFamily(this TextStyle style, params string[] values)
         public static TextStyle FontFamily(this TextStyle style, params string[] values)
         {
         {
+            if (values == null || values.Length == 0)
+                return style;
+            
             return style.Mutate(TextStyleProperty.FontFamilies, values);
             return style.Mutate(TextStyleProperty.FontFamilies, values);
         }
         }
         
         
@@ -60,10 +63,11 @@ namespace QuestPDF.Fluent
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.lineHeight"]/*' />
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.lineHeight"]/*' />
         public static TextStyle LineHeight(this TextStyle style, float? factor)
         public static TextStyle LineHeight(this TextStyle style, float? factor)
         {
         {
-            if (factor <= 0)
+            factor ??= TextStyle.NormalLineHeightCalculatedFromFontMetrics;
+            
+            if (factor < 0)
                 throw new ArgumentException("Line height must be greater than 0.");
                 throw new ArgumentException("Line height must be greater than 0.");
             
             
-            factor ??= TextStyle.NormalLineHeightCalculatedFromFontMetrics;
             return style.Mutate(TextStyleProperty.LineHeight, factor);
             return style.Mutate(TextStyleProperty.LineHeight, factor);
         }
         }
 
 

+ 6 - 0
Source/QuestPDF/Helpers/PageSizes.cs

@@ -22,6 +22,12 @@ namespace QuestPDF.Helpers
         
         
         public PageSize(float width, float height, Unit unit = Unit.Point)
         public PageSize(float width, float height, Unit unit = Unit.Point)
         {
         {
+            if (width < 0)
+                throw new ArgumentOutOfRangeException(nameof(width), "Page width must be greater than 0.");
+            
+            if (height < 0)
+                throw new ArgumentOutOfRangeException(nameof(height), "Page height must be greater than 0.");
+            
             Width = width.ToPoints(unit);
             Width = width.ToPoints(unit);
             Height = height.ToPoints(unit);
             Height = height.ToPoints(unit);
         }
         }