Skip to content

Commit 21ff38f

Browse files
Address pr comments
1 parent 4951d99 commit 21ff38f

6 files changed

Lines changed: 23 additions & 36 deletions

File tree

packages/rrdom/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ export class Mirror implements IMirror<RRNode> {
368368

369369
// removes the node from idNodeMap
370370
// doesn't remove the node from nodeMetaMap
371-
removeNodeFromMap(n: RRNode) {
371+
removeNodeFromMap(n: RRNode, _permanent?: boolean) {
372372
const id = this.getId(n);
373373
this.idNodeMap.delete(id);
374374

packages/rrweb-snapshot/src/utils.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,26 +203,15 @@ export class Mirror implements IMirror<Node> {
203203
}
204204

205205
// removes the node from idNodeMap
206-
// doesn't remove the node from nodeMetaMap
207-
removeNodeFromMap(n: Node) {
206+
// if permanent is true, also removes from nodeMetaMap
207+
removeNodeFromMap(n: Node, permanent = false) {
208208
const id = this.getId(n);
209209
this.idNodeMap.delete(id);
210+
if (permanent) this.nodeMetaMap.delete(n);
210211

211212
if (n.childNodes) {
212213
n.childNodes.forEach((childNode) =>
213-
this.removeNodeFromMap(childNode as unknown as Node),
214-
);
215-
}
216-
}
217-
218-
removeNodeFromMapPermanently(n: Node) {
219-
const id = this.getId(n);
220-
this.idNodeMap.delete(id);
221-
this.nodeMetaMap.delete(n);
222-
223-
if (n.childNodes) {
224-
n.childNodes.forEach((childNode) =>
225-
this.removeNodeFromMapPermanently(childNode as unknown as Node),
214+
this.removeNodeFromMap(childNode as unknown as Node, permanent),
226215
);
227216
}
228217
}

packages/rrweb-snapshot/test/utils.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ describe('utils', () => {
283283
});
284284

285285
describe('Mirror', () => {
286-
describe('removeNodeFromMapPermanently', () => {
286+
describe('removeNodeFromMap with permanent flag', () => {
287287
it('should remove node from both idNodeMap and nodeMetaMap', () => {
288288
const mirror = new Mirror();
289289
const div = document.createElement('div');
@@ -300,7 +300,7 @@ describe('utils', () => {
300300
expect(mirror.hasNode(div)).toBe(true);
301301
expect(mirror.getNode(1)).toBe(div);
302302

303-
mirror.removeNodeFromMapPermanently(div);
303+
mirror.removeNodeFromMap(div, true);
304304

305305
expect(mirror.getId(div)).toBe(-1);
306306
expect(mirror.hasNode(div)).toBe(false);
@@ -358,7 +358,7 @@ describe('utils', () => {
358358
expect(mirror.hasNode(child2)).toBe(true);
359359
expect(mirror.hasNode(grandchild)).toBe(true);
360360

361-
mirror.removeNodeFromMapPermanently(parent);
361+
mirror.removeNodeFromMap(parent, true);
362362

363363
expect(mirror.hasNode(parent)).toBe(false);
364364
expect(mirror.hasNode(child1)).toBe(false);
@@ -370,7 +370,7 @@ describe('utils', () => {
370370
expect(mirror.getId(grandchild)).toBe(-1);
371371
});
372372

373-
it('should differ from removeNodeFromMap by also clearing nodeMetaMap', () => {
373+
it('should differ from non-permanent removeNodeFromMap by also clearing nodeMetaMap', () => {
374374
const mirror1 = new Mirror();
375375
const div1 = document.createElement('div');
376376
const meta1 = {
@@ -399,9 +399,9 @@ describe('utils', () => {
399399
} as serializedNodeWithId;
400400

401401
mirror2.add(div2, meta2);
402-
mirror2.removeNodeFromMapPermanently(div2);
402+
mirror2.removeNodeFromMap(div2, true);
403403

404-
// removeNodeFromMapPermanently clears both maps
404+
// removeNodeFromMap with permanent=true clears both maps
405405
expect(mirror2.getNode(1)).toBeNull();
406406
expect(mirror2.hasNode(div2)).toBe(false); // Also removed from nodeMetaMap
407407
});

packages/rrweb/src/record/iframe-manager.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ export class IframeManager {
7878
}
7979

8080
public removeIframe(iframeEl: HTMLIFrameElement): void {
81+
const storedDoc = this.iframeContentDocumentMap.get(iframeEl);
82+
83+
this.stylesheetManager.cleanupStylesheetsForRemovedNode(
84+
iframeEl,
85+
storedDoc,
86+
);
87+
if (storedDoc) {
88+
this.mirror.removeNodeFromMap(storedDoc, true);
89+
}
90+
8191
this.iframes.delete(iframeEl);
8292
this.iframeContentDocumentMap.delete(iframeEl);
8393

packages/rrweb/src/record/mutation.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,18 +370,8 @@ export default class MutationBuffer {
370370
const removedNode = this.mapRemoves.shift()!;
371371

372372
if (removedNode.nodeName === 'IFRAME') {
373-
const iframeEl = removedNode as HTMLIFrameElement;
374373
try {
375-
const storedDoc =
376-
this.iframeManager.getIframeContentDocument(iframeEl);
377-
this.stylesheetManager.cleanupStylesheetsForRemovedNode(
378-
removedNode,
379-
storedDoc,
380-
);
381-
if (storedDoc) {
382-
this.mirror.removeNodeFromMapPermanently(storedDoc);
383-
}
384-
this.iframeManager.removeIframe(iframeEl);
374+
this.iframeManager.removeIframe(removedNode as HTMLIFrameElement);
385375
} catch (e) {
386376
// Ignore errors during iframe cleanup
387377
}

packages/types/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,7 @@ export interface IMirror<TNode> {
828828

829829
getMeta(n: TNode): serializedNodeWithId | null;
830830

831-
removeNodeFromMap(n: TNode): void;
832-
833-
removeNodeFromMapPermanently?(n: TNode): void;
831+
removeNodeFromMap(n: TNode, permanent?: boolean): void;
834832

835833
has(id: number): boolean;
836834

0 commit comments

Comments
 (0)