using System; using System.Text; using System.Text.RegularExpressions; using System.Web; namespace Hua.DotNet.Code.Helper.Html; public class HtmlHelper { private static readonly Regex paragraphStartRegex = new Regex("

", RegexOptions.IgnoreCase); private static readonly Regex paragraphEndRegex = new Regex("

", RegexOptions.IgnoreCase); private static string EnsureOnlyAllowedHtml(string text) { if (string.IsNullOrEmpty(text)) { return string.Empty; } MatchCollection matchCollection = Regex.Matches(text, "<.*?>", RegexOptions.IgnoreCase); for (int num = matchCollection.Count - 1; num >= 0; num--) { if (!IsValidTag(text.Substring(matchCollection[num].Index + 1, matchCollection[num].Length - 1).Trim().ToLower(), "br,hr,b,i,u,a,div,ol,ul,li,blockquote,img,span,p,em,strong,font,pre,h1,h2,h3,h4,h5,h6,address,cite")) { text = text.Remove(matchCollection[num].Index, matchCollection[num].Length); } } return text; } private static bool IsValidTag(string tag, string tags) { string[] array = tags.Split(','); if (tag.IndexOf("javascript") >= 0) { return false; } if (tag.IndexOf("vbscript") >= 0) { return false; } if (tag.IndexOf("onclick") >= 0) { return false; } char[] anyOf = new char[4] { ' ', '>', '/', '\t' }; int num = tag.IndexOfAny(anyOf, 1); if (num > 0) { tag = tag.Substring(0, num); } if (tag[0] == '/') { tag = tag.Substring(1); } string[] array2 = array; foreach (string text in array2) { if (tag == text) { return true; } } return false; } public static string FormatText(string text, bool stripTags, bool convertPlainTextToHtml, bool allowHtml, bool allowBBCode, bool resolveLinks, bool addNoFollowTag) { if (string.IsNullOrEmpty(text)) { return string.Empty; } try { if (stripTags) { text = StripTags(text); } text = ((!allowHtml) ? HttpUtility.HtmlEncode(text) : EnsureOnlyAllowedHtml(text)); if (convertPlainTextToHtml) { text = ConvertPlainTextToHtml(text); } if (resolveLinks) { text = ResolveLinksHelper.FormatText(text); } } catch (Exception ex) { text = $"Text cannot be formatted. Error: {ex.Message}"; } return text; } public static string StripTags(string text) { if (string.IsNullOrEmpty(text)) { return string.Empty; } text = Regex.Replace(text, "(>)(\\r|\\n)*(<)", "><"); text = Regex.Replace(text, "(<[^>]*>)([^<]*)", "$2"); text = Regex.Replace(text, "(&#x?[0-9]{2,4};|"|&| |<|>|€|©|®|‰|‡|†|‹|›|„|”|“|‚|’|‘|—|–|‏|‎|‍|‌| | | |˜|ˆ|Ÿ|š|Š)", "@"); return text; } public static string ReplaceAnchorTags(string text) { if (string.IsNullOrEmpty(text)) { return string.Empty; } text = Regex.Replace(text, "]+>([^<]*(?:(?!", "$1", RegexOptions.IgnoreCase); return text; } public static string ConvertPlainTextToHtml(string text) { if (string.IsNullOrEmpty(text)) { return string.Empty; } text = text.Replace("\r\n", "
"); text = text.Replace("\r", "
"); text = text.Replace("\n", "
"); text = text.Replace("\t", "  "); text = text.Replace(" ", "  "); return text; } public static string ConvertHtmlToPlainText(string text, bool decode = false, bool replaceAnchorTags = false) { if (string.IsNullOrEmpty(text)) { return string.Empty; } if (decode) { text = HttpUtility.HtmlDecode(text); } text = text.Replace("
", "\n"); text = text.Replace("
", "\n"); text = text.Replace("
", "\n"); text = text.Replace("  ", "\t"); text = text.Replace("  ", " "); if (replaceAnchorTags) { text = ReplaceAnchorTags(text); } return text; } public static string ConvertPlainTextToParagraph(string text) { if (string.IsNullOrEmpty(text)) { return string.Empty; } text = paragraphStartRegex.Replace(text, string.Empty); text = paragraphEndRegex.Replace(text, "\n"); text = text.Replace("\r\n", "\n").Replace("\r", "\n"); text += "\n\n"; text = text.Replace("\n\n", "\n"); string[] array = text.Split(new char[1] { '\n' }); StringBuilder stringBuilder = new StringBuilder(); string[] array2 = array; foreach (string text2 in array2) { if (text2 != null && text2.Trim().Length > 0) { stringBuilder.AppendFormat("

{0}

\n", text2); } } return stringBuilder.ToString(); } }