From e0089662911b928c6e221f9ad94ec9998e249134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Wed, 1 Dec 2021 13:16:32 +0100 Subject: [PATCH] [FIX] compiler: allow t-if with empty content --- src/compiler/parser.ts | 5 +---- tests/compiler/__snapshots__/t_if.test.ts.snap | 17 +++++++++++++++++ tests/compiler/parser.test.ts | 13 +++++++++++++ tests/compiler/t_if.test.ts | 6 ++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 443af533..63ac828b 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -639,10 +639,7 @@ function parseTIf(node: Element, ctx: ParsingContext): AST | null { } const condition = node.getAttribute("t-if")!; node.removeAttribute("t-if"); - const content = parseNode(node, ctx); - if (!content) { - throw new Error("hmmm"); - } + const content = parseNode(node, ctx) || { type: ASTType.Text, value: "" }; let nextElement = node.nextElementSibling; // t-elifs diff --git a/tests/compiler/__snapshots__/t_if.test.ts.snap b/tests/compiler/__snapshots__/t_if.test.ts.snap index f69bec38..202e852b 100644 --- a/tests/compiler/__snapshots__/t_if.test.ts.snap +++ b/tests/compiler/__snapshots__/t_if.test.ts.snap @@ -384,6 +384,23 @@ exports[`t-if t-if in a t-if 1`] = ` }" `; +exports[`t-if t-if with empty content 1`] = ` +"function anonymous(bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, component, comment } = bdom; + let { withDefault, getTemplate, prepareList, withKey, zero, call, callSlot, capture, isBoundary, shallowEqual, setContextValue, toNumber, safeOutput } = helpers; + + return function template(ctx, node, key = \\"\\") { + let b2,b3; + b2 = text(\`hello\`); + if (ctx['condition']) { + b3 = text(\`\`); + } + return multi([b2, b3]); + } +}" +`; + exports[`t-if t-if/t-else with more content 1`] = ` "function anonymous(bdom, helpers ) { diff --git a/tests/compiler/parser.test.ts b/tests/compiler/parser.test.ts index 39f6dc94..9f1d102a 100644 --- a/tests/compiler/parser.test.ts +++ b/tests/compiler/parser.test.ts @@ -395,6 +395,19 @@ describe("qweb parser", () => { }); }); + test("t-if with empty content", async () => { + expect(parse(``)).toEqual({ + type: ASTType.TIf, + condition: "condition", + content: { + type: ASTType.Text, + value: "", + }, + tElif: null, + tElse: null, + }); + }); + test("t-if (on dom node", async () => { expect(parse(`
hey
`)).toEqual({ type: ASTType.TIf, diff --git a/tests/compiler/t_if.test.ts b/tests/compiler/t_if.test.ts index 02a83130..d5af1a72 100644 --- a/tests/compiler/t_if.test.ts +++ b/tests/compiler/t_if.test.ts @@ -16,6 +16,12 @@ describe("t-if", () => { expect(renderToString(template, {})).toBe("
"); }); + test("t-if with empty content", () => { + const template = `hello`; + expect(renderToString(template, { condition: true })).toBe("hello"); + expect(renderToString(template, { condition: false })).toBe("hello"); + }); + test("boolean value condition missing", () => { const template = `fail`; expect(renderToString(template)).toBe("");