Skip to content

Commit

Permalink
Make HtmlEncodedString a suitable base for MVC's HtmlString
Browse files Browse the repository at this point in the history
- #5
- provide `ToString()` implementation
- add missing `null` checks in `WriteTo()`; remove extraneous one in the constructor
- special-case `HtmlEncodedString` in `IHtmlContentBuilder.AppendFormat()` extension method

nits:
- ignore more files
- add missing `null` checks in `HtmlContentBuilderExtensions` too
  • Loading branch information
dougbu committed Dec 15, 2015
1 parent 736f2bc commit 234073c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
[Bb]in/
TestResults/
.nuget/
*.sln.ide/
_ReSharper.*/
packages/
artifacts/
PublishProfiles/
.vs/
bower_components/
node_modules/
**/wwwroot/lib/
debugSettings.json
project.lock.json
*.user
*.suo
*.cache
Expand All @@ -23,5 +30,8 @@ nuget.exe
*.ncrunchsolution
*.*sdf
*.ipch
.settings
*.sln.ide
project.lock.json
node_modules
**/[Cc]ompiler/[Rr]esources/**/*.js
*launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public static IHtmlContentBuilder AppendFormat(
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

builder.Append(HtmlEncodedString.NewLine);
return builder;
}
Expand All @@ -112,6 +117,11 @@ public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder)
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, string unencoded)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

builder.Append(unencoded);
builder.Append(HtmlEncodedString.NewLine);
return builder;
Expand All @@ -125,6 +135,11 @@ public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, s
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, IHtmlContent content)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

builder.Append(content);
builder.Append(HtmlEncodedString.NewLine);
return builder;
Expand All @@ -139,6 +154,11 @@ public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, I
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
public static IHtmlContentBuilder AppendHtmlLine(this IHtmlContentBuilder builder, string encoded)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

builder.AppendHtml(encoded);
builder.Append(HtmlEncodedString.NewLine);
return builder;
Expand All @@ -153,6 +173,11 @@ public static IHtmlContentBuilder AppendHtmlLine(this IHtmlContentBuilder builde
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, string unencoded)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

builder.Clear();
builder.Append(unencoded);
return builder;
Expand All @@ -166,6 +191,11 @@ public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, s
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, IHtmlContent content)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

builder.Clear();
builder.Append(content);
return builder;
Expand All @@ -180,6 +210,11 @@ public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, I
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
public static IHtmlContentBuilder SetHtmlContent(this IHtmlContentBuilder builder, string encoded)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

builder.Clear();
builder.AppendHtml(encoded);
return builder;
Expand Down Expand Up @@ -256,8 +291,14 @@ public EncodingFormatProvider(IFormatProvider formatProvider, HtmlEncoder encode

public string Format(string format, object arg, IFormatProvider formatProvider)
{
// This is the case we need to special case. We trust the IHtmlContent instance to do the
// right thing with encoding.
// These are the cases we need to special case. We trust the HtmlEncodedString or IHtmlContent instance
// to do the right thing with encoding.
var htmlString = arg as HtmlEncodedString;
if (htmlString != null)
{
return htmlString.ToString();
}

var htmlContent = arg as IHtmlContent;
if (htmlContent != null)
{
Expand Down
24 changes: 14 additions & 10 deletions src/Microsoft.AspNet.Html.Abstractions/HtmlEncodedString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// 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
{
/// <summary>
/// An <see cref="IHtmlContent"/> impelementation that wraps an HTML encoded <see cref="string"/>.
/// An <see cref="IHtmlContent"/> implementation that wraps an HTML encoded <see cref="string"/>.
/// </summary>
[DebuggerDisplay("{DebuggerToString()}")]
public class HtmlEncodedString : IHtmlContent
{
/// <summary>
Expand All @@ -27,23 +25,29 @@ public class HtmlEncodedString : IHtmlContent
/// <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)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}

if (encoder == null)
{
throw new ArgumentNullException(nameof(encoder));
}

writer.Write(_value);
}

private string DebuggerToString()
/// <inheritdoc />
public override string ToString()
{
return _value;
return _value ?? string.Empty;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ public void Builder_AppendFormat_HtmlContent()
HtmlContentToString(builder));
}

[Fact]
public void Builder_AppendFormat_HtmlEncodedString()
{
// Arrange
var builder = new TestHtmlContentBuilder();

// Act
builder.AppendFormat("{0}!", new HtmlEncodedString("First"));

// Assert
Assert.Equal("First!", HtmlContentToString(builder));
}

[Fact]
public void Builder_AppendFormatContent_With1Argument()
{
Expand Down

0 comments on commit 234073c

Please sign in to comment.