From 229422d0502683e80339f46deb9ea3f3a73a8216 Mon Sep 17 00:00:00 2001 From: David Hunt Date: Fri, 20 Jan 2023 07:47:57 -0800 Subject: [PATCH 1/2] fixes sveltejs/svelte#8214 bind:group to undefined --- src/compiler/compile/render_dom/wrappers/Element/Binding.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/compile/render_dom/wrappers/Element/Binding.ts b/src/compiler/compile/render_dom/wrappers/Element/Binding.ts index 9e2ec85f0fe8..2e3a5766a3fb 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/Binding.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/Binding.ts @@ -264,7 +264,7 @@ function get_dom_updater( const type = node.get_static_attribute_value('type'); const condition = type === 'checkbox' - ? x`~${binding.snippet}.indexOf(${element.var}.__value)` + ? x`~(${binding.snippet} ?? [])?.indexOf(${element.var}.__value)` : x`${element.var}.__value === ${binding.snippet}`; return b`${element.var}.checked = ${condition};`; From 9b571f75dd6ea5240636704571f6417960dd8005 Mon Sep 17 00:00:00 2001 From: Yuichiro Yamashita Date: Thu, 23 Feb 2023 16:02:54 +0900 Subject: [PATCH 2/2] fix code and add test --- .../render_dom/wrappers/Element/Binding.ts | 2 +- .../binding-input-group-undefined/_config.js | 25 +++++++++++++++++++ .../binding-input-group-undefined/main.svelte | 15 +++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/binding-input-group-undefined/_config.js create mode 100644 test/runtime/samples/binding-input-group-undefined/main.svelte diff --git a/src/compiler/compile/render_dom/wrappers/Element/Binding.ts b/src/compiler/compile/render_dom/wrappers/Element/Binding.ts index 2e3a5766a3fb..6ea687e0d14d 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/Binding.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/Binding.ts @@ -264,7 +264,7 @@ function get_dom_updater( const type = node.get_static_attribute_value('type'); const condition = type === 'checkbox' - ? x`~(${binding.snippet} ?? [])?.indexOf(${element.var}.__value)` + ? x`~(${binding.snippet} || []).indexOf(${element.var}.__value)` : x`${element.var}.__value === ${binding.snippet}`; return b`${element.var}.checked = ${condition};`; diff --git a/test/runtime/samples/binding-input-group-undefined/_config.js b/test/runtime/samples/binding-input-group-undefined/_config.js new file mode 100644 index 000000000000..501e37813ced --- /dev/null +++ b/test/runtime/samples/binding-input-group-undefined/_config.js @@ -0,0 +1,25 @@ +export default { + + async test({ assert, target, component, window }) { + const [input1, input2, input3] = target.querySelectorAll('input'); + const event = new window.Event('change'); + + function validate_inputs(v1, v2, v3) { + assert.equal(input1.checked, v1); + assert.equal(input2.checked, v2); + assert.equal(input3.checked, v3); + } + + assert.deepEqual(component.values.inner, []); + validate_inputs(false, false, false); + + component.values = { inner: undefined }; + assert.deepEqual(component.values.inner, undefined); + validate_inputs(false, false, false); + + input1.checked = true; + await input1.dispatchEvent(event); + assert.deepEqual(component.values.inner, ['first']); + validate_inputs(true, false, false); + } +}; diff --git a/test/runtime/samples/binding-input-group-undefined/main.svelte b/test/runtime/samples/binding-input-group-undefined/main.svelte new file mode 100644 index 000000000000..4dadc84a3106 --- /dev/null +++ b/test/runtime/samples/binding-input-group-undefined/main.svelte @@ -0,0 +1,15 @@ + + + + + + +
+ {#each ['first', 'second', 'third'] as k} + {k} + {/each} +