Skip to content

chore: added new config option XmlCharEscaping #13

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

Merged
merged 1 commit into from
May 16, 2023
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
48 changes: 48 additions & 0 deletions ObjectSemantics.NET.Tests/ObjectSemanticsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -784,5 +784,53 @@ public void Should_Act_On_IfCondition_Having_Multiple_IF_Condition_Blocks_MultiL
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
}
#endregion


#region Xml Char Escape Tests
[Fact]
public void Should_Escape_Xml_Char_If_Option_Is_Enabled()
{
//Create Model
Student student = new Student { StudentName = "I've got \"special\" < & also >" };
var template = new ObjectSemanticsTemplate
{
FileContents = @"{{ StudentName }}"
};
string generatedTemplate = TemplateMapper.Map(student, template, null, new TemplateMapperOptions
{
XmlCharEscaping = true
});
string expectedString = "I&apos;ve got &quot;special&quot; &lt; &amp; also &gt;";
Assert.Equal(expectedString, generatedTemplate, false, true, true);
}


[Fact]
public void Should_Escape_Xml_Char_In_LOOPS_If_Option_Is_Enabled()
{
//Create Model
Student student = new Student
{
Invoices = new List<Invoice>
{
new Invoice{ Narration="I've got \"special\""},
new Invoice{ Narration="I've got < & also >"}
}
};
//Template
var template = new ObjectSemanticsTemplate
{
FileContents = @"{{ #foreach(invoices) }} [{{ Narration }}] {{ #endforeach }}"
};
string generatedTemplate = TemplateMapper.Map(student, template, null, new TemplateMapperOptions
{
XmlCharEscaping = true
});
string expectedResult = @" [I&apos;ve got &quot;special&quot;] [I&apos;ve got &lt; &amp; also &gt;] ";
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
}
#endregion


}
}
4 changes: 2 additions & 2 deletions ObjectSemantics.NET/Algorithim/GavinsAlgorithim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static class GavinsAlgorithim
{
ExtractedObjProperty objProperty = rowRecordValues.FirstOrDefault(x => x.Name.ToUpper().Equals(objLoopCode.TargetPropertyName.ToUpper()));
if (objProperty != null)
activeRow = activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, objProperty.GetValueFromPropertyFormatted(objLoopCode.FormattingCommand));
activeRow = activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, objProperty.GetPropertyDisplayString(objLoopCode.FormattingCommand, options));
else
activeRow = activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, objLoopCode.ReplaceCommand);
}
Expand All @@ -85,7 +85,7 @@ public static class GavinsAlgorithim
{
ExtractedObjProperty property = objProperties.FirstOrDefault(x => x.Name.ToUpper().Equals(replaceCode.TargetPropertyName.ToUpper()));
if (property != null)
clonedTemplate.Template = clonedTemplate.Template.ReplaceFirstOccurrence(replaceCode.ReplaceRef, property.GetValueFromPropertyFormatted(replaceCode.FormattingCommand));
clonedTemplate.Template = clonedTemplate.Template.ReplaceFirstOccurrence(replaceCode.ReplaceRef, property.GetPropertyDisplayString(replaceCode.FormattingCommand, options));
else
clonedTemplate.Template = clonedTemplate.Template.ReplaceFirstOccurrence(replaceCode.ReplaceRef, @"{{ ##command## }}".Replace("##command##", replaceCode.ReplaceCommand));
}
Expand Down
13 changes: 12 additions & 1 deletion ObjectSemantics.NET/Extensions/ExtractedObjPropertyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;

namespace ObjectSemantics.NET
{
public static class ExtractedObjPropertyExtensions
{
public static string GetValueFromPropertyFormatted(this ExtractedObjProperty p, string customFormattingValue)
public static string GetPropertyDisplayString(this ExtractedObjProperty p, string stringFormatting, TemplateMapperOptions templateMapperOptions)
{
string formattedPropertyString = GetAppliedPropertyFormatting(p, stringFormatting);
//Apply Options to Property value string
if (templateMapperOptions==null) return formattedPropertyString;
if (templateMapperOptions.XmlCharEscaping)
formattedPropertyString = SecurityElement.Escape(formattedPropertyString);
return formattedPropertyString;
}
private static string GetAppliedPropertyFormatting(this ExtractedObjProperty p, string customFormattingValue)
{
if (string.IsNullOrWhiteSpace(customFormattingValue))
return p.StringFormatted;
Expand Down
11 changes: 10 additions & 1 deletion ObjectSemantics.NET/TemplateMapperOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
{
public class TemplateMapperOptions
{

/// <summary>
/// This will apply XML Character Escape on invalid characters in a Property Value String with their valid XML Equivalent
/// Example of Characters;
/// " &quot;
/// ' &apos;
/// < &lt;
/// > &gt;
/// & &amp;
/// </summary>
public bool XmlCharEscaping { get; set; } = false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like we got our first config

}
}