-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add string sanitisation to strings being exported to word
- Loading branch information
Dominic NEED
authored and
Dominic NEED
committed
Mar 15, 2024
1 parent
f9258bb
commit de01c3b
Showing
8 changed files
with
169 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...ons/Dfe.PrepareConversions/Services/DocumentGenerator/DocumentGeneratorStringSanitiser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Dfe.PrepareConversions.DocumentGeneration.Elements; | ||
using System.Linq; | ||
using System.Xml; | ||
|
||
namespace Dfe.PrepareConversions.Services.DocumentGenerator | ||
{ | ||
public static class DocumentGeneratorStringSanitiser | ||
{ | ||
public static string SanitizeString(string input) | ||
{ | ||
if (string.IsNullOrEmpty(input)) return input; | ||
|
||
string output = input.Replace("<", "<") | ||
.Replace(">", ">") | ||
.Replace("&", "&") | ||
.Replace("\"", """) | ||
.Replace("\'", "'") | ||
.Replace("&amp;", "&"); | ||
output = new string(output.Where(ch => XmlConvert.IsXmlChar(ch)).ToArray()); | ||
return output; | ||
} | ||
|
||
// SanitizeTextElements method as provided | ||
public static TextElement[] SanitizeTextElements(TextElement[] elements) | ||
{ | ||
return elements.Select(element => new TextElement | ||
{ | ||
Value = SanitizeString(element.Value), | ||
Bold = element.Bold | ||
}).ToArray(); | ||
} | ||
|
||
// Utility method to create and sanitize TextElement arrays | ||
public static TextElement[] CreateTextElements(string label, string value) | ||
{ | ||
return SanitizeTextElements(new[] | ||
{ | ||
new TextElement { Value = label, Bold = true }, | ||
new TextElement { Value = value } | ||
}); | ||
} | ||
public static TextElement[] CreateSingleTextElement(string value) | ||
{ | ||
return new TextElement[] | ||
{ | ||
new() { Value = SanitizeString(value) } | ||
}; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.