EventArgs.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using AngleSharp.Css.Dom;
  2. using AngleSharp.Dom;
  3. using AngleSharp.Html.Dom;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. namespace Ganss.Xss;
  8. /// <summary>
  9. /// Provides data for the <see cref="HtmlSanitizer.PostProcessDom"/> event.
  10. /// </summary>
  11. /// <remarks>
  12. /// Initializes a new instance of the <see cref="PostProcessDomEventArgs"/> class.
  13. /// </remarks>
  14. public class PostProcessDomEventArgs(IHtmlDocument document) : EventArgs
  15. {
  16. /// <summary>
  17. /// Gets the document.
  18. /// </summary>
  19. /// <value>
  20. /// The document.
  21. /// </value>
  22. public IHtmlDocument Document { get; private set; } = document;
  23. }
  24. /// <summary>
  25. /// Provides data for the <see cref="HtmlSanitizer.PostProcessNode"/> event.
  26. /// </summary>
  27. /// <remarks>
  28. /// Initializes a new instance of the <see cref="PostProcessNodeEventArgs"/> class.
  29. /// </remarks>
  30. public class PostProcessNodeEventArgs(IHtmlDocument document, INode node) : EventArgs
  31. {
  32. /// <summary>
  33. /// Gets the document.
  34. /// </summary>
  35. /// <value>
  36. /// The document.
  37. /// </value>
  38. public IHtmlDocument Document { get; private set; } = document;
  39. /// <summary>
  40. /// Gets the DOM node to be processed.
  41. /// </summary>
  42. /// <value>
  43. /// The DOM node.
  44. /// </value>
  45. public INode Node { get; private set; } = node;
  46. /// <summary>
  47. /// Gets the replacement nodes. Leave empty if no replacement should occur.
  48. /// </summary>
  49. /// <value>
  50. /// The replacement nodes.
  51. /// </value>
  52. public ICollection<INode> ReplacementNodes { get; private set; } = [];
  53. }
  54. /// <summary>
  55. /// Provides data for the <see cref="HtmlSanitizer.RemovingTag"/> event.
  56. /// </summary>
  57. /// <remarks>
  58. /// Initializes a new instance of the <see cref="RemovingTagEventArgs"/> class.
  59. /// </remarks>
  60. /// <param name="tag">The element to be removed.</param>
  61. /// <param name="reason">The reason why the tag will be removed.</param>
  62. public class RemovingTagEventArgs(IElement tag, RemoveReason reason) : CancelEventArgs
  63. {
  64. /// <summary>
  65. /// Gets the tag to be removed.
  66. /// </summary>
  67. /// <value>
  68. /// The tag.
  69. /// </value>
  70. public IElement Tag { get; private set; } = tag;
  71. /// <summary>
  72. /// Gets the reason why the tag will be removed.
  73. /// </summary>
  74. /// <value>
  75. /// The reason.
  76. /// </value>
  77. public RemoveReason Reason { get; private set; } = reason;
  78. }
  79. /// <summary>
  80. /// Provides data for the <see cref="HtmlSanitizer.RemovingAttribute"/> event.
  81. /// </summary>
  82. /// <remarks>
  83. /// Initializes a new instance of the <see cref="RemovingAttributeEventArgs"/> class.
  84. /// </remarks>
  85. /// <param name="tag">The element containing the attribute.</param>
  86. /// <param name="attribute">The attribute to be removed.</param>
  87. /// <param name="reason">The reason why the attribute will be removed.</param>
  88. public class RemovingAttributeEventArgs(IElement tag, IAttr attribute, RemoveReason reason) : CancelEventArgs
  89. {
  90. /// <summary>
  91. /// Gets the tag containing the attribute to be removed.
  92. /// </summary>
  93. /// <value>
  94. /// The tag.
  95. /// </value>
  96. public IElement Tag { get; private set; } = tag;
  97. /// <summary>
  98. /// Gets the attribute to be removed.
  99. /// </summary>
  100. /// <value>
  101. /// The attribute.
  102. /// </value>
  103. public IAttr Attribute { get; private set; } = attribute;
  104. /// <summary>
  105. /// Gets the reason why the attribute will be removed.
  106. /// </summary>
  107. /// <value>
  108. /// The reason.
  109. /// </value>
  110. public RemoveReason Reason { get; private set; } = reason;
  111. }
  112. /// <summary>
  113. /// Provides data for the <see cref="HtmlSanitizer.RemovingStyle"/> event.
  114. /// </summary>
  115. /// <remarks>
  116. /// Initializes a new instance of the <see cref="RemovingStyleEventArgs"/> class.
  117. /// </remarks>
  118. /// <param name="tag">The element containing the attribute.</param>
  119. /// <param name="style">The style to be removed.</param>
  120. /// <param name="reason">The reason why the attribute will be removed.</param>
  121. public class RemovingStyleEventArgs(IElement tag, ICssProperty style, RemoveReason reason) : CancelEventArgs
  122. {
  123. /// <summary>
  124. /// Gets the tag containing the style to be removed.
  125. /// </summary>
  126. /// <value>
  127. /// The tag.
  128. /// </value>
  129. public IElement Tag { get; private set; } = tag;
  130. /// <summary>
  131. /// Gets the style to be removed.
  132. /// </summary>
  133. /// <value>
  134. /// The style.
  135. /// </value>
  136. public ICssProperty Style { get; private set; } = style;
  137. /// <summary>
  138. /// Gets the reason why the style will be removed.
  139. /// </summary>
  140. /// <value>
  141. /// The reason.
  142. /// </value>
  143. public RemoveReason Reason { get; private set; } = reason;
  144. }
  145. /// <summary>
  146. /// Provides data for the <see cref="HtmlSanitizer.RemovingAtRule"/> event.
  147. /// </summary>
  148. /// <remarks>
  149. /// Initializes a new instance of the <see cref="RemovingAtRuleEventArgs"/> class.
  150. /// </remarks>
  151. /// <param name="tag">The element containing the attribute.</param>
  152. /// <param name="rule">The rule to be removed.</param>
  153. public class RemovingAtRuleEventArgs(IElement tag, ICssRule rule) : CancelEventArgs
  154. {
  155. /// <summary>
  156. /// Gets the tag containing the at-rule to be removed.
  157. /// </summary>
  158. /// <value>
  159. /// The tag.
  160. /// </value>
  161. public IElement Tag { get; private set; } = tag;
  162. /// <summary>
  163. /// Gets the rule to be removed.
  164. /// </summary>
  165. /// <value>
  166. /// The rule.
  167. /// </value>
  168. public ICssRule Rule { get; private set; } = rule;
  169. }
  170. /// <summary>
  171. /// Provides data for the <see cref="HtmlSanitizer.RemovingComment"/> event.
  172. /// </summary>
  173. /// <remarks>
  174. /// Initializes a new instance of the <see cref="RemovingCommentEventArgs"/> class.
  175. /// </remarks>
  176. /// <param name="comment">The comment to be removed.</param>
  177. public class RemovingCommentEventArgs(IComment comment) : CancelEventArgs
  178. {
  179. /// <summary>
  180. /// Gets the comment node to be removed.
  181. /// </summary>
  182. /// <value>
  183. /// The comment node.
  184. /// </value>
  185. public IComment Comment { get; private set; } = comment;
  186. }
  187. /// <summary>
  188. /// Provides data for the <see cref="HtmlSanitizer.RemovingCssClass"/> event.
  189. /// </summary>
  190. /// <remarks>
  191. /// Initializes a new instance of the <see cref="RemovingCssClassEventArgs"/> class.
  192. /// </remarks>
  193. /// <param name="tag">The element containing the attribute.</param>
  194. /// <param name="cssClass">The CSS class to be removed.</param>
  195. /// <param name="reason">The reason why the attribute will be removed.</param>
  196. public class RemovingCssClassEventArgs(IElement tag, string cssClass, RemoveReason reason) : CancelEventArgs
  197. {
  198. /// <summary>
  199. /// Gets the tag containing the CSS class to be removed.
  200. /// </summary>
  201. /// <value>
  202. /// The tag.
  203. /// </value>
  204. public IElement Tag { get; private set; } = tag;
  205. /// <summary>
  206. /// Gets the CSS class to be removed.
  207. /// </summary>
  208. /// <value>
  209. /// The CSS class.
  210. /// </value>
  211. public string CssClass { get; private set; } = cssClass;
  212. /// <summary>
  213. /// Gets the reason why the CSS class will be removed.
  214. /// </summary>
  215. /// <value>
  216. /// The reason.
  217. /// </value>
  218. public RemoveReason Reason { get; private set; } = reason;
  219. }
  220. /// <summary>
  221. /// Provides data for the <see cref="HtmlSanitizer.FilterUrl"/> event.
  222. /// </summary>
  223. /// <remarks>
  224. /// Initializes a new instance of the <see cref="FilterUrlEventArgs"/> class.
  225. /// </remarks>
  226. /// <param name="tag">The tag containing the URI being sanitized.</param>
  227. /// <param name="originalUrl">The original URL.</param>
  228. /// <param name="sanitizedUrl">The sanitized URL.</param>
  229. public class FilterUrlEventArgs(IElement tag, string originalUrl, string? sanitizedUrl = null) : EventArgs
  230. {
  231. /// <summary>
  232. /// Gets the original URL.
  233. /// </summary>
  234. /// <value>
  235. /// The original URL.
  236. /// </value>
  237. public string OriginalUrl { get; private set; } = originalUrl;
  238. /// <summary>
  239. /// Gets or sets the sanitized URL.
  240. /// </summary>
  241. /// <value>
  242. /// The sanitized URL. If it is null, it will be removed.
  243. /// </value>
  244. public string? SanitizedUrl { get; set; } = sanitizedUrl;
  245. /// <summary>
  246. /// Gets the tag containing the URI being sanitized.
  247. /// </summary>
  248. /// <value>
  249. /// The tag.
  250. /// </value>
  251. public IElement Tag { get; private set; } = tag;
  252. }