Skip to content
Open
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
11 changes: 7 additions & 4 deletions packages/rrweb/src/replay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,10 @@ export class Replayer {
const mirror = this.usingVirtualDom ? this.virtualDom.mirror : this.mirror;
type TNode = typeof mirror extends Mirror ? Node : RRNode;

d.removes = d.removes.filter((mutation) => {
// filter into a local list rather than reassigning `d.removes`: the event
// object is re-applied on every seek, so a node that is only transiently
// absent here must not drop the removal from later replays
const removes = d.removes.filter((mutation) => {
// warn of absence from mirror before we start applying each removal
// as earlier removals could remove a tree that includes a later removal
if (!mirror.getNode(mutation.id)) {
Expand All @@ -1611,7 +1614,7 @@ export class Replayer {
}
return true;
});
d.removes.forEach((mutation) => {
removes.forEach((mutation) => {
const target = mirror.getNode(mutation.id);
if (!target) {
// no need to warn here, an ancestor may have already been removed
Expand Down Expand Up @@ -1905,7 +1908,7 @@ export class Replayer {
uniqueTextMutations(d.texts).forEach((mutation) => {
const target = mirror.getNode(mutation.id);
if (!target) {
if (d.removes.find((r) => r.id === mutation.id)) {
if (removes.find((r) => r.id === mutation.id)) {
// no need to warn, element was already removed
return;
}
Expand All @@ -1925,7 +1928,7 @@ export class Replayer {
d.attributes.forEach((mutation) => {
const target = mirror.getNode(mutation.id);
if (!target) {
if (d.removes.find((r) => r.id === mutation.id)) {
if (removes.find((r) => r.id === mutation.id)) {
// no need to warn, element was already removed
return;
}
Expand Down
91 changes: 91 additions & 0 deletions packages/rrweb/test/events/remove-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { EventType, IncrementalSource } from '@sentry/rrweb-types';
import type { eventWithTime } from '@sentry/rrweb-types';

const now = Date.now();

// a node (id 10) is added by one mutation and removed by a later one
const events: eventWithTime[] = [
{
type: EventType.Meta,
data: {
href: 'http://localhost',
width: 1000,
height: 800,
},
timestamp: now,
},
{
type: EventType.FullSnapshot,
data: {
node: {
id: 1,
type: 0,
childNodes: [
{ id: 2, name: 'html', type: 1, publicId: '', systemId: '' },
{
id: 3,
type: 2,
tagName: 'html',
attributes: {},
childNodes: [
{
id: 4,
type: 2,
tagName: 'head',
attributes: {},
childNodes: [],
},
{
id: 5,
type: 2,
tagName: 'body',
attributes: {},
childNodes: [],
},
],
},
],
},
initialOffset: { top: 0, left: 0 },
},
timestamp: now + 10,
},
// add <div id="modal">
{
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.Mutation,
adds: [
{
parentId: 5,
nextId: null,
node: {
id: 10,
type: 2,
tagName: 'div',
attributes: { id: 'modal' },
childNodes: [],
},
},
],
removes: [],
texts: [],
attributes: [],
},
timestamp: now + 100,
},
// remove <div id="modal">
{
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.Mutation,
adds: [],
removes: [{ parentId: 5, id: 10 }],
texts: [],
attributes: [],
},
timestamp: now + 200,
},
];

export default events;
44 changes: 44 additions & 0 deletions packages/rrweb/test/replayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import adoptedStyleSheetRemovedHost, {
import documentReplacementEvents from './events/document-replacement';
import hoverInIframeShadowDom from './events/iframe-shadowdom-hover';
import customElementDefineClass from './events/custom-element-define-class';
import removeNodeEvents from './events/remove-node';
import { ReplayerEvents } from '@sentry/rrweb-types';

interface ISuite {
Expand Down Expand Up @@ -1766,4 +1767,47 @@ describe('replayer', function () {
expect((result as any).speedServiceState).toBe('normal');
});
});

it('does not drop removals from mutation events whose node is not in the mirror', async () => {
await page.evaluate(`events = ${JSON.stringify(removeNodeEvents)}`);

const result = (await page.evaluate(`
const { Replayer } = rrweb;
const [meta, fullSnapshot, addEvent, removeEvent] = events;
const removesBefore = JSON.stringify(removeEvent.data.removes);

// apply the removal while its node is not in the mirror
const first = new Replayer([meta, fullSnapshot, removeEvent]);
first.pause(1000);
const removesAfterMiss = JSON.stringify(removeEvent.data.removes);
first.destroy();

// replay the same event objects again, now with the node present,
// seeking forward, back and forward again
const second = new Replayer(events);
second.pause(150);
const presentMidway = !!second.iframe.contentDocument.querySelector('#modal');
second.pause(1000);
second.pause(50);
second.pause(1000);
({
removesBefore,
removesAfterMiss,
removesAfterSeeks: JSON.stringify(removeEvent.data.removes),
presentMidway,
presentAtEnd: !!second.iframe.contentDocument.querySelector('#modal'),
});
`)) as {
removesBefore: string;
removesAfterMiss: string;
removesAfterSeeks: string;
presentMidway: boolean;
presentAtEnd: boolean;
};

expect(result.removesAfterMiss).toEqual(result.removesBefore);
expect(result.removesAfterSeeks).toEqual(result.removesBefore);
expect(result.presentMidway).toBe(true);
expect(result.presentAtEnd).toBe(false);
});
});
Loading