From a2f91c5e1a20a33a88f0be572debe9066034e103 Mon Sep 17 00:00:00 2001 From: Justin Halsall Date: Wed, 29 Jun 2022 14:34:48 +0200 Subject: [PATCH 1/3] Remove children of Document even if doc not in mirror --- packages/rrdom/src/diff.ts | 2 +- packages/rrdom/test/diff.test.ts | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/rrdom/src/diff.ts b/packages/rrdom/src/diff.ts index 73850d6453..c3c9acd544 100644 --- a/packages/rrdom/src/diff.ts +++ b/packages/rrdom/src/diff.ts @@ -316,7 +316,7 @@ function diffChildren( * We should delete it before insert a serialized one. Otherwise, an error 'Only one element on document allowed' will be thrown. */ if ( - replayer.mirror.getMeta(parentNode)?.type === RRNodeType.Document && + parentNode.nodeName === '#document' && replayer.mirror.getMeta(newNode)?.type === RRNodeType.Element && (parentNode as Document).documentElement ) { diff --git a/packages/rrdom/test/diff.test.ts b/packages/rrdom/test/diff.test.ts index bb4544e888..561b989a44 100644 --- a/packages/rrdom/test/diff.test.ts +++ b/packages/rrdom/test/diff.test.ts @@ -1056,6 +1056,24 @@ describe('diff algorithm for rrdom', () => { expect(element.tagName).toBe('DIV'); expect(mirror.getId(element)).toEqual(2); }); + + it('should remove children from document before adding new nodes', () => { + document.write(''); // old document with elements that need removing + + const rrDocument = new RRDocument(); + const docType = rrDocument.createDocumentType('html', '', ''); + rrDocument.mirror.add(docType, getDefaultSN(docType, 1)); + rrDocument.appendChild(docType); + const htmlEl = rrDocument.createElement('html'); + rrDocument.mirror.add(htmlEl, getDefaultSN(htmlEl, 2)); + rrDocument.appendChild(htmlEl); + + diff(document, rrDocument, replayer); + expect(document.childNodes.length).toBe(2); + const element = document.childNodes[0] as HTMLElement; + expect(element.DOCUMENT_TYPE_NODE).toBe(10); + expect(mirror.getId(element)).toEqual(1); + }); }); describe('create or get a Node', () => { From 8b01750a9499afb4fc2e7fc600501fb4ff97e874 Mon Sep 17 00:00:00 2001 From: Justin Halsall Date: Wed, 29 Jun 2022 14:44:21 +0200 Subject: [PATCH 2/3] fix flaky test --- packages/rrweb/test/integration.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rrweb/test/integration.test.ts b/packages/rrweb/test/integration.test.ts index 797d537346..452dd56469 100644 --- a/packages/rrweb/test/integration.test.ts +++ b/packages/rrweb/test/integration.test.ts @@ -576,7 +576,7 @@ describe('record integration tests', function (this: ISuite) { ); }); }); - await page.waitForTimeout(50); + await waitForRAF(page); const snapshots = await page.evaluate('window.snapshots'); assertSnapshot(snapshots); From b6d1d82c103ad0b5e0e65e8efffcde1040ba9f5e Mon Sep 17 00:00:00 2001 From: Justin Halsall Date: Wed, 29 Jun 2022 17:59:21 +0200 Subject: [PATCH 3/3] Update packages/rrdom/test/diff.test.ts Co-authored-by: Yun Feng --- packages/rrdom/test/diff.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rrdom/test/diff.test.ts b/packages/rrdom/test/diff.test.ts index 561b989a44..efb5aaaa8f 100644 --- a/packages/rrdom/test/diff.test.ts +++ b/packages/rrdom/test/diff.test.ts @@ -1071,7 +1071,7 @@ describe('diff algorithm for rrdom', () => { diff(document, rrDocument, replayer); expect(document.childNodes.length).toBe(2); const element = document.childNodes[0] as HTMLElement; - expect(element.DOCUMENT_TYPE_NODE).toBe(10); + expect(element.nodeType).toBe(element.DOCUMENT_TYPE_NODE); expect(mirror.getId(element)).toEqual(1); }); });