Skip to content

Commit 9254a13

Browse files
committed
Create customizable comment encoding method (fixes #525)
1 parent 8a62437 commit 9254a13

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/HtmlSanitizer/HtmlSanitizer.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public HtmlSanitizer(HtmlSanitizerOptions options)
9999
AllowedAtRules = new HashSet<CssRuleType>(options.AllowedAtRules);
100100
}
101101

102+
/// <summary>
103+
/// Gets or sets the default <see cref="Action{IComment}"/> method that encodes comments.
104+
/// </summary>
105+
public Action<IComment> EncodeComment { get; set; } = DefaultEncodeComment;
106+
102107
/// <summary>
103108
/// Gets or sets the default <see cref="Action{IElement}"/> method that encodes literal text content.
104109
/// </summary>
@@ -458,9 +463,7 @@ private void RemoveComments(INode context)
458463
{
459464
foreach (var comment in GetAllNodes(context).OfType<IComment>().ToList())
460465
{
461-
var escapedText = comment.TextContent.Replace("<", "&lt;").Replace(">", "&gt;");
462-
if (escapedText != comment.TextContent)
463-
comment.TextContent = escapedText;
466+
EncodeComment(comment);
464467

465468
var e = new RemovingCommentEventArgs(comment);
466469
OnRemovingComment(e);
@@ -470,6 +473,13 @@ private void RemoveComments(INode context)
470473
}
471474
}
472475

476+
private static void DefaultEncodeComment(IComment comment)
477+
{
478+
var escapedText = comment.TextContent.Replace("<", "&lt;").Replace(">", "&gt;");
479+
if (escapedText != comment.TextContent)
480+
comment.TextContent = escapedText;
481+
}
482+
473483
private static void DefaultEncodeLiteralTextElementContent(IElement tag)
474484
{
475485
var escapedHtml = tag.InnerHtml.Replace("<", "&lt;").Replace(">", "&gt;");

0 commit comments

Comments
 (0)