public string FilterHtml(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return(string.Empty);
            }

            if (SiteConfiguration.ValidCommentTags == null || SiteConfiguration.ValidCommentTags[0].Tag.Count(s => s.Allowed == true) == 0)
            {
                return(WebUtility.HtmlEncode(input));
            }

            // check for matches
            var matches = htmlFilterRegex.Matches(input);

            // no matches, normal encoding
            if (matches.Count == 0)
            {
                return(WebUtility.HtmlEncode(input));
            }

            var sb = new StringBuilder();


            var collection = new MatchedTagCollection(SiteConfiguration.ValidCommentTags);

            collection.Init(matches);

            int inputIndex = 0;

            foreach (MatchedTag tag in collection)
            {
                // add the normal text between the current index and the index of the current tag
                if (inputIndex < tag.Index)
                {
                    sb.Append(WebUtility.HtmlEncode(input.Substring(inputIndex, tag.Index - inputIndex)));
                }

                // add the filtered value
                sb.Append(tag.FilteredValue);

                // move the current index past the tag
                inputIndex = tag.Index + tag.Length;
            }

            // add remainder
            if (inputIndex < input.Length)
            {
                sb.Append(WebUtility.HtmlEncode(input.Substring(inputIndex)));
            }

            return(sb.ToString());
        }
Exemple #2
0
        public string FilterHtml(string input)
        {
#if POSIX
            return(input);
#else
            if (SiteConfiguration.AllowedTags == null || SiteConfiguration.AllowedTags.Count == 0)
            {
                return(WebUtility.HtmlEncode(input));
            }

            // check for matches
            MatchCollection matches = htmlFilterRegex.Matches(input);

            // no matches, normal encoding
            if (matches.Count == 0)
            {
                return(WebUtility.HtmlEncode(input));
            }

            StringBuilder sb = new StringBuilder();


            MatchedTagCollection collection = new MatchedTagCollection(SiteConfiguration.AllowedTags);
            collection.Init(matches);

            int inputIndex = 0;

            foreach (MatchedTag tag in collection)
            {
                // add the normal text between the current index and the index of the current tag
                if (inputIndex < tag.Index)
                {
                    sb.Append(WebUtility.HtmlEncode(input.Substring(inputIndex, tag.Index - inputIndex)));
                }

                // add the filtered value
                sb.Append(tag.FilteredValue);

                // move the current index past the tag
                inputIndex = tag.Index + tag.Length;
            }

            // add remainder
            if (inputIndex < input.Length)
            {
                sb.Append(WebUtility.HtmlEncode(input.Substring(inputIndex)));
            }

            return(sb.ToString());
#endif // #if POSIX #else
        }