File tree Expand file tree Collapse file tree 4 files changed +34
-0
lines changed
JSInterop/Microsoft.JSInterop.JS/src/src Expand file tree Collapse file tree 4 files changed +34
-0
lines changed Original file line number Diff line number Diff 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" ,
Original file line number Diff line number Diff line change 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 >
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" ));
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" );
Original file line number Diff line number Diff 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+
376381function addViaJSObjectReference ( jsObjectReference , a , b ) {
377382 return jsObjectReference . nested . add ( a , b ) ;
378383}
Original file line number Diff line number Diff 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 ) {
You can’t perform that action at this time.
0 commit comments