Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: html placeholder renders empty if model contains string empty or… #26

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions DocxTemplater.Test/DocxTemplateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" +
"<w:r>" +
"<w:t xml:space=\"preserve\">This should render nothing </w:t>" +
"</w:r>" +
"</w:p>"));
}

[Test]
public void InsertHtmlInLoop()
{
Expand Down
15 changes: 6 additions & 9 deletions DocxTemplater/Formatter/HtmlFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <html> and <body>
// fix html - ensure starts and ends with <html>
if (!html.StartsWith("<html>", StringComparison.CurrentCultureIgnoreCase))
{
html = "<html>" + html;
Expand All @@ -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)
Expand Down
Loading