-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
StringHtmlContent.cs
46 lines (38 loc) · 1.23 KB
/
StringHtmlContent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable enable
using System.Diagnostics;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Html;
namespace Microsoft.AspNetCore.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)
{
ArgumentNullException.ThrowIfNull(writer);
ArgumentNullException.ThrowIfNull(encoder);
encoder.Encode(writer, _input);
}
private string DebuggerToString()
{
using (var writer = new StringWriter())
{
WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
}
}