From f20f3d59b175125d23fcd2579ca32562466a519e Mon Sep 17 00:00:00 2001 From: Billy Vong Date: Mon, 17 Jul 2023 19:35:44 -0400 Subject: [PATCH 1/2] feat: Pass parent element or node to `maskTextFn` Allows `maskTextFn` to access to parent element or node so that it can make masking decisions based on the element. --- packages/rrweb-snapshot/src/snapshot.ts | 2 +- packages/rrweb-snapshot/src/types.ts | 2 +- packages/rrweb/src/record/mutation.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/rrweb-snapshot/src/snapshot.ts b/packages/rrweb-snapshot/src/snapshot.ts index bb18542d26..13d8b517c8 100644 --- a/packages/rrweb-snapshot/src/snapshot.ts +++ b/packages/rrweb-snapshot/src/snapshot.ts @@ -586,7 +586,7 @@ function serializeTextNode( needMaskingText(n, maskTextClass, maskTextSelector) ) { textContent = maskTextFn - ? maskTextFn(textContent) + ? maskTextFn(textContent, n.parentNode as HTMLElement) : textContent.replace(/[\S]/g, '*'); } diff --git a/packages/rrweb-snapshot/src/types.ts b/packages/rrweb-snapshot/src/types.ts index 9edb4dd6d4..a31f0c0315 100644 --- a/packages/rrweb-snapshot/src/types.ts +++ b/packages/rrweb-snapshot/src/types.ts @@ -153,7 +153,7 @@ export type DataURLOptions = Partial<{ quality: number; }>; -export type MaskTextFn = (text: string) => string; +export type MaskTextFn = (text: string, element?: HTMLElement | Node) => string; export type MaskInputFn = (text: string, element: HTMLElement) => string; export type KeepIframeSrcFn = (src: string) => boolean; diff --git a/packages/rrweb/src/record/mutation.ts b/packages/rrweb/src/record/mutation.ts index 39a6107635..1446a18569 100644 --- a/packages/rrweb/src/record/mutation.ts +++ b/packages/rrweb/src/record/mutation.ts @@ -496,7 +496,7 @@ export default class MutationBuffer { this.maskTextSelector, ) && value ? this.maskTextFn - ? this.maskTextFn(value) + ? this.maskTextFn(value, m.target) : value.replace(/[\S]/g, '*') : value, node: m.target, From 07c18b686447e4c05ab2cf6f2e4b909418433107 Mon Sep 17 00:00:00 2001 From: Billy Vong Date: Mon, 17 Jul 2023 19:53:47 -0400 Subject: [PATCH 2/2] add changeset --- .changeset/wet-snails-rhyme.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/wet-snails-rhyme.md diff --git a/.changeset/wet-snails-rhyme.md b/.changeset/wet-snails-rhyme.md new file mode 100644 index 0000000000..a3893eced3 --- /dev/null +++ b/.changeset/wet-snails-rhyme.md @@ -0,0 +1,6 @@ +--- +'rrweb-snapshot': patch +'rrweb': patch +--- + +Pass parent element/node to `maskTextFn` callback, similar to `maskInputFn`