@@ -341,84 +341,123 @@ export default class Agent extends EventEmitter<{
341
341
}
342
342
343
343
getIDForHostInstance ( target : HostInstance ) : number | null {
344
- let bestMatch : null | HostInstance = null ;
345
- let bestRenderer : null | RendererInterface = null ;
346
- // Find the nearest ancestor which is mounted by a React.
347
- for ( const rendererID in this . _rendererInterfaces ) {
348
- const renderer = ( ( this . _rendererInterfaces [
349
- ( rendererID : any )
350
- ] : any ) : RendererInterface ) ;
351
- const nearestNode : null = renderer . getNearestMountedHostInstance ( target ) ;
352
- if ( nearestNode != = null ) {
353
- if ( nearestNode === target ) {
354
- // Exact match we can exit early.
355
- bestMatch = nearestNode ;
356
- bestRenderer = renderer ;
357
- break ;
344
+ if ( isReactNativeEnvironment ( ) || typeof target . nodeType !== 'number' ) {
345
+ // In React Native or non-DOM we simply pick any renderer that has a match.
346
+ for ( const rendererID in this . _rendererInterfaces ) {
347
+ const renderer = ( ( this . _rendererInterfaces [
348
+ ( rendererID : any )
349
+ ] : any ) : RendererInterface ) ;
350
+ try {
351
+ const match = renderer . getElementIDForHostInstance ( target ) ;
352
+ if ( match ! = null ) {
353
+ return match ;
354
+ }
355
+ } catch ( error ) {
356
+ // Some old React versions might throw if they can't find a match.
357
+ // If so we should ignore it...
358
358
}
359
- if (
360
- bestMatch === null ||
361
- ( ! isReactNativeEnvironment ( ) && bestMatch . contains ( nearestNode ) )
362
- ) {
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
+ }
360
+ return null ;
361
+ } else {
362
+ // In the DOM we use a smarter mechanism to find the deepest a DOM node
363
+ // that is registered if there isn't an exact match.
364
+ let bestMatch : null | Element = null ;
365
+ let bestRenderer : null | RendererInterface = null ;
366
+ // Find the nearest ancestor which is mounted by a React.
367
+ for ( const rendererID in this . _rendererInterfaces ) {
368
+ const renderer = ( ( this . _rendererInterfaces [
369
+ ( rendererID : any )
370
+ ] : any ) : RendererInterface ) ;
371
+ const nearestNode : null | Element = renderer . getNearestMountedDOMNode (
372
+ ( target : any ) ,
373
+ ) ;
374
+ if ( nearestNode !== null ) {
375
+ if ( nearestNode === target ) {
376
+ // Exact match we can exit early.
377
+ bestMatch = nearestNode ;
378
+ bestRenderer = renderer ;
379
+ break ;
380
+ }
381
+ if ( bestMatch === null || bestMatch . contains ( nearestNode ) ) {
382
+ // If this is the first match or the previous match contains the new match,
383
+ // so the new match is a deeper and therefore better match.
384
+ bestMatch = nearestNode ;
385
+ bestRenderer = renderer ;
386
+ }
367
387
}
368
388
}
369
- }
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...
389
+ if ( bestRenderer != null && bestMatch != null ) {
390
+ try {
391
+ return bestRenderer . getElementIDForHostInstance ( bestMatch ) ;
392
+ } catch ( error ) {
393
+ // Some old React versions might throw if they can't find a match.
394
+ // If so we should ignore it.. .
395
+ }
376
396
}
397
+ return null ;
377
398
}
378
- return null ;
379
399
}
380
400
381
401
getComponentNameForHostInstance ( target : HostInstance ) : string | null {
382
402
// We duplicate this code from getIDForHostInstance to avoid an object allocation.
383
- let bestMatch : null | HostInstance = null ;
384
- let bestRenderer : null | RendererInterface = null ;
385
- // Find the nearest ancestor which is mounted by a React.
386
- for ( const rendererID in this . _rendererInterfaces ) {
387
- const renderer = ( ( this . _rendererInterfaces [
388
- ( rendererID : any )
389
- ] : any ) : RendererInterface ) ;
390
- const nearestNode = renderer . getNearestMountedHostInstance ( target ) ;
391
- if ( nearestNode !== null ) {
392
- if ( nearestNode === target ) {
393
- // Exact match we can exit early.
394
- bestMatch = nearestNode ;
395
- bestRenderer = renderer ;
396
- break ;
403
+ if ( isReactNativeEnvironment ( ) || typeof target . nodeType !== ' number ' ) {
404
+ // In React Native or non-DOM we simply pick any renderer that has a match.
405
+ for ( const rendererID in this . _rendererInterfaces ) {
406
+ const renderer = ( ( this . _rendererInterfaces [
407
+ ( rendererID : any )
408
+ ] : any ) : RendererInterface ) ;
409
+ try {
410
+ const id = renderer . getElementIDForHostInstance ( target ) ;
411
+ if ( id ) {
412
+ return renderer . getDisplayNameForElementID ( id ) ;
413
+ }
414
+ } catch ( error ) {
415
+ // Some old React versions might throw if they can't find a match.
416
+ // If so we should ignore it...
397
417
}
398
- if (
399
- bestMatch === null ||
400
- ( ! isReactNativeEnvironment ( ) && bestMatch . contains ( nearestNode ) )
401
- ) {
402
- // If this is the first match or the previous match contains the new match,
403
- // so the new match is a deeper and therefore better match.
404
- bestMatch = nearestNode ;
405
- bestRenderer = renderer ;
418
+ }
419
+ return null ;
420
+ } else {
421
+ // In the DOM we use a smarter mechanism to find the deepest a DOM node
422
+ // that is registered if there isn't an exact match.
423
+ let bestMatch : null | Element = null ;
424
+ let bestRenderer : null | RendererInterface = null ;
425
+ // Find the nearest ancestor which is mounted by a React.
426
+ for ( const rendererID in this . _rendererInterfaces ) {
427
+ const renderer = ( ( this . _rendererInterfaces [
428
+ ( rendererID : any )
429
+ ] : any ) : RendererInterface ) ;
430
+ const nearestNode : null | Element = renderer . getNearestMountedDOMNode (
431
+ ( target : any ) ,
432
+ ) ;
433
+ if ( nearestNode !== null ) {
434
+ if ( nearestNode === target ) {
435
+ // Exact match we can exit early.
436
+ bestMatch = nearestNode ;
437
+ bestRenderer = renderer ;
438
+ break ;
439
+ }
440
+ if ( bestMatch === null || bestMatch . contains ( nearestNode ) ) {
441
+ // If this is the first match or the previous match contains the new match,
442
+ // so the new match is a deeper and therefore better match.
443
+ bestMatch = nearestNode ;
444
+ bestRenderer = renderer ;
445
+ }
406
446
}
407
447
}
408
- }
409
-
410
- if ( bestRenderer != null && bestMatch != null ) {
411
- try {
412
- const id = bestRenderer . getElementIDForHostInstance ( bestMatch , true ) ;
413
- if ( id ) {
414
- return bestRenderer . getDisplayNameForElementID ( id ) ;
448
+ if ( bestRenderer != null && bestMatch != null ) {
449
+ try {
450
+ const id = bestRenderer . getElementIDForHostInstance ( bestMatch ) ;
451
+ if ( id ) {
452
+ return bestRenderer . getDisplayNameForElementID ( id ) ;
453
+ }
454
+ } catch ( error ) {
455
+ // Some old React versions might throw if they can't find a match.
456
+ // If so we should ignore it...
415
457
}
416
- } catch ( error ) {
417
- // Some old React versions might throw if they can't find a match.
418
- // If so we should ignore it...
419
458
}
459
+ return null ;
420
460
}
421
- return null ;
422
461
}
423
462
424
463
getBackendVersion : ( ) = > void = ( ) => {
0 commit comments