Support top-layer <dialog> recording & replay (#1503)

* chore: its important to run `yarn build:all` before running `yarn dev`

* feat: trigger showModal from rrdom and rrweb

* feat: Add support for replaying modal and non modal dialog elements

* chore: Update dev script to remove CLEAR_DIST_DIR flag

* Get modal recording and replay working

* DRY up dialog test and dedupe snapshot images

* feat: Refactor dialog test to use updated attribute name

* feat: Update dialog test to include rr_open attribute

* chore: Add npm dependency happy-dom@14.12.0

* Add more test cases for dialog

* Clean up naming

* Refactor dialog open code

* Revert changed code that doesn't do anything

* Add documentation for unimplemented type

* chore: Remove unnecessary comments in dialog.test.ts

* rename rr_open to rr_openMode

* Replace todo with a skipped test

* Add better logging for CI

* Rename rr_openMode to rr_open_mode

rrdom downcases all attribute names which made `rr_openMode` tricky to deal with

* Remove unused images

* Move after iframe append based on @YunFeng0817's comment
https://github.com/rrweb-io/rrweb/pull/1503#discussion_r1666363931

* Remove redundant dialog handling from rrdom.

rrdom already handles dialog element creation it's self

* Rename variables for dialog handling in rrweb replay module

* Update packages/rrdom/src/document.ts

---------

Co-authored-by: Eoghan Murray <eoghan@getthere.ie>
This commit is contained in:
Justin Halsall
2026-04-01 12:00:00 +08:00
committed by GitHub
parent c110e1c21d
commit 5217a09c60
38 changed files with 1902 additions and 75 deletions

View File

@@ -46,6 +46,7 @@
"@typescript-eslint/eslint-plugin": "^5.23.0",
"@typescript-eslint/parser": "^5.23.0",
"eslint": "^8.15.0",
"happy-dom": "^14.12.0",
"puppeteer": "^17.1.3",
"typescript": "^5.4.5",
"vite": "^5.3.1",

View File

@@ -21,6 +21,7 @@ import type {
} from './document';
import type {
RRCanvasElement,
RRDialogElement,
RRElement,
RRIFrameElement,
RRMediaElement,
@@ -285,6 +286,29 @@ function diffAfterUpdatingChildren(
);
break;
}
case 'DIALOG': {
const dialog = oldElement as HTMLDialogElement;
const rrDialog = newRRElement as unknown as RRDialogElement;
const wasOpen = dialog.open;
const wasModal = dialog.matches('dialog:modal');
const shouldBeOpen = rrDialog.open;
const shouldBeModal = rrDialog.isModal;
const modalChanged = wasModal !== shouldBeModal;
const openChanged = wasOpen !== shouldBeOpen;
if (modalChanged || (wasOpen && openChanged)) dialog.close();
if (shouldBeOpen && (openChanged || modalChanged)) {
try {
if (shouldBeModal) dialog.showModal();
else dialog.show();
} catch (e) {
console.warn(e);
}
}
break;
}
}
break;
}
@@ -335,7 +359,6 @@ function diffProps(
for (const { name } of Array.from(oldAttributes))
if (!(name in newAttributes)) oldTree.removeAttribute(name);
newTree.scrollLeft && (oldTree.scrollLeft = newTree.scrollLeft);
newTree.scrollTop && (oldTree.scrollTop = newTree.scrollTop);
}

View File

@@ -474,7 +474,8 @@ export class BaseRRElement extends BaseRRNode implements IRRElement {
}
public getAttribute(name: string): string | null {
return this.attributes[name] || null;
if (this.attributes[name] === undefined) return null;
return this.attributes[name];
}
public setAttribute(name: string, attribute: string) {
@@ -547,6 +548,30 @@ export class BaseRRMediaElement extends BaseRRElement {
}
}
export class BaseRRDialogElement extends BaseRRElement {
public readonly tagName = 'DIALOG' as const;
public readonly nodeName = 'DIALOG' as const;
get isModal() {
return this.getAttribute('rr_open_mode') === 'modal';
}
get open() {
return this.getAttribute('open') !== null;
}
public close() {
this.removeAttribute('open');
this.removeAttribute('rr_open_mode');
}
public show() {
this.setAttribute('open', '');
this.setAttribute('rr_open_mode', 'non-modal');
}
public showModal() {
this.setAttribute('open', '');
this.setAttribute('rr_open_mode', 'modal');
}
}
export class BaseRRText extends BaseRRNode implements IRRText {
public readonly nodeType: number = NodeType.TEXT_NODE;
public readonly nodeName = '#text' as const;

View File

@@ -31,6 +31,7 @@ import {
type IRRDocumentType,
type IRRText,
type IRRComment,
BaseRRDialogElement,
} from './document';
export class RRDocument extends BaseRRDocument {
@@ -104,6 +105,9 @@ export class RRDocument extends BaseRRDocument {
case 'STYLE':
element = new RRStyleElement(upperTagName);
break;
case 'DIALOG':
element = new RRDialogElement(upperTagName);
break;
default:
element = new RRElement(upperTagName);
break;
@@ -151,6 +155,8 @@ export class RRElement extends BaseRRElement {
export class RRMediaElement extends BaseRRMediaElement {}
export class RRDialogElement extends BaseRRDialogElement {}
export class RRCanvasElement extends RRElement implements IRRElement {
public rr_dataURL: string | null = null;
public canvasMutations: {

View File

@@ -0,0 +1,112 @@
/**
* @vitest-environment happy-dom
*/
import { vi, MockInstance } from 'vitest';
import {
NodeType as RRNodeType,
createMirror,
Mirror as NodeMirror,
serializedNodeWithId,
} from 'rrweb-snapshot';
import { RRDocument } from '../../src';
import { diff, ReplayerHandler } from '../../src/diff';
describe('diff algorithm for rrdom', () => {
let mirror: NodeMirror;
let replayer: ReplayerHandler;
let warn: MockInstance;
let elementSn: serializedNodeWithId;
let elementSn2: serializedNodeWithId;
beforeEach(() => {
mirror = createMirror();
replayer = {
mirror,
applyCanvas: () => {},
applyInput: () => {},
applyScroll: () => {},
applyStyleSheetMutation: () => {},
afterAppend: () => {},
};
document.write('<!DOCTYPE html><html><head></head><body></body></html>');
// Mock the original console.warn function to make the test fail once console.warn is called.
warn = vi.spyOn(console, 'warn');
elementSn = {
type: RRNodeType.Element,
tagName: 'DIALOG',
attributes: {},
childNodes: [],
id: 1,
};
elementSn2 = {
...elementSn,
attributes: {},
};
});
afterEach(() => {
// Check that warn was not called (fail on warning)
expect(warn).not.toBeCalled();
vi.resetAllMocks();
});
describe('diff dialog elements', () => {
vi.setConfig({ testTimeout: 60_000 });
it('should trigger `showModal` on rr_open_mode:modal attributes', () => {
const tagName = 'DIALOG';
const node = document.createElement(tagName) as HTMLDialogElement;
vi.spyOn(node, 'matches').mockReturnValue(false); // matches is used to check if the dialog was opened with showModal
const showModalFn = vi.spyOn(node, 'showModal');
const rrDocument = new RRDocument();
const rrNode = rrDocument.createElement(tagName);
rrNode.attributes = { rr_open_mode: 'modal', open: '' };
mirror.add(node, elementSn);
rrDocument.mirror.add(rrNode, elementSn);
diff(node, rrNode, replayer);
expect(showModalFn).toBeCalled();
});
it('should trigger `close` on rr_open_mode removed', () => {
const tagName = 'DIALOG';
const node = document.createElement(tagName) as HTMLDialogElement;
node.showModal();
vi.spyOn(node, 'matches').mockReturnValue(true); // matches is used to check if the dialog was opened with showModal
const closeFn = vi.spyOn(node, 'close');
const rrDocument = new RRDocument();
const rrNode = rrDocument.createElement(tagName);
rrNode.attributes = {};
mirror.add(node, elementSn);
rrDocument.mirror.add(rrNode, elementSn);
diff(node, rrNode, replayer);
expect(closeFn).toBeCalled();
});
it('should not trigger `close` on rr_open_mode is kept', () => {
const tagName = 'DIALOG';
const node = document.createElement(tagName) as HTMLDialogElement;
vi.spyOn(node, 'matches').mockReturnValue(true); // matches is used to check if the dialog was opened with showModal
node.setAttribute('rr_open_mode', 'modal');
node.setAttribute('open', '');
const closeFn = vi.spyOn(node, 'close');
const rrDocument = new RRDocument();
const rrNode = rrDocument.createElement(tagName);
rrNode.attributes = { rr_open_mode: 'modal', open: '' };
mirror.add(node, elementSn);
rrDocument.mirror.add(rrNode, elementSn);
diff(node, rrNode, replayer);
expect(closeFn).not.toBeCalled();
expect(node.open).toBe(true);
});
});
});