From 86a61342c156efadb71ff00ef290a28ec5a3b7e5 Mon Sep 17 00:00:00 2001 From: Vadim Korolik Date: Wed, 29 Jun 2022 17:56:25 -0700 Subject: [PATCH 1/5] Revert "improve error handling in rrdom (#76)" This reverts commit c728b71c3929ea9e81c7ade31cb31b465b54e883. --- packages/rrdom/package.json | 2 +- packages/rrdom/src/diff.ts | 16 ++++++++-------- packages/rrweb/package.json | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/rrdom/package.json b/packages/rrdom/package.json index 0e7f747d..cff62be9 100644 --- a/packages/rrdom/package.json +++ b/packages/rrdom/package.json @@ -1,6 +1,6 @@ { "name": "@highlight-run/rrdom", - "version": "0.1.9", + "version": "0.1.8", "scripts": { "dev": "rollup -c -w", "bundle": "rollup --config", diff --git a/packages/rrdom/src/diff.ts b/packages/rrdom/src/diff.ts index e0f097f1..900d7d61 100644 --- a/packages/rrdom/src/diff.ts +++ b/packages/rrdom/src/diff.ts @@ -283,22 +283,22 @@ function diffChildren( ) { try { parentNode.insertBefore(oldStartNode, oldEndNode.nextSibling); - } catch (e) { - console.error(e, parentNode, oldStartNode, oldEndNode); diff(oldStartNode, newEndNode, replayer, rrnodeMirror); oldStartNode = oldChildren[++oldStartIndex]; newEndNode = newChildren[--newEndIndex]; + } catch (e) { + console.error(e, parentNode, oldStartNode, oldEndNode); } } else if ( replayer.mirror.getId(oldEndNode) === rrnodeMirror.getId(newStartNode) ) { try { parentNode.insertBefore(oldEndNode, oldStartNode); - } catch (e) { - console.error(e, parentNode, oldEndNode, oldStartNode); diff(oldEndNode, newStartNode, replayer, rrnodeMirror); oldEndNode = oldChildren[--oldEndIndex]; newStartNode = newChildren[++newStartIndex]; + } catch (e) { + console.error(e, parentNode, oldEndNode, oldStartNode); } } else { if (!oldIdToIndex) { @@ -314,10 +314,10 @@ function diffChildren( const nodeToMove = oldChildren[indexInOld]!; try { parentNode.insertBefore(nodeToMove, oldStartNode); - } catch (e) { - console.error(e, parentNode, nodeToMove, oldStartNode); diff(nodeToMove, newStartNode, replayer, rrnodeMirror); oldChildren[indexInOld] = undefined; + } catch (e) { + console.error(e, parentNode, nodeToMove, oldStartNode); } } else { const newNode = createOrGetNode( @@ -341,9 +341,9 @@ function diffChildren( } try { parentNode.insertBefore(newNode, oldStartNode || null); + diff(newNode, newStartNode, replayer, rrnodeMirror); } catch (e) { console.error(e, parentNode, newNode, oldStartNode || null); - diff(newNode, newStartNode, replayer, rrnodeMirror); } } newStartNode = newChildren[++newStartIndex]; @@ -367,9 +367,9 @@ function diffChildren( ); try { parentNode.insertBefore(newNode, referenceNode); + diff(newNode, newChildren[newStartIndex], replayer, rrnodeMirror); } catch (e) { console.error(e, parentNode, newNode, referenceNode); - diff(newNode, newChildren[newStartIndex], replayer, rrnodeMirror); } } } else if (newStartIndex > newEndIndex) { diff --git a/packages/rrweb/package.json b/packages/rrweb/package.json index a0ab5b3a..1c508245 100644 --- a/packages/rrweb/package.json +++ b/packages/rrweb/package.json @@ -1,6 +1,6 @@ { "name": "@highlight-run/rrweb", - "version": "2.0.10", + "version": "2.0.9", "description": "record and replay the web", "scripts": { "prepare": "npm run prepack", From b93b582eb86b42219bfc2003d84f87894fd93317 Mon Sep 17 00:00:00 2001 From: Justin Halsall Date: Wed, 29 Jun 2022 14:34:48 +0200 Subject: [PATCH 2/5] 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 900d7d61..ef28095e 100644 --- a/packages/rrdom/src/diff.ts +++ b/packages/rrdom/src/diff.ts @@ -331,7 +331,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 a52c7f31..34487c85 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 5f886cde15f2fb464a62f6a78116f72112734a98 Mon Sep 17 00:00:00 2001 From: Justin Halsall Date: Wed, 29 Jun 2022 14:44:21 +0200 Subject: [PATCH 3/5] fix flaky test --- packages/rrweb/test/integration.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/rrweb/test/integration.test.ts b/packages/rrweb/test/integration.test.ts index 29b3ba63..fc6d4e4f 100644 --- a/packages/rrweb/test/integration.test.ts +++ b/packages/rrweb/test/integration.test.ts @@ -576,8 +576,7 @@ describe('record integration tests', function (this: ISuite) { ); }); }); - await page.waitForTimeout(20); // 20ms of sleep time - await waitForRAF(page); // wait for events to get created + await waitForRAF(page); const snapshots = await page.evaluate('window.snapshots'); assertSnapshot(snapshots); From ce297295bf2b3f013bcd5811dd269115ee7dc675 Mon Sep 17 00:00:00 2001 From: Justin Halsall Date: Wed, 29 Jun 2022 17:59:21 +0200 Subject: [PATCH 4/5] 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 34487c85..656d10fb 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); }); }); From 216c4f739b9403de9744324110139728706ed34c Mon Sep 17 00:00:00 2001 From: Vadim Korolik Date: Wed, 29 Jun 2022 17:57:52 -0700 Subject: [PATCH 5/5] bump versions --- packages/rrdom/package.json | 2 +- packages/rrweb/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rrdom/package.json b/packages/rrdom/package.json index cff62be9..6a87c55a 100644 --- a/packages/rrdom/package.json +++ b/packages/rrdom/package.json @@ -1,6 +1,6 @@ { "name": "@highlight-run/rrdom", - "version": "0.1.8", + "version": "0.1.10", "scripts": { "dev": "rollup -c -w", "bundle": "rollup --config", diff --git a/packages/rrweb/package.json b/packages/rrweb/package.json index 1c508245..e23e543b 100644 --- a/packages/rrweb/package.json +++ b/packages/rrweb/package.json @@ -1,6 +1,6 @@ { "name": "@highlight-run/rrweb", - "version": "2.0.9", + "version": "2.0.11", "description": "record and replay the web", "scripts": { "prepare": "npm run prepack",