Fixed NullReferenceException when the text is null

This commit is contained in:
MarcinZiabek
2022-03-15 11:55:59 +01:00
parent 208235ad1e
commit a67fa754af
3 changed files with 42 additions and 6 deletions
+32
View File
@@ -339,5 +339,37 @@ namespace QuestPDF.Examples
});
});
}
[Test]
public void DrawingNullTextShouldNotThrowException()
{
RenderingTest
.Create()
.ProduceImages()
.ShowResults()
.RenderDocument(container =>
{
container.Page(page =>
{
page.Margin(50);
page.PageColor(Colors.White);
page.Size(PageSizes.A4);
page.Content().Column(column =>
{
column.Item().Text(null);
column.Item().Text(text =>
{
text.Span(null);
text.Line(null);
text.Hyperlink(null, "http://www.questpdf.com");
text.TotalPages().Format(x => null);
});
});
});
});
}
}
}
@@ -11,19 +11,19 @@ namespace QuestPDF.Elements.Text.Items
public override TextMeasurementResult? Measure(TextMeasurementRequest request)
{
SetPageNumber(request.PageContext);
UpdatePageNumberText(request.PageContext);
return MeasureWithoutCache(request);
}
public override void Draw(TextDrawingRequest request)
{
SetPageNumber(request.PageContext);
UpdatePageNumberText(request.PageContext);
base.Draw(request);
}
private void SetPageNumber(IPageContext context)
private void UpdatePageNumberText(IPageContext context)
{
Text = Source(context) ?? PageNumberPlaceholder;
Text = Source(context) ?? string.Empty;
}
}
}
+6 -2
View File
@@ -24,7 +24,7 @@ namespace QuestPDF.Fluent
public class TextPageNumberDescriptor : TextSpanDescriptor
{
internal PageNumberFormatter FormatFunction { get; private set; } = x => (x ?? 123).ToString();
internal PageNumberFormatter FormatFunction { get; private set; } = x => x?.ToString() ?? string.Empty;
internal TextPageNumberDescriptor(TextStyle textStyle) : base(textStyle)
{
@@ -92,6 +92,10 @@ namespace QuestPDF.Fluent
public TextSpanDescriptor Span(string? text)
{
var style = DefaultStyle.Clone();
var descriptor = new TextSpanDescriptor(style);
if (text == null)
return descriptor;
var items = text
.Replace("\r", string.Empty)
@@ -114,7 +118,7 @@ namespace QuestPDF.Fluent
.ToList()
.ForEach(TextBlocks.Add);
return new TextSpanDescriptor(style);
return descriptor;
}
public TextSpanDescriptor Line(string? text)