Skip to content

Commit

Permalink
feat: Add ignoreSelector option (#1262)
Browse files Browse the repository at this point in the history
* feat: Add `ignoreSelector` option

Similar to `ignoreClass`, but accepts a CSS selector so that you can use any CSS selector.

* Apply formatting changes

* Create clean-shrimps-lay.md

* Apply formatting changes
  • Loading branch information
billyvg authored Aug 4, 2023
1 parent 7103625 commit 36da39d
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 2 deletions.
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 @@ -298,9 +298,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 @@ -597,6 +597,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

0 comments on commit 36da39d

Please sign in to comment.