/// <summary>
        /// Filters text to only allow defined HTML.
        /// </summary>
        /// <param name="allowedHtmlTags">The allowed html tags.</param>
        /// <param name="text">Text.</param>
        /// <returns></returns>
        public static string ConvertToAllowedHtml(NameValueCollection allowedHtmlTags, string text)
        {
            if (allowedHtmlTags == null || allowedHtmlTags.Count == 0)
            {
                //This indicates that the AllowableCommentHtml configuration is either missing or
                //has no values, therefore just strip the text as normal.
                return(HtmlSafe(text));
            }
            var             regex   = new HtmlTagRegex();
            MatchCollection matches = regex.Matches(text);

            if (matches.Count == 0)
            {
                return(HtmlSafe(text));
            }

            var sb = new StringBuilder();

            int currentIndex = 0;

            foreach (Match match in matches)
            {
                //Append text before the match.
                if (currentIndex < match.Index)
                {
                    sb.Append(HtmlSafe(text.Substring(currentIndex, match.Index - currentIndex)));
                }

                string tagName = match.Groups["tagname"].Value.ToLower(CultureInfo.InvariantCulture);

                //check each match against the list of allowable tags.
                if (allowedHtmlTags.Get(tagName) == null)
                {
                    sb.Append(HtmlSafe(match.Value));
                }
                else
                {
                    bool isEndTag = match.Groups["endTag"].Value.Length > 0;
                    if (isEndTag)
                    {
                        sb.AppendFormat("</{0}>", tagName);
                    }
                    else
                    {
                        sb.Append("<" + tagName);
                        sb.Append(FilterAttributes(tagName, match, allowedHtmlTags) + ">");
                    }
                }
                currentIndex = match.Index + match.Length;
            }
            //add the remaining text.
            if (currentIndex < text.Length)
            {
                sb.Append(HtmlSafe(text.Substring(currentIndex)));
            }

            var converter = new XhtmlConverter();

            return(converter.Transform(sb.ToString()));
        }
示例#2
0
        public void Transform_WithValidMarkup_DoesNotChangeIt(string markup, string expected)
        {
            //arrange
            var converter = new XhtmlConverter();

            //act
            string result = converter.Transform(markup);

            //assert
            Assert.AreEqual(expected, result);
        }
示例#3
0
        public void ConvertHtmlToXhtmlEnsuresSomeTagsMustNotBeSelfClosed(string html, string expected, string message)
        {
            //arrange
            var converter = new XhtmlConverter();

            //act
            string result = converter.Transform(html);

            //assert
            Assert.AreEqual(expected, result);
        }
示例#4
0
        public void ConvertHtmlToXHtmlCorrectsInvalidMarkup(string badMarkup, string corrected)
        {
            //arrange
            var converter = new XhtmlConverter();

            //act
            string result = converter.Transform(badMarkup);

            //assert
            Assert.AreEqual(corrected, result);
        }
示例#5
0
        public void ConvertHtmlToXHtmlLeavesNestedMarkupAlone()
        {
            //arrange
            const string expected  = "<p><span>This is some text</span> <span>this is more text</span></p>";
            var          converter = new XhtmlConverter();

            //act
            string result = converter.Transform(expected);

            //assert
            Assert.AreEqual(expected, result);
        }
示例#6
0
        public void ConvertHtmlToXhtmlEnsuresSomeTagsMustBeSelfClosed(string tag)
        {
            //arrange
            string html      = string.Format(CultureInfo.InvariantCulture, "<{0} src=\"blah-blah\"></{0}>", tag);
            string expected  = string.Format(CultureInfo.InvariantCulture, "<{0} src=\"blah-blah\" />", tag);
            var    converter = new XhtmlConverter();

            //act
            string result = converter.Transform(html);

            //assert
            Assert.AreEqual(expected, result);
        }
示例#7
0
        public void Transform_WithConverter_AppliesConverterWhileConvertingHtml()
        {
            const string html     = "<p title=\"blah blah\"> blah blah </p>";
            const string expected = "<p title=\"blah blah\"> yadda yadda </p>";

            //arrange
            var converter = new XhtmlConverter(input => input.Replace("blah", "yadda"));

            //act
            string result = converter.Transform(html);

            //assert
            Assert.AreEqual(expected, result);
        }
示例#8
0
        public void Transform_WithStyleTag_DoesNotWrapStyleInCdata()
        {
            const string html     = "<style>.test {color: blue;}</style>";
            const string expected = html;

            //arrange
            var converter = new XhtmlConverter();

            //act
            string result = converter.Transform(html);

            //assert
            Assert.AreEqual(expected, result);
        }
示例#9
0
        public void Transform_WithAngleBracketInAttributeValue_EncodesAttribute()
        {
            const string html     = @"<a title="">"">b</a>";
            const string expected = @"<a title=""&gt;"">b</a>";

            //arrange
            var converter = new XhtmlConverter();

            //act
            string result = converter.Transform(html);

            //assert
            Assert.AreEqual(expected, result);
        }
示例#10
0
        /// <summary>
        /// Wraps an anchor tag around all urls. Makes sure not to wrap already
        /// wrapped urls.
        /// </summary>
        /// <param name="html">Html containing urls to convert.</param>
        /// <returns></returns>
        public static string ConvertUrlsToHyperLinks(string html)
        {
            if (html == null)
            {
                throw new ArgumentNullException("html");
            }

            if (html.Length == 0)
            {
                return(string.Empty);
            }

            var xhtmlConverter = new XhtmlConverter(text =>
            {
                const string pattern    = @"((https?|ftp)://|www\.)[\w]+(.[\w]+)([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])";
                MatchCollection matches =
                    Regex.Matches(text, pattern,
                                  RegexOptions.
                                  IgnoreCase |
                                  RegexOptions.Compiled);
                foreach (Match m in matches)
                {
                    string httpPortion = string.Empty;
                    if (!m.Value.Contains("://"))
                    {
                        httpPortion = "http://";
                    }

                    text =
                        text.Replace(m.Value,
                                     string.Format(CultureInfo.InvariantCulture,
                                                   "<a rel=\"nofollow external\" href=\"{0}{1}\" title=\"{1}\">{2}</a>",
                                                   httpPortion, m.Value, ShortenUrl(m.Value, 50))
                                     );
                }
                return(text);
            });

            return(xhtmlConverter.Transform(html));
        }