Skip to content

Commit 34079a0

Browse files
fix: restore value after attribute removal during hydration (#11465)
Fix #11457
1 parent f2f71ae commit 34079a0

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

.changeset/loud-numbers-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: restore value after attribute removal during hydration

packages/svelte/src/internal/client/dom/elements/attributes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ import * as w from '../../warnings.js';
1616
*/
1717
export function remove_input_attr_defaults(dom) {
1818
if (hydrating) {
19+
// using getAttribute instead of dome.value allow us to have
20+
// null instead of "on" if the user didn't set a value
21+
const value = dom.getAttribute('value');
1922
set_attribute(dom, 'value', null);
2023
set_attribute(dom, 'checked', null);
24+
if (value) dom.value = value;
2125
}
2226
}
2327

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { test } from '../../assert';
2+
3+
export default test({
4+
html: `<input type="checkbox" name="lang" value="keep" />`,
5+
ssrHtml: `<input type="checkbox" name="lang" value="keep" checked />`,
6+
test({ window, assert, target, mod }) {
7+
const input = target.querySelector('input');
8+
assert.equal(input?.checked, true);
9+
}
10+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let to_check = $state("keep")
3+
</script>
4+
5+
<input type="checkbox" name="lang" value="keep" checked={to_check === "keep"} />

0 commit comments

Comments
 (0)