forked from reactjs/React.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionHtmlString.cs
78 lines (70 loc) · 2.03 KB
/
ActionHtmlString.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright (c) 2014-Present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System;
using System.IO;
#if LEGACYASPNET
using System.Text;
using System.Web;
#else
using System.Text.Encodings.Web;
using IHtmlString = Microsoft.AspNetCore.Html.IHtmlContent;
#endif
#if LEGACYASPNET
namespace React.Web.Mvc
#else
namespace React.AspNet
#endif
{
/// <summary>
/// IHtmlString or IHtmlString action wrapper implementation
/// </summary>
public class ActionHtmlString : IHtmlString
{
private readonly Action<TextWriter> _textWriter;
/// <summary>
/// Constructor IHtmlString or IHtmlString action wrapper implementation
/// </summary>
/// <param name="textWriter"></param>
public ActionHtmlString(Action<TextWriter> textWriter)
{
_textWriter = textWriter;
}
#if LEGACYASPNET
[ThreadStatic]
private static StringWriter _sharedStringWriter;
/// <summary>Returns an HTML-encoded string.</summary>
/// <returns>An HTML-encoded string.</returns>
public string ToHtmlString()
{
var stringWriter = _sharedStringWriter;
if (stringWriter != null)
{
stringWriter.GetStringBuilder().Clear();
}
else
{
_sharedStringWriter = stringWriter = new StringWriter(new StringBuilder(128));
}
_textWriter(stringWriter);
return stringWriter.ToString();
}
#else
/// <summary>
/// Writes the content by encoding it with the specified <paramref name="encoder" />
/// to the specified <paramref name="writer" />.
/// </summary>
/// <param name="writer">The <see cref="T:System.IO.TextWriter" /> to which the content is written.</param>
/// <param name="encoder">The <see cref="T:System.Text.Encodings.Web.HtmlEncoder" /> which encodes the content to be written.</param>
public void WriteTo(TextWriter writer, HtmlEncoder encoder)
{
_textWriter(writer);
}
#endif
}
}