11using SSR . Net . Exceptions ;
2+ using SSR . Net . Extensions ;
23using SSR . Net . Models ;
34using System ;
45
@@ -20,48 +21,54 @@ public React17Renderer(IJavaScriptEnginePool javaScriptEnginePool) =>
2021 public RenderedComponent RenderComponent ( string componentName ,
2122 string propsAsJson ,
2223 int waitForEngineTimeoutMs = 50 ,
23- bool fallbackToClientSideRender = true )
24+ bool fallbackToClientSideRender = true ,
25+ bool sanitize = true )
2426 {
2527 var result = new RenderedComponent ( ) ;
2628 var id = CreateId ( ) ;
2729 var script = string . Format ( SSREngineScript , componentName , propsAsJson ) ;
2830 string html ;
29- try
30- {
31+ try {
3132 html = _javaScriptEnginePool . EvaluateJs ( script , waitForEngineTimeoutMs ) ;
3233 }
3334 catch ( Exception ex ) {
3435 if ( ! fallbackToClientSideRender )
3536 throw ex ;
36- return FallbackToCSRWithException ( componentName , propsAsJson , ex ) ;
37+ return FallbackToCSRWithException ( componentName , propsAsJson , ex , sanitize ) ;
3738 }
3839 if ( html is null )
39- return RenderComponentCSR ( componentName , propsAsJson ) ;
40+ return RenderComponentCSR ( componentName , propsAsJson , sanitize ) ;
4041 result . Html = string . Format ( SSRHtml , id , html ) ;
4142 result . InitScript = string . Format ( ClientHydrateScript , componentName , propsAsJson , id ) ;
43+ if ( sanitize )
44+ result . InitScript = result . InitScript . SanitizeInitScript ( ) ;
4245 return result ;
4346 }
4447
45- private RenderedComponent FallbackToCSRWithException ( string componentName , string propsAsJson , Exception ex ) {
46- var result = RenderComponentCSR ( componentName , propsAsJson ) ;
48+ private RenderedComponent FallbackToCSRWithException ( string componentName , string propsAsJson , Exception ex , bool sanitize )
49+ {
50+ var result = RenderComponentCSR ( componentName , propsAsJson , sanitize ) ;
4751 if ( ex is AcquireJavaScriptEngineTimeoutException timeoutException )
4852 result . TimeoutException = timeoutException ;
4953 else
5054 result . RenderException = ex ;
5155 return result ;
5256 }
5357
54- private static string CreateId ( ) =>
58+ private static string CreateId ( ) =>
5559 "react_" + Guid . NewGuid ( ) . ToString ( ) . Replace ( "-" , "" ) ;
5660
57- public RenderedComponent RenderComponentCSR ( string componentName , string propsAsJson )
61+ public RenderedComponent RenderComponentCSR ( string componentName , string propsAsJson , bool sanitize = true )
5862 {
5963 var id = CreateId ( ) ;
60- return new RenderedComponent
64+ var result = new RenderedComponent
6165 {
6266 Html = string . Format ( CSRHtml , id ) ,
6367 InitScript = string . Format ( ClientRenderScript , componentName , propsAsJson , id )
6468 } ;
69+ if ( sanitize )
70+ result . InitScript = result . InitScript . SanitizeInitScript ( ) ;
71+ return result ;
6572 }
6673 }
6774}
0 commit comments