EscapeHtml() static private method

Escapes text and writes the result to the StringBuilder.
static private EscapeHtml ( StringBuilder b, string text, HtmlOptions options ) : void
b StringBuilder
text string
options HtmlOptions
return void
示例#1
0
        /// <summary>
        /// Produces HTML code for a section of the line, with &lt;span class="colorName"&gt; tags.
        /// </summary>
        public string ToHtml(int startOffset, int endOffset, HtmlOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            int documentLineStartOffset = this.DocumentLine.Offset;
            int documentLineEndOffset   = documentLineStartOffset + this.DocumentLine.Length;

            if (startOffset < documentLineStartOffset || startOffset > documentLineEndOffset)
            {
                throw new ArgumentOutOfRangeException("startOffset", startOffset, "Value must be between " + documentLineStartOffset + " and " + documentLineEndOffset);
            }
            if (endOffset < startOffset || endOffset > documentLineEndOffset)
            {
                throw new ArgumentOutOfRangeException("endOffset", endOffset, "Value must be between startOffset and " + documentLineEndOffset);
            }
            ISegment requestedSegment = new SimpleSegment(startOffset, endOffset - startOffset);

            List <HtmlElement> elements = new List <HtmlElement>();

            for (int i = 0; i < this.Sections.Count; i++)
            {
                HighlightedSection s = this.Sections[i];
                if (s.GetOverlap(requestedSegment).Length > 0)
                {
                    elements.Add(new HtmlElement(s.Offset, i, false, s.Color));
                    elements.Add(new HtmlElement(s.Offset + s.Length, i, true, s.Color));
                }
            }
            elements.Sort();

            TextDocument document   = this.Document;
            StringWriter w          = new StringWriter(CultureInfo.InvariantCulture);
            int          textOffset = startOffset;

            foreach (HtmlElement e in elements)
            {
                int newOffset = Math.Min(e.Offset, endOffset);
                if (newOffset > startOffset)
                {
                    HtmlClipboard.EscapeHtml(w, document.GetText(textOffset, newOffset - textOffset), options);
                }
                textOffset = Math.Max(textOffset, newOffset);
                if (e.IsEnd)
                {
                    w.Write("</span>");
                }
                else
                {
                    w.Write("<span");
                    options.WriteStyleAttributeForColor(w, e.Color);
                    w.Write('>');
                }
            }
            HtmlClipboard.EscapeHtml(w, document.GetText(textOffset, endOffset - textOffset), options);
            return(w.ToString());
        }
示例#2
0
        /// <summary>
        /// Produces HTML code for a section of the line, with &lt;span class="colorName"&gt; tags.
        /// </summary>
        public string ToHtml(int startOffset, int endOffset, HtmlOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            int documentLineStartOffset = this.DocumentLine.Offset;
            int documentLineEndOffset   = documentLineStartOffset + this.DocumentLine.Length;

            if (startOffset < documentLineStartOffset || startOffset > documentLineEndOffset)
            {
                throw new ArgumentOutOfRangeException("startOffset", startOffset, "Value must be between " + documentLineStartOffset + " and " + documentLineEndOffset);
            }
            if (endOffset < startOffset || endOffset > documentLineEndOffset)
            {
                throw new ArgumentOutOfRangeException("endOffset", endOffset, "Value must be between startOffset and " + documentLineEndOffset);
            }
            ISegment requestedSegment = new SimpleSegment(startOffset, endOffset - startOffset);

            List <HtmlElement> elements = new List <HtmlElement>();

            for (int i = 0; i < this.Sections.Count; i++)
            {
                HighlightedSection s = this.Sections[i];
                if (s.GetOverlap(requestedSegment).Length > 0)
                {
                    elements.Add(new HtmlElement(s.Offset, i, false, s.Color));
                    elements.Add(new HtmlElement(s.Offset + s.Length, i, true, s.Color));
                }
            }
            elements.Sort();

            TextDocument  document   = DocumentLine.Document;
            StringBuilder b          = new StringBuilder();
            int           textOffset = startOffset;

            foreach (HtmlElement e in elements)
            {
                int newOffset = Math.Min(e.Offset, endOffset);
                if (newOffset > startOffset)
                {
                    HtmlClipboard.EscapeHtml(b, document.GetText(textOffset, newOffset - textOffset), options);
                }
                textOffset = newOffset;
                if (e.IsEnd)
                {
                    b.Append("</span>");
                }
                else
                {
                    b.Append("<span style=\"");
                    b.Append(e.Color.ToCss());
                    b.Append("\">");
                }
            }
            HtmlClipboard.EscapeHtml(b, document.GetText(textOffset, endOffset - textOffset), options);
            return(b.ToString());
        }