Move BufferedHtmlContent to HttpAbstractions
This commit is contained in:
parent
8ef20c5c56
commit
5f7a17b8e3
|
|
@ -49,6 +49,10 @@ 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.Framework.BufferedHtmlContent.Sources", "src\Microsoft.Framework.BufferedHtmlContent.Sources\Microsoft.Framework.BufferedHtmlContent.Sources.xproj", "{B1B2B906-24AE-4C57-AAC5-19B734014504}"
|
||||
EndProject
|
||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.BufferedHtmlContent.Test", "test\Microsoft.Framework.BufferedHtmlContent.Test\Microsoft.Framework.BufferedHtmlContent.Test.xproj", "{3E5311E2-A73E-40CC-A58C-5A62CEAD43AE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -275,6 +279,30 @@ 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
|
||||
|
|
@ -300,5 +328,7 @@ 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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,125 @@
|
|||
// 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 Microsoft.AspNet.Html.Abstractions;
|
||||
using Microsoft.Framework.WebEncoders;
|
||||
|
||||
namespace Microsoft.Framework.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// Enumerable object collection which knows how to write itself.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("{DebuggerToString()}")]
|
||||
internal class BufferedHtmlContent : 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.
|
||||
/// </summary>
|
||||
/// <param name="value">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 value)
|
||||
{
|
||||
Entries.Add(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends a <see cref="IHtmlContent"/> to the collection.
|
||||
/// </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>
|
||||
public IHtmlContentBuilder Append(IHtmlContent htmlContent)
|
||||
{
|
||||
Entries.Add(htmlContent);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends the HTML encoded <see cref="string"/> to the collection.
|
||||
/// </summary>
|
||||
/// <param name="value">The HTML encoded <c>string</c> to be appended.</param>
|
||||
/// <returns>A reference to this instance after the Append operation has completed.</returns>
|
||||
public IHtmlContentBuilder AppendEncoded(string value)
|
||||
{
|
||||
Entries.Add(new HtmlEncodedString(value));
|
||||
return this;
|
||||
}
|
||||
/// <summary>
|
||||
/// Removes all the entries from the collection.
|
||||
/// </summary>
|
||||
/// <returns>A reference to this instance after the Clear operation has completed.</returns>
|
||||
public IHtmlContentBuilder Clear()
|
||||
{
|
||||
Entries.Clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void WriteTo(TextWriter writer, IHtmlEncoder encoder)
|
||||
{
|
||||
if (writer == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(writer));
|
||||
}
|
||||
|
||||
if (encoder == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(encoder));
|
||||
}
|
||||
|
||||
foreach (var entry in Entries)
|
||||
{
|
||||
if (entry == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var entryAsString = entry as string;
|
||||
if (entryAsString != null)
|
||||
{
|
||||
encoder.HtmlEncode(entryAsString, writer);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Only string, IHtmlContent values can be added to the buffer.
|
||||
((IHtmlContent)entry).WriteTo(writer, encoder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string DebuggerToString()
|
||||
{
|
||||
using (var writer = new StringWriter())
|
||||
{
|
||||
WriteTo(writer, HtmlEncoder.Default);
|
||||
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, IHtmlEncoder encoder)
|
||||
{
|
||||
writer.Write(_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?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.Framework.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>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"version": "1.0.0-*",
|
||||
"shared": "*.cs",
|
||||
"dependencies": { },
|
||||
"frameworks" : {
|
||||
"net45": { },
|
||||
"dnx451": { },
|
||||
"dnxcore50": {
|
||||
"dependencies": {
|
||||
"System.Runtime": "4.0.21-beta-*"
|
||||
}
|
||||
},
|
||||
"dotnet": {
|
||||
"dependencies": {
|
||||
"System.Runtime": "4.0.21-beta-*"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,146 @@
|
|||
// 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.IO;
|
||||
using Microsoft.AspNet.Html.Abstractions;
|
||||
using Microsoft.Framework.WebEncoders;
|
||||
using Microsoft.Framework.WebEncoders.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Framework.Internal
|
||||
{
|
||||
public class BufferedHtmlContentTest
|
||||
{
|
||||
[Fact]
|
||||
public void AppendString_AppendsAString()
|
||||
{
|
||||
// Arrange
|
||||
var content = new BufferedHtmlContent();
|
||||
|
||||
// Act
|
||||
content.Append("Hello");
|
||||
|
||||
// Assert
|
||||
var result = Assert.Single(content.Entries);
|
||||
Assert.IsType<string>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AppendString_WrittenAsEncoded()
|
||||
{
|
||||
// Arrange
|
||||
var content = new BufferedHtmlContent();
|
||||
content.Append("Hello");
|
||||
|
||||
var writer = new StringWriter();
|
||||
|
||||
// Act
|
||||
content.WriteTo(writer, new CommonTestEncoder());
|
||||
|
||||
// Assert
|
||||
Assert.Equal("HtmlEncode[[Hello]]", writer.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AppendEncoded_DoesNotGetWrittenAsEncoded()
|
||||
{
|
||||
// Arrange
|
||||
var content = new BufferedHtmlContent();
|
||||
content.AppendEncoded("Hello");
|
||||
|
||||
var writer = new StringWriter();
|
||||
|
||||
// Act
|
||||
content.WriteTo(writer, new CommonTestEncoder());
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Hello", writer.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AppendIHtmlContent_AppendsAsIs()
|
||||
{
|
||||
// Arrange
|
||||
var content = new BufferedHtmlContent();
|
||||
var writer = new StringWriter();
|
||||
|
||||
// Act
|
||||
content.Append(new TestHtmlContent("Hello"));
|
||||
|
||||
// Assert
|
||||
var result = Assert.Single(content.Entries);
|
||||
var testHtmlContent = Assert.IsType<TestHtmlContent>(result);
|
||||
testHtmlContent.WriteTo(writer, new CommonTestEncoder());
|
||||
Assert.Equal("Written from TestHtmlContent: Hello", writer.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanAppendMultipleItems()
|
||||
{
|
||||
// Arrange
|
||||
var content = new BufferedHtmlContent();
|
||||
|
||||
// Act
|
||||
content.Append(new TestHtmlContent("hello"));
|
||||
content.Append("Test");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, content.Entries.Count);
|
||||
Assert.Equal("Written from TestHtmlContent: hello", content.Entries[0].ToString());
|
||||
Assert.Equal("Test", content.Entries[1]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clear_DeletesAllItems()
|
||||
{
|
||||
// Arrange
|
||||
var content = new BufferedHtmlContent();
|
||||
content.Append(new TestHtmlContent("hello"));
|
||||
content.Append("Test");
|
||||
|
||||
// Act
|
||||
content.Clear();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0, content.Entries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WriteTo_WritesAllItems()
|
||||
{
|
||||
// Arrange
|
||||
var content = new BufferedHtmlContent();
|
||||
var writer = new StringWriter();
|
||||
content.Append(new TestHtmlContent("Hello"));
|
||||
content.Append("Test");
|
||||
|
||||
// Act
|
||||
content.WriteTo(writer, new CommonTestEncoder());
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, content.Entries.Count);
|
||||
Assert.Equal("Written from TestHtmlContent: HelloHtmlEncode[[Test]]", writer.ToString());
|
||||
}
|
||||
|
||||
private class TestHtmlContent : IHtmlContent
|
||||
{
|
||||
private string _content;
|
||||
|
||||
public TestHtmlContent(string content)
|
||||
{
|
||||
_content = content;
|
||||
}
|
||||
|
||||
public void WriteTo(TextWriter writer, IHtmlEncoder encoder)
|
||||
{
|
||||
writer.Write(ToString());
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "Written from TestHtmlContent: " + _content;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?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.Framework.BufferedHtmlContent.Test</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>
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"compilationOptions": {
|
||||
"warningsAsErrors": true
|
||||
},
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Html.Abstractions": "1.0.0-*",
|
||||
"Microsoft.Framework.BufferedHtmlContent.Sources": { "type": "build", "version": "1.0.0-*" },
|
||||
"Microsoft.Framework.WebEncoders.Testing": "1.0.0-*",
|
||||
"xunit.runner.aspnet": "2.0.0-aspnet-*"
|
||||
},
|
||||
"commands": {
|
||||
"test": "xunit.runner.aspnet"
|
||||
},
|
||||
"frameworks": {
|
||||
"dnx451": { },
|
||||
"dnxcore50": { }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue