From c718d8e6c6070dd34a80968db7abc24ead506979 Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Wed, 10 Nov 2021 09:04:22 +0100 Subject: [PATCH] [FIX] qweb: fix crash when component only renders empty slot --- src/qweb/template_helpers.ts | 4 ++-- tests/components/__snapshots__/slots.test.ts.snap | 12 ++++++++++++ tests/components/slots.test.ts | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/qweb/template_helpers.ts b/src/qweb/template_helpers.ts index 9c679220..432de023 100644 --- a/src/qweb/template_helpers.ts +++ b/src/qweb/template_helpers.ts @@ -18,7 +18,7 @@ function callSlot( name: string, defaultSlot?: (ctx: any, key: string) => BDom, dynamic?: boolean -): BDom | null { +): BDom { const slots = ctx.__owl__.slots; const slotFn = slots[name]; const slotBDom = slotFn ? slotFn(parent, key) : null; @@ -33,7 +33,7 @@ function callSlot( } return multi([child1, child2]); } - return slotBDom; + return slotBDom || text(""); } function capture(ctx: any): any { diff --git a/tests/components/__snapshots__/slots.test.ts.snap b/tests/components/__snapshots__/slots.test.ts.snap index 12297dba..1d1ca6de 100644 --- a/tests/components/__snapshots__/slots.test.ts.snap +++ b/tests/components/__snapshots__/slots.test.ts.snap @@ -43,6 +43,18 @@ exports[`slots can define and call slots 2`] = ` }" `; +exports[`slots can render only empty slot 1`] = ` +"function anonymous(bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, component } = bdom; + let { withDefault, getTemplate, prepareList, withKey, zero, call, callSlot, capture, isBoundary, shallowEqual, setContextValue, toNumber } = helpers; + + return function template(ctx, node, key = \\"\\") { + return callSlot(ctx, node, key, 'default'); + } +}" +`; + exports[`slots content is the default slot (variation) 1`] = ` "function anonymous(bdom, helpers ) { diff --git a/tests/components/slots.test.ts b/tests/components/slots.test.ts index 3048924c..9fbea862 100644 --- a/tests/components/slots.test.ts +++ b/tests/components/slots.test.ts @@ -1332,4 +1332,19 @@ describe("slots", () => { expect(fixture.innerHTML).toBe("

Ablip

Bblip

"); }); + + test("can render only empty slot", async () => { + class Parent extends Component { + static template = xml``; + } + + let error = null; + try { + await mount(Parent, fixture); + } catch (e) { + error = e; + } + expect(error).toBeNull(); + expect(fixture.innerHTML).toEqual(""); + }); });