Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛[RUMF-1450] stop computing coordinates for focus/blur records #1985

Merged
merged 4 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 89 additions & 3 deletions packages/rum/src/domain/record/observers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { ActionType, LifeCycle, LifeCycleEventType, RumEventType, FrustrationTyp
import type { RawRumEventCollectedData } from 'packages/rum-core/src/domain/lifeCycle'
import { createNewEvent, isFirefox } from '../../../../core/test/specHelper'
import { NodePrivacyLevel, PRIVACY_ATTR_NAME, PRIVACY_ATTR_VALUE_MASK_USER_INPUT } from '../../constants'
import { RecordType } from '../../types'
import type { FrustrationCallback, InputCallback, StyleSheetCallback } from './observers'
import { initStyleSheetObserver, initFrustrationObserver, initInputObserver } from './observers'
import { IncrementalSource, MouseInteractionType, RecordType } from '../../types'
import type { FrustrationCallback, InputCallback, MouseInteractionCallBack, StyleSheetCallback } from './observers'
import {
initStyleSheetObserver,
initFrustrationObserver,
initInputObserver,
initMouseInteractionObserver,
} from './observers'
import { serializeDocument, SerializationContextStatus } from './serialize'
import { createElementsScrollPositions } from './elementsScrollPositions'
import type { ShadowRootsController } from './shadowRootsController'
Expand Down Expand Up @@ -328,3 +333,84 @@ describe('initStyleSheetObserver', () => {
})
})
})

describe('initMouseInteractionObserver', () => {
let mouseInteractionCallbackSpy: jasmine.Spy<MouseInteractionCallBack>
let stopObserver: () => void
let sandbox: HTMLDivElement
let a: HTMLAnchorElement
let coordinatesComputed: boolean

beforeEach(() => {
if (isIE()) {
pending('IE not supported')
}
if (!window.visualViewport) {
pending('no visualViewport')
}
bcaudan marked this conversation as resolved.
Show resolved Hide resolved

sandbox = document.createElement('div')
a = document.createElement('a')
a.setAttribute('tabindex', '0') // make the element focusable
sandbox.appendChild(a)
document.body.appendChild(sandbox)
a.focus()

serializeDocument(document, DEFAULT_CONFIGURATION, {
shadowRootsController: DEFAULT_SHADOW_ROOT_CONTROLLER,
status: SerializationContextStatus.INITIAL_FULL_SNAPSHOT,
elementsScrollPositions: createElementsScrollPositions(),
})

coordinatesComputed = false
Object.defineProperty(window.visualViewport, 'offsetTop', {
get() {
coordinatesComputed = true
return 0
},
configurable: true,
})

mouseInteractionCallbackSpy = jasmine.createSpy()
stopObserver = initMouseInteractionObserver(mouseInteractionCallbackSpy, DefaultPrivacyLevel.ALLOW)
})

afterEach(() => {
sandbox.remove()
delete (window.visualViewport as any).offsetTop
stopObserver()
})

it('should compute x/y coordinates for click record', () => {
a.click()
expect(mouseInteractionCallbackSpy).toHaveBeenCalledWith({
id: jasmine.any(Number),
type: RecordType.IncrementalSnapshot,
timestamp: jasmine.any(Number),
data: {
source: IncrementalSource.MouseInteraction,
type: MouseInteractionType.Click,
id: jasmine.any(Number),
x: jasmine.any(Number),
y: jasmine.any(Number),
},
})
expect(coordinatesComputed).toBeTrue()
})

// related to safari issue, see RUMF-1450
it('should not compute x/y coordinates for blur record', () => {
a.blur()
expect(mouseInteractionCallbackSpy).toHaveBeenCalledWith({
id: jasmine.any(Number),
type: RecordType.IncrementalSnapshot,
timestamp: jasmine.any(Number),
data: {
source: IncrementalSource.MouseInteraction,
type: MouseInteractionType.Blur,
id: jasmine.any(Number),
},
})
expect(coordinatesComputed).toBeFalse()
})
})
36 changes: 22 additions & 14 deletions packages/rum/src/domain/record/observers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type MousemoveCallBack = (

export type MutationCallBack = (m: BrowserMutationPayload) => void

type MouseInteractionCallBack = (record: BrowserIncrementalSnapshotRecord) => void
export type MouseInteractionCallBack = (record: BrowserIncrementalSnapshotRecord) => void

type ScrollCallback = (p: ScrollPosition) => void

Expand Down Expand Up @@ -194,7 +194,7 @@ const eventTypeToMouseInteraction = {
[DOM_EVENT.TOUCH_START]: MouseInteractionType.TouchStart,
[DOM_EVENT.TOUCH_END]: MouseInteractionType.TouchEnd,
}
function initMouseInteractionObserver(
export function initMouseInteractionObserver(
cb: MouseInteractionCallBack,
defaultPrivacyLevel: DefaultPrivacyLevel
): ListenerHandler {
Expand All @@ -203,22 +203,20 @@ function initMouseInteractionObserver(
if (getNodePrivacyLevel(target, defaultPrivacyLevel) === NodePrivacyLevel.HIDDEN || !hasSerializedNode(target)) {
return
}
const { clientX, clientY } = isTouchEvent(event) ? event.changedTouches[0] : event
const position: MouseInteraction = {
id: getSerializedNodeId(target),
type: eventTypeToMouseInteraction[event.type as keyof typeof eventTypeToMouseInteraction],
x: clientX,
y: clientY,
}
if (window.visualViewport) {
const { visualViewportX, visualViewportY } = convertMouseEventToLayoutCoordinates(clientX, clientY)
position.x = visualViewportX
position.y = visualViewportY
const id = getSerializedNodeId(target)
const type = eventTypeToMouseInteraction[event.type as keyof typeof eventTypeToMouseInteraction]

let interaction: MouseInteraction
if (type !== MouseInteractionType.Blur && type !== MouseInteractionType.Focus) {
const { x, y } = computeCoordinates(event)
interaction = { id, type, x, y }
} else {
interaction = { id, type }
}
BenoitZugmeyer marked this conversation as resolved.
Show resolved Hide resolved

const record = assign(
{ id: getRecordIdForEvent(event) },
assembleIncrementalSnapshot<MouseInteractionData>(IncrementalSource.MouseInteraction, position)
assembleIncrementalSnapshot<MouseInteractionData>(IncrementalSource.MouseInteraction, interaction)
)
cb(record)
}
Expand All @@ -228,6 +226,16 @@ function initMouseInteractionObserver(
}).stop
}

function computeCoordinates(event: MouseEvent | TouchEvent) {
let { clientX: x, clientY: y } = isTouchEvent(event) ? event.changedTouches[0] : event
if (window.visualViewport) {
const { visualViewportX, visualViewportY } = convertMouseEventToLayoutCoordinates(x, y)
x = visualViewportX
y = visualViewportY
}
return { x, y }
}

function initScrollObserver(
cb: ScrollCallback,
defaultPrivacyLevel: DefaultPrivacyLevel,
Expand Down
53 changes: 32 additions & 21 deletions packages/rum/src/types/sessionReplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,38 @@ export type MouseInteractionData = {
*/
readonly source: 2
} & MouseInteraction
/**
* Browser-specific. Schema of a MouseInteraction.
*/
export type MouseInteraction =
| {
/**
* The type of MouseInteraction: 0=mouseup, 1=mousedown, 2=click, 3=contextmenu, 4=dblclick, 7=touchstart, 9=touchend
*/
readonly type: 0 | 1 | 2 | 3 | 4 | 7 | 9
/**
* Id for the target node for this MouseInteraction.
*/
id: number
/**
* X-axis coordinate for this MouseInteraction.
*/
x: number
/**
* Y-axis coordinate for this MouseInteraction.
*/
y: number
}
| {
/**
* The type of MouseInteraction: 5=focus, 6=blur
*/
readonly type: 5 | 6
/**
* Id for the target node for this MouseInteraction.
*/
id: number
}
/**
* Browser-specific. Schema of a ScrollData.
*/
Expand Down Expand Up @@ -583,27 +615,6 @@ export interface MousePosition {
*/
timeOffset: number
}
/**
* Browser-specific. Schema of a MouseInteraction.
*/
export interface MouseInteraction {
/**
* The type of MouseInteraction.
*/
readonly type: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9
/**
* Id for the target node for this MouseInteraction.
*/
id: number
/**
* X-axis coordinate for this MouseInteraction.
*/
x: number
/**
* Y-axis coordinate for this MouseInteraction.
*/
y: number
}
/**
* Browser-specific. Schema of a ScrollPosition.
*/
Expand Down