Correctly html encode text (#6235)

(cherry picked from commit 1bf85ab1db)
This commit is contained in:
Mads Krohn
2019-09-02 08:46:33 +02:00
committed by Sebastiaan Janssen
parent f9319079c7
commit 0e8b4b8342

View File

@@ -19,10 +19,26 @@ namespace Umbraco.Web
/// Replaces text line breaks with HTML line breaks
/// </summary>
/// <param name="text">The text.</param>
/// <returns>The text with text line breaks replaced with HTML line breaks (<br/>)</returns>
/// <returns>The text with text line breaks replaced with HTML line breaks (<c>&lt;br /&gt;</c>).
[Obsolete("This method doesn't HTML encode the text. Use ReplaceLineBreaks instead.")]
public HtmlString ReplaceLineBreaksForHtml(string text)
{
return new HtmlString(text.Replace("\r\n", @"<br />").Replace("\n", @"<br />").Replace("\r", @"<br />"));
return new HtmlString(text.Replace("\r\n", @"<br />").Replace("\n", @"<br />").Replace("\r", @"<br />"));
}
/// <summary>
/// HTML encodes the text and replaces text line breaks with HTML line breaks.
/// </summary>
/// <param name="text">The text.</param>
/// <returns>The HTML encoded text with text line breaks replaced with HTML line breaks (<c>&lt;br /&gt;</c>).</returns>
public IHtmlString ReplaceLineBreaks(string text)
{
var value = HttpUtility.HtmlEncode(text)?
.Replace("\r\n", "<br />")
.Replace("\r", "<br />")
.Replace("\n", "<br />");
return new HtmlString(value);
}
public HtmlString StripHtmlTags(string html, params string[] tags)