AjaxControlToolkit.HtmlEditor.Sanitizer.DefaultHtmlSanitizer.GetSafeHtmlFragment C# (CSharp) Method

GetSafeHtmlFragment() public method

public GetSafeHtmlFragment ( string htmlText, string[]>.Dictionary tagsWhiteList ) : string
htmlText string
tagsWhiteList string[]>.Dictionary
return string
        public string GetSafeHtmlFragment(string htmlText, Dictionary<string, string[]> tagsWhiteList)
        {
            var html = new HtmlDocument();
            html.OptionFixNestedTags = true;
            html.OptionAutoCloseOnEnd = true;
            html.OptionDefaultStreamEncoding = Encoding.UTF8;
            html.LoadHtml(htmlText);

            if(html == null)
                return String.Empty;

            Sanitize(new HtmlNodeWrapper(html.DocumentNode), tagsWhiteList);

            return html.DocumentNode.InnerHtml;
        }

Usage Example

        public void MakeSureItSanitized(string htmlFragment, string message)
        {
            var target = new DefaultHtmlSanitizer();
            var elementWhiteList = CreateElementWhiteList();

            var actual = target.GetSafeHtmlFragment(htmlFragment, elementWhiteList);

            if(htmlFragment != "See Below")
                StringAssert.AreNotEqualIgnoringCase(htmlFragment, actual, message);
        }
All Usage Examples Of AjaxControlToolkit.HtmlEditor.Sanitizer.DefaultHtmlSanitizer::GetSafeHtmlFragment