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

fix(rrdom): Ignore invalid DOM attributes when diffing #213

Merged
merged 2 commits into from
Aug 26, 2024
Merged
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: 10 additions & 1 deletion packages/rrdom/src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,16 @@ function diffProps(
}
};
} else if (newTree.tagName === 'IFRAME' && name === 'srcdoc') continue;
else oldTree.setAttribute(name, newValue);
else {
try {
oldTree.setAttribute(name, newValue);
} catch (err) {
// We want to continue diffing so we quietly catch
// this exception. Otherwise, this can throw and bubble up to
// the `ReplayerEvents.Flush` listener and break rendering
console.warn(err);
}
}
}

for (const { name } of Array.from(oldAttributes))
Expand Down
26 changes: 26 additions & 0 deletions packages/rrdom/test/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,32 @@ describe('diff algorithm for rrdom', () => {
expect((node as Node as HTMLElement).className).toBe('node');
});

it('ignores invalid attributes', () => {
const tagName = 'DIV';
const node = document.createElement(tagName);
const sn = Object.assign({}, elementSn, {
attributes: { '@click': 'foo' },
tagName,
});
mirror.add(node, sn);

const rrDocument = new RRDocument();
const rrNode = rrDocument.createElement(tagName);
const sn2 = Object.assign({}, elementSn, {
attributes: { '@click': 'foo' },
tagName,
});
rrDocument.mirror.add(rrNode, sn2);

rrNode.attributes = { id: 'node1', class: 'node', '@click': 'foo' };
diff(node, rrNode, replayer);
expect((node as Node as HTMLElement).id).toBe('node1');
expect((node as Node as HTMLElement).className).toBe('node');
expect('@click' in (node as Node as HTMLElement)).toBe(false);
expect(warn).toHaveBeenCalledTimes(1);
warn.mockClear();
});

it('can update exist properties', () => {
const tagName = 'DIV';
const node = document.createElement(tagName);
Expand Down
Loading