From 5810154826034cee955033ea3980e676bd2b3aa8 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Tue, 10 Nov 2015 08:57:12 -0800 Subject: [PATCH] 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. --- .../TagHelperContentWrapperTextWriter.cs | 13 ++++++++++--- .../ViewFeatures/StringCollectionTextWriter.cs | 10 +++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs b/src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs index 1baca26ced..9b8ae174dd 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs @@ -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 { /// - /// implementation which writes to a instance. + /// implementation which writes to a instance. /// - public class TagHelperContentWrapperTextWriter : TextWriter + public class TagHelperContentWrapperTextWriter : HtmlTextWriter { /// /// Initializes a new instance of the class. @@ -63,6 +64,12 @@ namespace Microsoft.AspNet.Mvc.Razor Content.AppendHtml(value.ToString()); } + /// + public override void Write(IHtmlContent value) + { + Content.Append(value); + } + /// public override string ToString() { diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/StringCollectionTextWriter.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/StringCollectionTextWriter.cs index 2190eb44a3..cf04bfe499 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/StringCollectionTextWriter.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/StringCollectionTextWriter.cs @@ -176,17 +176,17 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures } /// - /// If the specified is a the contents + /// If the specified is a the contents /// are copied. It is just written to the otherwise. /// /// The to which the content must be copied/written. /// The to encode the copied/written content. 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 } /// - /// If the specified is a the contents + /// If the specified is a the contents /// are copied. It is just written to the otherwise. /// /// The to which the content must be copied/written.