Skip to content

Duplicate css rule with !important is sanitized #587

@prostakov

Description

@prostakov

I am having this one speciifc problem of duplicate css rules being sanitized.

Example 1:

var sanitizer = new HtmlSanitizer(new HtmlSanitizerOptions
{
    AllowedTags = new HashSet<string> { "style" },
    AllowedAtRules = new HashSet<CssRuleType> { CssRuleType.Style },
    AllowedCssProperties = new HashSet<string> { "font-size", "padding", "padding-bottom", "padding-left", "padding-right", "padding-top" }
});

const string input =
    """
    <style>
        .someClass {
            padding: 20px !important;
            font-size: 20px;
            padding: 0;
        }
    </style>
    """;

var output = sanitizer.Sanitize(input);

The output is:

<style>.someClass { padding: 0; font-size: 20px }</style>

Example 2:

var sanitizer = new HtmlSanitizer(new HtmlSanitizerOptions
{
    AllowedTags = new HashSet<string> { "p" },
    AllowedAttributes = new HashSet<string> { "style" },
    AllowedAtRules = new HashSet<CssRuleType> { CssRuleType.Style },
    AllowedCssProperties = new HashSet<string> { "font-size", "padding", "padding-bottom", "padding-left", "padding-right", "padding-top" }
});

const string input =
    """
    <p style="padding: 20px !important; font-size: 20px; padding: 0;">Test</p>
    """;

var output = sanitizer.Sanitize(input);

The output is:

<p style="padding: 0; font-size: 20px">Test</p>

In both cases we sanitize the first padding, even though it is marked as !important.

If it hadn't been sanitized - on the actual render the rule would've take precedence, because it's marked as !important.

I am wondering is there maybe a setting to not remove the duplicates on sanitize.

Or maybe there is some config that I have missed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions