From a35b9814c08e7933556bd2acbd7d2ef1ed870a55 Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Fri, 24 Feb 2023 13:53:21 +0100 Subject: [PATCH] [FIX] compiler: dynamic value on inputs doesn't turn 0 into empty string In #1161, we fixed blockdom treating 0 as falsy when patching a value, which would result in an empty attribute. It turns out that there is another place where we had the same fallback, which is when we compute a dynamic attribute value, so while blockdom had the ability to set the value to 0, when using a dynamic attribute value we would never give it 0 as a value. This commit changes the fallback strategy for dynamic attribute values to be the same as the one in blockdom. closes #1358 --- src/compiler/code_generator.ts | 4 +- .../__snapshots__/attributes.test.ts.snap | 66 +++++++++++++++++-- .../compiler/__snapshots__/misc.test.ts.snap | 2 +- tests/compiler/attributes.test.ts | 8 +++ .../__snapshots__/t_model.test.ts.snap | 2 +- 5 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/compiler/code_generator.ts b/src/compiler/code_generator.ts index 3b9a0e72..005700e1 100644 --- a/src/compiler/code_generator.ts +++ b/src/compiler/code_generator.ts @@ -580,8 +580,8 @@ export class CodeGenerator { if (attrName && isProp(ast.tag, attrName)) { // we force a new string or new boolean to bypass the equality check in blockdom when patching same value if (attrName === "value") { - // When the expression is falsy, fall back to an empty string - expr = `new String((${expr}) || "")`; + // When the expression is falsy (except 0), fall back to an empty string + expr = `new String((${expr}) === 0 ? 0 : ((${expr}) || ""))`; } else { expr = `new Boolean(${expr})`; } diff --git a/tests/compiler/__snapshots__/attributes.test.ts.snap b/tests/compiler/__snapshots__/attributes.test.ts.snap index 6fc68183..edad8536 100644 --- a/tests/compiler/__snapshots__/attributes.test.ts.snap +++ b/tests/compiler/__snapshots__/attributes.test.ts.snap @@ -238,6 +238,62 @@ exports[`attributes dynamic formatted attributes with a dash 1`] = ` }" `; +exports[`attributes dynamic input value: falsy values 1`] = ` +"function anonymous(app, bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, comment } = bdom; + + let block1 = createBlock(\`\`); + + return function template(ctx, node, key = \\"\\") { + let attr1 = new String((0) === 0 ? 0 : ((0) || \\"\\")); + return block1([attr1]); + } +}" +`; + +exports[`attributes dynamic input value: falsy values 2`] = ` +"function anonymous(app, bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, comment } = bdom; + + let block1 = createBlock(\`\`); + + return function template(ctx, node, key = \\"\\") { + let attr1 = new String((false) === 0 ? 0 : ((false) || \\"\\")); + return block1([attr1]); + } +}" +`; + +exports[`attributes dynamic input value: falsy values 3`] = ` +"function anonymous(app, bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, comment } = bdom; + + let block1 = createBlock(\`\`); + + return function template(ctx, node, key = \\"\\") { + let attr1 = new String((undefined) === 0 ? 0 : ((undefined) || \\"\\")); + return block1([attr1]); + } +}" +`; + +exports[`attributes dynamic input value: falsy values 4`] = ` +"function anonymous(app, bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, comment } = bdom; + + let block1 = createBlock(\`\`); + + return function template(ctx, node, key = \\"\\") { + let attr1 = new String(('') === 0 ? 0 : (('') || \\"\\")); + return block1([attr1]); + } +}" +`; + exports[`attributes dynamic undefined class attribute 1`] = ` "function anonymous(app, bdom, helpers ) { @@ -729,7 +785,7 @@ exports[`attributes updating property with falsy value 1`] = ` let block1 = createBlock(\`\`); return function template(ctx, node, key = \\"\\") { - let attr1 = new String((ctx['v']) || \\"\\"); + let attr1 = new String((ctx['v']) === 0 ? 0 : ((ctx['v']) || \\"\\")); return block1([attr1]); } }" @@ -800,7 +856,7 @@ exports[`special cases for some specific html attributes/properties input with t let block1 = createBlock(\`\`); return function template(ctx, node, key = \\"\\") { - let attr1 = new String((ctx['v']) || \\"\\"); + let attr1 = new String((ctx['v']) === 0 ? 0 : ((ctx['v']) || \\"\\")); return block1([attr1]); } }" @@ -814,7 +870,7 @@ exports[`special cases for some specific html attributes/properties input with t let block1 = createBlock(\`\`); return function template(ctx, node, key = \\"\\") { - let attr1 = new String((ctx['v']) || \\"\\"); + let attr1 = new String((ctx['v']) === 0 ? 0 : ((ctx['v']) || \\"\\")); return block1([attr1]); } }" @@ -842,7 +898,7 @@ exports[`special cases for some specific html attributes/properties select with let block1 = createBlock(\`\`); return function template(ctx, node, key = \\"\\") { - let attr1 = new String((ctx['value']) || \\"\\"); + let attr1 = new String((ctx['value']) === 0 ? 0 : ((ctx['value']) || \\"\\")); return block1([attr1]); } }" @@ -856,7 +912,7 @@ exports[`special cases for some specific html attributes/properties textarea wit let block1 = createBlock(\`