From 8f199223958e9d995ea0533256ef35946aa12061 Mon Sep 17 00:00:00 2001 From: Nicolas Fahrni Date: Thu, 31 Oct 2024 11:57:34 +0100 Subject: [PATCH] fix: html placeholder renders empty if model contains string empty or whitespace --- DocxTemplater.Test/DocxTemplateTest.cs | 27 ++++++++++++++++++++++++ DocxTemplater/Formatter/HtmlFormatter.cs | 15 ++++++------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/DocxTemplater.Test/DocxTemplateTest.cs b/DocxTemplater.Test/DocxTemplateTest.cs index ec2968c..ed3bb95 100644 --- a/DocxTemplater.Test/DocxTemplateTest.cs +++ b/DocxTemplater.Test/DocxTemplateTest.cs @@ -225,6 +225,33 @@ public void HtmlIsAlwaysEnclosedWithHtmlTags(string html, string expexted) // check html part contains html; } + [TestCase("")] + [TestCase(" ")] + public void HtmlIsAlwaysShouldRenderEmptyStrings(string html) + { + using var memStream = new MemoryStream(); + using var wpDocument = WordprocessingDocument.Create(memStream, WordprocessingDocumentType.Document); + MainDocumentPart mainPart = wpDocument.AddMainDocumentPart(); + mainPart.Document = new Document(new Body(new Paragraph(new Run(new Text("This should render nothing {{ds}:html}"))))); + wpDocument.Save(); + memStream.Position = 0; + var docTemplate = new DocxTemplate(memStream); + docTemplate.BindModel("ds", html); + + var result = docTemplate.Process(); + docTemplate.Validate(); + Assert.That(result, Is.Not.Null); + result.SaveAsFileAndOpenInWord(); + result.Position = 0; + var document = WordprocessingDocument.Open(result, false); + var body = document.MainDocumentPart.Document.Body; + Assert.That(body.InnerXml, Is.EqualTo("" + + "" + + "This should render nothing " + + "" + + "")); + } + [Test] public void InsertHtmlInLoop() { diff --git a/DocxTemplater/Formatter/HtmlFormatter.cs b/DocxTemplater/Formatter/HtmlFormatter.cs index 8817fd1..5b9a219 100644 --- a/DocxTemplater/Formatter/HtmlFormatter.cs +++ b/DocxTemplater/Formatter/HtmlFormatter.cs @@ -17,16 +17,13 @@ public bool CanHandle(Type type, string prefix) public void ApplyFormat(FormatterContext context, Text target) { - if (context.Value is not string html) - { - return; - } - if (string.IsNullOrWhiteSpace(html)) + if (context.Value is not string html || string.IsNullOrWhiteSpace(html)) { + target.RemoveWithEmptyParent(); return; } - // fix html - ensure starts and ends with and + // fix html - ensure starts and ends with if (!html.StartsWith("", StringComparison.CurrentCultureIgnoreCase)) { html = "" + html; @@ -41,15 +38,15 @@ public void ApplyFormat(FormatterContext context, Text target) { if (openXmlPartRootElement.OpenXmlPart is HeaderPart headerPart) { - alternativeFormatImportPartId = HtmlFormatter.CreateAlternativeFormatImportPart(headerPart, html); + alternativeFormatImportPartId = CreateAlternativeFormatImportPart(headerPart, html); } if (openXmlPartRootElement.OpenXmlPart is FooterPart footerPart) { - alternativeFormatImportPartId = HtmlFormatter.CreateAlternativeFormatImportPart(footerPart, html); + alternativeFormatImportPartId = CreateAlternativeFormatImportPart(footerPart, html); } if (openXmlPartRootElement.OpenXmlPart is MainDocumentPart mainDocumentPart) { - alternativeFormatImportPartId = HtmlFormatter.CreateAlternativeFormatImportPart(mainDocumentPart, html); + alternativeFormatImportPartId = CreateAlternativeFormatImportPart(mainDocumentPart, html); } } if (alternativeFormatImportPartId == null)