@@ -52,6 +52,8 @@ namespace Ganss.Xss
52
52
/// </example>
53
53
public class HtmlSanitizer : IHtmlSanitizer
54
54
{
55
+ private const string StyleAttributeName = "style" ;
56
+
55
57
// from http://genshi.edgewall.org/
56
58
private static readonly Regex CssUnicodeEscapes = new ( @"\\([0-9a-fA-F]{1,6})\s?|\\([^\r\n\f0-9a-fA-F'""{};:()#*])" , RegexOptions . Compiled ) ;
57
59
private static readonly Regex CssComments = new ( @"/\*.*?\*/" , RegexOptions . Compiled ) ;
@@ -529,7 +531,7 @@ private void DoSanitize(IHtmlDocument dom, IParentNode context, string baseUrl =
529
531
}
530
532
531
533
// sanitize the style attribute
532
- var oldStyleEmpty = string . IsNullOrEmpty ( tag . GetAttribute ( "style" ) ) ;
534
+ var oldStyleEmpty = string . IsNullOrEmpty ( tag . GetAttribute ( StyleAttributeName ) ) ;
533
535
SanitizeStyle ( tag , baseUrl ) ;
534
536
535
537
// sanitize the value of the attributes
@@ -553,7 +555,7 @@ private void DoSanitize(IHtmlDocument dom, IParentNode context, string baseUrl =
553
555
if ( ! tag . ClassList . Any ( ) )
554
556
RemoveAttribute ( tag , attribute , RemoveReason . ClassAttributeEmpty ) ;
555
557
}
556
- else if ( ! oldStyleEmpty && attribute . Name == "style" && string . IsNullOrEmpty ( attribute . Value ) )
558
+ else if ( ! oldStyleEmpty && attribute . Name == StyleAttributeName && string . IsNullOrEmpty ( attribute . Value ) )
557
559
{
558
560
RemoveAttribute ( tag , attribute , RemoveReason . StyleAttributeEmpty ) ;
559
561
}
@@ -574,8 +576,9 @@ private void SanitizeStyleSheets(IHtmlDocument dom, string baseUrl)
574
576
foreach ( var styleSheet in dom . StyleSheets . OfType < ICssStyleSheet > ( ) )
575
577
{
576
578
var styleTag = styleSheet . OwnerNode ;
579
+ var i = 0 ;
577
580
578
- for ( int i = 0 ; i < styleSheet . Rules . Length ; )
581
+ while ( i < styleSheet . Rules . Length )
579
582
{
580
583
var rule = styleSheet . Rules [ i ] ;
581
584
if ( ! SanitizeStyleRule ( rule , styleTag , baseUrl ) && RemoveAtRule ( styleTag , rule ) )
@@ -599,7 +602,9 @@ private bool SanitizeStyleRule(ICssRule rule, IElement styleTag, string baseUrl)
599
602
{
600
603
if ( rule is ICssGroupingRule groupingRule )
601
604
{
602
- for ( int i = 0 ; i < groupingRule . Rules . Length ; )
605
+ var i = 0 ;
606
+
607
+ while ( i < groupingRule . Rules . Length )
603
608
{
604
609
var childRule = groupingRule . Rules [ i ] ;
605
610
if ( ! SanitizeStyleRule ( childRule , styleTag , baseUrl ) && RemoveAtRule ( styleTag , childRule ) )
@@ -702,17 +707,17 @@ protected void SanitizeStyle(IElement element, string baseUrl)
702
707
{
703
708
// filter out invalid CSS declarations
704
709
// see https://github.com/AngleSharp/AngleSharp/issues/101
705
- var attribute = element . GetAttribute ( "style" ) ;
710
+ var attribute = element . GetAttribute ( StyleAttributeName ) ;
706
711
if ( attribute == null )
707
712
return ;
708
713
709
714
if ( element . GetStyle ( ) == null )
710
715
{
711
- element . RemoveAttribute ( "style" ) ;
716
+ element . RemoveAttribute ( StyleAttributeName ) ;
712
717
return ;
713
718
}
714
719
715
- element . SetAttribute ( "style" , element . GetStyle ( ) . ToCss ( StyleFormatter ) ) ;
720
+ element . SetAttribute ( StyleAttributeName , element . GetStyle ( ) . ToCss ( StyleFormatter ) ) ;
716
721
717
722
var styles = element . GetStyle ( ) ;
718
723
if ( styles == null || styles . Length == 0 )
0 commit comments