Skip to content

Commit

Permalink
Extended text masking function to include relevant HTMLElement (rrweb…
Browse files Browse the repository at this point in the history
…-io#1310)

* Extends maskTextFn to pass the HTMLElement to the deciding function

---------

Authored-by: benjackwhite <[email protected]>
Co-authored-by: Justin Halsall <[email protected]>
Co-authored-by: Eoghan Murray <[email protected]>
  • Loading branch information
3 people authored and billyvg committed Jan 31, 2024
1 parent 0b0e26d commit ed1a2ed
Show file tree
Hide file tree
Showing 8 changed files with 495 additions and 29 deletions.
6 changes: 6 additions & 0 deletions .changeset/swift-dancers-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'rrweb-snapshot': minor
'rrweb': minor
---

Extends maskTextFn to pass the HTMLElement to the deciding function
2 changes: 1 addition & 1 deletion packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ function serializeTextNode(

if (!isStyle && !isScript && !isTextarea && textContent && forceMask) {
textContent = maskTextFn
? maskTextFn(textContent)
? maskTextFn(textContent, n.parentElement)
: textContent.replace(/[\S]/g, '*');
}
if (isTextarea && textContent && (maskInputOptions.textarea || forceMask)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb-snapshot/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export type DataURLOptions = Partial<{
quality: number;
}>;

export type MaskTextFn = (text: string) => string;
export type MaskTextFn = (text: string, element: HTMLElement | null) => string;
export type MaskInputFn = (text: string, element: HTMLElement) => string;
export type MaskAttributeFn = (
attributeName: string,
Expand Down
4 changes: 3 additions & 1 deletion packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
isSerializedStylesheet,
inDom,
getShadowHost,
closestElementOfNode,
} from '../utils';

type DoubleLinkedListNode = {
Expand Down Expand Up @@ -525,6 +526,7 @@ export default class MutationBuffer {
switch (m.type) {
case 'characterData': {
const value = m.target.textContent;

if (
!isBlocked(
m.target,
Expand All @@ -546,7 +548,7 @@ export default class MutationBuffer {
this.maskAllText,
) && value
? this.maskTextFn
? this.maskTextFn(value)
? this.maskTextFn(value, closestElementOfNode(m.target))
: value.replace(/[\S]/g, '*')
: value,
node: m.target,
Expand Down
27 changes: 22 additions & 5 deletions packages/rrweb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ export function getWindowWidth(): number {
);
}

/**
* Returns the given node as an HTMLElement if it is one, otherwise the parent node as an HTMLElement
* @param node - node to check
* @returns HTMLElement or null
*/

export function closestElementOfNode(node: Node | null): HTMLElement | null {
if (!node) {
return null;
}
const el: HTMLElement | null =
node.nodeType === node.ELEMENT_NODE
? (node as HTMLElement)
: node.parentElement;
return el;
}

/**
* Checks if the given element set to be blocked by rrweb
* @param node - node to check
Expand All @@ -237,11 +254,11 @@ export function isBlocked(
if (!node) {
return false;
}
const el: HTMLElement | null =
node.nodeType === node.ELEMENT_NODE
? (node as HTMLElement)
: node.parentElement;
if (!el) return false;
const el = closestElementOfNode(node);

if (!el) {
return false;
}

const blockedPredicate = createMatchPredicate(blockClass, blockSelector);

Expand Down
Loading

0 comments on commit ed1a2ed

Please sign in to comment.