├── .gitattributes ├── .gitignore ├── Blazor.DynamicJavascriptRuntime.Evaluator.SystemTests ├── Blazor.DynamicJavascriptRuntime.Evaluator.SystemTests.csproj └── EvalContextSystemTest.cs ├── Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client ├── App.razor ├── Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client.csproj ├── Pages │ ├── Index.razor │ ├── IndexPage.cs │ └── Info.razor ├── Program.cs ├── Properties │ └── launchSettings.json ├── Shared │ └── MainLayout.razor ├── _Imports.razor └── wwwroot │ ├── JsInterop.js │ └── index.html ├── Blazor.DynamicJavascriptRuntime.Evaluator.Tests ├── Blazor.DynamicJavascriptRuntime.Evaluator.Tests.csproj └── EvalContextTest.cs ├── Blazor.DynamicJavascriptRuntime.Evaluator.sln ├── Blazor.DynamicJavascriptRuntime.Evaluator ├── Blazor.DynamicJavascriptRuntime.Evaluator.csproj ├── EvalContext.cs ├── EvalContextSettings.cs ├── IEvalContext.cs ├── JavascriptLiteral │ ├── Literal.cs │ └── LiteralConverter.cs ├── Properties │ └── launchSettings.json ├── icon.ico ├── icon.png └── wwwroot │ └── BlazorDynamicJavascriptRuntime.js ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.SystemTests/Blazor.DynamicJavascriptRuntime.Evaluator.SystemTests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | false 7 | 8 | 9 | 10 | true 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.SystemTests/EvalContextSystemTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Xunit; 3 | using OpenQA.Selenium; 4 | using OpenQA.Selenium.Chrome; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | 8 | namespace Blazor.DynamicJavascriptRuntime.Evaluator.SystemTests 9 | { 10 | public class EvalContextSystemTest : IDisposable 11 | { 12 | 13 | private readonly ChromeDriver _driver; 14 | 15 | public EvalContextSystemTest() 16 | { 17 | var options = new ChromeOptions(); 18 | //options.AddArgument("no-sandbox"); 19 | options.AddArguments("headless"); 20 | _driver = new ChromeDriver(".", options); 21 | } 22 | 23 | public void Dispose() 24 | { 25 | _driver.Close(); 26 | _driver.Dispose(); 27 | } 28 | 29 | [Fact] 30 | public void Given_a_blazor_app_When_passing_anonymous_argument_Then_should_serialize_and_execute() 31 | { 32 | _driver.Navigate().GoToUrl("http://localhost:54235"); 33 | 34 | object actual = null; 35 | 36 | SpinWait.SpinUntil(() => 37 | { 38 | actual = _driver.ExecuteScript("return JSON.stringify(JsInterop.anonymous)"); 39 | return actual != null && actual.ToString() != "null"; 40 | }, TimeSpan.FromSeconds(10)); 41 | 42 | Assert.Equal("{\"property\":\"Value\",\"field\":123,\"child\":{\"member\":\"2001-01-01T00:00:00\"}}", actual); 43 | } 44 | 45 | [Fact] 46 | public void Given_a_blazor_app_When_passing_argument_And_specifying_as_serializable_type_Then_should_serialize_and_execute() 47 | { 48 | _driver.Navigate().GoToUrl("http://localhost:54235"); 49 | 50 | object actual = null; 51 | 52 | SpinWait.SpinUntil(() => 53 | { 54 | actual = _driver.ExecuteScript("return JSON.stringify(JsInterop.specified)"); 55 | return actual != null && actual.ToString() != "null"; 56 | }, TimeSpan.FromSeconds(10)); 57 | 58 | Assert.Equal("{\"member\":\"abc\"}", actual); 59 | } 60 | 61 | [Fact] 62 | public void Given_a_blazor_app_When_invoking_synchronously_Then_should_execute() 63 | { 64 | _driver.Navigate().GoToUrl("http://localhost:54235"); 65 | 66 | object actual = null; 67 | 68 | SpinWait.SpinUntil(() => 69 | { 70 | actual = _driver.ExecuteScript("return JsInterop.returnValue"); 71 | return actual != null && actual.ToString() != "null"; 72 | }, TimeSpan.FromSeconds(10)); 73 | 74 | Assert.Equal(2, (long)actual); 75 | } 76 | 77 | [Fact] 78 | public void Given_a_blazor_app_When_invoking_asynchronously_Then_should_execute() 79 | { 80 | _driver.Navigate().GoToUrl("http://localhost:54235"); 81 | 82 | object actual = null; 83 | 84 | SpinWait.SpinUntil(() => 85 | { 86 | actual = _driver.ExecuteScript("return JsInterop.anotherReturnValue"); 87 | return actual != null && actual.ToString() != "null"; 88 | }, TimeSpan.FromSeconds(10)); 89 | 90 | Assert.Equal(2, (long)actual); 91 | } 92 | 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/App.razor: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 |

Sorry, there's nothing at this address.

8 |
9 |
10 |
11 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | <_RazorComponentInclude>Pages\**\*.cshtml 6 | <_RazorComponentInclude>Pages\**\*.razor 7 | Exe 8 | AnyCPU 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/Pages/Index.razor: -------------------------------------------------------------------------------- 1 | @page "/" 2 | @inherits Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client.Pages.IndexPage 3 | @inject Microsoft.JSInterop.IJSRuntime JSRuntime 4 | 5 | 6 | Info -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/Pages/IndexPage.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Components; 2 | using Microsoft.JSInterop; 3 | using System; 4 | using System.Threading.Tasks; 5 | 6 | namespace Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client.Pages 7 | { 8 | public class IndexPage : ComponentBase 9 | { 10 | 11 | [Inject] 12 | public IJSRuntime JsRuntime { get; set; } 13 | 14 | protected override async Task OnInitializedAsync() 15 | { 16 | using (dynamic context = new EvalContext(JsRuntime)) 17 | { 18 | var arg = new { Property = "Value", Field = 123, child = new { Member = new DateTime(2001, 1, 1) } }; 19 | (context as EvalContext).Expression = () => context.JsInterop.set(arg); 20 | } 21 | 22 | var settings = new EvalContextSettings(); 23 | settings.SerializableTypes.Add(typeof(Specified)); 24 | using (dynamic context = new EvalContext(JsRuntime, settings)) 25 | { 26 | var arg = new Specified { Member = "abc", Empty = null }; 27 | (context as EvalContext).Expression = () => context.JsInterop.setSpecified(arg); 28 | } 29 | 30 | double value = 1; 31 | using (dynamic context = new EvalContext(JsRuntime)) 32 | { 33 | (context as EvalContext).Expression = () => context.JsInterop.callMethod(value); 34 | value = (context as EvalContext).Invoke(); 35 | } 36 | 37 | using (dynamic context = new EvalContext(JsRuntime)) 38 | { 39 | (context as EvalContext).Expression = () => context.JsInterop.returnValue = value; 40 | } 41 | 42 | new EvalContext(JsRuntime).Invoke($"JsInterop.returnValue = {value}"); 43 | 44 | dynamic anotherContext = new EvalContext(JsRuntime); 45 | anotherContext.JsInterop.anotherReturnValue = value; 46 | await (anotherContext as EvalContext).InvokeVoidAsync(); 47 | 48 | await base.OnInitializedAsync(); 49 | } 50 | 51 | private class Specified 52 | { 53 | public string Member { get; set; } 54 | public string Empty { get; set; } 55 | } 56 | 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/Pages/Info.razor: -------------------------------------------------------------------------------- 1 | @page "/Info" 2 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Components.WebAssembly.Hosting; 2 | using Microsoft.Extensions.DependencyInjection; 3 | using System; 4 | using System.Threading.Tasks; 5 | 6 | namespace Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client 7 | { 8 | public class Program 9 | { 10 | public static async Task Main(string[] args) 11 | { 12 | var builder = WebAssemblyHostBuilder.CreateDefault(args); 13 | builder.Services.AddSingleton(builder.Services.BuildServiceProvider()); 14 | 15 | builder.RootComponents.Add("#app"); 16 | 17 | await builder.Build().RunAsync(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:54235/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", 15 | "launchUrl": "http://localhost:54235/", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client": { 21 | "commandName": "Project", 22 | "launchBrowser": true, 23 | "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", 24 | "environmentVariables": { 25 | "ASPNETCORE_ENVIRONMENT": "Development" 26 | }, 27 | "applicationUrl": "http://localhost:54236/" 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/Shared/MainLayout.razor: -------------------------------------------------------------------------------- 1 | @inherits LayoutComponentBase 2 | 3 | @Body 4 | 5 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/_Imports.razor: -------------------------------------------------------------------------------- 1 | @using System.Net.Http 2 | @using Microsoft.AspNetCore.Components.Forms 3 | @using Microsoft.AspNetCore.Components.Routing 4 | @using Microsoft.AspNetCore.Components.Web 5 | @using Microsoft.JSInterop 6 | @using Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client 7 | @using Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client.Shared 8 | @using System.Net.Http.Json 9 | @using Microsoft.AspNetCore.Components.Web.Virtualization 10 | @using Microsoft.AspNetCore.Components.WebAssembly.Http -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/wwwroot/JsInterop.js: -------------------------------------------------------------------------------- 1 | window.JsInterop = { 2 | 3 | anonymous: null, 4 | 5 | set: function (value) { 6 | this.anonymous = value; 7 | }, 8 | 9 | specified: null, 10 | 11 | setSpecified: function (value) { 12 | this.specified = value; 13 | }, 14 | 15 | callMethod: function (value) { 16 | return value + 1; 17 | }, 18 | 19 | returnValue: null, 20 | 21 | anotherReturnValue: null 22 | 23 | }; 24 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/wwwroot/index.html: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client 7 | 8 | 9 | 10 | 11 |
12 |
13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests/Blazor.DynamicJavascriptRuntime.Evaluator.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.Tests/EvalContextTest.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.JSInterop; 2 | using Moq; 3 | using System; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Xunit; 7 | 8 | namespace Blazor.DynamicJavascriptRuntime.Evaluator.Tests 9 | { 10 | public class EvalContextTest 11 | { 12 | 13 | [Fact] 14 | public void Given_a_dynamic_expression_When_invoked_Then_should_execute_evaluation() 15 | { 16 | var runtime = new Mock(); 17 | using (dynamic context = new EvalContext(runtime.Object)) 18 | { 19 | (context as EvalContext).Expression = () => context.Chart.defaults.global.animation.duration = 0; 20 | } 21 | Verify(runtime, "Chart.defaults.global.animation.duration = 0"); 22 | 23 | using (dynamic context = new EvalContext(runtime.Object)) 24 | { 25 | (context as EvalContext).Expression = () => context.Chart.defaults.global.defaultFontColor = "#FFF"; 26 | } 27 | Verify(runtime, "Chart.defaults.global.defaultFontColor = \"#FFF\""); 28 | } 29 | 30 | [Fact] 31 | public void Given_a_dynamic_expression_called_on_context_When_invoked_Then_should_execute_evaluation() 32 | { 33 | var runtime = new Mock(); 34 | using (var context = new EvalContext(runtime.Object)) 35 | { 36 | (context as dynamic).Chart.defaults.global.animation.duration = 0; 37 | } 38 | Verify(runtime, "Chart.defaults.global.animation.duration = 0"); 39 | 40 | using (var context = new EvalContext(runtime.Object)) 41 | { 42 | (context as dynamic).Chart.defaults.global.defaultFontColor = "#FFF"; 43 | } 44 | Verify(runtime, "Chart.defaults.global.defaultFontColor = \"#FFF\""); 45 | } 46 | 47 | [Fact] 48 | public async Task Given_a_dynamic_expression_When_invoke_void_is_called_Then_should_execute_evaluation() 49 | { 50 | var runtime = new Mock(); 51 | var context = new EvalContext(runtime.Object); 52 | 53 | (context as dynamic).Chart.defaults.global.animation.duration = 0; 54 | await context.InvokeVoidAsync(); 55 | 56 | Verify(runtime, "Chart.defaults.global.animation.duration = 0"); 57 | 58 | context.Reset(); 59 | (context as dynamic).Chart.defaults.global.defaultFontColor = "#FFF"; 60 | await context.InvokeVoidAsync(); 61 | 62 | 63 | Verify(runtime, "Chart.defaults.global.defaultFontColor = \"#FFF\""); 64 | } 65 | 66 | [Theory] 67 | [InlineData("message", "\"message\"")] 68 | [InlineData("mess\rage", "`mess\rage`")] 69 | [InlineData("mess\nage", "`mess\nage`")] 70 | [InlineData("mess\r\nage", "`mess\r\nage`")] 71 | public void Given_a_dynamic_expression_And_multiline_string_argument_When_invoked_Then_should_execute_evaluation(string argument, string expected) 72 | { 73 | var runtime = new Mock(); 74 | using (dynamic context = new EvalContext(runtime.Object)) 75 | { 76 | (context as EvalContext).Expression = () => context.alert(argument); 77 | } 78 | Verify(runtime, $"alert({expected})"); 79 | } 80 | 81 | [Fact] 82 | public void Given_a_dynamic_expression_And_arguments_are_supplied_When_invoked_Then_should_execute_evaluation() 83 | { 84 | var runtime = new Mock(); 85 | using (dynamic context = new EvalContext(runtime.Object)) 86 | { 87 | dynamic arg = new EvalContext(runtime.Object); 88 | dynamic arg2 = new EvalContext(runtime.Object); 89 | (context as EvalContext).Expression = () => context.Chart.config.data.datasets[arg.i].data.push(arg2.config.data.datasets[arg.i].data); 90 | } 91 | 92 | Verify(runtime, "Chart.config.data.datasets[i].data.push(config.data.datasets[i].data)"); 93 | } 94 | 95 | [Fact] 96 | public void Given_a_dynamic_expression_And_a_function_is_called_When_invoked_Then_should_execute_evaluation() 97 | { 98 | var runtime = new Mock(); 99 | using (dynamic context = new EvalContext(runtime.Object)) 100 | { 101 | dynamic arg = new EvalContext(runtime.Object); 102 | (context as EvalContext).Expression = () => context.Do("stuff"); 103 | } 104 | Verify(runtime, "Do(\"stuff\")"); 105 | 106 | using (dynamic context = new EvalContext(runtime.Object)) 107 | { 108 | dynamic arg = new EvalContext(runtime.Object); 109 | (context as EvalContext).Expression = () => context.Do(arg.stuff); 110 | } 111 | Verify(runtime, "Do(stuff)", 2); 112 | 113 | using (dynamic context = new EvalContext(runtime.Object)) 114 | { 115 | dynamic arg = new EvalContext(runtime.Object); 116 | dynamic arg2 = new EvalContext(runtime.Object); 117 | (context as EvalContext).Expression = () => context.Do("document", arg2.@this); 118 | } 119 | Verify(runtime, "Do(\"document\", this)", 3); 120 | } 121 | 122 | [Fact] 123 | public void Given_a_dynamic_expression_And_a_function_is_called_And_context_is_reused_When_invoked_Then_should_not_throw_exception() 124 | { 125 | var runtime = new Mock(); 126 | using (dynamic context = new EvalContext(runtime.Object)) 127 | { 128 | dynamic arg = new EvalContext(runtime.Object); 129 | dynamic arg2 = new EvalContext(runtime.Object); 130 | dynamic arg3 = new EvalContext(runtime.Object); 131 | (context as EvalContext).Expression = () => context.Do(arg.document, arg2.@this); context.After(arg3.that); 132 | } 133 | //todo: prevent bad syntax 134 | } 135 | 136 | [Fact] 137 | public void Given_a_dynamic_expression_And_a_string_indexer_When_invoked_Then_should_execute_evaluation() 138 | { 139 | var runtime = new Mock(); 140 | using (dynamic context = new EvalContext(runtime.Object)) 141 | { 142 | (context as EvalContext).Expression = () => context.person["firstname"] = "Joe"; 143 | } 144 | Verify(runtime, "person[\"firstname\"] = \"Joe\""); 145 | } 146 | 147 | [Fact] 148 | public void Given_a_dynamic_expression_And_a_call_to_window_When_invoked_Then_should_execute_evaluation() 149 | { 150 | var runtime = new Mock(); 151 | using (dynamic context = new EvalContext(runtime.Object)) 152 | { 153 | (context as EvalContext).Expression = () => context.window.location = "www.be-evil\'\".com"; 154 | } 155 | Verify(runtime, "window.location = \"www.be-evil\'\\u0022.com\""); 156 | } 157 | 158 | [Fact] 159 | public void Given_a_dynamic_expression_And_a_declaration_When_invoked_Then_should_execute_evaluation() 160 | { 161 | var runtime = new Mock(); 162 | using (dynamic context = new EvalContext(runtime.Object, new EvalContextSettings { EnableSpaceCharacterPlaceholderReplacement = true })) 163 | { 164 | dynamic arg = new EvalContext(runtime.Object); 165 | (context as EvalContext).Expression = () => context.var_instance = arg.new_object(); 166 | } 167 | Verify(runtime, "var instance = new object()"); 168 | } 169 | 170 | [Theory] 171 | [InlineData("__", true, "var _instance = new object()")] 172 | [InlineData("__", false, "var___instance = new__object()")] 173 | public void Given_a_dynamic_expression_And_settings_Then_should_execute_evaluation(string replacing, bool enabled, string expected) 174 | { 175 | var runtime = new Mock(); 176 | var settings = new EvalContextSettings { SpaceCharacterPlaceholder = replacing, EnableSpaceCharacterPlaceholderReplacement = enabled }; 177 | using (dynamic context = new EvalContext(runtime.Object, settings)) 178 | { 179 | dynamic arg = new EvalContext(runtime.Object, settings); 180 | //alt+166 181 | (context as EvalContext).Expression = () => context.var___instance = arg.new__object(); 182 | } 183 | Verify(runtime, expected); 184 | } 185 | 186 | [Fact] 187 | public async Task Given_a_string_of_javascript_And_a_value_is_returned_When_invoked_Then_should_execute_evaluation() 188 | { 189 | var runtime = new Mock(); 190 | var expected = "expected"; 191 | var script = "return " + expected; 192 | SetupReturnValue(runtime, expected, script); 193 | 194 | using (dynamic context = new EvalContext(runtime.Object)) 195 | { 196 | var actual = await (context as EvalContext).InvokeAsync(script); 197 | Assert.Equal(expected, actual); 198 | } 199 | Verify(runtime, script); 200 | } 201 | 202 | [Fact] 203 | public async Task Given_a_string_of_javascript_When_invoked_Then_should_execute_evaluation() 204 | { 205 | var runtime = new Mock(); 206 | var script = "Console.Write('expected')"; 207 | 208 | await (new EvalContext(runtime.Object).InvokeVoidAsync(script)); 209 | Verify(runtime, script); 210 | } 211 | 212 | [Fact] 213 | public async Task Given_a_dynamic_expression_And_a_value_is_returned_When_invoked_Then_should_execute_evaluation_And_return_value() 214 | { 215 | var runtime = new Mock(); 216 | var expected = "abc123"; 217 | var script = "document.cookie"; 218 | SetupReturnValue(runtime, expected, script); 219 | 220 | dynamic context = new EvalContext(runtime.Object); 221 | (context as EvalContext).Expression = () => context.document.cookie; 222 | var actual = await (context as EvalContext).InvokeAsync(); 223 | 224 | Assert.Equal(expected, actual); 225 | runtime.Verify(v => v.InvokeAsync($"BlazorDynamicJavascriptRuntime.evaluate", It.Is(m => m[0].ToString() == script)), Times.Once); 226 | } 227 | 228 | [Fact] 229 | public async Task Given_a_dynamic_expression_And_a_value_is_returned_by_index_When_invoked_Then_should_execute_evaluation_And_return_value() 230 | { 231 | var runtime = new Mock(); 232 | var script = "list[0]"; 233 | var expected = "abc123"; 234 | SetupReturnValue(runtime, expected, script); 235 | 236 | dynamic context = new EvalContext(runtime.Object); 237 | (context as EvalContext).Expression = () => context.list[0]; 238 | var actual = await (context as EvalContext).InvokeAsync(); 239 | 240 | Assert.Equal(expected, actual); 241 | runtime.Verify(v => v.InvokeAsync($"BlazorDynamicJavascriptRuntime.evaluate", It.Is(m => m[0].ToString() == script)), Times.Once); 242 | 243 | script = "list[\"index\"]"; 244 | SetupReturnValue(runtime, expected, script); 245 | 246 | context = new EvalContext(runtime.Object); 247 | (context as EvalContext).Expression = () => context.list["index"]; 248 | actual = await (context as EvalContext).InvokeAsync(); 249 | 250 | Assert.Equal(expected, actual); 251 | runtime.Verify(v => v.InvokeAsync($"BlazorDynamicJavascriptRuntime.evaluate", It.Is(m => m[0].ToString() == script)), Times.Once); 252 | } 253 | 254 | [Fact] 255 | public void Given_a_dynamic_expression_And_a_value_is_returned_When_invoked_synchronously_Then_should_execute_evaluation_And_return_value() 256 | { 257 | var runtime = new Mock(); 258 | var script = "method(\"1\")"; 259 | var expected = "2"; 260 | runtime.Setup(v => v.Invoke($"BlazorDynamicJavascriptRuntime.evaluate", It.Is(m => m[0].ToString() == script))).Returns(expected); 261 | 262 | dynamic context = new EvalContext(runtime.Object); 263 | (context as EvalContext).Expression = () => context.method("1"); 264 | var actual = (context as EvalContext).Invoke(); 265 | 266 | Assert.Equal(expected, actual); 267 | runtime.Verify(v => v.Invoke($"BlazorDynamicJavascriptRuntime.evaluate", It.Is(m => m[0].ToString() == script)), Times.Once); 268 | } 269 | 270 | [Fact] 271 | public void Given_a_string_of_javascript_And_a_value_is_returned_When_invoked_synchronously_Then_should_execute_evaluation_And_return_value() 272 | { 273 | var runtime = new Mock(); 274 | var script = "method(\"1\")"; 275 | var expected = "2"; 276 | runtime.Setup(v => v.Invoke($"BlazorDynamicJavascriptRuntime.evaluate", It.Is(m => m[0].ToString() == script))).Returns(expected); 277 | 278 | var actual = new EvalContext(runtime.Object).Invoke(script); 279 | 280 | Assert.Equal(expected, actual); 281 | runtime.Verify(v => v.Invoke($"BlazorDynamicJavascriptRuntime.evaluate", It.Is(m => m[0].ToString() == script)), Times.Once); 282 | } 283 | 284 | [Fact] 285 | public void Given_a_dynamic_expression_And_a_date_When_invoked_Then_should_execute_evaluation() 286 | { 287 | var runtime = new Mock(); 288 | using (dynamic context = new EvalContext(runtime.Object)) 289 | { 290 | dynamic arg = new EvalContext(runtime.Object); 291 | (context as EvalContext).Expression = () => context.date = new DateTime(2001, 1, 1, 1, 1, 1, DateTimeKind.Utc); 292 | } 293 | Verify(runtime, "date = new Date(\"2001-01-01T01:01:01.0000000+00:00\")"); 294 | } 295 | 296 | [Fact] 297 | public void Given_a_dynamic_expression_And_a_null_When_invoked_Then_should_execute_evaluation() 298 | { 299 | var runtime = new Mock(); 300 | using (dynamic context = new EvalContext(runtime.Object)) 301 | { 302 | dynamic arg = new EvalContext(runtime.Object); 303 | (context as EvalContext).Expression = () => context.something = new int?(); 304 | } 305 | Verify(runtime, "something = null"); 306 | } 307 | 308 | [Fact] 309 | public void Given_a_dynamic_expression_And_operators_When_invoked_Then_should_execute_evaluation() 310 | { 311 | var runtime = new Mock(); 312 | using (dynamic context = new EvalContext(runtime.Object)) 313 | { 314 | dynamic arg = new EvalContext(runtime.Object); 315 | (context as EvalContext).Expression = () => context.sum = 1 + 1 * 2 / 2; 316 | } 317 | Verify(runtime, "sum = 2"); 318 | } 319 | 320 | [Fact] 321 | public void Given_a_dynamic_expression_And_floating_point_sum_When_invoked_Then_should_derive_result_in_csharp_And_execute_evaluation() 322 | { 323 | var runtime = new Mock(); 324 | using (dynamic context = new EvalContext(runtime.Object)) 325 | { 326 | dynamic arg = new EvalContext(runtime.Object); 327 | (context as EvalContext).Expression = () => context.sum = 0.1 + 0.2 * 0.5 / 0.5; 328 | } 329 | Verify(runtime, "sum = 0.30000000000000004"); 330 | } 331 | 332 | [Fact] 333 | public void Given_a_dynamic_expression_And_decimal_sum_When_invoked_Then_should_derive_result_in_csharp_And_execute_evaluation() 334 | { 335 | var runtime = new Mock(); 336 | using (dynamic context = new EvalContext(runtime.Object)) 337 | { 338 | dynamic arg = new EvalContext(runtime.Object); 339 | (context as EvalContext).Expression = () => context.sum = 0.1m + 0.2m * 0.5m / 0.5m; 340 | } 341 | Verify(runtime, "sum = 0.3"); 342 | } 343 | 344 | [Fact] 345 | public async Task Given_multiple_dynamic_expressions_When_invoked_And_reset_And_invoked_Then_should_execute_multiple_evaluations() 346 | { 347 | var runtime = new Mock(); 348 | var expected = "value"; 349 | SetupReturnValue(runtime, expected, "instance.property"); 350 | using (dynamic context = new EvalContext(runtime.Object, new EvalContextSettings { EnableSpaceCharacterPlaceholderReplacement = true })) 351 | { 352 | var evalContext = (context as EvalContext); 353 | dynamic arg = new EvalContext(runtime.Object); 354 | evalContext.Expression = () => context.var_instance = arg.new_object(); 355 | await evalContext.InvokeVoidAsync(); 356 | evalContext.Reset(); 357 | evalContext.Expression = () => context.instance.property = expected; 358 | await evalContext.InvokeAsync(); 359 | evalContext.Reset(); 360 | arg = new EvalContext(runtime.Object); 361 | evalContext.Expression = () => context.instance.property; 362 | var actual = await evalContext.InvokeAsync(); 363 | Assert.Equal(expected, actual); 364 | } 365 | 366 | Verify(runtime, "var instance = new object()"); 367 | Verify(runtime, "instance.property = \"value\""); 368 | Verify(runtime, "instance.property"); 369 | } 370 | 371 | 372 | [Fact] 373 | public void Given_a_dynamic_expression_And_a_function_chain_is_called_When_invoked_Then_should_execute_evaluation() 374 | { 375 | var runtime = new Mock(); 376 | using (dynamic context = new EvalContext(runtime.Object)) 377 | { 378 | (context as EvalContext).Expression = () => context.jQuery("body").css("overflow-y", "hidden"); 379 | } 380 | 381 | Verify(runtime, "jQuery(\"body\").css(\"overflow-y\", \"hidden\")"); 382 | } 383 | 384 | [Fact] 385 | public void Given_a_dynamic_expression_And_anonymous_type_argument_is_supplied_When_invoked_Then_should_serialize_argument_And_execute_evaluation() 386 | { 387 | var runtime = new Mock(); 388 | using (dynamic context = new EvalContext(runtime.Object)) 389 | { 390 | 391 | var arg = new { Property = "Value", Field = 123, child = new { Member = new DateTime(2001, 1, 1) } }; 392 | (context as EvalContext).Expression = () => context.Bind(arg); 393 | } 394 | 395 | Verify(runtime, "Bind({\"property\":\"Value\",\"field\":123,\"child\":{\"member\":\"2001-01-01T00:00:00\"}})"); 396 | } 397 | 398 | private static void SetupReturnValue(Mock runtime, string expected, string script) 399 | { 400 | runtime.Setup(v => v.InvokeAsync($"BlazorDynamicJavascriptRuntime.evaluate", It.Is(m => m[0].ToString() == script))) 401 | .Returns(new ValueTask(Task.FromResult((T)Convert.ChangeType(expected, typeof(T))))); 402 | } 403 | 404 | private void Verify(Mock runtime, string script, int expectedInvocations = 1) 405 | { 406 | SpinWait.SpinUntil(() => runtime.Invocations.Count == expectedInvocations, TimeSpan.FromSeconds(1)); 407 | runtime.Verify(v => v.InvokeAsync($"BlazorDynamicJavascriptRuntime.evaluate", It.Is(m => m[0].ToString() == script)), Times.Once); 408 | } 409 | 410 | private void Verify(Mock runtime, string script, int expectedInvocations = 1) 411 | { 412 | SpinWait.SpinUntil(() => runtime.Invocations.Count == expectedInvocations, TimeSpan.FromSeconds(1)); 413 | runtime.Verify(v => v.InvokeAsync($"BlazorDynamicJavascriptRuntime.evaluate", It.Is(m => m[0].ToString() == script)), Times.Once); 414 | } 415 | 416 | } 417 | } 418 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.3.32922.545 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.DynamicJavascriptRuntime.Evaluator", "Blazor.DynamicJavascriptRuntime.Evaluator\Blazor.DynamicJavascriptRuntime.Evaluator.csproj", "{0BBC05CC-450B-4F02-B1C1-BA6F37D68F64}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.DynamicJavascriptRuntime.Evaluator.Tests", "Blazor.DynamicJavascriptRuntime.Evaluator.Tests\Blazor.DynamicJavascriptRuntime.Evaluator.Tests.csproj", "{87A44053-DCF8-4F05-8377-DD000A6DD80E}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9223D055-9A13-40ED-82E3-4535AE43D28F}" 11 | ProjectSection(SolutionItems) = preProject 12 | README.md = README.md 13 | EndProjectSection 14 | EndProject 15 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client", "Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client\Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client.csproj", "{23D543D0-77CE-4273-B3FF-867E6D54F15A}" 16 | EndProject 17 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.DynamicJavascriptRuntime.Evaluator.SystemTests", "Blazor.DynamicJavascriptRuntime.Evaluator.SystemTests\Blazor.DynamicJavascriptRuntime.Evaluator.SystemTests.csproj", "{DD27E4E5-640C-48F9-9915-2FD882E10708}" 18 | EndProject 19 | Global 20 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 21 | Debug|Any CPU = Debug|Any CPU 22 | Debug|x64 = Debug|x64 23 | Release|Any CPU = Release|Any CPU 24 | Release|x64 = Release|x64 25 | EndGlobalSection 26 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 27 | {0BBC05CC-450B-4F02-B1C1-BA6F37D68F64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 28 | {0BBC05CC-450B-4F02-B1C1-BA6F37D68F64}.Debug|Any CPU.Build.0 = Debug|Any CPU 29 | {0BBC05CC-450B-4F02-B1C1-BA6F37D68F64}.Debug|x64.ActiveCfg = Debug|Any CPU 30 | {0BBC05CC-450B-4F02-B1C1-BA6F37D68F64}.Debug|x64.Build.0 = Debug|Any CPU 31 | {0BBC05CC-450B-4F02-B1C1-BA6F37D68F64}.Release|Any CPU.ActiveCfg = Release|Any CPU 32 | {0BBC05CC-450B-4F02-B1C1-BA6F37D68F64}.Release|Any CPU.Build.0 = Release|Any CPU 33 | {0BBC05CC-450B-4F02-B1C1-BA6F37D68F64}.Release|x64.ActiveCfg = Release|Any CPU 34 | {0BBC05CC-450B-4F02-B1C1-BA6F37D68F64}.Release|x64.Build.0 = Release|Any CPU 35 | {87A44053-DCF8-4F05-8377-DD000A6DD80E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 36 | {87A44053-DCF8-4F05-8377-DD000A6DD80E}.Debug|Any CPU.Build.0 = Debug|Any CPU 37 | {87A44053-DCF8-4F05-8377-DD000A6DD80E}.Debug|x64.ActiveCfg = Debug|Any CPU 38 | {87A44053-DCF8-4F05-8377-DD000A6DD80E}.Debug|x64.Build.0 = Debug|Any CPU 39 | {87A44053-DCF8-4F05-8377-DD000A6DD80E}.Release|Any CPU.ActiveCfg = Release|Any CPU 40 | {87A44053-DCF8-4F05-8377-DD000A6DD80E}.Release|Any CPU.Build.0 = Release|Any CPU 41 | {87A44053-DCF8-4F05-8377-DD000A6DD80E}.Release|x64.ActiveCfg = Release|Any CPU 42 | {87A44053-DCF8-4F05-8377-DD000A6DD80E}.Release|x64.Build.0 = Release|Any CPU 43 | {23D543D0-77CE-4273-B3FF-867E6D54F15A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 44 | {23D543D0-77CE-4273-B3FF-867E6D54F15A}.Debug|Any CPU.Build.0 = Debug|Any CPU 45 | {23D543D0-77CE-4273-B3FF-867E6D54F15A}.Debug|x64.ActiveCfg = Debug|Any CPU 46 | {23D543D0-77CE-4273-B3FF-867E6D54F15A}.Debug|x64.Build.0 = Debug|Any CPU 47 | {23D543D0-77CE-4273-B3FF-867E6D54F15A}.Release|Any CPU.ActiveCfg = Release|Any CPU 48 | {23D543D0-77CE-4273-B3FF-867E6D54F15A}.Release|Any CPU.Build.0 = Release|Any CPU 49 | {23D543D0-77CE-4273-B3FF-867E6D54F15A}.Release|x64.ActiveCfg = Release|Any CPU 50 | {23D543D0-77CE-4273-B3FF-867E6D54F15A}.Release|x64.Build.0 = Release|Any CPU 51 | {DD27E4E5-640C-48F9-9915-2FD882E10708}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 52 | {DD27E4E5-640C-48F9-9915-2FD882E10708}.Debug|Any CPU.Build.0 = Debug|Any CPU 53 | {DD27E4E5-640C-48F9-9915-2FD882E10708}.Debug|x64.ActiveCfg = Debug|Any CPU 54 | {DD27E4E5-640C-48F9-9915-2FD882E10708}.Debug|x64.Build.0 = Debug|Any CPU 55 | {DD27E4E5-640C-48F9-9915-2FD882E10708}.Release|Any CPU.ActiveCfg = Release|Any CPU 56 | {DD27E4E5-640C-48F9-9915-2FD882E10708}.Release|Any CPU.Build.0 = Release|Any CPU 57 | {DD27E4E5-640C-48F9-9915-2FD882E10708}.Release|x64.ActiveCfg = Release|Any CPU 58 | {DD27E4E5-640C-48F9-9915-2FD882E10708}.Release|x64.Build.0 = Release|Any CPU 59 | EndGlobalSection 60 | GlobalSection(SolutionProperties) = preSolution 61 | HideSolutionNode = FALSE 62 | EndGlobalSection 63 | GlobalSection(ExtensibilityGlobals) = postSolution 64 | SolutionGuid = {B15E7B0F-F30D-47B4-BCA7-691510AA33BD} 65 | EndGlobalSection 66 | EndGlobal 67 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator/Blazor.DynamicJavascriptRuntime.Evaluator.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | jameschch 6 | 7 | icon.ico 8 | true 9 | 1.4.1.0 10 | https://github.com/jameschch/Blazor.DynamicJavascriptRuntime.Evaluator/blob/master/LICENSE 11 | https://github.com/jameschch/Blazor.DynamicJavascriptRuntime.Evaluator 12 | https://raw.githubusercontent.com/jameschch/Blazor.DynamicJavascriptRuntime.Evaluator/master/Blazor.DynamicJavascriptRuntime.Evaluator/icon.png 13 | DynamicJavascriptRuntime.Blazor.Evaluator 14 | Execute dynamic object expressions as Javascript in Blazor WebAssembly 15 | blazor javascript dynamic JSRuntime JSInterop evaluator Blazor razor JS gasmask wasm webassembly 16 | Blazor DynamicJavascriptRuntime Evaluator 17 | 18 | 1.4.1.0 19 | 1.4.1.0 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator/EvalContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.JSInterop; 2 | using System; 3 | using System.Diagnostics; 4 | using System.Dynamic; 5 | using System.Linq; 6 | using System.Runtime.CompilerServices; 7 | using System.Text; 8 | using System.Text.Json; 9 | using System.Text.RegularExpressions; 10 | using System.Threading.Tasks; 11 | 12 | namespace Blazor.DynamicJavascriptRuntime.Evaluator 13 | { 14 | 15 | public class EvalContext : DynamicObject, IDisposable, IEvalContext 16 | { 17 | 18 | private readonly StringBuilder _script; 19 | private bool _hasInvoked; 20 | private readonly IJSRuntime _runtime; 21 | private readonly EvalContextSettings _settings; 22 | private string _escaped; 23 | 24 | public Func Expression { get; set; } 25 | 26 | public EvalContext(IJSRuntime runtime) 27 | { 28 | _runtime = runtime; 29 | _script = new StringBuilder(); 30 | _settings = new EvalContextSettings(); 31 | } 32 | 33 | public EvalContext(IJSRuntime runtime, EvalContextSettings settings) 34 | { 35 | _runtime = runtime; 36 | _settings = settings; 37 | _script = new StringBuilder(); 38 | } 39 | 40 | private void Append(string value) 41 | { 42 | if (_script.Length > 0) 43 | { 44 | _script.Append('.'); 45 | } 46 | 47 | _script.Append(value); 48 | } 49 | 50 | public string this[string i] 51 | { 52 | set 53 | { 54 | SetIndexer(i, value); 55 | } 56 | } 57 | 58 | private void SetIndexer(string index, string value) 59 | { 60 | if (!_hasInvoked) 61 | { 62 | _script.Append('['); 63 | _script.Append(Massage(index)); 64 | _script.Append(']'); 65 | _script.Append(" = "); 66 | _script.Append(Massage(value)); 67 | } 68 | } 69 | 70 | public override bool TryInvoke(InvokeBinder binder, object[] args, out object result) 71 | { 72 | if (!_hasInvoked) 73 | { 74 | Append(binder.CallInfo.ArgumentNames.Single()); 75 | } 76 | result = this; 77 | return true; 78 | } 79 | 80 | public override bool TryGetMember(GetMemberBinder binder, out object result) 81 | { 82 | if (!_hasInvoked) 83 | { 84 | Append(binder.Name); 85 | } 86 | result = this; 87 | return true; 88 | } 89 | 90 | public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) 91 | { 92 | if (!_hasInvoked) 93 | { 94 | var isString = indexes[0] is string; 95 | if (indexes[0] is EvalContext || indexes[0] is int || isString) 96 | { 97 | _script.Append('['); 98 | var value = isString ? Massage(indexes[0]) : indexes[0].ToString(); 99 | _script.Append(value); 100 | _script.Append(']'); 101 | } 102 | else 103 | { 104 | throw new Exception("Unexpected indexer type"); 105 | } 106 | } 107 | 108 | result = this; 109 | return true; 110 | } 111 | 112 | public override bool TryCreateInstance(CreateInstanceBinder binder, object[] args, out object result) 113 | { 114 | if (!_hasInvoked) 115 | { 116 | Append(binder.CallInfo.ArgumentNames.Single()); 117 | } 118 | result = this; 119 | return true; 120 | } 121 | 122 | public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) 123 | { 124 | if (!_hasInvoked) 125 | { 126 | if (_script.Length > 0) 127 | { 128 | _script.Append('.'); 129 | } 130 | 131 | var attempts = args.Select(a => Massage(a)); 132 | 133 | _script.Append($"{binder.Name}({string.Join(", ", attempts)})"); 134 | } 135 | 136 | result = this; 137 | return true; 138 | } 139 | 140 | public override bool TrySetMember(SetMemberBinder binder, object value) 141 | { 142 | if (!_hasInvoked) 143 | { 144 | Append(binder.Name); 145 | _script.Append(" = "); 146 | _script.Append(Massage(value)); 147 | } 148 | return true; 149 | } 150 | 151 | private object Massage(object value) 152 | { 153 | if (value == null) 154 | { 155 | return "null"; 156 | } 157 | if (value is string) 158 | { 159 | var delimiter = Regex.IsMatch(value.ToString(), "[\r\n]") ? '`' : '\"'; 160 | return $"{delimiter}{value.ToString().Replace("'", "\u0027").Replace("\"", "\\u0022")}{delimiter}"; 161 | } 162 | else if (value is DateTime) 163 | { 164 | return "new Date(\u0022" + ((DateTime)value).ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz") + "\u0022)"; 165 | } 166 | else if (IsAnonymousType(value.GetType()) || _settings.SerializableTypes.Contains(value.GetType())) 167 | { 168 | return JsonSerializer.Serialize(value, _settings.JsonSerializerOptions); 169 | } 170 | //todo: support enums 171 | return value; 172 | } 173 | 174 | 175 | /// 176 | /// Returns the Javascript expression as a string 177 | /// 178 | /// the Javascript expression 179 | public override string ToString() 180 | { 181 | if (!_hasInvoked) 182 | { 183 | Build(); 184 | } 185 | return _escaped; 186 | } 187 | 188 | /// 189 | /// Invokes the Javascript expression and returns a value 190 | /// 191 | /// The return type 192 | /// The value returned from Javascript 193 | public async virtual ValueTask InvokeAsync() 194 | { 195 | if (!_hasInvoked) 196 | { 197 | Build(); 198 | } 199 | return await InvokeAsync(_escaped); 200 | } 201 | 202 | /// 203 | /// Invokes the Javascript expression 204 | /// 205 | public async virtual ValueTask InvokeVoidAsync() 206 | { 207 | await InvokeAsync(); 208 | } 209 | 210 | /// 211 | /// Invokes the Javascript expression and returns a value 212 | /// 213 | /// The return type 214 | /// The value returned from Javascript 215 | public virtual T Invoke() 216 | { 217 | if (!_hasInvoked) 218 | { 219 | Build(); 220 | } 221 | return Invoke(_escaped); 222 | } 223 | 224 | /// 225 | /// Invokes the Javascript string and returns a value 226 | /// 227 | /// The return type 228 | /// The value returned from Javascript 229 | public virtual T Invoke(string script) 230 | { 231 | _hasInvoked = true; 232 | #if DEBUG 233 | Debug.WriteLine("BDJR: " + script); 234 | #else 235 | if (_settings.EnableDebugLogging) 236 | { 237 | Debug.WriteLine("BDJR: " + script); 238 | } 239 | #endif 240 | 241 | return ((IJSInProcessRuntime)_runtime).Invoke("BlazorDynamicJavascriptRuntime.evaluate", script); 242 | } 243 | 244 | /// 245 | /// Executes the provided string of Javascript and returns a value 246 | /// 247 | /// The return type 248 | /// A string of Javascript 249 | /// 250 | public async virtual ValueTask InvokeAsync(string script) 251 | { 252 | _hasInvoked = true; 253 | #if DEBUG 254 | Debug.WriteLine("BDJR: " + script); 255 | #else 256 | if (_settings.EnableDebugLogging) 257 | { 258 | Debug.WriteLine("BDJR: " + script); 259 | } 260 | #endif 261 | 262 | return await _runtime.InvokeAsync("BlazorDynamicJavascriptRuntime.evaluate", script); 263 | } 264 | 265 | /// 266 | /// Executes the provided string of Javascript 267 | /// 268 | /// A string of Javascript 269 | public async virtual ValueTask InvokeVoidAsync(string script) 270 | { 271 | await InvokeAsync(script); 272 | } 273 | 274 | public void Dispose() 275 | { 276 | if (!_hasInvoked) 277 | { 278 | Build(); 279 | Task.Run(async () => await InvokeAsync(_escaped)); 280 | } 281 | } 282 | 283 | /// 284 | /// Resets the context allowing for multiple invocations on a single instance 285 | /// 286 | public void Reset() 287 | { 288 | Expression = null; 289 | _script.Clear(); 290 | _hasInvoked = false; 291 | _escaped = null; 292 | } 293 | 294 | private void Build() 295 | { 296 | Expression?.Invoke(); 297 | _hasInvoked = true; 298 | _escaped = _settings.EnableSpaceCharacterPlaceholderReplacement ? _script.ToString().Replace(_settings.SpaceCharacterPlaceholder, " ") 299 | : _script.ToString(); 300 | 301 | } 302 | 303 | private static bool IsAnonymousType(Type type) 304 | { 305 | return type.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length > 0 && type.FullName.Contains("AnonymousType"); 306 | } 307 | 308 | } 309 | 310 | } 311 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator/EvalContextSettings.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Text.Json; 5 | 6 | namespace Blazor.DynamicJavascriptRuntime.Evaluator 7 | { 8 | public class EvalContextSettings 9 | { 10 | 11 | /// 12 | /// If set to true will disable replacing instance of the space character placeholder with a space 13 | /// 14 | public bool EnableSpaceCharacterPlaceholderReplacement { get; set; } 15 | 16 | /// 17 | /// Allows the definition of a placeholder for the space character. Defaults to underscore. 18 | /// 19 | public string SpaceCharacterPlaceholder { get; set; } = "_"; 20 | 21 | 22 | /// 23 | /// Forces executed Javascript to be logged to the debug output 24 | /// 25 | public bool EnableDebugLogging { get; set; } 26 | 27 | /// 28 | /// Allows options to be specifed for Json Serialization of arguments 29 | /// 30 | public JsonSerializerOptions JsonSerializerOptions { get; set; } = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, IgnoreNullValues = true }; 31 | 32 | /// 33 | /// Specified types will be serialized into Javascript objects 34 | /// 35 | public IList SerializableTypes { get; } = new List(); 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator/IEvalContext.cs: -------------------------------------------------------------------------------- 1 | using System.Dynamic; 2 | using System.Threading.Tasks; 3 | 4 | namespace Blazor.DynamicJavascriptRuntime.Evaluator 5 | { 6 | public interface IEvalContext 7 | { 8 | string this[string i] { set; } 9 | 10 | System.Func Expression { get; set; } 11 | 12 | void Dispose(); 13 | ValueTask InvokeAsync(); 14 | ValueTask InvokeAsync(string script); 15 | string ToString(); 16 | bool TryCreateInstance(CreateInstanceBinder binder, object[] args, out object result); 17 | bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result); 18 | bool TryGetMember(GetMemberBinder binder, out object result); 19 | bool TryInvoke(InvokeBinder binder, object[] args, out object result); 20 | bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result); 21 | bool TrySetMember(SetMemberBinder binder, object value); 22 | } 23 | } -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator/JavascriptLiteral/Literal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Text.Json.Serialization; 6 | using System.Threading.Tasks; 7 | 8 | namespace Blazor.DynamicJavascriptRuntime.Evaluator.JavascriptLiteral 9 | { 10 | [JsonConverter(typeof(LiteralConverter))] 11 | public class Literal 12 | { 13 | public string Value { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator/JavascriptLiteral/LiteralConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Text.Json; 6 | using System.Text.Json.Serialization; 7 | using System.Threading.Tasks; 8 | 9 | namespace Blazor.DynamicJavascriptRuntime.Evaluator.JavascriptLiteral 10 | { 11 | public class LiteralConverter : JsonConverter 12 | { 13 | public override Literal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) 14 | { 15 | throw new NotImplementedException(); 16 | } 17 | 18 | public override void Write(Utf8JsonWriter writer, Literal value, JsonSerializerOptions options) 19 | { 20 | writer.WriteRawValue(value.Value, true); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:58821/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "environmentVariables": { 15 | "ASPNETCORE_ENVIRONMENT": "Development" 16 | } 17 | }, 18 | "Blazor.DynamicJavascriptRuntime.Evaluator": { 19 | "commandName": "Project", 20 | "launchBrowser": true, 21 | "environmentVariables": { 22 | "ASPNETCORE_ENVIRONMENT": "Development" 23 | }, 24 | "applicationUrl": "http://localhost:58822/" 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator/icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jameschch/Blazor.DynamicJavascriptRuntime.Evaluator/4f57a0105ed291973524ccf34c84de2c293488b4/Blazor.DynamicJavascriptRuntime.Evaluator/icon.ico -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jameschch/Blazor.DynamicJavascriptRuntime.Evaluator/4f57a0105ed291973524ccf34c84de2c293488b4/Blazor.DynamicJavascriptRuntime.Evaluator/icon.png -------------------------------------------------------------------------------- /Blazor.DynamicJavascriptRuntime.Evaluator/wwwroot/BlazorDynamicJavascriptRuntime.js: -------------------------------------------------------------------------------- 1 | window.BlazorDynamicJavascriptRuntime = { 2 | 3 | evaluate: function (script) { 4 | return eval(script); 5 | } 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017 jameschch 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Blazor.DynamicJavascriptRuntime.Evaluator (A.K.A. Gasmask) 2 | 3 | Wouldn't it be really useful if you could run a line or two of Javascript in your Blazor C# app? 4 | Wouldn't it be handy if you could execute arbitrary Javascript at runtime without strings of script? 5 | Wouldn't it be a big plus if the Javascript code was declared as a dynamic object expression and could be exposed to unit tests? 6 | Wouldn't it be nice if you could consume Javascript library API's without creating interop wrappers? 7 | 8 | Calling Javascript dynamically from C# couldn't be easier: 9 | 10 | ```csharp 11 | var context = (new EvalContext(JSRuntimeInstance) as dynamic).window.location = "www.github.com"; 12 | await (context as EvalContext).InvokeAsync(); 13 | //window.location = "www.github.com"; 14 | ``` 15 | 16 | ...or with alternate syntax: 17 | 18 | ```csharp 19 | using (dynamic context = new EvalContext(JSRuntimeInstance)) 20 | { 21 | (context as EvalContext).Expression = () => context.window.location = "www.github.com"; 22 | //window.location = "www.github.com"; 23 | } 24 | ``` 25 | 26 | When it comes to executing code with arguments, the C# parser will support this: 27 | 28 | ```csharp 29 | using (dynamic context = new EvalContext(JSRuntimeInstance)) 30 | { 31 | dynamic arg = new EvalContext(JSRuntimeInstance); 32 | dynamic arg2 = new EvalContext(JSRuntimeInstance); 33 | (context as EvalContext).Expression = () => context.Chart.config.data.datasets[arg.i].data.push(arg2.config.data.datasets[arg.i].data); 34 | //Chart.config.data.datasets[i].data.push(config.data.datasets[i].data); 35 | } 36 | ``` 37 | 38 | Need to call some script that returns a value? No problem: 39 | 40 | ```csharp 41 | dynamic context = new EvalContext(runtime.Object); 42 | (context as EvalContext).Expression = () => context.document.cookie; 43 | var cookie = await (context as EvalContext).InvokeAsync(); 44 | //document.cookie 45 | ``` 46 | 47 | In order to satisfy the C# parser, an underscore ("_") can stand in for a space character in Javascript. This is disabled by default and you can configure your space placeholder: 48 | 49 | ```csharp 50 | using (dynamic context = new EvalContext(JSRuntimeInstance)) 51 | { 52 | var settings = new EvalContextSettings { EnableSpaceCharacterPlaceholderReplacement = true, SpaceCharacterPlaceholder = "_" }; 53 | dynamic arg = new EvalContext(JSRuntimeInstance, settings); 54 | (context as EvalContext).Expression = () => context.var_instance = arg.new_object(); 55 | //var instance = new object(); 56 | } 57 | ``` 58 | 59 | The dynamic expression is eagerly evaluated. This means decimal arithmetic will not be mangled by Javascript: 60 | 61 | ```csharp 62 | using (dynamic context = new EvalContext(JSRuntimeInstance)) 63 | { 64 | (context as EvalContext).Expression = () => context.sum = 0.1M + 0.2M * 0.5M / 0.5M; 65 | //sum = 0.3; 66 | } 67 | ``` 68 | 69 | Maybe you feel like a bit of JQuery? 70 | 71 | ```csharp 72 | using (dynamic context = new EvalContext(JsRuntime)) 73 | { 74 | (context as EvalContext).Expression = () => context.jQuery("body").css("overflow-y", "hidden"); 75 | //jQuery("body").css("overflow-y", "hidden") 76 | } 77 | ``` 78 | Sorry, no $ allowed. 79 | 80 | How about passing complex types as arguments? We've got you covered for anonymous types: 81 | 82 | ```csharp 83 | using (dynamic context = new EvalContext(JsRuntime)) 84 | { 85 | var arg = new { Property = "Value", Field = 123, Child = new { Member = new DateTime(2001, 1, 1) } }; 86 | (context as EvalContext).Expression = () => context.myScript.set(arg); 87 | //myScript.set({"property":"Value","field":123,"child":{"member":"2001-01-01T00:00:00"}}) 88 | } 89 | ``` 90 | 91 | Passing user-defined types takes more effort, but not too much: 92 | 93 | ```csharp 94 | var settings = new EvalContextSettings(); 95 | settings.SerializableTypes.Add(typeof(Specified)); 96 | using (dynamic context = new EvalContext(JsRuntime, settings)) 97 | { 98 | var arg = new Specified { Member = "abc" }; 99 | (context as EvalContext).Expression = () => context.myScript.setSpecified(arg); 100 | //myScript.setSpecified({"member":"abc"}) 101 | } 102 | ``` 103 | 104 | The execution of Javascript is performed with the eval() function, so it's imperative to sanitize user input that's passed into the Javascript runtime. You have been warned. 105 | 106 | ## Setup 107 | 108 | First, install from nuget: 109 | 110 | ``` 111 | Install-Package DynamicJavascriptRuntime.Blazor.Evaluator 112 | ``` 113 | 114 | [https://www.nuget.org/packages/DynamicJavascriptRuntime.Blazor.Evaluator/](https://www.nuget.org/packages/DynamicJavascriptRuntime.Blazor.Evaluator/) 115 | 116 | You then need to add a script include to your index.htm: 117 | 118 | ```html 119 | 120 | ``` 121 | 122 | ## Syntax 123 | 124 | The are a few different syntax options for dynamic expressions: 125 | 126 | - The ```using``` blocks wrapping the EvalContext are optional but prevent forgotten calls to Invoke. 127 | - Setting the ```Expression``` property is optional. You can chain an expression directly on the EvalContext e.g.: 128 | ```csharp 129 | dynamic context = new EvalContext(); 130 | context.alert("Gasmask"); 131 | await (context as EvalContext).InvokeVoidAsync() 132 | ``` 133 | - You can chain multiple javascript calls on separate lines with a single EvalContext. This makes sense for some "fluent" libraries like jQuery. 134 | --------------------------------------------------------------------------------