Skip to content

Commit

Permalink
feat: Enable serialization of multiple DOM attributes for breadcrumbs. (
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan authored Jun 28, 2021
1 parent 61eda62 commit a3f70d8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
12 changes: 8 additions & 4 deletions packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
/** JSDoc */
interface BreadcrumbsOptions {
console: boolean;
dom: boolean | { serializeAttribute: string };
dom: boolean | { serializeAttribute: string | string[] };
fetch: boolean;
history: boolean;
sentry: boolean;
Expand Down Expand Up @@ -162,13 +162,17 @@ export class Breadcrumbs implements Integration {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _domBreadcrumb(handlerData: { [key: string]: any }): void {
let target;
const keyAttr = typeof this._options.dom === 'object' ? this._options.dom.serializeAttribute : undefined;
let keyAttrs = typeof this._options.dom === 'object' ? this._options.dom.serializeAttribute : undefined;

if (typeof keyAttrs === 'string') {
keyAttrs = [keyAttrs];
}

// Accessing event.target can throw (see getsentry/raven-js#838, #768)
try {
target = handlerData.event.target
? htmlTreeAsString(handlerData.event.target as Node, keyAttr)
: htmlTreeAsString((handlerData.event as unknown) as Node, keyAttr);
? htmlTreeAsString(handlerData.event.target as Node, keyAttrs)
: htmlTreeAsString((handlerData.event as unknown) as Node, keyAttrs);
} catch (e) {
target = '<unknown>';
}
Expand Down
18 changes: 12 additions & 6 deletions packages/utils/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isString } from './is';
* e.g. [HTMLElement] => body > div > input#foo.btn[name=baz]
* @returns generated DOM path
*/
export function htmlTreeAsString(elem: unknown, keyAttr?: string): string {
export function htmlTreeAsString(elem: unknown, keyAttrs?: string[]): string {
type SimpleNode = {
parentNode: SimpleNode;
} | null;
Expand All @@ -28,7 +28,7 @@ export function htmlTreeAsString(elem: unknown, keyAttr?: string): string {

// eslint-disable-next-line no-plusplus
while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) {
nextStr = _htmlElementAsString(currentElem, keyAttr);
nextStr = _htmlElementAsString(currentElem, keyAttrs);
// bail out if
// - nextStr is the 'html' element
// - the length of the string that would be created exceeds MAX_OUTPUT_LEN
Expand All @@ -54,7 +54,7 @@ export function htmlTreeAsString(elem: unknown, keyAttr?: string): string {
* e.g. [HTMLElement] => input#foo.btn[name=baz]
* @returns generated DOM path
*/
function _htmlElementAsString(el: unknown, keyAttr?: string): string {
function _htmlElementAsString(el: unknown, keyAttrs?: string[]): string {
const elem = el as {
tagName?: string;
id?: string;
Expand All @@ -75,9 +75,15 @@ function _htmlElementAsString(el: unknown, keyAttr?: string): string {

out.push(elem.tagName.toLowerCase());

const keyAttrValue = keyAttr ? elem.getAttribute(keyAttr) : null;
if (keyAttrValue) {
out.push(`[${keyAttr}="${keyAttrValue}"]`);
// Pairs of attribute keys defined in `serializeAttribute` and their values on element.
const keyAttrPairs = keyAttrs?.length
? keyAttrs.filter(keyAttr => elem.getAttribute(keyAttr)).map(keyAttr => [keyAttr, elem.getAttribute(keyAttr)])
: null;

if (keyAttrPairs?.length) {
keyAttrPairs.forEach(keyAttrPair => {
out.push(`[${keyAttrPair[0]}="${keyAttrPair[1]}"]`);
});
} else {
if (elem.id) {
out.push(`#${elem.id}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/test/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('htmlTreeAsString', () => {
</li>`;
document.body.appendChild(el);

expect(htmlTreeAsString(document.getElementById('cat-2'), 'test-id')).toBe(
expect(htmlTreeAsString(document.getElementById('cat-2'), ['test-id'])).toBe(
'body > ul > li.li-class[title="li-title"] > img[test-id="cat-2-test-id"]',
);
});
Expand Down

0 comments on commit a3f70d8

Please sign in to comment.