From 13f76d2af9944a62bb8d4c5ce33fd1f0c90d38dc Mon Sep 17 00:00:00 2001 From: Harvey Williams Date: Fri, 11 Aug 2017 11:42:35 +0100 Subject: [PATCH] Updated ReplaceLineBreaksForHtml to include \r I was surprised when this method did not replace a `\r` character with an HTML break. I have updated it to not only replace `\n`, but also `\r\n` and just `\r`. I think these cases should be considered because of [this SO thread](https://stackoverflow.com/questions/4824621/is-a-new-line-n-or-r-n). The updated code will not replace `\n\r` with one break but with two. I used [this SO answer](https://stackoverflow.com/a/8196219/2963111) to update the method. ``` [TestMethod] public void ReplaceLineBreaksForHtmlTest() { var result = "foo\rbar\nbaz\r\nbiff\n\r".ReplaceLineBreaksForHtml(); Assert.AreEqual(result, "foo
\rbar
\nbaz
\r\nbiff
\n
\r"); } ``` --- src/Umbraco.Web/HtmlStringUtilities.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Web/HtmlStringUtilities.cs b/src/Umbraco.Web/HtmlStringUtilities.cs index 24a643b5b0..1737aca365 100644 --- a/src/Umbraco.Web/HtmlStringUtilities.cs +++ b/src/Umbraco.Web/HtmlStringUtilities.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Text; using System.Web; +using System.Text.RegularExpressions; using HtmlAgilityPack; namespace Umbraco.Web @@ -23,7 +24,7 @@ namespace Umbraco.Web /// The text with text line breaks replaced with html linebreaks (
)
public string ReplaceLineBreaksForHtml(string text) { - return text.Replace("\n", "
\n"); + return Regex.Replace(text, @"(\r\n?|\n)", "
$0"); } public HtmlString StripHtmlTags(string html, params string[] tags) @@ -241,4 +242,4 @@ namespace Umbraco.Web } } } -} \ No newline at end of file +}