예제 #1
0
        private static async Task RenderHtml(IEnumerable<Fortune> model, HttpContext httpContext, HtmlEncoder htmlEncoder)
        {
            httpContext.Response.StatusCode = StatusCodes.Status200OK;
            httpContext.Response.ContentType = "text/html; charset=UTF-8";

            await httpContext.Response.WriteAsync("<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>");

            foreach (var item in model)
            {
                await httpContext.Response.WriteAsync(
                    $"<tr><td>{htmlEncoder.Encode(item.Id.ToString())}</td><td>{htmlEncoder.Encode(item.Message)}</td></tr>");
            }

            await httpContext.Response.WriteAsync("</table></body></html>");
        }
예제 #2
0
        public void HtmlEncode_PositiveTestCase()
        {
            // Arrange
            HtmlEncoder  encoder = HtmlEncoder.Create(UnicodeRanges.All);
            StringWriter writer  = new StringWriter();

            // Act
            encoder.Encode(writer, "Hello+there!");

            // Assert
            Assert.Equal("Hello&#x2B;there!", writer.ToString());
        }
예제 #3
0
        public void HtmlEncode_PositiveTestCase_CreateWithSettings()
        {
            // Arrange
            TextEncoderSettings settings = new TextEncoderSettings(UnicodeRanges.All);
            HtmlEncoder         encoder  = HtmlEncoder.Create(settings);
            StringWriter        writer   = new StringWriter();

            // Act
            encoder.Encode(writer, "Hello+there!");

            // Assert
            Assert.Equal("Hello&#x2B;there!", writer.ToString());
        }
예제 #4
0
        /// <inheritdoc />
        public void WriteTo(TextWriter writer, HtmlEncoder encoder)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (encoder == null)
            {
                throw new ArgumentNullException(nameof(encoder));
            }

            encoder.Encode(writer, _input);
        }
        public static async Task RenderFortunesHtml(IEnumerable<Fortune> model, HttpContext httpContext, HtmlEncoder htmlEncoder)
        {
            httpContext.Response.StatusCode = StatusCodes.Status200OK;
            httpContext.Response.ContentType = "text/html; charset=UTF-8";

            var sb = new StringBuilder();
            sb.Append("<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>");
            foreach (var item in model)
            {
                sb.Append("<tr><td>");
                sb.Append(item.Id.ToString(CultureInfo.InvariantCulture));
                sb.Append("</td><td>");
                sb.Append(htmlEncoder.Encode(item.Message));
                sb.Append("</td></tr>");
            }

            sb.Append("</table></body></html>");
            var response = sb.ToString();
            // fortunes includes multibyte characters so response.Length is incorrect
            httpContext.Response.ContentLength = Encoding.UTF8.GetByteCount(response);
            await httpContext.Response.WriteAsync(response);
        }
예제 #6
0
        /// <inheritdoc />
        public void WriteTo(TextWriter writer, HtmlEncoder encoder)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (encoder == null)
            {
                throw new ArgumentNullException(nameof(encoder));
            }

            var htmlTextWriter = writer as HtmlTextWriter;
            if (htmlTextWriter != null)
            {
                // As a perf optimization, we can buffer this output rather than writing it
                // out character by character.
                htmlTextWriter.Write(this);
            }
            else
            {
                encoder.Encode(writer, _input);
            }
        }
예제 #7
0
 public void HtmlEncode(char[] value, int startIndex, int characterCount, TextWriter output)
 {
     _encoder.Encode(output, value, startIndex, characterCount);
 }
 public void WriteTo(TextWriter writer, HtmlEncoder encoder)
 {
     encoder.Encode(writer, Value);
 }
예제 #9
0
        private void WriteToCore(object entry, TextWriter writer, HtmlEncoder encoder)
        {
            if (entry == null)
            {
                return;
            }

            var stringValue = entry as string;
            if (stringValue != null)
            {
                encoder.Encode(writer, stringValue);
            }
            else
            {
                ((IHtmlContent)entry).WriteTo(writer, encoder);
            }
        }
예제 #10
0
        /// <inheritdoc />
        public override void WriteTo(TextWriter writer, HtmlEncoder encoder)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (encoder == null)
            {
                throw new ArgumentNullException(nameof(encoder));
            }

            if (_buffer == null)
            {
                return;
            }

            foreach (var entry in _buffer)
            {
                if (entry == null)
                {
                    continue;
                }

                var stringValue = entry as string;
                if (stringValue != null)
                {
                    encoder.Encode(writer, stringValue);
                }
                else
                {
                    ((IHtmlContent)entry).WriteTo(writer, encoder);
                }
            }
        }
예제 #11
0
        /// <inheritdoc />
        public void WriteTo(TextWriter writer, HtmlEncoder encoder)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            _preElement?.WriteTo(writer, encoder);

            var isTagNameNullOrWhitespace = string.IsNullOrWhiteSpace(TagName);

            if (!isTagNameNullOrWhitespace)
            {
                writer.Write('<');
                writer.Write(TagName);

                // Perf: Avoid allocating enumerator
                for (var i = 0; i < Attributes.Count; i++)
                {
                    var attribute = Attributes[i];
                    writer.Write(' ');
                    writer.Write(attribute.Name);

                    if (attribute.Minimized)
                    {
                        continue;
                    }

                    writer.Write("=\"");
                    var value = attribute.Value;
                    var htmlContent = value as IHtmlContent;
                    if (htmlContent != null)
                    {
                        // There's no way of tracking the attribute value quotations in the Razor source. Therefore, we
                        // must escape any IHtmlContent double quote values in the case that a user wrote:
                        // <p name='A " is valid in single quotes'></p>
                        using (var stringWriter = new StringWriter())
                        {
                            htmlContent.WriteTo(stringWriter, encoder);

                            var stringValue = stringWriter.ToString();
                            stringValue = stringValue.Replace("\"", "&quot;");

                            writer.Write(stringValue);
                        }
                    }
                    else if (value != null)
                    {
                        encoder.Encode(writer, value.ToString());
                    }

                    writer.Write('"');
                }

                if (TagMode == TagMode.SelfClosing)
                {
                    writer.Write(" /");
                }

                writer.Write('>');
            }

            if (isTagNameNullOrWhitespace || TagMode == TagMode.StartTagAndEndTag)
            {
                _preContent?.WriteTo(writer, encoder);

                _content?.WriteTo(writer, encoder);

                _postContent?.WriteTo(writer, encoder);
            }

            if (!isTagNameNullOrWhitespace && TagMode == TagMode.StartTagAndEndTag)
            {
                writer.Write("</");
                writer.Write(TagName);
                writer.Write(">");
            }

            _postElement?.WriteTo(writer, encoder);
        }
예제 #12
0
 public void WriteTo(TextWriter writer, HtmlEncoder encoder)
 {
     encoder.Encode(writer, _firstSegment, 0, _firstSegmentLength);
     writer.Write(_secondSegment);
 }
예제 #13
0
        /// <inheritdoc />
        public void WriteTo(TextWriter writer, HtmlEncoder encoder)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (encoder == null)
            {
                throw new ArgumentNullException(nameof(encoder));
            }

            for (var i = 0; i < Entries.Count; i++)
            {
                var entry = Entries[i];

                var entryAsString = entry as string;
                if (entryAsString != null)
                {
                    encoder.Encode(writer, entryAsString);
                }
                else
                {
                    // Only string, IHtmlContent values can be added to the buffer.
                    ((IHtmlContent)entry).WriteTo(writer, encoder);
                }
            }
        }
예제 #14
0
 public void WriteTo(TextWriter writer, HtmlEncoder encoder)
 {
     var htmlTextWriter = writer as HtmlTextWriter;
     if (htmlTextWriter != null)
     {
         htmlTextWriter.Write(this);
     }
     else
     {
         encoder.Encode(writer, _firstSegment, 0, _firstSegmentLength);
         writer.Write(_secondSegment);
     }
 }
예제 #15
0
        /// <inheritdoc />
        public void WriteTo(TextWriter writer, HtmlEncoder encoder)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (encoder == null)
            {
                throw new ArgumentNullException(nameof(encoder));
            }

            writer.Write(Name);

            if (ValueStyle == HtmlAttributeValueStyle.Minimized)
            {
                return;
            }

            var valuePrefix = GetAttributeValuePrefix(ValueStyle);
            if (valuePrefix != null)
            {
                writer.Write(valuePrefix);
            }

            var htmlContent = Value as IHtmlContent;
            if (htmlContent != null)
            {
                htmlContent.WriteTo(writer, encoder);
            }
            else if (Value != null)
            {
                encoder.Encode(writer, Value.ToString());
            }

            var valueSuffix = GetAttributeValueSuffix(ValueStyle);
            if (valueSuffix != null)
            {
                writer.Write(valueSuffix);
            }
        }