diff --git a/src/HtmlSanitizer/HtmlSanitizer.cs b/src/HtmlSanitizer/HtmlSanitizer.cs index 42979c1..f70bf64 100644 --- a/src/HtmlSanitizer/HtmlSanitizer.cs +++ b/src/HtmlSanitizer/HtmlSanitizer.cs @@ -70,6 +70,16 @@ public HtmlSanitizer(IEnumerable allowedTags = null, IEnumerable AllowedAtRules = new HashSet(DefaultAllowedAtRules); } + /// + /// Gets or sets the default object used for generating output. Default is . + /// + public static IMarkupFormatter DefaultOutputFormatter { get; set; } = HtmlMarkupFormatter.Instance; + + /// + /// Gets or sets the object used for generating output. Default is . + /// + public IMarkupFormatter OutputFormatter { get; set; } = DefaultOutputFormatter; + /// /// Gets or sets the allowed CSS at-rules such as "@media" and "@font-face". /// @@ -349,7 +359,7 @@ private static IEnumerable GetAllNodes(INode dom) /// /// The HTML body fragment to sanitize. /// The base URL relative URLs are resolved against. No resolution if empty. - /// The formatter used to render the DOM. Using the default formatter if null. + /// The formatter used to render the DOM. Using the if null. /// The sanitized HTML body fragment. public string Sanitize(string html, string baseUrl = "", IMarkupFormatter outputFormatter = null) { @@ -357,9 +367,9 @@ public string Sanitize(string html, string baseUrl = "", IMarkupFormatter output var dom = parser.Parse(""); 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; } @@ -369,16 +379,16 @@ public string Sanitize(string html, string baseUrl = "", IMarkupFormatter output /// /// The HTML document to sanitize. /// The base URL relative URLs are resolved against. No resolution if empty. - /// The formatter used to render the DOM. Using the default formatter if null. + /// The formatter used to render the DOM. Using the if null. /// The sanitized HTML document. 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; } @@ -408,7 +418,7 @@ private static void RemoveComments(List 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())