Skip to content

Commit

Permalink
🐛 [RUMF-1344] scroll positions: remove fallback for null scrollingEle…
Browse files Browse the repository at this point in the history
…ment (#1694)
  • Loading branch information
bcaudan authored Aug 17, 2022
1 parent 33f310f commit c727be2
Showing 1 changed file with 8 additions and 35 deletions.
43 changes: 8 additions & 35 deletions packages/rum/src/domain/record/elementsScrollPositions.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import { addTelemetryDebug } from '@datadog/browser-core'

export type ElementsScrollPositions = ReturnType<typeof createElementsScrollPositions>
export type ScrollPositions = { scrollLeft: number; scrollTop: number }

export function createElementsScrollPositions() {
const scrollPositionsByElement = new WeakMap<Element, ScrollPositions>()
let documentScrollingElement: Element | null
return {
set(element: Element | Document, scrollPositions: ScrollPositions) {
if (element === document && !documentScrollingElement) {
documentScrollingElement = tryToFindScrollingElement(scrollPositions)
if (!documentScrollingElement) {
return
}
}
try {
scrollPositionsByElement.set(
element === document ? documentScrollingElement! : (element as Element),
scrollPositions
)
} catch (e) {
addTelemetryDebug(`invalid element: ${String(element)}`)
if (element === document && !document.scrollingElement) {
// cf https://drafts.csswg.org/cssom-view/#dom-document-scrollingelement,
// in some cases scrolling elements can not be defined, we don't support those for now
return
}
scrollPositionsByElement.set(
element === document ? document.scrollingElement! : (element as Element),
scrollPositions
)
},
get(element: Element) {
return scrollPositionsByElement.get(element)
Expand All @@ -31,22 +23,3 @@ export function createElementsScrollPositions() {
},
}
}

function tryToFindScrollingElement(scrollPositions: ScrollPositions) {
if (document.scrollingElement) {
return document.scrollingElement
}
addTelemetryDebug('null document scrolling element')
if (scrollPositions.scrollLeft === 0 && scrollPositions.scrollTop === 0) {
addTelemetryDebug('Unable to find scrolling element for scroll (0,0)')
return null
}
if (
Math.round(document.documentElement.scrollLeft) === scrollPositions.scrollLeft &&
Math.round(document.documentElement.scrollTop) === scrollPositions.scrollTop
) {
return document.documentElement
}
addTelemetryDebug('Unable to find scrolling element')
return null
}

0 comments on commit c727be2

Please sign in to comment.