@@ -343,31 +343,72 @@ export default class Agent extends EventEmitter<{
343
343
return renderer . getInstanceAndStyle ( id ) ;
344
344
}
345
345
346
- getBestMatchingRendererInterface ( node : Object ) : RendererInterface | null {
347
- let bestMatch = null ;
346
+ getIDForDOMNode ( node : HTMLElement ) : number | null {
347
+ let bestMatch : null | HTMLElement = null ;
348
+ let bestRenderer : null | RendererInterface = null ;
349
+ // Find the nearest ancestor which is mounted by a React.
348
350
for ( const rendererID in this . _rendererInterfaces ) {
349
351
const renderer = ( ( this . _rendererInterfaces [
350
352
( rendererID : any )
351
353
] : any ) : RendererInterface ) ;
352
- const fiber = renderer . getFiberForNative ( node ) ;
353
- if ( fiber !== null ) {
354
- // check if fiber.stateNode is matching the original hostInstance
355
- if ( fiber . stateNode === node ) {
356
- return renderer ;
357
- } else if ( bestMatch === null ) {
358
- bestMatch = renderer ;
354
+ const nearestNode = renderer . getNearestMountedHostInstance ( node ) ;
355
+ if ( nearestNode !== null ) {
356
+ if ( nearestNode === node ) {
357
+ // Exact match we can exit early.
358
+ bestMatch = nearestNode ;
359
+ bestRenderer = renderer ;
360
+ break ;
361
+ }
362
+ if ( bestMatch === null || bestMatch . contains ( nearestNode ) ) {
363
+ // If this is the first match or the previous match contains the new match,
364
+ // so the new match is a deeper and therefore better match.
365
+ bestMatch = nearestNode ;
366
+ bestRenderer = renderer ;
359
367
}
360
368
}
361
369
}
362
- // if an exact match is not found, return the first valid renderer as fallback
363
- return bestMatch ;
370
+ if ( bestRenderer != null && bestMatch != null ) {
371
+ try {
372
+ return bestRenderer . getElementIDForHostInstance ( bestMatch , true ) ;
373
+ } catch ( error ) {
374
+ // Some old React versions might throw if they can't find a match.
375
+ // If so we should ignore it...
376
+ }
377
+ }
378
+ return null ;
364
379
}
365
380
366
- getIDForNode ( node : Object ) : number | null {
367
- const rendererInterface = this . getBestMatchingRendererInterface ( node ) ;
368
- if ( rendererInterface != null ) {
381
+ getComponentNameForDOMNode ( node : HTMLElement ) : string | null {
382
+ let bestMatch : null | HTMLElement = null ;
383
+ let bestRenderer : null | RendererInterface = null ;
384
+ // Find the nearest ancestor which is mounted by a React.
385
+ // We duplicate this code from getIDForDOMNode to avoid an object allocation.
386
+ for ( const rendererID in this . _rendererInterfaces ) {
387
+ const renderer = ( ( this . _rendererInterfaces [
388
+ ( rendererID : any )
389
+ ] : any ) : RendererInterface ) ;
390
+ const nearestNode = renderer . getNearestMountedHostInstance ( node ) ;
391
+ if ( nearestNode !== null ) {
392
+ if ( nearestNode === node ) {
393
+ // Exact match we can exit early.
394
+ bestMatch = nearestNode ;
395
+ bestRenderer = renderer ;
396
+ break ;
397
+ }
398
+ if ( bestMatch === null || bestMatch . contains ( nearestNode ) ) {
399
+ // If this is the first match or the previous match contains the new match,
400
+ // so the new match is a deeper and therefore better match.
401
+ bestMatch = nearestNode ;
402
+ bestRenderer = renderer ;
403
+ }
404
+ }
405
+ }
406
+ if ( bestRenderer != null && bestMatch != null ) {
369
407
try {
370
- return rendererInterface . getElementIDForHostInstance ( node , true ) ;
408
+ const id = bestRenderer . getElementIDForHostInstance ( node , true ) ;
409
+ if ( id ) {
410
+ return bestRenderer . getDisplayNameForElementID ( id ) ;
411
+ }
371
412
} catch ( error ) {
372
413
// Some old React versions might throw if they can't find a match.
373
414
// If so we should ignore it...
@@ -616,8 +657,8 @@ export default class Agent extends EventEmitter<{
616
657
}
617
658
} ;
618
659
619
- selectNode ( target : Object ) : void {
620
- const id = this . getIDForNode ( target ) ;
660
+ selectNode ( target : HTMLElement ) : void {
661
+ const id = this . getIDForDOMNode ( target ) ;
621
662
if ( id !== null ) {
622
663
this . _bridge . send ( 'selectElement' , id ) ;
623
664
}
0 commit comments