Skip to content

Commit

Permalink
Add OutputFormatter and DefaultOutputFormatter.
Browse files Browse the repository at this point in the history
Closes #50
  • Loading branch information
mganss committed Aug 1, 2016
1 parent 1136674 commit 3c02cec
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/HtmlSanitizer/HtmlSanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public HtmlSanitizer(IEnumerable<string> allowedTags = null, IEnumerable<string>
AllowedAtRules = new HashSet<CssRuleType>(DefaultAllowedAtRules);
}

/// <summary>
/// Gets or sets the default <see cref="IMarkupFormatter"/> object used for generating output. Default is <see cref="HtmlMarkupFormatter.Instance"/>.
/// </summary>
public static IMarkupFormatter DefaultOutputFormatter { get; set; } = HtmlMarkupFormatter.Instance;

/// <summary>
/// Gets or sets the <see cref="IMarkupFormatter"/> object used for generating output. Default is <see cref="DefaultOutputFormatter"/>.
/// </summary>
public IMarkupFormatter OutputFormatter { get; set; } = DefaultOutputFormatter;

/// <summary>
/// Gets or sets the allowed CSS at-rules such as "@media" and "@font-face".
/// </summary>
Expand Down Expand Up @@ -349,17 +359,17 @@ private static IEnumerable<INode> GetAllNodes(INode dom)
/// </summary>
/// <param name="html">The HTML body fragment to sanitize.</param>
/// <param name="baseUrl">The base URL relative URLs are resolved against. No resolution if empty.</param>
/// <param name="outputFormatter">The formatter used to render the DOM. Using the default formatter if null.</param>
/// <param name="outputFormatter">The formatter used to render the DOM. Using the <see cref="OutputFormatter"/> if null.</param>
/// <returns>The sanitized HTML body fragment.</returns>
public string Sanitize(string html, string baseUrl = "", IMarkupFormatter outputFormatter = null)
{
var parser = CreateParser();
var dom = parser.Parse("<html><body></body></html>");
dom.Body.InnerHtml = html;

DoSanitize(dom, dom.Body, baseUrl, outputFormatter);
DoSanitize(dom, dom.Body, baseUrl);

var output = dom.Body.ChildNodes.ToHtml(outputFormatter ?? HtmlMarkupFormatter.Instance);
var output = dom.Body.ChildNodes.ToHtml(outputFormatter ?? OutputFormatter);

return output;
}
Expand All @@ -369,16 +379,16 @@ public string Sanitize(string html, string baseUrl = "", IMarkupFormatter output
/// </summary>
/// <param name="html">The HTML document to sanitize.</param>
/// <param name="baseUrl">The base URL relative URLs are resolved against. No resolution if empty.</param>
/// <param name="outputFormatter">The formatter used to render the DOM. Using the default formatter if null.</param>
/// <param name="outputFormatter">The formatter used to render the DOM. Using the <see cref="OutputFormatter"/> if null.</param>
/// <returns>The sanitized HTML document.</returns>
public string SanitizeDocument(string html, string baseUrl = "", IMarkupFormatter outputFormatter = null)
{
var parser = CreateParser();
var dom = parser.Parse(html);

DoSanitize(dom, dom.DocumentElement, baseUrl, outputFormatter);
DoSanitize(dom, dom.DocumentElement, baseUrl);

var output = dom.ToHtml(outputFormatter ?? HtmlMarkupFormatter.Instance);
var output = dom.ToHtml(outputFormatter ?? OutputFormatter);

return output;
}
Expand Down Expand Up @@ -408,7 +418,7 @@ private static void RemoveComments(List<INode> nodes)
comment.Remove();
}

private void DoSanitize(IHtmlDocument dom, IElement context, string baseUrl = "", IMarkupFormatter outputFormatter = null)
private void DoSanitize(IHtmlDocument dom, IElement context, string baseUrl = "")
{
// remove non-whitelisted tags
foreach (var tag in context.QuerySelectorAll("*").Where(t => !IsAllowedTag(t)).ToList())
Expand Down

0 comments on commit 3c02cec

Please sign in to comment.