mirror of https://github.com/sveltejs/svelte
fix: set input type before spread value (#18345)
### Before submitting the PR, please make sure you do the following - [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs - [x] Prefix your PR title with `feat:`, `fix:`, `chore:`, or `docs:`. - [x] This message body should clearly illustrate what problems it solves. - [x] Ideally, include a test that fails without this PR but passes with it. - [x] If this PR changes code within `packages/svelte/src`, add a changeset (`npx changeset`). ### Tests and linting - [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` Fixes #18332. When spread props put `value` before a later `type="hidden"` on an `<input>`, `set_attributes` currently writes the value while the element is still a text input. Browsers sanitize text input values by removing newlines, so the hidden input permanently loses them before `type` is applied. This sets an input's `type` first whenever the same spread update also includes `value` or `__value`, so the existing value handling runs with the final input type. The new runtime-browser sample covers both the problematic spread-first order and the already-working type-first order. Validation: - `corepack pnpm lint` - `CI=1 corepack pnpm test` - `corepack pnpm vitest run packages/svelte/tests/runtime-browser/test.ts -t input-type-before-value-spread` --------- Co-authored-by: Rich Harris <rich.harris@vercel.com> Co-authored-by: Rich Harris <hello@rich-harris.dev>pull/18348/head
parent
2f6307af65
commit
378bb25097
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: preserve newlines in spread input values when the `type` attribute is applied after `value`
|
||||
@ -0,0 +1,15 @@
|
||||
import { test } from '../../test';
|
||||
|
||||
const value = 'line1\nline2\nline3';
|
||||
|
||||
export default test({
|
||||
mode: ['client'],
|
||||
test({ assert, target }) {
|
||||
const [spread_first, type_first] = target.querySelectorAll('input');
|
||||
|
||||
assert.equal(spread_first?.type, 'hidden');
|
||||
assert.equal(spread_first?.value, value);
|
||||
assert.equal(type_first?.type, 'hidden');
|
||||
assert.equal(type_first?.value, value);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,7 @@
|
||||
<script>
|
||||
const value = 'line1\nline2\nline3';
|
||||
const spread = { name: 'field', value };
|
||||
</script>
|
||||
|
||||
<input {...spread} type="hidden" />
|
||||
<input type="hidden" {...spread} />
|
||||
Loading…
Reference in new issue