Skip to content

Commit 17db0d1

Browse files
committed
refactor(utils): move htmlTreeAsString args to object
1 parent 99d7476 commit 17db0d1

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function _domBreadcrumb(dom: BreadcrumbsOptions['dom']): (handlerData: { [key: s
103103
function _innerDomBreadcrumb(handlerData: { [key: string]: any }): void {
104104
let target;
105105
let keyAttrs = typeof dom === 'object' ? dom.serializeAttribute : undefined;
106-
const customMaxStringLength =
106+
const maxStringLength =
107107
typeof dom === 'object' && typeof dom.maxStringLength === 'number'
108108
? Math.min(dom.maxStringLength, 1024)
109109
: undefined;
@@ -115,8 +115,8 @@ function _domBreadcrumb(dom: BreadcrumbsOptions['dom']): (handlerData: { [key: s
115115
// Accessing event.target can throw (see getsentry/raven-js#838, #768)
116116
try {
117117
target = handlerData.event.target
118-
? htmlTreeAsString(handlerData.event.target as Node, keyAttrs, customMaxStringLength)
119-
: htmlTreeAsString(handlerData.event as unknown as Node, keyAttrs, customMaxStringLength);
118+
? htmlTreeAsString(handlerData.event.target as Node, { keyAttrs, maxStringLength })
119+
: htmlTreeAsString(handlerData.event as unknown as Node, { keyAttrs, maxStringLength });
120120
} catch (e) {
121121
target = '<unknown>';
122122
}

packages/utils/src/browser.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ const DEFAULT_MAX_STRING_LENGTH = 80;
1212
* e.g. [HTMLElement] => body > div > input#foo.btn[name=baz]
1313
* @returns generated DOM path
1414
*/
15-
export function htmlTreeAsString(elem: unknown, keyAttrs?: string[], customMaxStringLength?: number): string {
15+
export function htmlTreeAsString(
16+
elem: unknown,
17+
options: { keyAttrs?: string[]; maxStringLength?: number } = {},
18+
): string {
1619
type SimpleNode = {
1720
parentNode: SimpleNode;
1821
} | null;
@@ -24,13 +27,13 @@ export function htmlTreeAsString(elem: unknown, keyAttrs?: string[], customMaxSt
2427
try {
2528
let currentElem = elem as SimpleNode;
2629
const MAX_TRAVERSE_HEIGHT = 5;
27-
const maxStringLength = customMaxStringLength || DEFAULT_MAX_STRING_LENGTH;
2830
const out = [];
2931
let height = 0;
3032
let len = 0;
3133
const separator = ' > ';
3234
const sepLength = separator.length;
3335
let nextStr;
36+
const { keyAttrs, maxStringLength = DEFAULT_MAX_STRING_LENGTH } = options;
3437

3538
// eslint-disable-next-line no-plusplus
3639
while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) {

packages/utils/test/browser.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('htmlTreeAsString', () => {
4444
</li>`;
4545
document.body.appendChild(el);
4646

47-
expect(htmlTreeAsString(document.getElementById('cat-2'), ['test-id'])).toBe(
47+
expect(htmlTreeAsString(document.getElementById('cat-2'), { keyAttrs: ['test-id'] })).toBe(
4848
'body > ul > li.li-class[title="li-title"] > img[test-id="cat-2-test-id"]',
4949
);
5050
});
@@ -61,7 +61,7 @@ describe('htmlTreeAsString', () => {
6161
expect(htmlTreeAsString(document.querySelector('button'))).toBe(
6262
'button.bg-blue-500.hover:bg-blue-700.text-white.hover:text-blue-100',
6363
);
64-
expect(htmlTreeAsString(document.querySelector('button'), undefined, 100)).toBe(
64+
expect(htmlTreeAsString(document.querySelector('button'), { maxStringLength: 100 })).toBe(
6565
'div#main-cta > div.container > button.bg-blue-500.hover:bg-blue-700.text-white.hover:text-blue-100',
6666
);
6767
});

0 commit comments

Comments
 (0)