Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ignoreSelector option #1262

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/clean-shrimps-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'rrweb': patch
---

feat: Add `ignoreSelector` option

Similar to ignoreClass, but accepts a CSS selector so that you can use any CSS selector.
1 change: 1 addition & 0 deletions guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ The parameter of `rrweb.record` accepts the following options.
| blockClass | 'rr-block' | Use a string or RegExp to configure which elements should be blocked, refer to the [privacy](#privacy) chapter |
| blockSelector | null | Use a string to configure which selector should be blocked, refer to the [privacy](#privacy) chapter |
| ignoreClass | 'rr-ignore' | Use a string or RegExp to configure which elements should be ignored, refer to the [privacy](#privacy) chapter |
| ignoreSelector | null | Use a string to configure which selector should be ignored, refer to the [privacy](#privacy) chapter |
| ignoreCSSAttributes | null | array of CSS attributes that should be ignored |
| maskTextClass | 'rr-mask' | Use a string or RegExp to configure which elements should be masked, refer to the [privacy](#privacy) chapter |
| maskTextSelector | null | Use a string to configure which selector should be masked, refer to the [privacy](#privacy) chapter |
Expand Down
2 changes: 2 additions & 0 deletions packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function record<T = eventWithTime>(
blockClass = 'rr-block',
blockSelector = null,
ignoreClass = 'rr-ignore',
ignoreSelector = null,
maskTextClass = 'rr-mask',
maskTextSelector = null,
inlineStylesheet = true,
Expand Down Expand Up @@ -522,6 +523,7 @@ function record<T = eventWithTime>(
},
blockClass,
ignoreClass,
ignoreSelector,
maskTextClass,
maskTextSelector,
maskInputOptions,
Expand Down
6 changes: 5 additions & 1 deletion packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ function initInputObserver({
blockClass,
blockSelector,
ignoreClass,
ignoreSelector,
maskInputOptions,
maskInputFn,
sampling,
Expand All @@ -449,7 +450,10 @@ function initInputObserver({
return;
}

if (target.classList.contains(ignoreClass)) {
if (
target.classList.contains(ignoreClass) ||
(ignoreSelector && target.matches(ignoreSelector))
) {
return;
}
let text = (target as HTMLInputElement).value;
Expand Down
2 changes: 2 additions & 0 deletions packages/rrweb/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type recordOptions<T> = {
blockClass?: blockClass;
blockSelector?: string;
ignoreClass?: string;
ignoreSelector?: string;
maskTextClass?: maskTextClass;
maskTextSelector?: string;
maskAllInputs?: boolean;
Expand Down Expand Up @@ -84,6 +85,7 @@ export type observerParam = {
blockClass: blockClass;
blockSelector: string | null;
ignoreClass: string;
ignoreSelector: string | null;
maskTextClass: maskTextClass;
maskTextSelector: string | null;
maskInputOptions: MaskInputOptions;
Expand Down
1 change: 1 addition & 0 deletions packages/rrweb/test/html/ignore.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<body>
<form>
<label for="ignore text"> <input type="text" class="rr-ignore" /> </label>
<label for="ignore selector"> <input type="text" data-rr-ignore /> </label>
</form>
</body>
</html>
7 changes: 6 additions & 1 deletion packages/rrweb/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,14 @@ describe('record integration tests', function (this: ISuite) {
it('should not record input events on ignored elements', async () => {
const page: puppeteer.Page = await browser.newPage();
await page.goto('about:blank');
await page.setContent(getHtml.call(this, 'ignore.html'));
await page.setContent(
getHtml.call(this, 'ignore.html', {
ignoreSelector: '[data-rr-ignore]',
}),
);

await page.type('.rr-ignore', 'secret');
await page.type('[data-rr-ignore]', 'secret');

const snapshots = (await page.evaluate(
'window.snapshots',
Expand Down
1 change: 1 addition & 0 deletions packages/rrweb/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ export function generateRecordSnippet(options: recordOptions<eventWithTime>) {
emit: event => {
window.snapshots.push(event);
},
ignoreSelector: ${options.ignoreSelector},
maskTextSelector: ${JSON.stringify(options.maskTextSelector)},
maskAllInputs: ${options.maskAllInputs},
maskInputOptions: ${JSON.stringify(options.maskAllInputs)},
Expand Down