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: ensure input value is correctly set during hydration #12083

Merged
merged 9 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/moody-toys-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: ensure input value is correctly set during hydration
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ export function remove_input_attr_defaults(dom) {
const remove_defaults = () => {
if (already_removed) return;
already_removed = true;
const value = dom.getAttribute('value');
debugger;
const value_attribute = dom.getAttribute('value');
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
const value = dom.value;
set_attribute(dom, 'value', null);
set_attribute(dom, 'checked', null);
if (value) dom.value = value;
if (value_attribute) dom.value = value;
};
// @ts-expect-error
dom.__on_r = remove_defaults;
Expand All @@ -50,6 +52,7 @@ export function set_value(element, value) {
var attributes = (element.__attributes ??= {});

if (attributes.value === (attributes.value = value)) return;
debugger;
trueadm marked this conversation as resolved.
Show resolved Hide resolved
// @ts-expect-error
element.value = value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test } from '../../test';

export default test({
skip_mode: ['client'],

async test({ assert, target, hydrate }) {
// @ts-ignore
window.is_browser = true;
hydrate();
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
assert.htmlEqual(target.innerHTML, '<input type="text">');
const input = /** @type {HTMLInputElement} */ (target.querySelector('input'));
assert.htmlEqual(input.value, 'browser');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
// @ts-ignore
let value = window.is_browser ? 'browser' : 'server';
</script>

<input type="text" {value} />
Loading