Change `HtmlString` to inherit from `HtmlEncodedString`

- part of aspnet/HtmlAbstractions#5 fix
- also extend existing special cases to more general `HtmlEncodedString`
This commit is contained in:
Doug Bunting 2015-12-15 15:30:15 -08:00
parent 232b27ad5d
commit 3d8225502f
4 changed files with 8 additions and 41 deletions

View File

@ -176,7 +176,7 @@ namespace Microsoft.AspNet.Mvc.Razor.TagHelpers
var htmlContent = attribute.Value as IHtmlContent; var htmlContent = attribute.Value as IHtmlContent;
if (htmlContent != null) if (htmlContent != null)
{ {
var htmlString = htmlContent as HtmlString; var htmlString = htmlContent as HtmlEncodedString;
if (htmlString != null) if (htmlString != null)
{ {
// No need for a StringWriter in this case. // No need for a StringWriter in this case.

View File

@ -4,6 +4,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using Microsoft.AspNet.Html;
using Microsoft.AspNet.Mvc.Razor; using Microsoft.AspNet.Mvc.Razor;
using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Razor.TagHelpers; using Microsoft.AspNet.Razor.TagHelpers;
@ -48,7 +49,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
} }
string stringValue; string stringValue;
var htmlString = value as HtmlString; var htmlString = value as HtmlEncodedString;
if (htmlString != null) if (htmlString != null)
{ {
// No need for a StringWriter in this case. // No need for a StringWriter in this case.

View File

@ -1,59 +1,25 @@
// 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. // 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.Encodings.Web;
using Microsoft.AspNet.Html; using Microsoft.AspNet.Html;
namespace Microsoft.AspNet.Mvc.Rendering namespace Microsoft.AspNet.Mvc.Rendering
{ {
/// <summary> /// <inheritdoc />
/// String content which knows how to write itself. public class HtmlString : HtmlEncodedString
/// </summary>
public class HtmlString : IHtmlContent
{ {
private readonly string _input;
/// <summary> /// <summary>
/// Returns an <see cref="HtmlString"/> with empty content. /// Returns an <see cref="HtmlString"/> with empty content.
/// </summary> /// </summary>
public static readonly HtmlString Empty = new HtmlString(string.Empty); public static readonly HtmlString Empty = new HtmlString(string.Empty);
/// <summary>
/// Returns an <see cref="HtmlString"/> containing <see cref="Environment.NewLine"/>.
/// </summary>
public static readonly HtmlString NewLine = new HtmlString(Environment.NewLine);
/// <summary> /// <summary>
/// Creates a new instance of <see cref="HtmlString"/>. /// Creates a new instance of <see cref="HtmlString"/>.
/// </summary> /// </summary>
/// <param name="input"><c>string</c> to initialize <see cref="HtmlString"/>.</param> /// <param name="input">The HTML encoded value.</param>
public HtmlString(string input) public HtmlString(string input)
: base(input)
{ {
_input = input;
}
/// <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(_input);
}
/// <inheritdoc />
public override string ToString()
{
return _input;
} }
} }
} }

View File

@ -125,7 +125,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
{ {
return new TheoryData<string, string> return new TheoryData<string, string>
{ {
{ null, null }, { null, string.Empty },
{ string.Empty, string.Empty }, { string.Empty, string.Empty },
{ "<\">", "<\">" }, { "<\">", "<\">" },
{ "<br />", "<br />" }, { "<br />", "<br />" },