Skip to content

Commit

Permalink
isBlocked factors in the selector (#894)
Browse files Browse the repository at this point in the history
* isBlocked factors in the selector

* Ensure contains parameter is a node

* Fix blockSelector blocking for closest nodes

* Fix integration test

* adding ignoreCSSAttributes to ignore the addition of certain css attributes

* tested ignoreCSSAttributes

* Update test snapshot

* swapped the wrapping of htmlelement to be element

* Fix linter errors

* Address MR feedback

* Rebase

Co-authored-by: Filip <filipslatinac@gmail.com>
  • Loading branch information
dbseel and Filip authored Aug 30, 2022
1 parent ac7935e commit 5ba933c
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 28 deletions.
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 |
| 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 |
| maskAllInputs | false | mask all input content as \* |
Expand Down
1 change: 1 addition & 0 deletions guide.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ setInterval(save, 10 * 1000);
| blockClass | 'rr-block' | 字符串或正则表达式,可用于自定义屏蔽元素的类名,详见[“隐私”](#隐私)章节 |
| blockSelector | null | 所有 element.matches(blockSelector)为 true 的元素都不会被录制,回放时取而代之的是一个同等宽高的占位元素 |
| ignoreClass | 'rr-ignore' | 字符串或正则表达式,可用于自定义忽略元素的类名,详见[“隐私”](#隐私)章节 |
| ignoreCSSAttributes | null | 应该被忽略的 CSS 属性数组 |
| maskTextClass | 'rr-mask' | 字符串或正则表达式,可用于自定义忽略元素 text 内容的类名,详见[“隐私”](#隐私)章节 |
| maskTextSelector | null | 所有 element.matches(maskTextSelector)为 true 的元素及其子元素的 text 内容将会被屏蔽 |
| maskAllInputs | false | 将所有输入内容记录为 \* |
Expand Down
4 changes: 4 additions & 0 deletions packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ function record<T = eventWithTime>(
inlineImages = false,
plugins,
keepIframeSrcFn = () => false,
ignoreCSSAttributes = new Set([]),
} = options;

// runtime checks for user options
if (!emit) {
throw new Error('emit function is required');
Expand Down Expand Up @@ -226,6 +228,7 @@ function record<T = eventWithTime>(
mutationCb: wrappedCanvasMutationEmit,
win: window,
blockClass,
blockSelector,
mirror,
sampling: sampling.canvas,
});
Expand Down Expand Up @@ -464,6 +467,7 @@ function record<T = eventWithTime>(
stylesheetManager,
shadowDomManager,
canvasManager,
ignoreCSSAttributes,
plugins:
plugins
?.filter((p) => p.observer)
Expand Down
12 changes: 6 additions & 6 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export default class MutationBuffer {
rootShadowHost =
(rootShadowHost?.getRootNode?.() as ShadowRoot | undefined)?.host ||
null;
// ensure shadowHost is a Node, or doc.contains will throw an error
// ensure contains is passed a Node, or it will throw an error
const notInDoc =
!this.doc.contains(n) &&
(!rootShadowHost || !this.doc.contains(rootShadowHost));
Expand Down Expand Up @@ -446,7 +446,7 @@ export default class MutationBuffer {
case 'characterData': {
const value = m.target.textContent;
if (
!isBlocked(m.target, this.blockClass, false) &&
!isBlocked(m.target, this.blockClass, this.blockSelector, false) &&
value !== m.oldValue
) {
this.texts.push({
Expand Down Expand Up @@ -478,7 +478,7 @@ export default class MutationBuffer {
});
}
if (
isBlocked(m.target, this.blockClass, false) ||
isBlocked(m.target, this.blockClass, this.blockSelector, false) ||
value === m.oldValue
) {
return;
Expand Down Expand Up @@ -554,7 +554,7 @@ export default class MutationBuffer {
/**
* Parent is blocked, ignore all child mutations
*/
if (isBlocked(m.target, this.blockClass, true)) return;
if (isBlocked(m.target, this.blockClass, this.blockSelector, true)) return;

m.addedNodes.forEach((n) => this.genAdds(n, m.target));
m.removedNodes.forEach((n) => {
Expand All @@ -563,7 +563,7 @@ export default class MutationBuffer {
? this.mirror.getId(m.target.host)
: this.mirror.getId(m.target);
if (
isBlocked(m.target, this.blockClass, false) ||
isBlocked(m.target, this.blockClass, this.blockSelector, false) ||
isIgnored(n, this.mirror) ||
!isSerialized(n, this.mirror)
) {
Expand Down Expand Up @@ -635,7 +635,7 @@ export default class MutationBuffer {

// if this node is blocked `serializeNode` will turn it into a placeholder element
// but we have to remove it's children otherwise they will be added as placeholders too
if (!isBlocked(n, this.blockClass, false))
if (!isBlocked(n, this.blockClass, this.blockSelector, false))
n.childNodes.forEach((childN) => this.genAdds(childN));
};
}
Expand Down
30 changes: 21 additions & 9 deletions packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ function initMouseInteractionObserver({
doc,
mirror,
blockClass,
blockSelector,
sampling,
}: observerParam): listenerHandler {
if (sampling.mouseInteraction === false) {
Expand All @@ -227,7 +228,7 @@ function initMouseInteractionObserver({
const getHandler = (eventKey: keyof typeof MouseInteractions) => {
return (event: MouseEvent | TouchEvent) => {
const target = getEventTarget(event) as Node;
if (isBlocked(target, blockClass, true)) {
if (isBlocked(target, blockClass, blockSelector, true)) {
return;
}
const e = isTouchEvent(event) ? event.changedTouches[0] : event;
Expand Down Expand Up @@ -266,14 +267,15 @@ export function initScrollObserver({
doc,
mirror,
blockClass,
blockSelector,
sampling,
}: Pick<
observerParam,
'scrollCb' | 'doc' | 'mirror' | 'blockClass' | 'sampling'
'scrollCb' | 'doc' | 'mirror' | 'blockClass' | 'blockSelector' | 'sampling'
>): listenerHandler {
const updatePosition = throttle<UIEvent>((evt) => {
const target = getEventTarget(evt);
if (!target || isBlocked(target as Node, blockClass, true)) {
if (!target || isBlocked(target as Node, blockClass, blockSelector, true)) {
return;
}
const id = mirror.getId(target as Node);
Expand Down Expand Up @@ -331,6 +333,7 @@ function initInputObserver({
doc,
mirror,
blockClass,
blockSelector,
ignoreClass,
maskInputOptions,
maskInputFn,
Expand All @@ -350,7 +353,7 @@ function initInputObserver({
!target ||
!(target as Element).tagName ||
INPUT_TAGS.indexOf((target as Element).tagName) < 0 ||
isBlocked(target as Node, blockClass, true)
isBlocked(target as Node, blockClass, blockSelector, true)
) {
return;
}
Expand Down Expand Up @@ -613,7 +616,7 @@ function initStyleSheetObserver(
}

function initStyleDeclarationObserver(
{ styleDeclarationCb, mirror }: observerParam,
{ styleDeclarationCb, mirror, ignoreCSSAttributes }: observerParam,
{ win }: { win: IWindow },
): listenerHandler {
// eslint-disable-next-line @typescript-eslint/unbound-method
Expand All @@ -624,6 +627,10 @@ function initStyleDeclarationObserver(
value: string,
priority: string,
) {
// ignore this mutation if we do not care about this css attribute
if (ignoreCSSAttributes.has(property)) {
return setProperty.apply(this, [property, value, priority]);
}
const id = mirror.getId(this.parentRule?.parentStyleSheet?.ownerNode);
if (id !== -1) {
styleDeclarationCb({
Expand All @@ -645,6 +652,10 @@ function initStyleDeclarationObserver(
this: CSSStyleDeclaration,
property: string,
) {
// ignore this mutation if we do not care about this css attribute
if (ignoreCSSAttributes.has(property)) {
return removeProperty.apply(this, [property]);
}
const id = mirror.getId(this.parentRule?.parentStyleSheet?.ownerNode);
if (id !== -1) {
styleDeclarationCb({
Expand All @@ -667,13 +678,14 @@ function initStyleDeclarationObserver(
function initMediaInteractionObserver({
mediaInteractionCb,
blockClass,
blockSelector,
mirror,
sampling,
}: observerParam): listenerHandler {
const handler = (type: MediaInteractions) =>
throttle((event: Event) => {
const target = getEventTarget(event);
if (!target || isBlocked(target as Node, blockClass, true)) {
if (!target || isBlocked(target as Node, blockClass, blockSelector, true)) {
return;
}
const { currentTime, volume, muted } = target as HTMLMediaElement;
Expand Down Expand Up @@ -755,7 +767,7 @@ function initFontObserver({ fontCb, doc }: observerParam): listenerHandler {
}

function initSelectionObserver(param: observerParam): listenerHandler {
const { doc, mirror, blockClass, selectionCb } = param;
const { doc, mirror, blockClass, blockSelector, selectionCb } = param;
let collapsed = true;

const updateSelection = () => {
Expand All @@ -774,8 +786,8 @@ function initSelectionObserver(param: observerParam): listenerHandler {
const { startContainer, startOffset, endContainer, endOffset } = range;

const blocked =
isBlocked(startContainer, blockClass, true) ||
isBlocked(endContainer, blockClass, true);
isBlocked(startContainer, blockClass, blockSelector, true) ||
isBlocked(endContainer, blockClass, blockSelector, true);

if (blocked) continue;

Expand Down
3 changes: 2 additions & 1 deletion packages/rrweb/src/record/observers/canvas/2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function initCanvas2DMutationObserver(
cb: canvasManagerMutationCallback,
win: IWindow,
blockClass: blockClass,
blockSelector: string | null,
mirror: Mirror,
): listenerHandler {
const handlers: listenerHandler[] = [];
Expand Down Expand Up @@ -41,7 +42,7 @@ export default function initCanvas2DMutationObserver(
this: CanvasRenderingContext2D,
...args: Array<unknown>
) {
if (!isBlocked(this.canvas, blockClass, true)) {
if (!isBlocked(this.canvas, blockClass, blockSelector, true)) {
// Using setTimeout as toDataURL can be heavy
// and we'd rather not block the main thread
setTimeout(() => {
Expand Down
17 changes: 11 additions & 6 deletions packages/rrweb/src/record/observers/canvas/canvas-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,18 @@ export class CanvasManager {
mutationCb: canvasMutationCallback;
win: IWindow;
blockClass: blockClass;
blockSelector: string | null,
mirror: Mirror;
sampling?: 'all' | number;
}) {
const { sampling = 'all', win, blockClass, recordCanvas } = options;
const { sampling = 'all', win, blockClass, blockSelector, recordCanvas } = options;
this.mutationCb = options.mutationCb;
this.mirror = options.mirror;

if (recordCanvas && sampling === 'all')
this.initCanvasMutationObserver(win, blockClass);
this.initCanvasMutationObserver(win, blockClass, blockSelector);
if (recordCanvas && typeof sampling === 'number')
this.initCanvasFPSObserver(sampling, win, blockClass);
this.initCanvasFPSObserver(sampling, win, blockClass, blockSelector);
}

private processMutation: canvasManagerMutationCallback = (
Expand All @@ -94,8 +95,9 @@ export class CanvasManager {
fps: number,
win: IWindow,
blockClass: blockClass,
blockSelector: string | null,
) {
const canvasContextReset = initCanvasContextObserver(win, blockClass);
const canvasContextReset = initCanvasContextObserver(win, blockClass, blockSelector);
const snapshotInProgressMap: Map<number, boolean> = new Map();
const worker = new ImageBitmapDataURLWorker() as ImageBitmapDataURLRequestWorker;
worker.onmessage = (e) => {
Expand Down Expand Up @@ -141,7 +143,7 @@ export class CanvasManager {
const getCanvas = (): HTMLCanvasElement[] => {
const matchedCanvas: HTMLCanvasElement[] = [];
win.document.querySelectorAll('canvas').forEach(canvas => {
if (!isBlocked(canvas, blockClass, true)) {
if (!isBlocked(canvas, blockClass, blockSelector, true)) {
matchedCanvas.push(canvas);
}
})
Expand Down Expand Up @@ -208,22 +210,25 @@ export class CanvasManager {
private initCanvasMutationObserver(
win: IWindow,
blockClass: blockClass,
blockSelector: string | null,
): void {
this.startRAFTimestamping();
this.startPendingCanvasMutationFlusher();

const canvasContextReset = initCanvasContextObserver(win, blockClass);
const canvasContextReset = initCanvasContextObserver(win, blockClass, blockSelector);
const canvas2DReset = initCanvas2DMutationObserver(
this.processMutation.bind(this),
win,
blockClass,
blockSelector,
this.mirror,
);

const canvasWebGL1and2Reset = initCanvasWebGLMutationObserver(
this.processMutation.bind(this),
win,
blockClass,
blockSelector,
this.mirror,
);

Expand Down
3 changes: 2 additions & 1 deletion packages/rrweb/src/record/observers/canvas/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isBlocked, patch } from '../../../utils';
export default function initCanvasContextObserver(
win: IWindow,
blockClass: blockClass,
blockSelector: string | null,
): listenerHandler {
const handlers: listenerHandler[] = [];
try {
Expand All @@ -23,7 +24,7 @@ export default function initCanvasContextObserver(
contextType: string,
...args: Array<unknown>
) {
if (!isBlocked(this, blockClass, true)) {
if (!isBlocked(this, blockClass, blockSelector, true)) {
if (!('__context' in this)) this.__context = contextType;
}
return original.apply(this, [contextType, ...args]);
Expand Down
6 changes: 5 additions & 1 deletion packages/rrweb/src/record/observers/canvas/webgl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function patchGLPrototype(
type: CanvasContext,
cb: canvasManagerMutationCallback,
blockClass: blockClass,
blockSelector: string | null,
mirror: Mirror,
win: IWindow,
): listenerHandler[] {
Expand All @@ -36,7 +37,7 @@ function patchGLPrototype(
return function (this: typeof prototype, ...args: Array<unknown>) {
const result = original.apply(this, args);
saveWebGLVar(result, win, prototype);
if (!isBlocked(this.canvas, blockClass, true)) {
if (!isBlocked(this.canvas, blockClass, blockSelector, true)) {
const recordArgs = serializeArgs([...args], win, prototype);
const mutation: canvasMutationWithType = {
type,
Expand Down Expand Up @@ -76,6 +77,7 @@ export default function initCanvasWebGLMutationObserver(
cb: canvasManagerMutationCallback,
win: IWindow,
blockClass: blockClass,
blockSelector: string | null,
mirror: Mirror,
): listenerHandler {
const handlers: listenerHandler[] = [];
Expand All @@ -86,6 +88,7 @@ export default function initCanvasWebGLMutationObserver(
CanvasContext.WebGL,
cb,
blockClass,
blockSelector,
mirror,
win,
),
Expand All @@ -98,6 +101,7 @@ export default function initCanvasWebGLMutationObserver(
CanvasContext.WebGL2,
cb,
blockClass,
blockSelector,
mirror,
win,
),
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb/src/record/shadow-dom-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class ShadowDomManager {
const manager = this;
this.restorePatches.push(
patch(
HTMLElement.prototype,
Element.prototype,
'attachShadow',
function (original: (init: ShadowRootInit) => ShadowRoot) {
return function (this: HTMLElement, option: ShadowRootInit) {
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 @@ -245,6 +245,7 @@ export type recordOptions<T> = {
maskInputFn?: MaskInputFn;
maskTextFn?: MaskTextFn;
slimDOMOptions?: SlimDOMOptions | 'all' | true;
ignoreCSSAttributes?:Set<string>;
inlineStylesheet?: boolean;
hooks?: hooksParam;
packFn?: PackFn;
Expand Down Expand Up @@ -294,6 +295,7 @@ export type observerParam = {
stylesheetManager: StylesheetManager;
shadowDomManager: ShadowDomManager;
canvasManager: CanvasManager;
ignoreCSSAttributes:Set<string>;
plugins: Array<{
observer: (
cb: (...arg: Array<unknown>) => void,
Expand Down
Loading

0 comments on commit 5ba933c

Please sign in to comment.