-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathmaskAttribute.ts
43 lines (38 loc) · 1.02 KB
/
maskAttribute.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import type { getPrivacyOptions } from './getPrivacyOptions';
interface MaskAttributeParams {
maskAttributes: string[];
maskAllText: boolean;
privacyOptions: ReturnType<typeof getPrivacyOptions>;
key: string;
value: string;
el: HTMLElement;
}
/**
* Masks an attribute if necessary, otherwise return attribute value as-is.
*/
export function maskAttribute({
el,
key,
maskAttributes,
maskAllText,
privacyOptions,
value,
}: MaskAttributeParams): string {
// We only mask attributes if `maskAllText` is true
if (!maskAllText) {
return value;
}
// unmaskTextSelector takes precedence
if (privacyOptions.unmaskTextSelector && el.matches(privacyOptions.unmaskTextSelector)) {
return value;
}
if (
maskAttributes.includes(key) ||
// Need to mask `value` attribute for `<input>` if it's a button-like
// type
(key === 'value' && el.tagName === 'INPUT' && ['submit', 'button'].includes(el.getAttribute('type') || ''))
) {
return value.replace(/[\S]/g, '*');
}
return value;
}