Skip to content

Commit 4a3f7ac

Browse files
authored
fix: improved checked/value handling (#11726)
* fix: improved checked/value handling * tweak
1 parent 77f9145 commit 4a3f7ac

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -467,16 +467,9 @@ function serialize_dynamic_element_attributes(attributes, context, element_id) {
467467
* @param {import('estree').Identifier} node_id
468468
* @param {import('#compiler').Attribute} attribute
469469
* @param {import('../types.js').ComponentContext} context
470-
* @param {boolean} needs_isolation
471470
* @returns {boolean}
472471
*/
473-
function serialize_element_attribute_update_assignment(
474-
element,
475-
node_id,
476-
attribute,
477-
context,
478-
needs_isolation
479-
) {
472+
function serialize_element_attribute_update_assignment(element, node_id, attribute, context) {
480473
const state = context.state;
481474
const name = get_attribute_name(element, attribute, context);
482475
const is_svg = context.state.metadata.namespace === 'svg';
@@ -513,6 +506,10 @@ function serialize_element_attribute_update_assignment(
513506
value
514507
)
515508
);
509+
} else if (name === 'value') {
510+
update = b.stmt(b.call('$.set_value', node_id, value));
511+
} else if (name === 'checked') {
512+
update = b.stmt(b.call('$.set_checked', node_id, value));
516513
} else if (DOMProperties.includes(name)) {
517514
update = b.stmt(b.assignment('=', b.member(node_id, b.id(name)), value));
518515
} else {
@@ -521,7 +518,7 @@ function serialize_element_attribute_update_assignment(
521518
}
522519

523520
if (attribute.metadata.dynamic) {
524-
if (contains_call_expression || needs_isolation) {
521+
if (contains_call_expression) {
525522
state.init.push(serialize_update(update));
526523
} else {
527524
state.update.push(update);
@@ -2072,24 +2069,7 @@ export const template_visitors = {
20722069
const is =
20732070
is_custom_element && child_metadata.namespace !== 'foreign'
20742071
? serialize_custom_element_attribute_update_assignment(node_id, attribute, context)
2075-
: serialize_element_attribute_update_assignment(
2076-
node,
2077-
node_id,
2078-
attribute,
2079-
context,
2080-
/**
2081-
* if the input needs input or content reset we also
2082-
* want to isolate the template effect or else every
2083-
* unrelated change will reset the value (and the user could)
2084-
* change the value outside of the reactivity
2085-
*
2086-
* <input value={val} />
2087-
*
2088-
* should only be updated when val changes and not when another
2089-
* unrelated variable changes.
2090-
* */
2091-
needs_content_reset || needs_input_reset
2092-
);
2072+
: serialize_element_attribute_update_assignment(node, node_id, attribute, context);
20932073
if (is) is_attributes_reactive = true;
20942074
}
20952075
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ export function remove_input_attr_defaults(dom) {
3737
}
3838
}
3939

40+
/**
41+
* @param {Element} element
42+
* @param {any} value
43+
*/
44+
export function set_value(element, value) {
45+
// @ts-expect-error
46+
var attributes = (element.__attributes ??= {});
47+
48+
if (attributes.value === (attributes.value = value)) return;
49+
// @ts-expect-error
50+
element.value = value;
51+
}
52+
53+
/**
54+
* @param {Element} element
55+
* @param {boolean} checked
56+
*/
57+
export function set_checked(element, checked) {
58+
// @ts-expect-error
59+
var attributes = (element.__attributes ??= {});
60+
61+
if (attributes.checked === (attributes.checked = checked)) return;
62+
// @ts-expect-error
63+
element.checked = checked;
64+
}
65+
4066
/**
4167
* @param {Element} element
4268
* @param {string} attribute

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export {
2727
set_custom_element_data,
2828
set_dynamic_element_attributes,
2929
set_xlink_attribute,
30-
handle_lazy_img
30+
handle_lazy_img,
31+
set_value,
32+
set_checked
3133
} from './dom/elements/attributes.js';
3234
export { set_class, set_svg_class, set_mathml_class, toggle_class } from './dom/elements/class.js';
3335
export { event, delegate, replay_events } from './dom/elements/events.js';

0 commit comments

Comments
 (0)