HtmlTools.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using Ganss.XSS;
  2. using HtmlAgilityPack;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. namespace Masuit.Tools.Html
  8. {
  9. /// <summary>
  10. /// html工具类
  11. /// </summary>
  12. public static partial class HtmlTools
  13. {
  14. private static readonly HtmlSanitizer Sanitizer = new HtmlSanitizer();
  15. static HtmlTools()
  16. {
  17. Sanitizer.AllowedAttributes.Remove("id");
  18. Sanitizer.AllowedAttributes.Remove("alt");
  19. Sanitizer.AllowedCssProperties.Remove("font-family");
  20. Sanitizer.AllowedCssProperties.Remove("background-color");
  21. Sanitizer.KeepChildNodes = true;
  22. Sanitizer.AllowedTags.Remove("input");
  23. Sanitizer.AllowedTags.Remove("button");
  24. Sanitizer.AllowedTags.Remove("iframe");
  25. Sanitizer.AllowedTags.Remove("frame");
  26. Sanitizer.AllowedTags.Remove("textarea");
  27. Sanitizer.AllowedTags.Remove("select");
  28. Sanitizer.AllowedTags.Remove("form");
  29. Sanitizer.AllowedAttributes.Add("src");
  30. Sanitizer.AllowedAttributes.Add("class");
  31. Sanitizer.AllowedAttributes.Add("style");
  32. }
  33. /// <summary>
  34. /// 标准的防止html的xss净化器
  35. /// </summary>
  36. /// <param name="html"></param>
  37. /// <returns></returns>
  38. public static string HtmlSantinizerStandard(this string html)
  39. {
  40. return Sanitizer.Sanitize(html);
  41. }
  42. /// <summary>
  43. /// 自定义的防止html的xss净化器
  44. /// </summary>
  45. /// <param name="html">源html</param>
  46. /// <param name="labels">需要移除的标签集合</param>
  47. /// <param name="attributes">需要移除的属性集合</param>
  48. /// <param name="styles">需要移除的样式集合</param>
  49. /// <returns></returns>
  50. public static string HtmlSantinizerCustom(this string html, string[] labels = null, string[] attributes = null, string[] styles = null)
  51. {
  52. if (labels != null)
  53. {
  54. foreach (string label in labels)
  55. {
  56. Sanitizer.AllowedTags.Remove(label);
  57. }
  58. }
  59. if (attributes != null)
  60. {
  61. foreach (string attr in attributes)
  62. {
  63. Sanitizer.AllowedAttributes.Remove(attr);
  64. }
  65. }
  66. if (styles != null)
  67. {
  68. foreach (string p in styles)
  69. {
  70. Sanitizer.AllowedCssProperties.Remove(p);
  71. }
  72. }
  73. Sanitizer.KeepChildNodes = true;
  74. return Sanitizer.Sanitize(html);
  75. }
  76. /// <summary>
  77. /// 去除html标签后并截取字符串
  78. /// </summary>
  79. /// <param name="html">源html</param>
  80. /// <param name="length">截取长度</param>
  81. /// <returns></returns>
  82. public static string RemoveHtmlTag(this string html, int length = 0)
  83. {
  84. var doc = new HtmlDocument();
  85. doc.LoadHtml(html);
  86. var strText = doc.DocumentNode.InnerText;
  87. if (length > 0 && strText.Length > length)
  88. {
  89. return strText.Substring(0, length);
  90. }
  91. return strText;
  92. }
  93. /// <summary>
  94. /// 清理Word文档转html后的冗余标签属性
  95. /// </summary>
  96. /// <param name="html"></param>
  97. /// <returns></returns>
  98. public static string ClearHtml(this string html)
  99. {
  100. string s = Regex.Match(Regex.Replace(html, @"background-color:#?\w{3,7}|font-family:'?[\w|\(|\)]*'?;?", string.Empty), @"<body[^>]*>([\s\S]*)<\/body>").Groups[1].Value.Replace("&#xa0;", string.Empty);
  101. s = Regex.Replace(s, @"\w+-?\w+:0\w+;?", string.Empty); //去除多余的零值属性
  102. s = Regex.Replace(s, "alt=\"(.+?)\"", string.Empty); //除去alt属性
  103. s = Regex.Replace(s, @"-aw.+?\s", string.Empty); //去除Word产生的-aw属性
  104. return s;
  105. }
  106. /// <summary>
  107. /// 替换html的img路径为绝对路径
  108. /// </summary>
  109. /// <param name="html"></param>
  110. /// <param name="imgDest"></param>
  111. /// <returns></returns>
  112. public static string ReplaceHtmlImgSource(this string html, string imgDest) => html.Replace("<img src=\"", "<img src=\"" + imgDest + "/");
  113. /// <summary>
  114. /// 将src的绝对路径换成相对路径
  115. /// </summary>
  116. /// <param name="s"></param>
  117. /// <returns></returns>
  118. public static string ConvertImgSrcToRelativePath(this string s)
  119. {
  120. return Regex.Replace(s, @"<img src=""(http:\/\/.+?)/", @"<img src=""/");
  121. }
  122. /// <summary>
  123. /// 匹配html的所有img标签集合
  124. /// </summary>
  125. /// <param name="html"></param>
  126. /// <returns></returns>
  127. public static IEnumerable<HtmlNode> MatchImgTags(this string html)
  128. {
  129. var doc = new HtmlDocument();
  130. doc.LoadHtml(html);
  131. var nodes = doc.DocumentNode.Descendants("img");
  132. return nodes;
  133. }
  134. /// <summary>
  135. /// 匹配html的所有img标签的src集合
  136. /// </summary>
  137. /// <param name="html"></param>
  138. /// <returns></returns>
  139. public static IEnumerable<string> MatchImgSrcs(this string html)
  140. {
  141. return MatchImgTags(html).Where(n => n.Attributes.Contains("src")).Select(n => n.Attributes["src"].Value);
  142. }
  143. /// <summary>
  144. /// 获取html中第一个img标签的src
  145. /// </summary>
  146. /// <param name="html"></param>
  147. /// <returns></returns>
  148. public static string MatchFirstImgSrc(this string html)
  149. {
  150. return MatchImgSrcs(html).FirstOrDefault();
  151. }
  152. /// <summary>
  153. /// 随机获取html代码中的img标签的src属性
  154. /// </summary>
  155. /// <param name="html"></param>
  156. /// <returns></returns>
  157. public static string MatchRandomImgSrc(this string html)
  158. {
  159. int count = MatchImgSrcs(html).Count();
  160. var rnd = new Random();
  161. return MatchImgSrcs(html).ElementAtOrDefault(rnd.Next(count));
  162. }
  163. /// <summary>
  164. /// 替换回车换行符为html换行符
  165. /// </summary>
  166. /// <param name="str">html</param>
  167. public static string StrFormat(this string str)
  168. {
  169. str = str.Replace("\r\n", "<br />");
  170. str = str.Replace("\n", "<br />");
  171. var str2 = str;
  172. return str2;
  173. }
  174. /// <summary>
  175. /// 替换html字符
  176. /// </summary>
  177. /// <param name="strHtml">html</param>
  178. public static string EncodeHtml(this string strHtml)
  179. {
  180. if (strHtml != "")
  181. {
  182. strHtml = strHtml.Replace(",", "&def");
  183. strHtml = strHtml.Replace("'", "&dot");
  184. strHtml = strHtml.Replace(";", "&dec");
  185. return strHtml;
  186. }
  187. return "";
  188. }
  189. }
  190. }