convert string to html entities (for email protection)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.AspNetCore.Html;
|
||||
|
||||
namespace zero.Extensions;
|
||||
|
||||
@@ -271,4 +272,21 @@ public static class StringExtensions
|
||||
yield return text.Substring(i, groupSize);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static IHtmlContent ToHtmlEntities(this string text)
|
||||
{
|
||||
StringBuilder sb = new();
|
||||
|
||||
foreach (char ch in text)
|
||||
{
|
||||
sb.Append("&#");
|
||||
sb.Append((int)ch);
|
||||
sb.Append(';');
|
||||
}
|
||||
|
||||
HtmlContentBuilder builder = new();
|
||||
builder.SetHtmlContent(sb.ToString());
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,4 +22,14 @@ public static class LocalizerExtensions
|
||||
builder.SetHtmlContent(value);
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
||||
public static IHtmlContent HtmlEntities(this ILocalizer localizer, string key, Dictionary<string, string> tokens = null)
|
||||
{
|
||||
string value = localizer.Text(key, tokens);
|
||||
|
||||
HtmlContentBuilder builder = new();
|
||||
builder.SetHtmlContent(value.ToHtmlEntities());
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user