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

isBlocked factors in the selector #894

Merged
merged 16 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
40 changes: 26 additions & 14 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,27 +276,39 @@ export function transformAttribute(
}

export function _isBlockedElement(
element: HTMLElement,
node: Node | null,
blockClass: string | RegExp,
blockSelector: string | null,
): boolean {
if (typeof blockClass === 'string') {
if (element.classList.contains(blockClass)) {
return true;
}
} else {
for (let eIndex = element.classList.length; eIndex--; ) {
const className = element.classList[eIndex];
if (blockClass.test(className)) {
return true;
if (!node) {
return false;
}
if (node.nodeType === node.ELEMENT_NODE) {
let needBlock = false;
if (typeof blockClass === 'string') {
if ((node as HTMLElement).closest !== undefined) {
needBlock = (node as HTMLElement).closest('.' + blockClass) !== null;
} else {
needBlock = (node as HTMLElement).classList.contains(blockClass);
}
} else {
(node as HTMLElement).classList.forEach((className) => {
if (blockClass.test(className)) {
needBlock = true;
}
});
}
if (blockSelector) {
needBlock = needBlock || (node as HTMLElement).matches(blockSelector);
}

return needBlock || _isBlockedElement(node.parentNode, blockClass, blockSelector);
}
if (blockSelector) {
return element.matches(blockSelector);
if (node.nodeType === node.TEXT_NODE) {
// check parent node since text node do not have class name
return _isBlockedElement(node.parentNode, blockClass, blockSelector);
}

return false;
return _isBlockedElement(node.parentNode, blockClass, blockSelector);
}

export function classMatchesRegex(
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
15 changes: 10 additions & 5 deletions packages/rrweb/src/record/observers/canvas/canvas-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,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 @@ -93,8 +94,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 @@ -198,22 +200,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
Loading