Skip to content

Commit

Permalink
[RUMF-1280] collect click position (#1566)
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque authored Jun 7, 2022
1 parent b3b7688 commit 8577823
Show file tree
Hide file tree
Showing 15 changed files with 377 additions and 63 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/tools/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Clock } from '../../test/specHelper'
import { mockClock } from '../../test/specHelper'
import {
combine,
cssEscape,
deepClone,
findCommaSeparatedValue,
getType,
Expand Down Expand Up @@ -569,3 +570,12 @@ describe('startWith', () => {
expect(startsWith('barfoo', 'foo')).toEqual(false)
})
})

describe('cssEscape', () => {
it('should escape a string', () => {
expect(cssEscape('.foo#bar')).toEqual('\\.foo\\#bar')
expect(cssEscape('()[]{}')).toEqual('\\(\\)\\[\\]\\{\\}')
expect(cssEscape('--a')).toEqual('--a')
expect(cssEscape('\0')).toEqual('\ufffd')
})
})
29 changes: 29 additions & 0 deletions packages/core/src/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ export function includes(candidate: string | unknown[], search: any) {
return candidate.indexOf(search) !== -1
}

export function arrayFrom<T>(arrayLike: ArrayLike<T>): T[] {
const array = []
for (let i = 0; i < arrayLike.length; i++) {
array.push(arrayLike[i])
}
return array
}

export function find<T, S extends T>(
array: T[],
predicate: (item: T, index: number, array: T[]) => item is S
Expand Down Expand Up @@ -635,3 +643,24 @@ export function removeDuplicates<T>(array: T[]) {
export function matchList(list: Array<string | RegExp>, value: string) {
return list.some((item) => item === value || (item instanceof RegExp && item.test(value)))
}

// https://github.com/jquery/jquery/blob/a684e6ba836f7c553968d7d026ed7941e1a612d8/src/selector/escapeSelector.js
export function cssEscape(str: string) {
if (window.CSS && window.CSS.escape) {
return window.CSS.escape(str)
}

// eslint-disable-next-line no-control-regex
return str.replace(/([\0-\x1f\x7f]|^-?\d)|^-$|[^\x80-\uFFFF\w-]/g, function (ch, asCodePoint) {
if (asCodePoint) {
// U+0000 NULL becomes U+FFFD REPLACEMENT CHARACTER
if (ch === '\0') {
return '\uFFFD'
}
// Control characters and (dependent upon position) numbers get escaped as code points
return `${ch.slice(0, -1)}\\${ch.charCodeAt(ch.length - 1).toString(16)} `
}
// Other potentially-special ASCII characters get backslash-escaped
return `\\${ch}`
})
}
2 changes: 1 addition & 1 deletion packages/core/test/specHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class StubXhr extends StubEventEmitter {
}
}

export function createNewEvent(eventName: 'click', properties?: { [name: string]: unknown }): MouseEvent
export function createNewEvent<P extends Record<string, unknown>>(eventName: 'click', properties?: P): MouseEvent & P
export function createNewEvent(eventName: string, properties?: { [name: string]: unknown }): Event
export function createNewEvent(eventName: string, properties: { [name: string]: unknown } = {}) {
let event: Event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ describe('actionCollection', () => {
})
it('should create action from auto action', () => {
const { lifeCycle, rawRumEvents } = setupBuilder.build()
const event = createNewEvent('click')

const event = createNewEvent('click', { target: document.createElement('button') })
lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, {
counts: {
errorCount: 10,
Expand All @@ -39,6 +40,12 @@ describe('actionCollection', () => {
startClocks: { relative: 1234 as RelativeTime, timeStamp: 123456789 as TimeStamp },
type: ActionType.CLICK,
event,
target: {
selector: '#foo',
width: 1,
height: 2,
},
position: { x: 1, y: 2 },
})

expect(rawRumEvents[0].startTime).toBe(1234 as RelativeTime)
Expand All @@ -60,6 +67,13 @@ describe('actionCollection', () => {
},
target: {
name: 'foo',
selector: '#foo',
width: 1,
height: 2,
},
position: {
x: 1,
y: 2,
},
type: ActionType.CLICK,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ function processAction(
? {
action: {
id: action.id,
target: action.target,
position: action.position,
loading_time: toServerDuration(action.duration),
frustration: {
type: action.frustrationTypes,
Expand Down
Loading

0 comments on commit 8577823

Please sign in to comment.