mganss / HtmlSanitizer

Cleans HTML to avoid XSS attacks
MIT License
1.52k stars 198 forks source link

Any option to keep the self closing tag? #443

Closed tlyau62 closed 1 year ago

tlyau62 commented 1 year ago

For example the html <br /> is formatted with <br> as output. Is it possible to keep the /?

tlyau62 commented 1 year ago

looks like it's more related to the AngleSharp

tiesont commented 1 year ago

looks like it's more related to the AngleSharp

Correct. If an example helps, this is a method I created in a helper/utility class:

public static readonly string[] MinimumTags = { "b", "i", "u", "em", "strong", "q" };

public static string SanitizeReduceMarkup(string markup)
{
    if (!string.IsNullOrWhiteSpace(markup))
    {
        var options = new HtmlSanitizerOptions
        {
        AllowedTags = new HashSet<string>(MinimumTags)
        };

        var sanitizer = new HtmlSanitizer(options);
        markup = sanitizer.Sanitize(markup, outputFormatter: AngleSharp.Xhtml.XhtmlMarkupFormatter.Instance);
    }

    return markup;
}

AngleSharp.Xhtml.XhtmlMarkupFormatter.Instance replaces the default HTML formatter with one that follows XML rules. This is also covered in the wiki: https://github.com/mganss/HtmlSanitizer/wiki/Examples#ex3-replacing-the-default-formatter

tlyau62 commented 1 year ago

Great👍 Thank you