Add HtmlContentBuilder and HtmlTextWriter

Remove BufferedHtmlContent
This commit is contained in:
Ryan Nowak 2015-11-13 15:12:11 -08:00
parent bb8141710d
commit 88c356f645
14 changed files with 179 additions and 207 deletions

View File

@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
VisualStudioVersion = 14.0.24711.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A5A15F1C-885A-452A-A731-B0173DDBD913}"
EndProject
@ -46,10 +46,6 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SampleApp", "samples\Sample
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Html.Abstractions.Test", "test\Microsoft.AspNet.Html.Abstractions.Test\Microsoft.AspNet.Html.Abstractions.Test.xproj", "{2D187B88-94BD-4A39-AC97-F8F8B9363301}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Extensions.BufferedHtmlContent.Sources", "src\Microsoft.Extensions.BufferedHtmlContent.Sources\Microsoft.Extensions.BufferedHtmlContent.Sources.xproj", "{B1B2B906-24AE-4C57-AAC5-19B734014504}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Extensions.BufferedHtmlContent.Test", "test\Microsoft.Extensions.BufferedHtmlContent.Test\Microsoft.Extensions.BufferedHtmlContent.Test.xproj", "{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -264,30 +260,6 @@ Global
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Release|x86.ActiveCfg = Release|Any CPU
{2D187B88-94BD-4A39-AC97-F8F8B9363301}.Release|x86.Build.0 = Release|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Debug|x86.ActiveCfg = Debug|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Debug|x86.Build.0 = Debug|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Release|Any CPU.Build.0 = Release|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Release|x86.ActiveCfg = Release|Any CPU
{B1B2B906-24AE-4C57-AAC5-19B734014504}.Release|x86.Build.0 = Release|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Debug|x86.ActiveCfg = Debug|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Debug|x86.Build.0 = Debug|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Release|Any CPU.Build.0 = Release|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Release|x86.ActiveCfg = Release|Any CPU
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -312,7 +284,5 @@ Global
{68A28E4A-3ADE-4187-9625-4FF185887CB3} = {A5A15F1C-885A-452A-A731-B0173DDBD913}
{1D0764B4-1DEB-4232-A714-D4B7E846918A} = {982F09D8-621E-4872-BA7B-BBDEA47D1EFD}
{2D187B88-94BD-4A39-AC97-F8F8B9363301} = {F31FF137-390C-49BF-A3BD-7C6ED3597C21}
{B1B2B906-24AE-4C57-AAC5-19B734014504} = {A5A15F1C-885A-452A-A731-B0173DDBD913}
{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE} = {F31FF137-390C-49BF-A3BD-7C6ED3597C21}
EndGlobalSection
EndGlobal

View File

@ -1,62 +1,95 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Encodings.Web;
using Microsoft.AspNet.Html.Abstractions;
namespace Microsoft.Extensions.Internal
namespace Microsoft.AspNet.Html.Abstractions
{
/// <summary>
/// Enumerable object collection which knows how to write itself.
/// An <see cref="IHtmlContentBuilder"/> implementation using an in memory list.
/// </summary>
[DebuggerDisplay("{DebuggerToString()}")]
internal class BufferedHtmlContent : IHtmlContentBuilder
public class HtmlContentBuilder : IHtmlContentBuilder
{
// This is not List<IHtmlContent> because that would lead to wrapping all strings to IHtmlContent
// which is not space performant.
// internal for testing.
internal List<object> Entries { get; } = new List<object>();
/// <summary>
/// Appends the <see cref="string"/> to the collection.
/// Creates a new <see cref="HtmlContentBuilder"/>.
/// </summary>
/// <param name="unencoded">The <c>string</c> to be appended.</param>
/// <returns>A reference to this instance after the Append operation has completed.</returns>
public IHtmlContentBuilder Append(string unencoded)
public HtmlContentBuilder()
: this(new List<object>())
{
Entries.Add(unencoded);
return this;
}
/// <summary>
/// Appends a <see cref="IHtmlContent"/> to the collection.
/// Creates a new <see cref="HtmlContentBuilder"/> with the given initial capacity.
/// </summary>
/// <param name="htmlContent">The <see cref="IHtmlContent"/> to be appended.</param>
/// <returns>A reference to this instance after the Append operation has completed.</returns>
/// <param name="capacity">The initial capacity of the backing store.</param>
public HtmlContentBuilder(int capacity)
: this(new List<object>(capacity))
{
}
/// <summary>
/// Creates a new <see cref="HtmlContentBuilder"/> with the given list of entries.
/// </summary>
/// <param name="entries">
/// The list of entries. The <see cref="HtmlContentBuilder"/> will use this list without making a copy.
/// </param>
public HtmlContentBuilder(List<object> entries)
{
if (entries == null)
{
throw new ArgumentNullException(nameof(entries));
}
Entries = entries;
}
// This is not List<IHtmlContent> because that would lead to wrapping all strings to IHtmlContent
// which is not space performant.
//
// In general unencoded strings are added here. We're optimizing for that case, and allocating
// a wrapper when encoded strings are used.
//
// internal for testing.
internal List<object> Entries { get; }
/// <inheritdoc />
public IHtmlContentBuilder Append(string unencoded)
{
if (!string.IsNullOrEmpty(unencoded))
{
Entries.Add(unencoded);
}
return this;
}
/// <inheritdoc />
public IHtmlContentBuilder Append(IHtmlContent htmlContent)
{
if (htmlContent == null)
{
return this;
}
Entries.Add(htmlContent);
return this;
}
/// <summary>
/// Appends the HTML encoded <see cref="string"/> to the collection.
/// </summary>
/// <param name="encoded">The HTML encoded <c>string</c> to be appended.</param>
/// <returns>A reference to this instance after the Append operation has completed.</returns>
/// <inheritdoc />
public IHtmlContentBuilder AppendHtml(string encoded)
{
Entries.Add(new HtmlEncodedString(encoded));
if (!string.IsNullOrEmpty(encoded))
{
Entries.Add(new HtmlEncodedString(encoded));
}
return this;
}
/// <summary>
/// Removes all the entries from the collection.
/// </summary>
/// <returns>A reference to this instance after the Clear operation has completed.</returns>
/// <inheritdoc />
public IHtmlContentBuilder Clear()
{
Entries.Clear();
@ -78,11 +111,6 @@ namespace Microsoft.Extensions.Internal
foreach (var entry in Entries)
{
if (entry == null)
{
continue;
}
var entryAsString = entry as string;
if (entryAsString != null)
{
@ -104,22 +132,5 @@ namespace Microsoft.Extensions.Internal
return writer.ToString();
}
}
private class HtmlEncodedString : IHtmlContent
{
public static readonly IHtmlContent NewLine = new HtmlEncodedString(Environment.NewLine);
private readonly string _value;
public HtmlEncodedString(string value)
{
_value = value;
}
public void WriteTo(TextWriter writer, HtmlEncoder encoder)
{
writer.Write(_value);
}
}
}
}

View File

@ -185,33 +185,6 @@ namespace Microsoft.AspNet.Html.Abstractions
return builder;
}
[DebuggerDisplay("{DebuggerToString()}")]
private class HtmlEncodedString : IHtmlContent
{
public static readonly IHtmlContent NewLine = new HtmlEncodedString(Environment.NewLine);
private readonly string _value;
public HtmlEncodedString(string value)
{
_value = value;
}
public void WriteTo(TextWriter writer, HtmlEncoder encoder)
{
writer.Write(_value);
}
private string DebuggerToString()
{
using (var writer = new StringWriter())
{
WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
}
}
[DebuggerDisplay("{DebuggerToString()}")]
private class HtmlFormatString : IHtmlContent
{

View File

@ -0,0 +1,49 @@
// 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.Diagnostics;
using System.IO;
using System.Text.Encodings.Web;
namespace Microsoft.AspNet.Html.Abstractions
{
/// <summary>
/// An <see cref="IHtmlContent"/> impelementation that wraps an HTML encoded <see cref="string"/>.
/// </summary>
[DebuggerDisplay("{DebuggerToString()}")]
public class HtmlEncodedString : IHtmlContent
{
/// <summary>
/// An <see cref="IHtmlContent"/> instance for <see cref="Environment.NewLine"/>.
/// </summary>
public static readonly IHtmlContent NewLine = new HtmlEncodedString(Environment.NewLine);
private readonly string _value;
/// <summary>
/// Creates a new <see cref="HtmlEncodedString"/>.
/// </summary>
/// <param name="value">The HTML encoded value.</param>
public HtmlEncodedString(string value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_value = value;
}
/// <inheritdoc />
public void WriteTo(TextWriter writer, HtmlEncoder encoder)
{
writer.Write(_value);
}
private string DebuggerToString()
{
return _value;
}
}
}

View File

@ -0,0 +1,49 @@
// 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.IO;
namespace Microsoft.AspNet.Html.Abstractions
{
/// <summary>
/// A <see cref="TextWriter"/> which supports special processing of <see cref="IHtmlContent"/>.
/// </summary>
public abstract class HtmlTextWriter : TextWriter
{
/// <summary>
/// Writes an <see cref="IHtmlContent"/> value.
/// </summary>
/// <param name="value">The <see cref="IHtmlContent"/> value.</param>
public abstract void Write(IHtmlContent value);
/// <inheritdoc />
public override void Write(object value)
{
var htmlContent = value as IHtmlContent;
if (htmlContent == null)
{
base.Write(value);
}
else
{
Write(htmlContent);
}
}
/// <inheritdoc />
public override void WriteLine(object value)
{
var htmlContent = value as IHtmlContent;
if (htmlContent == null)
{
base.Write(value);
}
else
{
Write(htmlContent);
}
base.WriteLine();
}
}
}

View File

@ -3,6 +3,8 @@
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
[assembly: AssemblyMetadata("Serviceable", "True")]
[assembly: NeutralResourcesLanguage("en-us")]
[assembly: NeutralResourcesLanguage("en-us")]
[assembly: InternalsVisibleTo("Microsoft.AspNet.Html.Abstractions.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]

View File

@ -21,6 +21,7 @@
},
"dotnet5.4": {
"dependencies": {
"System.Collections": "4.0.11-*",
"System.Resources.ResourceManager": "4.0.1-*"
}
}

View File

@ -1,19 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>b1b2b906-24ae-4c57-aac5-19b734014504</ProjectGuid>
<RootNamespace>Microsoft.Extensions.BufferedHtmlContent.Sources</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -1,8 +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.Reflection;
using System.Resources;
[assembly: AssemblyMetadata("Serviceable", "True")]
[assembly: NeutralResourcesLanguage("en-us")]

View File

@ -1,18 +0,0 @@
{
"version": "1.0.0-*",
"compilationOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk"
},
"shared": "*.cs",
"dependencies": {},
"frameworks": {
"net451": {},
"dotnet5.4": {
"dependencies": {
"System.Resources.ResourceManager": "4.0.1-*",
"System.Runtime": "4.0.21-*"
}
}
}
}

View File

@ -9,13 +9,13 @@ using Xunit;
namespace Microsoft.Extensions.Internal
{
public class BufferedHtmlContentTest
public class HtmlContentBuilderTest
{
[Fact]
public void AppendString_AppendsAString()
{
// Arrange
var content = new BufferedHtmlContent();
var content = new HtmlContentBuilder();
// Act
content.Append("Hello");
@ -29,7 +29,7 @@ namespace Microsoft.Extensions.Internal
public void AppendString_WrittenAsEncoded()
{
// Arrange
var content = new BufferedHtmlContent();
var content = new HtmlContentBuilder();
content.Append("Hello");
var writer = new StringWriter();
@ -45,7 +45,7 @@ namespace Microsoft.Extensions.Internal
public void AppendHtml_DoesNotGetWrittenAsEncoded()
{
// Arrange
var content = new BufferedHtmlContent();
var content = new HtmlContentBuilder();
content.AppendHtml("Hello");
var writer = new StringWriter();
@ -61,7 +61,7 @@ namespace Microsoft.Extensions.Internal
public void AppendIHtmlContent_AppendsAsIs()
{
// Arrange
var content = new BufferedHtmlContent();
var content = new HtmlContentBuilder();
var writer = new StringWriter();
// Act
@ -78,7 +78,7 @@ namespace Microsoft.Extensions.Internal
public void CanAppendMultipleItems()
{
// Arrange
var content = new BufferedHtmlContent();
var content = new HtmlContentBuilder();
// Act
content.Append(new TestHtmlContent("hello"));
@ -94,7 +94,7 @@ namespace Microsoft.Extensions.Internal
public void Clear_DeletesAllItems()
{
// Arrange
var content = new BufferedHtmlContent();
var content = new HtmlContentBuilder();
content.Append(new TestHtmlContent("hello"));
content.Append("Test");
@ -109,7 +109,7 @@ namespace Microsoft.Extensions.Internal
public void WriteTo_WritesAllItems()
{
// Arrange
var content = new BufferedHtmlContent();
var content = new HtmlContentBuilder();
var writer = new StringWriter();
content.Append(new TestHtmlContent("Hello"));
content.Append("Test");

View File

@ -1,4 +1,8 @@
{
"compilationOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk"
},
"dependencies": {
"Microsoft.AspNet.Html.Abstractions": "1.0.0-*",
"Microsoft.AspNet.Testing": "1.0.0-*",

View File

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>3e5311e2-a73e-40cc-a58c-5a62cead43ae</ProjectGuid>
<RootNamespace>Microsoft.Extensions.BufferedHtmlContent.Test</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -1,21 +0,0 @@
{
"compilationOptions": {
"warningsAsErrors": true
},
"dependencies": {
"Microsoft.AspNet.Html.Abstractions": "1.0.0-*",
"Microsoft.Extensions.BufferedHtmlContent.Sources": {
"type": "build",
"version": "1.0.0-*"
},
"Microsoft.Extensions.WebEncoders.Tests" : "1.0.0-*",
"xunit.runner.aspnet": "2.0.0-aspnet-*"
},
"commands": {
"test": "xunit.runner.aspnet"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
}