Skip to content

Commit ebd01fa

Browse files
committed
fix: transform input defaults from spread
1 parent 6837246 commit ebd01fa

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

packages/svelte/src/compiler/phases/3-transform/server/visitors/shared/element.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { regex_starts_with_newline } from '../../../../patterns.js';
1212
import * as b from '#compiler/builders';
1313
import {
14+
ELEMENT_IS_INPUT,
1415
ELEMENT_IS_NAMESPACED,
1516
ELEMENT_PRESERVE_ATTRIBUTE_CASE
1617
} from '../../../../../../constants.js';
@@ -401,6 +402,8 @@ function build_element_spread_attributes(
401402
flags |= ELEMENT_IS_NAMESPACED | ELEMENT_PRESERVE_ATTRIBUTE_CASE;
402403
} else if (is_custom_element_node(element)) {
403404
flags |= ELEMENT_PRESERVE_ATTRIBUTE_CASE;
405+
} else if (element.type === 'RegularElement' && element.name === 'input') {
406+
flags |= ELEMENT_IS_INPUT;
404407
}
405408

406409
const object = build_spread_object(element, attributes, context);

packages/svelte/src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const HYDRATION_ERROR = {};
2828

2929
export const ELEMENT_IS_NAMESPACED = 1;
3030
export const ELEMENT_PRESERVE_ATTRIBUTE_CASE = 1 << 1;
31+
export const ELEMENT_IS_INPUT = 1 << 2;
3132

3233
export const UNINITIALIZED = Symbol();
3334

packages/svelte/src/internal/server/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { subscribe_to_store } from '../../store/utils.js';
88
import {
99
UNINITIALIZED,
1010
ELEMENT_PRESERVE_ATTRIBUTE_CASE,
11-
ELEMENT_IS_NAMESPACED
11+
ELEMENT_IS_NAMESPACED,
12+
ELEMENT_IS_INPUT
1213
} from '../../constants.js';
1314
import { escape_html } from '../../escaping.js';
1415
import { DEV } from 'esm-env';
@@ -187,6 +188,7 @@ export function spread_attributes(attrs, css_hash, classes, styles, flags = 0) {
187188

188189
const is_html = (flags & ELEMENT_IS_NAMESPACED) === 0;
189190
const lowercase = (flags & ELEMENT_PRESERVE_ATTRIBUTE_CASE) === 0;
191+
const ignore_defaults = (flags & ELEMENT_IS_INPUT) === 0;
190192

191193
for (name in attrs) {
192194
// omit functions, internal svelte properties and invalid attribute names
@@ -200,6 +202,14 @@ export function spread_attributes(attrs, css_hash, classes, styles, flags = 0) {
200202
name = name.toLowerCase();
201203
}
202204

205+
if (!ignore_defaults) {
206+
if (name === 'defaultvalue') {
207+
name = 'value';
208+
} else if (name === 'defaultchecked') {
209+
name = 'checked';
210+
}
211+
}
212+
203213
attr_str += attr(name, value, is_html && is_boolean_attribute(name));
204214
}
205215

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
mode: ['server'],
5+
html: `
6+
<input value="a">
7+
<input type="checkbox" checked>
8+
`
9+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
let text = { defaultValue: "a" };
3+
let checkbox = { defaultChecked: true }
4+
</script>
5+
6+
<input {...text} />
7+
<input type="checkbox" {...checkbox} />

0 commit comments

Comments
 (0)