Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove children of Document even if doc not in mirror #923

Merged
merged 4 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion packages/rrdom/src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down
18 changes: 18 additions & 0 deletions packages/rrdom/test/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<style></style>'); // 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);
Juice10 marked this conversation as resolved.
Show resolved Hide resolved
expect(mirror.getId(element)).toEqual(1);
});
});

describe('create or get a Node', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down