Skip to content

fix: prevent unnecessary property updating in set_attributes #6809

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

Closed
wants to merge 6 commits into from
Closed
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
13 changes: 12 additions & 1 deletion src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export function attr(node: Element, attribute: string, value?: string) {
}

export function set_attributes(node: Element & ElementCSSInlineStyle, attributes: { [x: string]: string }) {
// TODO: is this wasteful because we don't batch read and write operations, potentially causing layout thrashing?
// @ts-ignore
const descriptors = Object.getOwnPropertyDescriptors(node.__proto__);
for (const key in attributes) {
Expand All @@ -305,7 +306,17 @@ export function set_attributes(node: Element & ElementCSSInlineStyle, attributes
} else if (key === '__value') {
(node as any).value = node[key] = attributes[key];
} else if (descriptors[key] && descriptors[key].set) {
node[key] = attributes[key];
// When something changes, spread attributes will also be updated.
// If we don't do the equality check, some attributes cause weird behavior,
// for example the min attribute (from https://github.com/sveltejs/svelte/issues/6751) in Chrome:
// <script>
// const attributes = { min: '2021-09-21' };
// let value = '2021-09-21';
// </script>
// <input type='date' {...attributes} bind:value />
if (node[key] !== attributes[key]) {
node[key] = attributes[key];
}
} else {
attr(node, key, attributes[key]);
}
Expand Down