Skip to content

Commit 08df3fc

Browse files
committed
Use primary constructors
1 parent 7d6fc5e commit 08df3fc

File tree

2 files changed

+82
-145
lines changed

2 files changed

+82
-145
lines changed

src/HtmlSanitizer/EventArgs.cs

+74-131
Original file line numberDiff line numberDiff line change
@@ -10,323 +10,266 @@ namespace Ganss.Xss
1010
/// <summary>
1111
/// Provides data for the <see cref="HtmlSanitizer.PostProcessDom"/> event.
1212
/// </summary>
13-
public class PostProcessDomEventArgs : EventArgs
13+
/// <remarks>
14+
/// Initializes a new instance of the <see cref="PostProcessDomEventArgs"/> class.
15+
/// </remarks>
16+
public class PostProcessDomEventArgs(IHtmlDocument document) : EventArgs
1417
{
1518
/// <summary>
1619
/// Gets the document.
1720
/// </summary>
1821
/// <value>
1922
/// The document.
2023
/// </value>
21-
public IHtmlDocument Document { get; private set; }
22-
23-
/// <summary>
24-
/// Initializes a new instance of the <see cref="PostProcessDomEventArgs"/> class.
25-
/// </summary>
26-
public PostProcessDomEventArgs(IHtmlDocument document)
27-
{
28-
Document = document;
29-
}
24+
public IHtmlDocument Document { get; private set; } = document;
3025
}
3126

3227
/// <summary>
3328
/// Provides data for the <see cref="HtmlSanitizer.PostProcessNode"/> event.
3429
/// </summary>
35-
public class PostProcessNodeEventArgs : EventArgs
30+
/// <remarks>
31+
/// Initializes a new instance of the <see cref="PostProcessNodeEventArgs"/> class.
32+
/// </remarks>
33+
public class PostProcessNodeEventArgs(IHtmlDocument document, INode node) : EventArgs
3634
{
3735
/// <summary>
3836
/// Gets the document.
3937
/// </summary>
4038
/// <value>
4139
/// The document.
4240
/// </value>
43-
public IHtmlDocument Document { get; private set; }
41+
public IHtmlDocument Document { get; private set; } = document;
4442

4543
/// <summary>
4644
/// Gets the DOM node to be processed.
4745
/// </summary>
4846
/// <value>
4947
/// The DOM node.
5048
/// </value>
51-
public INode Node { get; private set; }
49+
public INode Node { get; private set; } = node;
5250

5351
/// <summary>
5452
/// Gets the replacement nodes. Leave empty if no replacement should occur.
5553
/// </summary>
5654
/// <value>
5755
/// The replacement nodes.
5856
/// </value>
59-
public ICollection<INode> ReplacementNodes { get; private set; }
60-
61-
/// <summary>
62-
/// Initializes a new instance of the <see cref="PostProcessNodeEventArgs"/> class.
63-
/// </summary>
64-
public PostProcessNodeEventArgs(IHtmlDocument document, INode node)
65-
{
66-
Document = document;
67-
Node = node;
68-
ReplacementNodes = new List<INode>();
69-
}
57+
public ICollection<INode> ReplacementNodes { get; private set; } = new List<INode>();
7058
}
7159

7260
/// <summary>
7361
/// Provides data for the <see cref="HtmlSanitizer.RemovingTag"/> event.
7462
/// </summary>
75-
public class RemovingTagEventArgs : CancelEventArgs
63+
/// <remarks>
64+
/// Initializes a new instance of the <see cref="RemovingTagEventArgs"/> class.
65+
/// </remarks>
66+
/// <param name="tag">The element to be removed.</param>
67+
/// <param name="reason">The reason why the tag will be removed.</param>
68+
public class RemovingTagEventArgs(IElement tag, RemoveReason reason) : CancelEventArgs
7669
{
7770
/// <summary>
7871
/// Gets the tag to be removed.
7972
/// </summary>
8073
/// <value>
8174
/// The tag.
8275
/// </value>
83-
public IElement Tag { get; private set; }
76+
public IElement Tag { get; private set; } = tag;
8477

8578
/// <summary>
8679
/// Gets the reason why the tag will be removed.
8780
/// </summary>
8881
/// <value>
8982
/// The reason.
9083
/// </value>
91-
public RemoveReason Reason { get; private set; }
92-
93-
/// <summary>
94-
/// Initializes a new instance of the <see cref="RemovingTagEventArgs"/> class.
95-
/// </summary>
96-
/// <param name="tag">The element to be removed.</param>
97-
/// <param name="reason">The reason why the tag will be removed.</param>
98-
public RemovingTagEventArgs(IElement tag, RemoveReason reason)
99-
{
100-
Tag = tag;
101-
Reason = reason;
102-
}
84+
public RemoveReason Reason { get; private set; } = reason;
10385
}
10486

10587
/// <summary>
10688
/// Provides data for the <see cref="HtmlSanitizer.RemovingAttribute"/> event.
10789
/// </summary>
108-
public class RemovingAttributeEventArgs : CancelEventArgs
90+
/// <remarks>
91+
/// Initializes a new instance of the <see cref="RemovingAttributeEventArgs"/> class.
92+
/// </remarks>
93+
/// <param name="tag">The element containing the attribute.</param>
94+
/// <param name="attribute">The attribute to be removed.</param>
95+
/// <param name="reason">The reason why the attribute will be removed.</param>
96+
public class RemovingAttributeEventArgs(IElement tag, IAttr attribute, RemoveReason reason) : CancelEventArgs
10997
{
11098
/// <summary>
11199
/// Gets the tag containing the attribute to be removed.
112100
/// </summary>
113101
/// <value>
114102
/// The tag.
115103
/// </value>
116-
public IElement Tag { get; private set; }
104+
public IElement Tag { get; private set; } = tag;
117105

118106
/// <summary>
119107
/// Gets the attribute to be removed.
120108
/// </summary>
121109
/// <value>
122110
/// The attribute.
123111
/// </value>
124-
public IAttr Attribute { get; private set; }
112+
public IAttr Attribute { get; private set; } = attribute;
125113

126114
/// <summary>
127115
/// Gets the reason why the attribute will be removed.
128116
/// </summary>
129117
/// <value>
130118
/// The reason.
131119
/// </value>
132-
public RemoveReason Reason { get; private set; }
133-
134-
/// <summary>
135-
/// Initializes a new instance of the <see cref="RemovingAttributeEventArgs"/> class.
136-
/// </summary>
137-
/// <param name="tag">The element containing the attribute.</param>
138-
/// <param name="attribute">The attribute to be removed.</param>
139-
/// <param name="reason">The reason why the attribute will be removed.</param>
140-
public RemovingAttributeEventArgs(IElement tag, IAttr attribute, RemoveReason reason)
141-
{
142-
Tag = tag;
143-
Attribute = attribute;
144-
Reason = reason;
145-
}
120+
public RemoveReason Reason { get; private set; } = reason;
146121
}
147122

148123
/// <summary>
149124
/// Provides data for the <see cref="HtmlSanitizer.RemovingStyle"/> event.
150125
/// </summary>
151-
public class RemovingStyleEventArgs : CancelEventArgs
126+
/// <remarks>
127+
/// Initializes a new instance of the <see cref="RemovingStyleEventArgs"/> class.
128+
/// </remarks>
129+
/// <param name="tag">The element containing the attribute.</param>
130+
/// <param name="style">The style to be removed.</param>
131+
/// <param name="reason">The reason why the attribute will be removed.</param>
132+
public class RemovingStyleEventArgs(IElement tag, ICssProperty style, RemoveReason reason) : CancelEventArgs
152133
{
153134
/// <summary>
154135
/// Gets the tag containing the style to be removed.
155136
/// </summary>
156137
/// <value>
157138
/// The tag.
158139
/// </value>
159-
public IElement Tag { get; private set; }
140+
public IElement Tag { get; private set; } = tag;
160141

161142
/// <summary>
162143
/// Gets the style to be removed.
163144
/// </summary>
164145
/// <value>
165146
/// The style.
166147
/// </value>
167-
public ICssProperty Style { get; private set; }
148+
public ICssProperty Style { get; private set; } = style;
168149

169150
/// <summary>
170151
/// Gets the reason why the style will be removed.
171152
/// </summary>
172153
/// <value>
173154
/// The reason.
174155
/// </value>
175-
public RemoveReason Reason { get; private set; }
176-
177-
/// <summary>
178-
/// Initializes a new instance of the <see cref="RemovingStyleEventArgs"/> class.
179-
/// </summary>
180-
/// <param name="tag">The element containing the attribute.</param>
181-
/// <param name="style">The style to be removed.</param>
182-
/// <param name="reason">The reason why the attribute will be removed.</param>
183-
public RemovingStyleEventArgs(IElement tag, ICssProperty style, RemoveReason reason)
184-
{
185-
Tag = tag;
186-
Style = style;
187-
Reason = reason;
188-
}
156+
public RemoveReason Reason { get; private set; } = reason;
189157
}
190158

191159
/// <summary>
192160
/// Provides data for the <see cref="HtmlSanitizer.RemovingAtRule"/> event.
193161
/// </summary>
194-
public class RemovingAtRuleEventArgs : CancelEventArgs
162+
/// <remarks>
163+
/// Initializes a new instance of the <see cref="RemovingAtRuleEventArgs"/> class.
164+
/// </remarks>
165+
/// <param name="tag">The element containing the attribute.</param>
166+
/// <param name="rule">The rule to be removed.</param>
167+
public class RemovingAtRuleEventArgs(IElement tag, ICssRule rule) : CancelEventArgs
195168
{
196169
/// <summary>
197170
/// Gets the tag containing the at-rule to be removed.
198171
/// </summary>
199172
/// <value>
200173
/// The tag.
201174
/// </value>
202-
public IElement Tag { get; private set; }
175+
public IElement Tag { get; private set; } = tag;
203176

204177
/// <summary>
205178
/// Gets the rule to be removed.
206179
/// </summary>
207180
/// <value>
208181
/// The rule.
209182
/// </value>
210-
public ICssRule Rule { get; private set; }
211-
212-
/// <summary>
213-
/// Initializes a new instance of the <see cref="RemovingAtRuleEventArgs"/> class.
214-
/// </summary>
215-
/// <param name="tag">The element containing the attribute.</param>
216-
/// <param name="rule">The rule to be removed.</param>
217-
public RemovingAtRuleEventArgs(IElement tag, ICssRule rule)
218-
{
219-
Tag = tag;
220-
Rule = rule;
221-
}
183+
public ICssRule Rule { get; private set; } = rule;
222184
}
223185

224186
/// <summary>
225187
/// Provides data for the <see cref="HtmlSanitizer.RemovingComment"/> event.
226188
/// </summary>
227-
public class RemovingCommentEventArgs : CancelEventArgs
189+
/// <remarks>
190+
/// Initializes a new instance of the <see cref="RemovingCommentEventArgs"/> class.
191+
/// </remarks>
192+
/// <param name="comment">The comment to be removed.</param>
193+
public class RemovingCommentEventArgs(IComment comment) : CancelEventArgs
228194
{
229195
/// <summary>
230196
/// Gets the comment node to be removed.
231197
/// </summary>
232198
/// <value>
233199
/// The comment node.
234200
/// </value>
235-
public IComment Comment { get; private set; }
236-
237-
/// <summary>
238-
/// Initializes a new instance of the <see cref="RemovingCommentEventArgs"/> class.
239-
/// </summary>
240-
/// <param name="comment">The comment to be removed.</param>
241-
public RemovingCommentEventArgs(IComment comment)
242-
{
243-
Comment = comment;
244-
}
201+
public IComment Comment { get; private set; } = comment;
245202
}
246203

247204
/// <summary>
248205
/// Provides data for the <see cref="HtmlSanitizer.RemovingCssClass"/> event.
249206
/// </summary>
250-
public class RemovingCssClassEventArgs : CancelEventArgs
207+
/// <remarks>
208+
/// Initializes a new instance of the <see cref="RemovingCssClassEventArgs"/> class.
209+
/// </remarks>
210+
/// <param name="tag">The element containing the attribute.</param>
211+
/// <param name="cssClass">The CSS class to be removed.</param>
212+
/// <param name="reason">The reason why the attribute will be removed.</param>
213+
public class RemovingCssClassEventArgs(IElement tag, string cssClass, RemoveReason reason) : CancelEventArgs
251214
{
252215
/// <summary>
253216
/// Gets the tag containing the CSS class to be removed.
254217
/// </summary>
255218
/// <value>
256219
/// The tag.
257220
/// </value>
258-
public IElement Tag { get; private set; }
221+
public IElement Tag { get; private set; } = tag;
259222

260223
/// <summary>
261224
/// Gets the CSS class to be removed.
262225
/// </summary>
263226
/// <value>
264227
/// The CSS class.
265228
/// </value>
266-
public string CssClass { get; private set; }
229+
public string CssClass { get; private set; } = cssClass;
267230

268231
/// <summary>
269232
/// Gets the reason why the CSS class will be removed.
270233
/// </summary>
271234
/// <value>
272235
/// The reason.
273236
/// </value>
274-
public RemoveReason Reason { get; private set; }
275-
276-
/// <summary>
277-
/// Initializes a new instance of the <see cref="RemovingCssClassEventArgs"/> class.
278-
/// </summary>
279-
/// <param name="tag">The element containing the attribute.</param>
280-
/// <param name="cssClass">The CSS class to be removed.</param>
281-
/// <param name="reason">The reason why the attribute will be removed.</param>
282-
public RemovingCssClassEventArgs(IElement tag, string cssClass, RemoveReason reason)
283-
{
284-
Tag = tag;
285-
CssClass = cssClass;
286-
Reason = reason;
287-
}
237+
public RemoveReason Reason { get; private set; } = reason;
288238
}
289239

290240
/// <summary>
291241
/// Provides data for the <see cref="HtmlSanitizer.FilterUrl"/> event.
292242
/// </summary>
293-
public class FilterUrlEventArgs: EventArgs
243+
/// <remarks>
244+
/// Initializes a new instance of the <see cref="FilterUrlEventArgs"/> class.
245+
/// </remarks>
246+
/// <param name="tag">The tag containing the URI being sanitized.</param>
247+
/// <param name="originalUrl">The original URL.</param>
248+
/// <param name="sanitizedUrl">The sanitized URL.</param>
249+
public class FilterUrlEventArgs(IElement tag, string originalUrl, string? sanitizedUrl = null) : EventArgs
294250
{
295251
/// <summary>
296252
/// Gets the original URL.
297253
/// </summary>
298254
/// <value>
299255
/// The original URL.
300256
/// </value>
301-
public string OriginalUrl { get; private set; }
257+
public string OriginalUrl { get; private set; } = originalUrl;
302258

303259
/// <summary>
304260
/// Gets or sets the sanitized URL.
305261
/// </summary>
306262
/// <value>
307263
/// The sanitized URL. If it is null, it will be removed.
308264
/// </value>
309-
public string? SanitizedUrl { get; set; }
265+
public string? SanitizedUrl { get; set; } = sanitizedUrl;
310266

311267
/// <summary>
312268
/// Gets the tag containing the URI being sanitized.
313269
/// </summary>
314270
/// <value>
315271
/// The tag.
316272
/// </value>
317-
public IElement Tag { get; private set; }
318-
319-
/// <summary>
320-
/// Initializes a new instance of the <see cref="FilterUrlEventArgs"/> class.
321-
/// </summary>
322-
/// <param name="tag">The tag containing the URI being sanitized.</param>
323-
/// <param name="originalUrl">The original URL.</param>
324-
/// <param name="sanitizedUrl">The sanitized URL.</param>
325-
public FilterUrlEventArgs(IElement tag, string originalUrl, string? sanitizedUrl = null)
326-
{
327-
OriginalUrl = originalUrl;
328-
SanitizedUrl = sanitizedUrl;
329-
Tag = tag;
330-
}
273+
public IElement Tag { get; private set; } = tag;
331274
}
332275
}

0 commit comments

Comments
 (0)