EventArgs.cs 9.1 KB

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