From 5f66403248aec2e4509858c6dc5a6201d7ff8480 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 18 Dec 2015 08:45:37 -0800 Subject: [PATCH] Remove TextWriter.ToString from RazorPage Fixes #3668 --- src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs | 48 ++---------- .../TagHelperContentWrapperTextWriter.cs | 78 ------------------- .../RazorPageTest.cs | 26 ------- 3 files changed, 8 insertions(+), 144 deletions(-) delete mode 100644 src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs index 2dd18edca1..ac655388f8 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs @@ -30,7 +30,7 @@ namespace Microsoft.AspNet.Mvc.Razor public abstract class RazorPage : IRazorPage { private readonly HashSet _renderedSections = new HashSet(StringComparer.OrdinalIgnoreCase); - private readonly Stack _writerScopes; + private readonly Stack _writerScopes; private TextWriter _originalWriter; private IUrlHelper _urlHelper; private ITagHelperActivator _tagHelperActivator; @@ -44,7 +44,7 @@ namespace Microsoft.AspNet.Mvc.Razor public RazorPage() { SectionWriters = new Dictionary(StringComparer.OrdinalIgnoreCase); - _writerScopes = new Stack(); + _writerScopes = new Stack(); } /// @@ -209,30 +209,15 @@ namespace Microsoft.AspNet.Mvc.Razor /// public void StartTagHelperWritingScope() { - var buffer = new ViewBuffer(BufferScope, Path); - StartTagHelperWritingScope(new HtmlContentWrapperTextWriter(buffer, Output.Encoding)); - } - - /// - /// Starts a new writing scope with the given . - /// - /// - /// All writes to the or after calling this method will - /// be buffered until is called. - /// - public void StartTagHelperWritingScope(TextWriter writer) - { - if (writer == null) - { - throw new ArgumentNullException(nameof(writer)); - } - // If there isn't a base writer take the ViewContext.Writer if (_originalWriter == null) { _originalWriter = ViewContext.Writer; } + var buffer = new ViewBuffer(BufferScope, Path); + var writer = new HtmlContentWrapperTextWriter(buffer, _originalWriter.Encoding); + // We need to replace the ViewContext's Writer to ensure that all content (including content written // from HTML helpers) is redirected. ViewContext.Writer = writer; @@ -243,8 +228,7 @@ namespace Microsoft.AspNet.Mvc.Razor /// /// Ends the current writing scope that was started by calling . /// - /// The that contains the content written to the or - /// during the writing scope. + /// The buffered . public TagHelperContent EndTagHelperWritingScope() { if (_writerScopes.Count == 0) @@ -253,6 +237,7 @@ namespace Microsoft.AspNet.Mvc.Razor } var writer = _writerScopes.Pop(); + Debug.Assert(writer == ViewContext.Writer); if (_writerScopes.Count > 0) { @@ -267,24 +252,7 @@ namespace Microsoft.AspNet.Mvc.Razor } var tagHelperContent = new DefaultTagHelperContent(); - var razorWriter = writer as RazorTextWriter; - if (razorWriter != null) - { - tagHelperContent.Append(razorWriter.Buffer); - } - else - { - var htmlContentTextWriter = writer as HtmlContentWrapperTextWriter; - if (htmlContentTextWriter != null) - { - tagHelperContent.Append(htmlContentTextWriter.ContentBuilder); - } - else - { - tagHelperContent.AppendHtml(writer.ToString()); - } - } - + tagHelperContent.Append(writer.ContentBuilder); return tagHelperContent; } diff --git a/src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs b/src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs deleted file mode 100644 index 2c58e06e7c..0000000000 --- a/src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Text; -using Microsoft.AspNet.Html; -using Microsoft.AspNet.Razor.TagHelpers; - -namespace Microsoft.AspNet.Mvc.Razor -{ - /// - /// implementation which writes to a instance. - /// - public class TagHelperContentWrapperTextWriter : HtmlTextWriter - { - /// - /// Initializes a new instance of the class. - /// - /// The in which output is written. - public TagHelperContentWrapperTextWriter(Encoding encoding) - : this(encoding, new DefaultTagHelperContent()) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The in which output is written. - /// The to write to. - public TagHelperContentWrapperTextWriter(Encoding encoding, TagHelperContent content) - { - if (encoding == null) - { - throw new ArgumentNullException(nameof(encoding)); - } - - if (content == null) - { - throw new ArgumentNullException(nameof(content)); - } - - Content = content; - Encoding = encoding; - } - - /// - /// The this writes to. - /// - public TagHelperContent Content { get; } - - /// - public override Encoding Encoding { get; } - - /// - public override void Write(string value) - { - Content.AppendHtml(value); - } - - /// - public override void Write(char value) - { - Content.AppendHtml(value.ToString()); - } - - /// - public override void Write(IHtmlContent value) - { - Content.Append(value); - } - - /// - public override string ToString() - { - return Content.ToString(); - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageTest.cs index f791f7051d..9d533c1281 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageTest.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageTest.cs @@ -175,32 +175,6 @@ namespace Microsoft.AspNet.Mvc.Razor await page.ExecuteAsync(); } - [Fact] - public async Task EndTagHelperWritingScope_CopiesContent_IfRazorTextWriter() - { - // Arrange - var viewContext = CreateViewContext(); - - // Act - var page = CreatePage(v => - { - v.HtmlEncoder = new HtmlTestEncoder(); - var buffer = new ViewBuffer(new TestViewBufferScope(), v.Path); - v.StartTagHelperWritingScope(new RazorTextWriter(TextWriter.Null, buffer, v.HtmlEncoder)); - v.Write("Hello "); - v.Write("World!"); - var returnValue = v.EndTagHelperWritingScope(); - - // Assert - var content = Assert.IsType(returnValue); - Assert.Equal("HtmlEncode[[Hello ]]HtmlEncode[[World!]]", content.GetContent()); - Assert.Equal( - "HtmlEncode[[Hello ]]HtmlEncode[[World!]]", - HtmlContentUtilities.HtmlContentToString(content)); - }, viewContext); - await page.ExecuteAsync(); - } - [Fact] public async Task DefineSection_ThrowsIfSectionIsAlreadyDefined() {