diff --git a/src/compiler/code_generator.ts b/src/compiler/code_generator.ts
index d983c933..81cf3523 100644
--- a/src/compiler/code_generator.ts
+++ b/src/compiler/code_generator.ts
@@ -523,7 +523,7 @@ export class CodeGenerator {
const isNewBlock = !block || forceNewBlock || ast.dynamicTag !== null;
let codeIdx = this.target.code.length;
if (isNewBlock) {
- if (ast.dynamicTag && ctx.block) {
+ if ((ast.dynamicTag || ctx.tKeyExpr) && ctx.block) {
this.insertAnchor(ctx.block!);
}
block = this.createBlock(block, "block", ctx);
diff --git a/tests/compiler/__snapshots__/t_key.test.ts.snap b/tests/compiler/__snapshots__/t_key.test.ts.snap
index b755f05f..65bff25d 100644
--- a/tests/compiler/__snapshots__/t_key.test.ts.snap
+++ b/tests/compiler/__snapshots__/t_key.test.ts.snap
@@ -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(\`
\`);
+ let block2 = createBlock(\`\`);
+ let block3 = createBlock(\`\`);
+
+ 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(\`\`);
+
+ return function template(ctx, node, key = \\"\\") {
+ const tKey_1 = ctx['key'];
+ return toggler(tKey_1, block1());
+ }
+}"
+`;
diff --git a/tests/compiler/t_key.test.ts b/tests/compiler/t_key.test.ts
index 20ad0643..7b13a8a5 100644
--- a/tests/compiler/t_key.test.ts
+++ b/tests/compiler/t_key.test.ts
@@ -43,4 +43,24 @@ describe("t-key", () => {
})
).toBe("");
});
+
+ test("t-key on sub dom node pushes a child block in its parent", async () => {
+ const template = `
+
+ `;
+
+ expect(renderToString(template, { key: "1" })).toBe("");
+ expect(renderToString(template, { hasSpan: true, key: "1" })).toBe(
+ ""
+ );
+
+ const template2 = `
+
+ `;
+
+ expect(renderToString(template2, { key: "1" })).toBe("");
+ });
});