Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added DefaultHtmlEncoder property to allow specifying the HtmlEncoder… #50

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion HtmlSanitizer.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CsQuery;
using CsQuery;
using Ganss.Text;
using NUnit.Framework;
using System;
Expand Down Expand Up @@ -2295,6 +2295,21 @@ public void QuotedBackgroundImageFromIE9()
var expected = "<span style='background-image: url(\"/api/users/defaultAvatar\")'></span>";
Assert.That(actual, Is.EqualTo(expected).IgnoreCase);
}

[Test]
public void HtmlEncoder_ShouldBeOverridable()
{
// Arrange
var s = new HtmlSanitizer
{
HtmlEncoder = HtmlEncoders.Minimum
};

// Act

Assert.That(s.Sanitize("é"), Is.EqualTo("é"));
Assert.That(s.Sanitize("€"), Is.EqualTo("€"));
}
}
}

Expand Down
13 changes: 12 additions & 1 deletion HtmlSanitizer/HtmlSanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ public HtmlSanitizer(IEnumerable<string> allowedTags = null, IEnumerable<string>
/// </value>
public ISet<string> AllowedCssProperties { get; private set; }

/// <summary>
/// Gets or sets the default HTML encoder that will be used to encode final output.
/// </summary>
/// <value>
/// The HtmlEncoder implementation.
/// </value>
public IHtmlEncoder HtmlEncoder { get; set; }

/// <summary>
/// The default allowed CSS properties.
/// </summary>
Expand Down Expand Up @@ -373,8 +381,11 @@ public string Sanitize(string html, string baseUrl = "", IOutputFormatter output
}
}

if (HtmlEncoder == null)
HtmlEncoder = HtmlEncoders.Default;

if (outputFormatter == null)
outputFormatter = new FormatDefault(DomRenderingOptions.RemoveComments | DomRenderingOptions.QuoteAllAttributes, HtmlEncoders.Default);
outputFormatter = new FormatDefault(DomRenderingOptions.RemoveComments | DomRenderingOptions.QuoteAllAttributes, HtmlEncoder);

var output = dom.Render(outputFormatter);

Expand Down