[FIX] compiler: t-key on a sub-domnode pushes an anchor in parent block

This commit is contained in:
Lucas Perais (lpe)
2022-01-20 16:26:05 +01:00
committed by Géry Debongnie
parent 45f6a8e97a
commit 055cbda750
3 changed files with 56 additions and 1 deletions
+1 -1
View File
@@ -523,7 +523,7 @@ export class CodeGenerator {
const isNewBlock = !block || forceNewBlock || ast.dynamicTag !== null; const isNewBlock = !block || forceNewBlock || ast.dynamicTag !== null;
let codeIdx = this.target.code.length; let codeIdx = this.target.code.length;
if (isNewBlock) { if (isNewBlock) {
if (ast.dynamicTag && ctx.block) { if ((ast.dynamicTag || ctx.tKeyExpr) && ctx.block) {
this.insertAnchor(ctx.block!); this.insertAnchor(ctx.block!);
} }
block = this.createBlock(block, "block", ctx); block = this.createBlock(block, "block", ctx);
@@ -68,3 +68,38 @@ exports[`t-key t-key directive in a list 1`] = `
} }
}" }"
`; `;
exports[`t-key t-key on sub dom node pushes a child block in its parent 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
let block1 = createBlock(\`<div><block-child-0/><block-child-1/></div>\`);
let block2 = createBlock(\`<span/>\`);
let block3 = createBlock(\`<div><h1/></div>\`);
return function template(ctx, node, key = \\"\\") {
let b2,b3;
if (ctx['hasSpan']) {
b2 = block2();
}
const tKey_1 = ctx['key'];
b3 = toggler(tKey_1, block3());
return block1([], [b2, b3]);
}
}"
`;
exports[`t-key t-key on sub dom node pushes a child block in its parent 2`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
let block1 = createBlock(\`<div><h1/></div>\`);
return function template(ctx, node, key = \\"\\") {
const tKey_1 = ctx['key'];
return toggler(tKey_1, block1());
}
}"
`;
+20
View File
@@ -43,4 +43,24 @@ describe("t-key", () => {
}) })
).toBe("<ul><li>Chimay Rouge</li></ul>"); ).toBe("<ul><li>Chimay Rouge</li></ul>");
}); });
test("t-key on sub dom node pushes a child block in its parent", async () => {
const template = `
<div>
<t t-if="hasSpan"><span /></t>
<div t-key="key"><h1 /></div>
</div>
`;
expect(renderToString(template, { key: "1" })).toBe("<div><div><h1></h1></div></div>");
expect(renderToString(template, { hasSpan: true, key: "1" })).toBe(
"<div><span></span><div><h1></h1></div></div>"
);
const template2 = `
<div t-key="key"><h1 /></div>
`;
expect(renderToString(template2, { key: "1" })).toBe("<div><h1></h1></div>");
});
}); });