Skip to content

Commit 65cebb7

Browse files
authored
Added replacers to the JSInterop (#63542)
1 parent a813ad4 commit 65cebb7

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

src/Components/test/E2ETest/Tests/InteropTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public void CanInvokeInteropMethods()
7878
["testDtoAsync"] = "Same",
7979
["returnPrimitiveAsync"] = "123",
8080
["returnArrayAsync"] = "first,second",
81+
["elementReference"] = "Success",
8182
["jsObjectReference.identity"] = "Invoked from JSObjectReference",
8283
["jsObjectReference.nested.add"] = "5",
8384
["addViaJSObjectReference"] = "5",

src/Components/test/testassets/BasicTestApp/InteropComponent.razor

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<h2>@nameof(JSObjectReferenceInvokeNonFunctionException)</h2>
5252
<p id="@nameof(JSObjectReferenceInvokeNonFunctionException)">@JSObjectReferenceInvokeNonFunctionException?.Message</p>
5353
</div>
54+
55+
<p @ref="element">Element reference.</p>
56+
5457
@if (DoneWithInterop)
5558
{
5659
<p id="done-with-interop">Done with interop.</p>
@@ -70,6 +73,8 @@
7073

7174
public bool DoneWithInterop { get; set; }
7275

76+
public ElementReference element;
77+
7378
public async Task InvokeInteropAsync()
7479
{
7580
var shouldSupportSyncInterop = RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"));
@@ -167,6 +172,15 @@
167172
ReturnValues["invokeAsyncThrowsSerializingCircularStructure"] = $"Failure: {ex.Message}";
168173
}
169174

175+
try
176+
{
177+
var elementReference = await JSRuntime.InvokeAsync<ElementReference>("returnElementReference", element);
178+
ReturnValues["elementReference"] = "Success";
179+
}
180+
catch (Exception ex)
181+
{
182+
ReturnValues["elementReference"] = $"Failure: {ex.Message}";
183+
}
170184

171185
var jsObjectReference = await JSRuntime.InvokeAsync<IJSObjectReference>("returnJSObjectReference");
172186
ReturnValues["jsObjectReference.identity"] = await jsObjectReference.InvokeAsync<string>("identity", "Invoked from JSObjectReference");

src/Components/test/testassets/BasicTestApp/wwwroot/js/jsinteroptests.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ window.jsInteropTests = {
254254
receiveDotNetObjectByRefAsync: receiveDotNetObjectByRefAsync,
255255
receiveDotNetStreamReference: receiveDotNetStreamReference,
256256
receiveDotNetStreamWrapperReference: receiveDotNetStreamWrapperReference,
257+
returnElementReference: returnElementReference,
257258
TestClass: TestClass,
258259
nonConstructorFunction: () => { return 42; },
259260
testObject: testObject,
@@ -373,6 +374,10 @@ function returnJSObjectReference() {
373374
};
374375
}
375376

377+
function returnElementReference(element) {
378+
return element;
379+
}
380+
376381
function addViaJSObjectReference(jsObjectReference, a, b) {
377382
return jsObjectReference.nested.add(a, b);
378383
}

src/JSInterop/Microsoft.JSInterop.JS/src/src/Microsoft.JSInterop.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export module DotNet {
99

1010
const jsObjectIdKey = "__jsObjectId";
1111
const dotNetObjectRefKey = "__dotNetObject";
12+
const dotNetElementRefKey = "__internalId";
1213
const byteArrayRefKey = "__byte[]";
1314
const dotNetStreamRefKey = "__dotNetStream";
1415
const jsStreamReferenceLengthKey = "__jsStreamReferenceLength";
@@ -807,7 +808,20 @@ export module DotNet {
807808
return result;
808809
}
809810

811+
function getCaptureIdFromElement(element: Element): string | null {
812+
for (let i = 0; i < element.attributes.length; i++) {
813+
const attr = element.attributes[i];
814+
if (attr.name.startsWith('_bl_')) {
815+
return attr.name.substring(4);
816+
}
817+
}
818+
return null;
819+
}
820+
810821
function argReplacer(key: string, value: any) {
822+
if (value instanceof Element) {
823+
return { [dotNetElementRefKey]: getCaptureIdFromElement(value) };
824+
}
811825
if (value instanceof DotNetObject) {
812826
return value.serializeAsArg();
813827
} else if (value instanceof Uint8Array) {

0 commit comments

Comments
 (0)