Skip to content

Commit

Permalink
Fix sonarcloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mganss committed Apr 16, 2024
1 parent 08df3fc commit f71dbb5
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/HtmlSanitizer/HtmlSanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ namespace Ganss.Xss
/// </example>
public class HtmlSanitizer : IHtmlSanitizer
{
private const string StyleAttributeName = "style";

// from http://genshi.edgewall.org/
private static readonly Regex CssUnicodeEscapes = new(@"\\([0-9a-fA-F]{1,6})\s?|\\([^\r\n\f0-9a-fA-F'""{};:()#*])", RegexOptions.Compiled);
private static readonly Regex CssComments = new(@"/\*.*?\*/", RegexOptions.Compiled);
Expand Down Expand Up @@ -529,7 +531,7 @@ private void DoSanitize(IHtmlDocument dom, IParentNode context, string baseUrl =
}

// sanitize the style attribute
var oldStyleEmpty = string.IsNullOrEmpty(tag.GetAttribute("style"));
var oldStyleEmpty = string.IsNullOrEmpty(tag.GetAttribute(StyleAttributeName));
SanitizeStyle(tag, baseUrl);

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

for (int i = 0; i < styleSheet.Rules.Length;)
while (i < styleSheet.Rules.Length)
{
var rule = styleSheet.Rules[i];
if (!SanitizeStyleRule(rule, styleTag, baseUrl) && RemoveAtRule(styleTag, rule))
Expand All @@ -599,7 +602,9 @@ private bool SanitizeStyleRule(ICssRule rule, IElement styleTag, string baseUrl)
{
if (rule is ICssGroupingRule groupingRule)
{
for (int i = 0; i < groupingRule.Rules.Length;)
var i = 0;

while (i < groupingRule.Rules.Length)
{
var childRule = groupingRule.Rules[i];
if (!SanitizeStyleRule(childRule, styleTag, baseUrl) && RemoveAtRule(styleTag, childRule))
Expand Down Expand Up @@ -702,17 +707,17 @@ protected void SanitizeStyle(IElement element, string baseUrl)
{
// filter out invalid CSS declarations
// see https://github.com/AngleSharp/AngleSharp/issues/101
var attribute = element.GetAttribute("style");
var attribute = element.GetAttribute(StyleAttributeName);
if (attribute == null)
return;

if (element.GetStyle() == null)
{
element.RemoveAttribute("style");
element.RemoveAttribute(StyleAttributeName);
return;
}

element.SetAttribute("style", element.GetStyle().ToCss(StyleFormatter));
element.SetAttribute(StyleAttributeName, element.GetStyle().ToCss(StyleFormatter));

var styles = element.GetStyle();
if (styles == null || styles.Length == 0)
Expand Down

0 comments on commit f71dbb5

Please sign in to comment.