[FIX] blockdom: toString method in multi could crash

This commit is contained in:
Géry Debongnie
2022-01-17 13:51:50 +01:00
committed by Aaron Bohy
parent 281b32965e
commit 7711733a23
4 changed files with 44 additions and 1 deletions
+1 -1
View File
@@ -127,7 +127,7 @@ export class VMulti {
}
toString(): string {
return this.children.map((c) => c!.toString()).join("");
return this.children.map((c) => (c ? c!.toString() : "")).join("");
}
}
+5
View File
@@ -77,6 +77,11 @@ describe("multi blocks", () => {
expect(fixture.innerHTML).toBe("ab");
});
test("multi vnode can be used as text", () => {
mount(text(multi([text("a"), undefined]) as any), fixture);
expect(fixture.innerHTML).toBe("a");
});
test("multi inside a block", async () => {
const block = createBlock("<div><block-child-0/></div>");
const tree = block([], [multi([text("foo"), text("bar")])]);
@@ -87,6 +87,31 @@ exports[`t-set set from attribute lookup 1`] = `
}"
`;
exports[`t-set set from body literal (with t-if/t-else 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
let { isBoundary, withDefault, LazyValue } = helpers;
function value1(ctx, node, key = \\"\\") {
let b2,b3;
if (ctx['condition']) {
b2 = text(\`true\`);
} else {
b3 = text(\`false\`);
}
return multi([b2, b3]);
}
return function template(ctx, node, key = \\"\\") {
ctx = Object.create(ctx);
ctx[isBoundary] = 1
ctx[\`value\`] = new LazyValue(value1, ctx, node);
return text(ctx['value']);
}
}"
`;
exports[`t-set set from body literal 1`] = `
"function anonymous(bdom, helpers
) {
+13
View File
@@ -38,6 +38,19 @@ describe("t-set", () => {
expect(renderToString(template)).toBe("ok");
});
test("set from body literal (with t-if/t-else", () => {
const template = `
<t>
<t t-set="value">
<t t-if="condition">true</t>
<t t-else="">false</t>
</t>
<t t-esc="value"/>
</t>`;
expect(renderToString(template, { condition: true })).toBe("true");
expect(renderToString(template, { condition: false })).toBe("false");
});
test("set from attribute lookup", () => {
const template = `<div><t t-set="stuff" t-value="value"/><t t-esc="stuff"/></div>`;
expect(renderToString(template, { value: "ok" })).toBe("<div>ok</div>");