From f9d810a42fdb3aabe8e42cddbe666e12ab9d9013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Mon, 24 Apr 2023 16:09:10 +0200 Subject: [PATCH] [FIX] t-out: does not crash when outputting null Before this commit, doing `t-out="null"` would crash, since the safeOutput function would assume that it is a block, thanks to the fact that `typeof null === 'object'`. We handle here the null value just as if it was undefined: it outputs an empty string. --- src/runtime/template_helpers.ts | 2 +- tests/compiler/__snapshots__/t_out.test.ts.snap | 12 ++++++++++++ tests/compiler/t_out.test.ts | 5 +++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/runtime/template_helpers.ts b/src/runtime/template_helpers.ts index e786f223..239e1029 100644 --- a/src/runtime/template_helpers.ts +++ b/src/runtime/template_helpers.ts @@ -134,7 +134,7 @@ class LazyValue { * Safely outputs `value` as a block depending on the nature of `value` */ export function safeOutput(value: any, defaultValue?: any): ReturnType { - if (value === undefined) { + if (value === undefined || value === null) { return defaultValue ? toggler("default", defaultValue) : toggler("undefined", text("")); } let safeKey; diff --git a/tests/compiler/__snapshots__/t_out.test.ts.snap b/tests/compiler/__snapshots__/t_out.test.ts.snap index d06f9f1d..0d01b718 100644 --- a/tests/compiler/__snapshots__/t_out.test.ts.snap +++ b/tests/compiler/__snapshots__/t_out.test.ts.snap @@ -434,6 +434,18 @@ exports[`t-out t-out with the 0 number, in a p 1`] = ` }" `; +exports[`t-out top level t-out with null 1`] = ` +"function anonymous(app, bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, comment } = bdom; + let { safeOutput } = helpers; + + return function template(ctx, node, key = \\"\\") { + return safeOutput(ctx['var']); + } +}" +`; + exports[`t-out top level t-out with undefined 1`] = ` "function anonymous(app, bdom, helpers ) { diff --git a/tests/compiler/t_out.test.ts b/tests/compiler/t_out.test.ts index 3718dae7..e9c98788 100644 --- a/tests/compiler/t_out.test.ts +++ b/tests/compiler/t_out.test.ts @@ -62,6 +62,11 @@ describe("t-out", () => { expect(renderToString(template, { var: undefined })).toBe(""); }); + test("top level t-out with null", () => { + const template = ``; + expect(renderToString(template, { var: null })).toBe(""); + }); + test("with an extended String class", () => { class LoveString extends String { valueOf(): string {