Skip to content

Commit 8f8a5b1

Browse files
committed
tweak implementation
1 parent 0a95949 commit 8f8a5b1

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

packages/svelte/src/internal/client/dom/blocks/svelte-html.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@ export function svelte_html(get_attributes) {
2222
attributes = get_attributes();
2323

2424
for (const name in attributes) {
25+
const current = (current_setters[name] ??= []);
26+
const idx = current.findIndex(([owner]) => owner === own);
27+
const old = idx === -1 ? null : current.splice(idx, 1)[0][1];
28+
2529
let value = attributes[name];
26-
current_setters[name] = (current_setters[name] ?? []).filter(([owner]) => owner !== own);
27-
current_setters[name].unshift([own, value]);
30+
current.push([own, value]);
2831

2932
// Do nothing on initial render during hydration: If there are attribute duplicates, the last value
3033
// wins, which could result in needless hydration repairs from earlier values.
3134
if (hydrating) continue;
3235

3336
if (name === 'class') {
34-
set_class(node, current_setters[name].map(([_, value]) => value).join(' '));
37+
// Avoid unrelated attribute changes from triggering class changes
38+
if (old !== value) {
39+
set_class(node, current_setters[name].map(([_, text]) => text).join(' '));
40+
}
3541
} else {
3642
set_attribute(node, name, value);
3743
}
@@ -45,9 +51,11 @@ export function svelte_html(get_attributes) {
4551
const current = current_setters[name];
4652

4753
if (name === 'class') {
48-
set_class(node, current.map(([_, value]) => value).join(' '));
49-
} else if (old[0][0] === own) {
50-
set_attribute(node, name, current[0]?.[1]);
54+
set_class(node, current.map(([_, text]) => text).join(' '));
55+
56+
// If this was the last one setting this attribute, revert to the previous value
57+
} else if (old[old.length - 1][0] === own) {
58+
set_attribute(node, name, current[current.length - 1]?.[1]);
5159
}
5260
}
5361
});

0 commit comments

Comments
 (0)