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>"); }
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+there!", writer.ToString()); }
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+there!", writer.ToString()); }
/// <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); }
/// <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); } }
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); }
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); } }
/// <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); } } }
/// <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("\"", """); 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); }
public void WriteTo(TextWriter writer, HtmlEncoder encoder) { encoder.Encode(writer, _firstSegment, 0, _firstSegmentLength); writer.Write(_secondSegment); }
/// <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); } } }
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); } }
/// <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); } }