├── Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client
├── Pages
│ ├── Info.razor
│ ├── Index.razor
│ └── IndexPage.cs
├── Shared
│ └── MainLayout.razor
├── App.razor
├── wwwroot
│ ├── JsInterop.js
│ └── index.html
├── _Imports.razor
├── Program.cs
├── Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client.csproj
└── Properties
│ └── launchSettings.json
├── Blazor.DynamicJavascriptRuntime.Evaluator
├── icon.ico
├── icon.png
├── wwwroot
│ └── BlazorDynamicJavascriptRuntime.js
├── JavascriptLiteral
│ ├── Literal.cs
│ └── LiteralConverter.cs
├── Properties
│ └── launchSettings.json
├── IEvalContext.cs
├── EvalContextSettings.cs
├── Blazor.DynamicJavascriptRuntime.Evaluator.csproj
└── EvalContext.cs
├── Blazor.DynamicJavascriptRuntime.Evaluator.Tests
├── Blazor.DynamicJavascriptRuntime.Evaluator.Tests.csproj
└── EvalContextTest.cs
├── Blazor.DynamicJavascriptRuntime.Evaluator.SystemTests
├── Blazor.DynamicJavascriptRuntime.Evaluator.SystemTests.csproj
└── EvalContextSystemTest.cs
├── .gitattributes
├── Blazor.DynamicJavascriptRuntime.Evaluator.sln
├── README.md
├── .gitignore
└── LICENSE
/Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/Pages/Info.razor:
--------------------------------------------------------------------------------
1 | @page "/Info"
2 |
--------------------------------------------------------------------------------
/Blazor.DynamicJavascriptRuntime.Evaluator.Tests.Client/Shared/MainLayout.razor:
--------------------------------------------------------------------------------
1 | @inherits LayoutComponentBase
2 |
3 | @Body
4 |
5 |
--------------------------------------------------------------------------------
/Blazor.DynamicJavascriptRuntime.Evaluator/icon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jameschch/Blazor.DynamicJavascriptRuntime.Evaluator/HEAD/Blazor.DynamicJavascriptRuntime.Evaluator/icon.ico
--------------------------------------------------------------------------------
/Blazor.DynamicJavascriptRuntime.Evaluator/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jameschch/Blazor.DynamicJavascriptRuntime.Evaluator/HEAD/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 |
--------------------------------------------------------------------------------
/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/App.razor:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Sorry, there's nothing at this address.
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/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.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/_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/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.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/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/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.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.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/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.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.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/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/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.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 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/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.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 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/.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/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