Skip to content

Commit 899e5c0

Browse files
committed
more
1 parent d6ad717 commit 899e5c0

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

packages/browser/src/integrations/view-hierarchy.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Attachment, Event, ViewHierarchyData, ViewHierarchyWindow } from '@sentry/core';
1+
import type { Attachment, Event, EventHint, ViewHierarchyData, ViewHierarchyWindow } from '@sentry/core';
22
import { defineIntegration, getComponentName } from '@sentry/core';
33
import { WINDOW } from '../helpers';
44

@@ -23,7 +23,14 @@ interface Options {
2323
*
2424
* Default: Always attach.
2525
*/
26-
shouldAttach?: (event: Event) => boolean;
26+
shouldAttach?: (event: Event, hint: EventHint) => boolean;
27+
28+
/**
29+
* A function that returns the root element to start walking the DOM from.
30+
*
31+
* Default: `window.document.body`
32+
*/
33+
rootElement?: () => HTMLElement | undefined;
2734

2835
/**
2936
* Called for each HTMLElement as we walk the DOM.
@@ -43,7 +50,7 @@ export const viewHierarchyIntegration = defineIntegration((options: Options = {}
4350

4451
/** Walk an element */
4552
function walk(element: HTMLElement, windows: ViewHierarchyWindow[]): void {
46-
// With Web Components, we need walk into shadow DOMs
53+
// With Web Components, we need to walk into shadow DOMs
4754
const children = 'shadowRoot' in element && element.shadowRoot ? element.shadowRoot.children : element.children;
4855

4956
for (const child of children) {
@@ -97,7 +104,7 @@ export const viewHierarchyIntegration = defineIntegration((options: Options = {}
97104
return {
98105
name: 'ViewHierarchy',
99106
processEvent: (event, hint) => {
100-
if (options.shouldAttach && options.shouldAttach(event) === false) {
107+
if (options.shouldAttach && options.shouldAttach(event, hint) === false) {
101108
return event;
102109
}
103110

@@ -107,7 +114,7 @@ export const viewHierarchyIntegration = defineIntegration((options: Options = {}
107114
windows: [],
108115
};
109116

110-
walk(WINDOW.document.body, root.windows);
117+
walk(options.rootElement?.() || WINDOW.document.body, root.windows);
111118

112119
const attachment: Attachment = {
113120
filename: 'view-hierarchy.json',

0 commit comments

Comments
 (0)