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-1344] remove fallback for null scrollingElement #1694

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Changes from all 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
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
)
Comment on lines +8 to +16
Copy link
Member

@BenoitZugmeyer BenoitZugmeyer Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥜 nitpick: ‏I feel writing it like that would be a tiny bit clearer, but either is fine

      if (element === document) {
        if (!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
        }
        element = document.scrollingElement
      }
      scrollPositionsByElement.set((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
}