@@ -99,6 +99,11 @@ public HtmlSanitizer(HtmlSanitizerOptions options)
99
99
AllowedAtRules = new HashSet < CssRuleType > ( options . AllowedAtRules ) ;
100
100
}
101
101
102
+ /// <summary>
103
+ /// Gets or sets the default <see cref="Action{IElement}"/> method that encodes literal text content.
104
+ /// </summary>
105
+ public Action < IElement > EncodeLiteralTextElementContent { get ; set ; } = DefaultEncodeLiteralTextElementContent ;
106
+
102
107
/// <summary>
103
108
/// Gets or sets the default value indicating whether to keep child nodes of elements that are removed. Default is false.
104
109
/// </summary>
@@ -465,6 +470,15 @@ private void RemoveComments(INode context)
465
470
}
466
471
}
467
472
473
+ private static void DefaultEncodeLiteralTextElementContent ( IElement tag )
474
+ {
475
+ var escapedHtml = tag . InnerHtml . Replace ( "<" , "<" ) . Replace ( ">" , ">" ) ;
476
+ if ( escapedHtml != tag . InnerHtml )
477
+ tag . InnerHtml = escapedHtml ;
478
+ if ( tag . InnerHtml != escapedHtml ) // setting InnerHtml does not work for noscript
479
+ tag . SetInnerText ( escapedHtml ) ;
480
+ }
481
+
468
482
private void DoSanitize ( IHtmlDocument dom , IParentNode context , string baseUrl = "" )
469
483
{
470
484
// remove disallowed tags
@@ -479,11 +493,7 @@ private void DoSanitize(IHtmlDocument dom, IParentNode context, string baseUrl =
479
493
&& t . Flags . HasFlag ( NodeFlags . LiteralText )
480
494
&& ! string . IsNullOrWhiteSpace ( t . InnerHtml ) ) )
481
495
{
482
- var escapedHtml = tag . InnerHtml . Replace ( "<" , "<" ) . Replace ( ">" , ">" ) ;
483
- if ( escapedHtml != tag . InnerHtml )
484
- tag . InnerHtml = escapedHtml ;
485
- if ( tag . InnerHtml != escapedHtml ) // setting InnerHtml does not work for noscript
486
- tag . SetInnerText ( escapedHtml ) ;
496
+ EncodeLiteralTextElementContent ( tag ) ;
487
497
}
488
498
489
499
SanitizeStyleSheets ( dom , baseUrl ) ;
0 commit comments