@@ -17,6 +17,7 @@ import {
17
17
legacy_isTouchEvent ,
18
18
StyleSheetMirror ,
19
19
nowTimestamp ,
20
+ closestElementOfNode ,
20
21
} from '../utils' ;
21
22
import { patch } from '@rrweb/utils' ;
22
23
import type { observerParam , MutationBufferParam } from '../types' ;
@@ -401,9 +402,11 @@ function initMouseInteractionObserver({
401
402
402
403
let target ;
403
404
let htargetBound = null ;
405
+ let significantEl : HTMLElement | null = null ;
404
406
405
407
if ( ! clientX ) {
406
408
target = getEventTarget ( event ) as Node ;
409
+ significantEl = closestElementOfNode ( target ) ;
407
410
} else {
408
411
// copy of getEventTarget taking into account clientX/clientY
409
412
// (this seems redundant now)
@@ -426,8 +429,13 @@ function initMouseInteractionObserver({
426
429
const node = maybeNode as Node ;
427
430
if ( node . nodeType === Node . DOCUMENT_NODE ) {
428
431
target = node ;
432
+ significantEl = null ;
429
433
break ;
430
434
}
435
+ if ( ! significantEl ) {
436
+ // first parent in path, even if it isn't within bounds
437
+ significantEl = closestElementOfNode ( node ) ;
438
+ }
431
439
htargetBound = ( node as HTMLElement ) . getBoundingClientRect ( ) ;
432
440
if (
433
441
htargetBound . left < clientX &&
@@ -441,6 +449,7 @@ function initMouseInteractionObserver({
441
449
}
442
450
} catch {
443
451
target = event . target as Node ;
452
+ significantEl = closestElementOfNode ( target ) ;
444
453
}
445
454
}
446
455
@@ -466,43 +475,48 @@ function initMouseInteractionObserver({
466
475
let src : string | null = null ;
467
476
let targetText : string | null = null ;
468
477
469
- let sig_target =
470
- event . target . closest &&
471
- event . target . closest (
478
+ if ( significantEl ) {
479
+ significantEl = significantEl . closest (
472
480
'a[href],area[href],button,input[type="submit"],input[type="button"]' ,
473
481
) ;
474
- if ( ! sig_target ) {
475
- sig_target = htarget ;
482
+ }
483
+ if ( ! significantEl ) {
484
+ significantEl = htarget ;
485
+ } else if ( htarget !== significantEl && htarget . contains ( significantEl ) ) {
486
+ emissionEvent = {
487
+ ...emissionEvent ,
488
+ sigTargetInternal : true ,
489
+ } ;
476
490
}
477
491
478
- if ( ! sig_target . tagName ) {
492
+ if ( ! significantEl . tagName ) {
479
493
// could be the #document element
480
- } else if ( sig_target . tagName . toLowerCase ( ) === 'a' ) {
481
- const a_target = sig_target as HTMLAnchorElement ;
494
+ } else if ( significantEl . tagName . toLowerCase ( ) === 'a' ) {
495
+ const a_target = significantEl as HTMLAnchorElement ;
482
496
if ( a_target . href ) {
483
497
href = a_target . href ; // this is worse for matching as resolves to a full absolute URL
484
498
hrefAttr = a_target . getAttribute ( 'href' ) ;
485
499
}
486
500
targetText = a_target . innerText . substring ( 0 , 40 ) ;
487
501
} else if (
488
- sig_target . tagName . toLowerCase ( ) === 'area' &&
489
- ( sig_target as HTMLAreaElement ) . href
502
+ significantEl . tagName . toLowerCase ( ) === 'area' &&
503
+ ( significantEl as HTMLAreaElement ) . href
490
504
) {
491
- const area_target = sig_target as HTMLAreaElement ;
505
+ const area_target = significantEl as HTMLAreaElement ;
492
506
href = area_target . href ; // this is worse for matching as resolves to a full absolute URL
493
507
hrefAttr = area_target . getAttribute ( 'href' ) ;
494
- } else if ( sig_target . tagName . toLowerCase ( ) === 'button' ) {
495
- const button_target = sig_target as HTMLButtonElement ;
508
+ } else if ( significantEl . tagName . toLowerCase ( ) === 'button' ) {
509
+ const button_target = significantEl as HTMLButtonElement ;
496
510
targetText = button_target . innerText . substring ( 0 , 40 ) ;
497
511
} else if (
498
- sig_target . tagName . toLowerCase ( ) === 'input' &&
499
- ( ( sig_target as HTMLInputElement ) . type === 'submit' ||
500
- ( sig_target as HTMLInputElement ) . type === 'button' )
512
+ significantEl . tagName . toLowerCase ( ) === 'input' &&
513
+ ( ( significantEl as HTMLInputElement ) . type === 'submit' ||
514
+ ( significantEl as HTMLInputElement ) . type === 'button' )
501
515
) {
502
- const button_target = sig_target as HTMLInputElement ;
516
+ const button_target = significantEl as HTMLInputElement ;
503
517
targetText = button_target . value . substring ( 0 , 40 ) ;
504
- } else if ( sig_target . tagName . toLowerCase ( ) === 'img' ) {
505
- const image_target = sig_target as HTMLImageElement ;
518
+ } else if ( significantEl . tagName . toLowerCase ( ) === 'img' ) {
519
+ const image_target = significantEl as HTMLImageElement ;
506
520
src = image_target . src ;
507
521
}
508
522
emissionEvent = {
@@ -520,10 +534,10 @@ function initMouseInteractionObserver({
520
534
} ;
521
535
}
522
536
523
- if ( htarget !== sig_target ) {
537
+ if ( htarget !== significantEl ) {
524
538
emissionEvent = {
525
539
...emissionEvent ,
526
- sigTargetTagName : sig_target . tagName ,
540
+ sigTargetTagName : significantEl . tagName ,
527
541
} ;
528
542
}
529
543
0 commit comments