-
Notifications
You must be signed in to change notification settings - Fork 927
/
Copy pathHtmlHelperExtensions.cs
205 lines (187 loc) · 6.86 KB
/
HtmlHelperExtensions.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
using System;
using System.IO;
using System.Text;
#if LEGACYASPNET
using System.Web;
using IHtmlHelper = System.Web.Mvc.HtmlHelper;
#else
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Html;
using IHtmlString = Microsoft.AspNetCore.Html.IHtmlContent;
#endif
#if LEGACYASPNET
namespace React.Web.Mvc
#else
namespace React.AspNet
#endif
{
/// <summary>
/// HTML Helpers for utilising React from an ASP.NET MVC application.
/// </summary>
public static class HtmlHelperExtensions
{
[ThreadStatic]
private static StringWriter _sharedStringWriter;
/// <summary>
/// Gets the React environment
/// </summary>
private static IReactEnvironment Environment
{
get
{
return ReactEnvironment.GetCurrentOrThrow;
}
}
/// <summary>
/// Renders the specified React component
/// </summary>
/// <typeparam name="T">Type of the props</typeparam>
/// <param name="htmlHelper">HTML helper</param>
/// <param name="componentName">Name of the component</param>
/// <param name="props">Props to initialise the component with</param>
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to <div></param>
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
/// <param name="serverOnly">Skip rendering React specific data-attributes, container and client-side initialisation during server side rendering. Defaults to <c>false</c></param>
/// <param name="containerClass">HTML class(es) to set on the container tag</param>
/// <param name="exceptionHandler">A custom exception handler that will be called if a component throws during a render. Args: (Exception ex, string componentName, string containerId)</param>
/// <param name="renderFunctions">Functions to call during component render</param>
/// <returns>The component's HTML</returns>
public static IHtmlString React<T>(
this IHtmlHelper htmlHelper,
string componentName,
T props,
string htmlTag = null,
string containerId = null,
bool clientOnly = false,
bool serverOnly = false,
string containerClass = null,
Action<Exception, string, string> exceptionHandler = null,
IRenderFunctions renderFunctions = null
)
{
try
{
var reactComponent = Environment.CreateComponent(componentName, props, containerId, clientOnly, serverOnly);
if (!string.IsNullOrEmpty(htmlTag))
{
reactComponent.ContainerTag = htmlTag;
}
if (!string.IsNullOrEmpty(containerClass))
{
reactComponent.ContainerClass = containerClass;
}
return RenderToString(writer => reactComponent.RenderHtml(writer, clientOnly, serverOnly, exceptionHandler, renderFunctions));
}
finally
{
Environment.ReturnEngineToPool();
}
}
/// <summary>
/// Renders the specified React component, along with its client-side initialisation code.
/// Normally you would use the <see cref="React{T}"/> method, but <see cref="ReactWithInit{T}"/>
/// is useful when rendering self-contained partial views.
/// </summary>
/// <typeparam name="T">Type of the props</typeparam>
/// <param name="htmlHelper">HTML helper</param>
/// <param name="componentName">Name of the component</param>
/// <param name="props">Props to initialise the component with</param>
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to <div></param>
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
/// <param name="serverOnly">Skip rendering React specific data-attributes, container and client-side initialisation during server side rendering. Defaults to <c>false</c></param>
/// <param name="containerClass">HTML class(es) to set on the container tag</param>
/// <param name="exceptionHandler">A custom exception handler that will be called if a component throws during a render. Args: (Exception ex, string componentName, string containerId)</param>
/// <param name="renderFunctions">Functions to call during component render</param>
/// <returns>The component's HTML</returns>
public static IHtmlString ReactWithInit<T>(
this IHtmlHelper htmlHelper,
string componentName,
T props,
string htmlTag = null,
string containerId = null,
bool clientOnly = false,
bool serverOnly = false,
string containerClass = null,
Action<Exception, string, string> exceptionHandler = null,
IRenderFunctions renderFunctions = null
)
{
try
{
var reactComponent = Environment.CreateComponent(componentName, props, containerId, clientOnly);
if (!string.IsNullOrEmpty(htmlTag))
{
reactComponent.ContainerTag = htmlTag;
}
if (!string.IsNullOrEmpty(containerClass))
{
reactComponent.ContainerClass = containerClass;
}
return RenderToString(writer =>
{
reactComponent.RenderHtml(writer, clientOnly, serverOnly, exceptionHandler: exceptionHandler, renderFunctions);
writer.WriteLine();
WriteScriptTag(writer, bodyWriter => reactComponent.RenderJavaScript(bodyWriter, waitForDOMContentLoad: true));
});
}
finally
{
Environment.ReturnEngineToPool();
}
}
/// <summary>
/// Renders the JavaScript required to initialise all components client-side. This will
/// attach event handlers to the server-rendered HTML.
/// </summary>
/// <returns>JavaScript for all components</returns>
public static IHtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper, bool clientOnly = false)
{
try
{
return RenderToString(writer =>
{
WriteScriptTag(writer, bodyWriter => Environment.GetInitJavaScript(bodyWriter, clientOnly));
});
}
finally
{
Environment.ReturnEngineToPool();
}
}
private static IHtmlString RenderToString(Action<StringWriter> withWriter)
{
var stringWriter = _sharedStringWriter;
if (stringWriter != null)
{
stringWriter.GetStringBuilder().Clear();
}
else
{
_sharedStringWriter = stringWriter = new StringWriter(new StringBuilder(128));
}
withWriter(stringWriter);
return new HtmlString(stringWriter.ToString());
}
private static void WriteScriptTag(TextWriter writer, Action<TextWriter> bodyWriter)
{
writer.Write("<script");
if (Environment.Configuration.ScriptNonceProvider != null)
{
writer.Write(" nonce=\"");
writer.Write(Environment.Configuration.ScriptNonceProvider());
writer.Write("\"");
}
writer.Write(">");
bodyWriter(writer);
writer.Write("</script>");
}
}
}