Skip to content
Merged
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions packages/browser-utils/src/component-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { GLOBAL_OBJ } from '@sentry/core';

type SimpleNode = {
parentNode: SimpleNode;
} | null;

/**
* Given a DOM element, traverses up the tree until it finds the first ancestor node
* that has the `data-sentry-component` or `data-sentry-element` attribute with `data-sentry-component` taking
* precedence. This attribute is added at build-time by projects that have the component name annotation plugin installed.
*
* @returns a string representation of the component for the provided DOM element, or `null` if not found
*/
export function getComponentName(elem: unknown, maxTraverseHeight: number = 5): string | null {
// @ts-expect-error WINDOW has HTMLElement
if (!GLOBAL_OBJ.HTMLElement) {
return null;
}

let currentElem = elem as SimpleNode;
for (let i = 0; i < maxTraverseHeight; i++) {
if (!currentElem) {
return null;
}

if (currentElem instanceof HTMLElement) {
if (currentElem.dataset['sentryComponent']) {
return currentElem.dataset['sentryComponent'];
}
if (currentElem.dataset['sentryElement']) {
return currentElem.dataset['sentryElement'];
}
}

currentElem = currentElem.parentNode;
}

return null;
}
2 changes: 2 additions & 0 deletions packages/browser-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export { resourceTimingToSpanAttributes } from './performance/resourceTiming';

export { htmlTreeAsString } from './htmlTreeAsString';

export { getComponentName } from './component-name';

export { isElement } from './is';

export { getAbsoluteUrl } from './instrumentation/location';
Expand Down
2 changes: 1 addition & 1 deletion packages/browser-utils/src/performance/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
debug,
defineIntegration,
getActiveSpan,
getComponentName,
getRootSpan,
spanToJSON,
} from '@sentry/core';
Expand All @@ -24,6 +23,7 @@ import { addPerformanceInstrumentationHandler } from '../instrumentation/perform
import { isBotUserAgent } from '../isBotUserAgent';
import { WINDOW } from '../types';
import { msToSec, startAndEndSpan } from './utils';
import { getComponentName } from '../component-name';

const INTEGRATION_NAME = 'Interactions';

Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
defineIntegration,
getBreadcrumbLogLevelFromHttpStatusCode,
getClient,
getComponentName,
getEventDescription,
parseUrl,
safeJoin,
Expand All @@ -35,6 +34,7 @@ import {
addHistoryInstrumentationHandler,
addXhrInstrumentationHandler,
htmlTreeAsString,
getComponentName,
SENTRY_XHR_DATA_KEY,
} from '@sentry/browser-utils';
import { DEBUG_BUILD } from '../debug-build';
Expand Down
3 changes: 2 additions & 1 deletion packages/browser/src/integrations/view-hierarchy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Attachment, Event, EventHint, ViewHierarchyData, ViewHierarchyWindow } from '@sentry/core/browser';
import { defineIntegration, getComponentName } from '@sentry/core/browser';
import { defineIntegration } from '@sentry/core/browser';
import { getComponentName } from '@sentry/browser-utils';
import { WINDOW } from '../helpers';

interface OnElementArgs {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/browser-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export { startIdleSpan } from './tracing/idleSpan';

export { spanStreamingIntegration } from './integrations/browserSpanStreaming';

export { getComponentName, getLocationHref } from './utils/browser';
export { getLocationHref } from './utils/browser';
export { supportsDOMError, supportsHistory, supportsNativeFetch, supportsReportingObserver } from './utils/supports';
export type { XhrBreadcrumbData, XhrBreadcrumbHint } from './types/breadcrumb';
export type {
Expand Down
38 changes: 0 additions & 38 deletions packages/core/src/utils/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { GLOBAL_OBJ } from './worldwide';

const WINDOW = GLOBAL_OBJ as unknown as Window;

type SimpleNode = {
parentNode: SimpleNode;
} | null;

/**
* A safe form of location.href
*/
Expand All @@ -16,37 +12,3 @@ export function getLocationHref(): string {
return '';
}
}

/**
* Given a DOM element, traverses up the tree until it finds the first ancestor node
* that has the `data-sentry-component` or `data-sentry-element` attribute with `data-sentry-component` taking
* precedence. This attribute is added at build-time by projects that have the component name annotation plugin installed.
*
* @returns a string representation of the component for the provided DOM element, or `null` if not found
*/
export function getComponentName(elem: unknown, maxTraverseHeight: number = 5): string | null {
// @ts-expect-error WINDOW has HTMLElement
if (!WINDOW.HTMLElement) {
return null;
}

let currentElem = elem as SimpleNode;
for (let i = 0; i < maxTraverseHeight; i++) {
if (!currentElem) {
return null;
}

if (currentElem instanceof HTMLElement) {
if (currentElem.dataset['sentryComponent']) {
return currentElem.dataset['sentryComponent'];
}
if (currentElem.dataset['sentryElement']) {
return currentElem.dataset['sentryElement'];
}
}

currentElem = currentElem.parentNode;
}

return null;
}
Loading