1
- /*
1
+ /*
2
2
* Copyright (c) 2014-Present, Facebook, Inc.
3
3
* All rights reserved.
4
4
*
8
8
*/
9
9
10
10
using System ;
11
- using React . Exceptions ;
12
- using React . TinyIoC ;
11
+ using System . IO ;
13
12
14
13
#if LEGACYASPNET
15
14
using System . Web ;
16
15
using System . Web . Mvc ;
17
16
using IHtmlHelper = System . Web . Mvc . HtmlHelper ;
18
17
#else
18
+ using System . Text . Encodings . Web ;
19
19
using Microsoft . AspNetCore . Mvc . Rendering ;
20
20
using IHtmlString = Microsoft . AspNetCore . Html . IHtmlContent ;
21
21
using Microsoft . AspNetCore . Html ;
@@ -129,16 +129,7 @@ public static IHtmlString ReactWithInit<T>(
129
129
}
130
130
var html = reactComponent . RenderHtml ( clientOnly , exceptionHandler : exceptionHandler ) ;
131
131
132
- #if LEGACYASPNET
133
- var script = new TagBuilder ( "script" )
134
- {
135
- InnerHtml = reactComponent . RenderJavaScript ( )
136
- } ;
137
- #else
138
- var script = new TagBuilder ( "script" ) ;
139
- script . InnerHtml . AppendHtml ( reactComponent . RenderJavaScript ( ) ) ;
140
- #endif
141
- return new HtmlString ( html + System . Environment . NewLine + script . ToString ( ) ) ;
132
+ return new HtmlString ( html + System . Environment . NewLine + RenderToString ( GetScriptTag ( reactComponent . RenderJavaScript ( ) ) ) ) ;
142
133
}
143
134
finally
144
135
{
@@ -155,23 +146,53 @@ public static IHtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper, bool
155
146
{
156
147
try
157
148
{
158
- var script = Environment . GetInitJavaScript ( clientOnly ) ;
149
+ return GetScriptTag ( Environment . GetInitJavaScript ( clientOnly ) ) ;
150
+ }
151
+ finally
152
+ {
153
+ Environment . ReturnEngineToPool ( ) ;
154
+ }
155
+ }
156
+
157
+ private static IHtmlString GetScriptTag ( string script )
158
+ {
159
159
#if LEGACYASPNET
160
- var tag = new TagBuilder ( "script" )
161
- {
162
- InnerHtml = script
163
- } ;
164
- return new HtmlString ( tag . ToString ( ) ) ;
160
+ var tag = new TagBuilder ( "script" )
161
+ {
162
+ InnerHtml = script ,
163
+ } ;
164
+
165
+ if ( Environment . Configuration . ScriptNonceProvider != null )
166
+ {
167
+ tag . Attributes . Add ( "nonce" , Environment . Configuration . ScriptNonceProvider ( ) ) ;
168
+ }
169
+
170
+ return new HtmlString ( tag . ToString ( ) ) ;
165
171
#else
166
172
var tag = new TagBuilder ( "script" ) ;
167
173
tag . InnerHtml . AppendHtml ( script ) ;
174
+
175
+ if ( Environment . Configuration . ScriptNonceProvider != null )
176
+ {
177
+ tag . Attributes . Add ( "nonce" , Environment . Configuration . ScriptNonceProvider ( ) ) ;
178
+ }
179
+
168
180
return tag ;
169
181
#endif
170
- }
171
- finally
182
+ }
183
+
184
+ // In ASP.NET Core, you can no longer call `.ToString` on `IHtmlString`
185
+ private static string RenderToString ( IHtmlString source )
186
+ {
187
+ #if LEGACYASPNET
188
+ return source . ToString ( ) ;
189
+ #else
190
+ using ( var writer = new StringWriter ( ) )
172
191
{
173
- Environment . ReturnEngineToPool ( ) ;
192
+ source . WriteTo ( writer , HtmlEncoder . Default ) ;
193
+ return writer . ToString ( ) ;
174
194
}
195
+ #endif
175
196
}
176
197
}
177
198
}
0 commit comments