Skip to content

Commit f71dbb5

Browse files
committed
Fix sonarcloud issues
1 parent 08df3fc commit f71dbb5

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/HtmlSanitizer/HtmlSanitizer.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ namespace Ganss.Xss
5252
/// </example>
5353
public class HtmlSanitizer : IHtmlSanitizer
5454
{
55+
private const string StyleAttributeName = "style";
56+
5557
// from http://genshi.edgewall.org/
5658
private static readonly Regex CssUnicodeEscapes = new(@"\\([0-9a-fA-F]{1,6})\s?|\\([^\r\n\f0-9a-fA-F'""{};:()#*])", RegexOptions.Compiled);
5759
private static readonly Regex CssComments = new(@"/\*.*?\*/", RegexOptions.Compiled);
@@ -529,7 +531,7 @@ private void DoSanitize(IHtmlDocument dom, IParentNode context, string baseUrl =
529531
}
530532

531533
// sanitize the style attribute
532-
var oldStyleEmpty = string.IsNullOrEmpty(tag.GetAttribute("style"));
534+
var oldStyleEmpty = string.IsNullOrEmpty(tag.GetAttribute(StyleAttributeName));
533535
SanitizeStyle(tag, baseUrl);
534536

535537
// sanitize the value of the attributes
@@ -553,7 +555,7 @@ private void DoSanitize(IHtmlDocument dom, IParentNode context, string baseUrl =
553555
if (!tag.ClassList.Any())
554556
RemoveAttribute(tag, attribute, RemoveReason.ClassAttributeEmpty);
555557
}
556-
else if (!oldStyleEmpty && attribute.Name == "style" && string.IsNullOrEmpty(attribute.Value))
558+
else if (!oldStyleEmpty && attribute.Name == StyleAttributeName && string.IsNullOrEmpty(attribute.Value))
557559
{
558560
RemoveAttribute(tag, attribute, RemoveReason.StyleAttributeEmpty);
559561
}
@@ -574,8 +576,9 @@ private void SanitizeStyleSheets(IHtmlDocument dom, string baseUrl)
574576
foreach (var styleSheet in dom.StyleSheets.OfType<ICssStyleSheet>())
575577
{
576578
var styleTag = styleSheet.OwnerNode;
579+
var i = 0;
577580

578-
for (int i = 0; i < styleSheet.Rules.Length;)
581+
while (i < styleSheet.Rules.Length)
579582
{
580583
var rule = styleSheet.Rules[i];
581584
if (!SanitizeStyleRule(rule, styleTag, baseUrl) && RemoveAtRule(styleTag, rule))
@@ -599,7 +602,9 @@ private bool SanitizeStyleRule(ICssRule rule, IElement styleTag, string baseUrl)
599602
{
600603
if (rule is ICssGroupingRule groupingRule)
601604
{
602-
for (int i = 0; i < groupingRule.Rules.Length;)
605+
var i = 0;
606+
607+
while (i < groupingRule.Rules.Length)
603608
{
604609
var childRule = groupingRule.Rules[i];
605610
if (!SanitizeStyleRule(childRule, styleTag, baseUrl) && RemoveAtRule(styleTag, childRule))
@@ -702,17 +707,17 @@ protected void SanitizeStyle(IElement element, string baseUrl)
702707
{
703708
// filter out invalid CSS declarations
704709
// see https://github.com/AngleSharp/AngleSharp/issues/101
705-
var attribute = element.GetAttribute("style");
710+
var attribute = element.GetAttribute(StyleAttributeName);
706711
if (attribute == null)
707712
return;
708713

709714
if (element.GetStyle() == null)
710715
{
711-
element.RemoveAttribute("style");
716+
element.RemoveAttribute(StyleAttributeName);
712717
return;
713718
}
714719

715-
element.SetAttribute("style", element.GetStyle().ToCss(StyleFormatter));
720+
element.SetAttribute(StyleAttributeName, element.GetStyle().ToCss(StyleFormatter));
716721

717722
var styles = element.GetStyle();
718723
if (styles == null || styles.Length == 0)

0 commit comments

Comments
 (0)