Skip to content

Commit 2f80141

Browse files
committed
refactor(utils): move htmlTreeAsString args to object
1 parent cd44969 commit 2f80141

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
@@ -123,7 +123,7 @@ function _domBreadcrumb(dom: BreadcrumbsOptions['dom']): (handlerData: { [key: s
123123
function _innerDomBreadcrumb(handlerData: { [key: string]: any }): void {
124124
let target;
125125
let keyAttrs = typeof dom === 'object' ? dom.serializeAttribute : undefined;
126-
const customMaxStringLength =
126+
const maxStringLength =
127127
typeof dom === 'object' && typeof dom.maxStringLength === 'number'
128128
? Math.min(dom.maxStringLength, 1024)
129129
: undefined;
@@ -135,8 +135,8 @@ function _domBreadcrumb(dom: BreadcrumbsOptions['dom']): (handlerData: { [key: s
135135
// Accessing event.target can throw (see getsentry/raven-js#838, #768)
136136
try {
137137
target = handlerData.event.target
138-
? htmlTreeAsString(handlerData.event.target as Node, keyAttrs, customMaxStringLength)
139-
: htmlTreeAsString(handlerData.event as unknown as Node, keyAttrs, customMaxStringLength);
138+
? htmlTreeAsString(handlerData.event.target as Node, { keyAttrs, maxStringLength })
139+
: htmlTreeAsString(handlerData.event as unknown as Node, { keyAttrs, maxStringLength });
140140
} catch (e) {
141141
target = '<unknown>';
142142
}

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
while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) {
3639
nextStr = _htmlElementAsString(currentElem, keyAttrs);

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)