@@ -6,6 +6,10 @@ const WINDOW = getGlobalObject<Window>();
6
6
7
7
const DEFAULT_MAX_STRING_LENGTH = 80 ;
8
8
9
+ type SimpleNode = {
10
+ parentNode : SimpleNode ;
11
+ } | null ;
12
+
9
13
/**
10
14
* Given a child DOM element, returns a query-selector statement describing that
11
15
* and its ancestors
@@ -16,10 +20,6 @@ export function htmlTreeAsString(
16
20
elem : unknown ,
17
21
options : string [ ] | { keyAttrs ?: string [ ] ; maxStringLength ?: number } = { } ,
18
22
) : string {
19
- type SimpleNode = {
20
- parentNode : SimpleNode ;
21
- } | null ;
22
-
23
23
if ( ! elem ) {
24
24
return '<unknown>' ;
25
25
}
@@ -86,6 +86,11 @@ function _htmlElementAsString(el: unknown, keyAttrs?: string[]): string {
86
86
return '' ;
87
87
}
88
88
89
+ // If using the component name annotation plugin, this value may be available on the DOM node
90
+ if ( elem instanceof HTMLElement && elem . dataset && elem . dataset [ 'sentryComponent' ] ) {
91
+ return elem . dataset [ 'sentryComponent' ] ;
92
+ }
93
+
89
94
out . push ( elem . tagName . toLowerCase ( ) ) ;
90
95
91
96
// Pairs of attribute keys defined in `serializeAttribute` and their values on element.
@@ -157,3 +162,28 @@ export function getDomElement<E = any>(selector: string): E | null {
157
162
}
158
163
return null ;
159
164
}
165
+
166
+ /**
167
+ * Given a DOM element, traverses up the tree until it finds the first ancestor node
168
+ * that has the `data-sentry-component` attribute. This attribute is added at build-time
169
+ * by projects that have the component name annotation plugin installed.
170
+ *
171
+ * @returns a string representation of the component for the provided DOM element, or `null` if not found
172
+ */
173
+ export function getComponentName ( elem : unknown ) : string | null {
174
+ let currentElem = elem as SimpleNode ;
175
+ const MAX_TRAVERSE_HEIGHT = 5 ;
176
+ for ( let i = 0 ; i < MAX_TRAVERSE_HEIGHT ; i ++ ) {
177
+ if ( ! currentElem ) {
178
+ return null ;
179
+ }
180
+
181
+ if ( currentElem instanceof HTMLElement && currentElem . dataset [ 'sentryComponent' ] ) {
182
+ return currentElem . dataset [ 'sentryComponent' ] ;
183
+ }
184
+
185
+ currentElem = currentElem . parentNode ;
186
+ }
187
+
188
+ return null ;
189
+ }
0 commit comments