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;
if (htmlContent != null)
{
var htmlString = htmlContent as HtmlString;
var htmlString = htmlContent as HtmlEncodedString;
if (htmlString != null)
{
// No need for a StringWriter in this case.

View File

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

View File

@ -1,59 +1,25 @@
// 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 System.Text.Encodings.Web;
using Microsoft.AspNet.Html;
namespace Microsoft.AspNet.Mvc.Rendering
{
/// <summary>
/// String content which knows how to write itself.
/// </summary>
public class HtmlString : IHtmlContent
/// <inheritdoc />
public class HtmlString : HtmlEncodedString
{
private readonly string _input;
/// <summary>
/// Returns an <see cref="HtmlString"/> with empty content.
/// </summary>
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>
/// Creates a new instance of <see cref="HtmlString"/>.
/// </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)
: 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>
{
{ null, null },
{ null, string.Empty },
{ string.Empty, string.Empty },
{ "<\">", "<\">" },
{ "<br />", "<br />" },