diff --git a/src/Umbraco.Web/HtmlStringUtilities.cs b/src/Umbraco.Web/HtmlStringUtilities.cs
index 6cf5092bb0..4606a58a3a 100644
--- a/src/Umbraco.Web/HtmlStringUtilities.cs
+++ b/src/Umbraco.Web/HtmlStringUtilities.cs
@@ -19,10 +19,26 @@ namespace Umbraco.Web
/// Replaces text line breaks with HTML line breaks
///
/// The text.
- /// The text with text line breaks replaced with HTML line breaks (
)
+ /// The text with text line breaks replaced with HTML line breaks (<br />).
+ [Obsolete("This method doesn't HTML encode the text. Use ReplaceLineBreaks instead.")]
public HtmlString ReplaceLineBreaksForHtml(string text)
{
- return new HtmlString(text.Replace("\r\n", @"
").Replace("\n", @"
").Replace("\r", @"
"));
+ return new HtmlString(text.Replace("\r\n", @"
").Replace("\n", @"
").Replace("\r", @"
"));
+ }
+
+ ///
+ /// HTML encodes the text and replaces text line breaks with HTML line breaks.
+ ///
+ /// The text.
+ /// The HTML encoded text with text line breaks replaced with HTML line breaks (<br />).
+ public IHtmlString ReplaceLineBreaks(string text)
+ {
+ var value = HttpUtility.HtmlEncode(text)?
+ .Replace("\r\n", "
")
+ .Replace("\r", "
")
+ .Replace("\n", "
");
+
+ return new HtmlString(value);
}
public HtmlString StripHtmlTags(string html, params string[] tags)