Ejemplo n.º 1
0
        private static bool RemoveMarqueeFromStartOfText(ref string text, int marqueeStartIndex,
                                                         int htmlStartIndex, bool returnEmptyOnError)
        {
            const string endMarqueeText = "</marquee>";
            const string endCenterText  = "</center>";
            const string endHtmlText    = "</html>";
            const string endBoldText    = "</b>";

            // Find </marquee>, </html>, </center> or </b> whichever comes first.

            var endMarquee = CultureInfo.CurrentCulture.CompareInfo.IndexOf(text, endMarqueeText, marqueeStartIndex, CompareOptions.IgnoreCase);

            if (endMarquee < marqueeStartIndex)
            {
                endMarquee = -1;
            }

            var endHtml = FindEndOfAny(text, htmlStartIndex, endHtmlText, endCenterText, endBoldText);

            if (endHtml == -1)
            {
                if (endMarquee != -1)
                {
                    // No </html>, so at least remove up to </marquee>.
                    endHtml = endMarquee + endMarqueeText.Length;
                }
                else
                {
                    if (returnEmptyOnError)
                    {
                        text = "";
                        return(true);
                    }

                    throw new ApplicationException("Failed to find the end of the marquee. Text: " + text);
                }
            }

            if (endHtml > text.Length - 100)
            {
                text = ""; // Looks like the whole thing is a marquee!
                return(false);
            }

            var endText = text.Substring(endHtml).Trim();

            if (htmlStartIndex == 0)
            {
                text = endText;
            }
            else
            {
                text = HtmlUtil.CloseHtmlTags(text.Substring(0, htmlStartIndex).Trim()) + Environment.NewLine + endText;
            }

            return(true);
        }
Ejemplo n.º 2
0
        string IResourceHighlighter.SummarizeContent(string text, bool htmlEncodeOutput)
        {
            if (string.IsNullOrEmpty(text) || _contentQuery == null)
            {
                return(string.Empty);
            }

            var highlightedString = Summarize(_contentQuery, text, htmlEncodeOutput);

            if (!string.IsNullOrEmpty(highlightedString) && highlightedString.Contains(_configuration.StartTag))
            {
                //some highlighting occured - return this string
                return(HtmlUtil.CloseHtmlTags(highlightedString));
            }

            return(string.Empty);
        }
Ejemplo n.º 3
0
        public override bool GetValue(object currentValue, out object value)
        {
            value = null;
            if (!(currentValue is string || currentValue is string[]))
            {
                return(false);
            }

            var s = currentValue as string;

            if (s != null)
            {
                if (s.Length == 0)
                {
                    return(false);
                }

                // Close any open HTML tags.

                value = HtmlUtil.CloseHtmlTags(s);
                return(true);
            }

            var a = currentValue as string[];

            if (a.Length == 0)
            {
                return(false);
            }

            var v = new string[a.Length];

            for (var index = 0; index < a.Length; ++index)
            {
                v[index] = HtmlUtil.CloseHtmlTags(a[index]);
            }

            value = v;
            return(true);
        }
Ejemplo n.º 4
0
        public void TestCloseHtmlTags()
        {
            // No HTML.

            Assert.AreEqual(null, HtmlUtil.CloseHtmlTags(null));
            Assert.AreEqual("", HtmlUtil.CloseHtmlTags(""));
            Assert.AreEqual("no html", HtmlUtil.CloseHtmlTags("no html"));

            // Simple HTML.

            Assert.AreEqual("<html><valid>all closed</valid></html>",
                            HtmlUtil.CloseHtmlTags("<html><valid>all closed</valid></html>"));
            Assert.AreEqual("<html><valid>all closed</valid></html",
                            HtmlUtil.CloseHtmlTags("<html><valid>all closed</valid></html"));
            Assert.AreEqual("<html><center>some text</center></html>",
                            HtmlUtil.CloseHtmlTags("<html><center>some text"));
            Assert.AreEqual("<html><center>some text</center></html>",
                            HtmlUtil.CloseHtmlTags("<html><center>some text</center>"));
            Assert.AreEqual("<html><center>some text<center /><br></center></html>",
                            HtmlUtil.CloseHtmlTags("<html><center>some text<center /><br></center>"));

            // Quotes.

            Assert.AreEqual("<html><center>some text<a name='blah'  /><br></center></html>",
                            HtmlUtil.CloseHtmlTags("<html><center>some text<a name='blah'  /><br></center>"));
            Assert.AreEqual("some text <and>Some</AND> <html>text<SPAN class='blah'>more text</SPAN></html>",
                            HtmlUtil.CloseHtmlTags("some text <and>Some</AND> <html>text<SPAN class='blah'>more text"));
            Assert.AreEqual("<html><a href=\"http://test/url\">link</a></html>",
                            HtmlUtil.CloseHtmlTags("<html><a href=\"http://test/url\">link"));
            Assert.AreEqual("<html><a href='http://test\"url\'>link</a></html>",
                            HtmlUtil.CloseHtmlTags("<html><a href='http://test\"url\'>link"));

            // Tags that don't need to be closed.

            Assert.AreEqual("<ul><li>one<li>two<li>three</ul>",
                            HtmlUtil.CloseHtmlTags("<ul><li>one<li>two<li>three"));
            Assert.AreEqual("<hr><span>some text</span>",
                            HtmlUtil.CloseHtmlTags("<hr><span>some text"));
        }