aspnetcore/src/Microsoft.AspNet.Mvc.ViewFe.../ViewFeatures/StringHtmlContent.cs

54 lines
1.5 KiB
C#

// 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;
using Microsoft.AspNet.Html.Abstractions;
namespace Microsoft.AspNet.Mvc.ViewFeatures
{
/// <summary>
/// String content which gets encoded when written.
/// </summary>
[DebuggerDisplay("{DebuggerToString()}")]
public class StringHtmlContent : IHtmlContent
{
private readonly string _input;
/// <summary>
/// Creates a new instance of <see cref="StringHtmlContent"/>
/// </summary>
/// <param name="input"><see cref="string"/> to be HTML encoded when <see cref="WriteTo"/> is called.</param>
public StringHtmlContent(string 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));
}
encoder.Encode(writer, _input);
}
private string DebuggerToString()
{
using (var writer = new StringWriter())
{
WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
}
}
}