fix: restore value after attribute removal during hydration (#11465)

Fix #11457
pull/11487/head
Paolo Ricciuti 8 months ago committed by GitHub
parent f2f71ae3a1
commit 34079a0ec5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: restore value after attribute removal during hydration

@ -16,8 +16,12 @@ import * as w from '../../warnings.js';
*/
export function remove_input_attr_defaults(dom) {
if (hydrating) {
// using getAttribute instead of dome.value allow us to have
// null instead of "on" if the user didn't set a value
const value = dom.getAttribute('value');
set_attribute(dom, 'value', null);
set_attribute(dom, 'checked', null);
if (value) dom.value = value;
}
}

@ -0,0 +1,10 @@
import { test } from '../../assert';
export default test({
html: `<input type="checkbox" name="lang" value="keep" />`,
ssrHtml: `<input type="checkbox" name="lang" value="keep" checked />`,
test({ window, assert, target, mod }) {
const input = target.querySelector('input');
assert.equal(input?.checked, true);
}
});

@ -0,0 +1,5 @@
<script>
let to_check = $state("keep")
</script>
<input type="checkbox" name="lang" value="keep" checked={to_check === "keep"} />
Loading…
Cancel
Save