From 1d7503913e1b0e5710df3cf1706e0f301859b86a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sat, 15 Feb 2020 22:17:59 +0100 Subject: [PATCH] [FIX] qweb: ignore comment nodes between t-if/t-elif/t-else closes #636 --- src/qweb/qweb.ts | 5 +++-- tests/qweb/__snapshots__/qweb.test.ts.snap | 24 ++++++++++++++++++++++ tests/qweb/qweb.test.ts | 13 ++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/qweb/qweb.ts b/src/qweb/qweb.ts index ec32ef81..00b43fba 100644 --- a/src/qweb/qweb.ts +++ b/src/qweb/qweb.ts @@ -328,10 +328,11 @@ export class QWeb extends EventBus { ) { throw new Error("Only one conditional branching directive is allowed per node"); } - // All text nodes between branch nodes are removed + // All text (with only spaces) and comment nodes (nodeType 8) between + // branch nodes are removed let textNode; while ((textNode = node.previousSibling) !== prevElem) { - if (textNode.nodeValue.trim().length) { + if (textNode.nodeValue.trim().length && textNode.nodeType !== 8) { throw new Error("text is not allowed between branching directives"); } textNode.remove(); diff --git a/tests/qweb/__snapshots__/qweb.test.ts.snap b/tests/qweb/__snapshots__/qweb.test.ts.snap index cb8a4f77..899a4397 100644 --- a/tests/qweb/__snapshots__/qweb.test.ts.snap +++ b/tests/qweb/__snapshots__/qweb.test.ts.snap @@ -987,6 +987,30 @@ exports[`static templates properly handle comments 1`] = ` }" `; +exports[`static templates properly handle comments between t-if/t-else 1`] = ` +"function anonymous(context, extra +) { + // Template name: \\"test\\" + let scope = Object.create(context); + let h = this.h; + let c1 = [], p1 = {key:1}; + let vn1 = h('div', p1, c1); + if (true) { + let c2 = [], p2 = {key:2}; + let vn2 = h('span', p2, c2); + c1.push(vn2); + c2.push({text: \`true\`}); + } + else { + let c3 = [], p3 = {key:3}; + let vn3 = h('span', p3, c3); + c1.push(vn3); + c3.push({text: \`owl\`}); + } + return vn1; +}" +`; + exports[`static templates simple dynamic value 1`] = ` "function anonymous(context, extra ) { diff --git a/tests/qweb/qweb.test.ts b/tests/qweb/qweb.test.ts index 7e0832fc..a98b3a1f 100644 --- a/tests/qweb/qweb.test.ts +++ b/tests/qweb/qweb.test.ts @@ -65,6 +65,19 @@ describe("static templates", () => { qweb.addTemplate("test", "
hello owl
"); expect(renderToString(qweb, "test")).toBe("
hello owl
"); }); + + test("properly handle comments between t-if/t-else", () => { + qweb.addTemplate( + "test", + ` +
+ true + + owl +
` + ); + expect(renderToString(qweb, "test")).toBe("
true
"); + }); }); describe("error handling", () => {