-
Notifications
You must be signed in to change notification settings - Fork 32
Description
MailMergeLib version: 5.11.1
Framework version: net8.0
Complex conditions that contain a ">" or "<" sign are not formatted correctly when the MailMergeMessage body is provided as HtmlText. The mail body is sent with the wrong condition output and/or may contain escape sequences for those signs.
This seems due to some parsing being done before the SmartFormat library is invoked, replacing those signs with their corresponding escape sequence.
Here's an unit test. The "GetMimeMessage" case produces the same output as using MailMergeSender.SendAsync method. The "ParseHtmlAndFormat" has a similar problem and tries to replicate the library way of working. The "FormatOnly" produces the expected result (except for the missing html/head/body tags obviously)
[TestCase(1, "GetMimeMessage")]
[TestCase(2, "ParseHtmlAndFormat")]
[TestCase(3, "FormatOnly")]
public void ComplexConditionTest(int value, string mode)
{
using var mmm = new MailMergeMessage("complex condition", "",
"{value:cond:>2?3 or greater|<2?1 or less|that must be 2}");
var variables = new Dictionary<string, object>() { { "value", value } };
var smartFormatter = mmm.SmartFormatter;
var expected = value > 2 ? "3 or greater" : value < 2 ? "1 or less" : "that must be 2";
switch (mode)
{
case "GetMimeMessage":
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "sender@mail.com"));
mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "customer@mail.com"));
var message = mmm.GetMimeMessage((object) variables);
Assert.That(message.HtmlBody, Is.EqualTo($"<body>{expected}</body>")); // --> fails
break;
case "ParseHtmlAndFormat":
var parser = new HtmlParser();
var htmlDocument = parser.ParseDocument(mmm.HtmlText);
var formattedDocumentHtml = smartFormatter.Format(htmlDocument?.Body?.InnerHtml ?? "", (object) variables);
Assert.That(formattedDocumentHtml, Is.EqualTo(expected)); // --> fails
break;
case "FormatOnly":
var formattedHtmlText = smartFormatter.Format(mmm.HtmlText, (object) variables);
Assert.That(formattedHtmlText, Is.EqualTo(expected)); // --> success
break;
}
}