Avoid creating intermediate strings with THCWTW

This textwriter needs to inherit HtmlTextWriter and the
StringCollectionTextWriter needs to have the right conditional test.

This allows us to 'pass-through' any IHtmlContent instances without
writing out intermediate strings.
This commit is contained in:
Ryan Nowak 2015-11-10 08:57:12 -08:00
parent f6a9bb1d13
commit 5810154826
2 changed files with 15 additions and 8 deletions

View File

@ -2,16 +2,17 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Text;
using Microsoft.AspNet.Html.Abstractions;
using Microsoft.AspNet.Mvc.ViewFeatures;
using Microsoft.AspNet.Razor.TagHelpers;
namespace Microsoft.AspNet.Mvc.Razor
{
/// <summary>
/// <see cref="TextWriter"/> implementation which writes to a <see cref="TagHelperContent"/> instance.
/// <see cref="HtmlTextWriter"/> implementation which writes to a <see cref="TagHelperContent"/> instance.
/// </summary>
public class TagHelperContentWrapperTextWriter : TextWriter
public class TagHelperContentWrapperTextWriter : HtmlTextWriter
{
/// <summary>
/// Initializes a new instance of the <see cref="TagHelperContentWrapperTextWriter"/> class.
@ -63,6 +64,12 @@ namespace Microsoft.AspNet.Mvc.Razor
Content.AppendHtml(value.ToString());
}
/// <inheritdoc />
public override void Write(IHtmlContent value)
{
Content.Append(value);
}
/// <inheritdoc />
public override string ToString()
{

View File

@ -176,17 +176,17 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
}
/// <summary>
/// If the specified <paramref name="writer"/> is a <see cref="StringCollectionTextWriter"/> the contents
/// If the specified <paramref name="writer"/> is a <see cref="HtmlTextWriter"/> the contents
/// are copied. It is just written to the <paramref name="writer"/> otherwise.
/// </summary>
/// <param name="writer">The <see cref="TextWriter"/> to which the content must be copied/written.</param>
/// <param name="encoder">The <see cref="HtmlEncoder"/> to encode the copied/written content.</param>
public void CopyTo(TextWriter writer, HtmlEncoder encoder)
{
var targetStringCollectionWriter = writer as StringCollectionTextWriter;
if (targetStringCollectionWriter != null)
var htmlTextWriter = writer as HtmlTextWriter;
if (htmlTextWriter != null)
{
targetStringCollectionWriter._content.Append(Content);
htmlTextWriter.Write(Content);
}
else
{
@ -195,7 +195,7 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures
}
/// <summary>
/// If the specified <paramref name="writer"/> is a <see cref="StringCollectionTextWriter"/> the contents
/// If the specified <paramref name="writer"/> is a <see cref="HtmlTextWriter"/> the contents
/// are copied. It is just written to the <paramref name="writer"/> otherwise.
/// </summary>
/// <param name="writer">The <see cref="TextWriter"/> to which the content must be copied/written.</param>