Skip to content

Commit 2a76b52

Browse files
committed
wip(vapor): fix children gen for dynamic with anchor insertion
1 parent a51dd42 commit 2a76b52

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformChildren.spec.ts.snap

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`compiler: children transform > anchor insertion in middle 1`] = `
4+
"import { child as _child, next as _next, setInsertionState as _setInsertionState, createIf as _createIf, template as _template } from 'vue';
5+
const t0 = _template("<div></div>")
6+
const t1 = _template("<div><div></div><!><div></div></div>", true)
7+
8+
export function render(_ctx) {
9+
const n4 = t1()
10+
const n3 = _next(_child(n4))
11+
_setInsertionState(n4, n3)
12+
const n0 = _createIf(() => (1), () => {
13+
const n2 = t0()
14+
return n2
15+
}, null, true)
16+
return n4
17+
}"
18+
`;
19+
320
exports[`compiler: children transform > children & sibling references 1`] = `
421
"import { child as _child, next as _next, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, template as _template } from 'vue';
522
const t0 = _template("<div><p> </p> <p> </p></div>", true)

packages/compiler-vapor/__tests__/transforms/transformChildren.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,18 @@ describe('compiler: children transform', () => {
5959
expect(code).contains(`const n0 = _nthChild(n1, 2)`)
6060
expect(code).toMatchSnapshot()
6161
})
62+
63+
test('anchor insertion in middle', () => {
64+
const { code } = compileWithElementTransform(
65+
`<div>
66+
<div></div>
67+
<div v-if="1"></div>
68+
<div></div>
69+
</div>`,
70+
)
71+
// ensure the insertion anchor is generated before the insertion statement
72+
expect(code).toMatch(`const n3 = _next(_child(n4))
73+
_setInsertionState(n4, n3)`)
74+
expect(code).toMatchSnapshot()
75+
})
6276
})

packages/compiler-vapor/src/generators/template.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,23 @@ export function genChildren(
9494
push(...init)
9595
}
9696
}
97+
98+
if (id === child.anchor) {
99+
push(...genSelf(child, context))
100+
}
101+
97102
if (id !== undefined) {
98103
push(...genDirectivesForElement(id, context))
99104
}
105+
100106
prev = [variable, elementIndex]
101107
childrenToGen.push([child, variable])
102108
}
103109

104-
for (const [child, from] of childrenToGen) {
105-
push(...genChildren(child, context, from))
110+
if (childrenToGen.length) {
111+
for (const [child, from] of childrenToGen) {
112+
push(...genChildren(child, context, from))
113+
}
106114
}
107115

108116
return frag

packages/runtime-vapor/__tests__/hydration.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,27 @@ describe('Vapor Mode hydration', () => {
239239
)
240240
})
241241

242+
// problem is the <!> placeholder does not exist in SSR output
243+
test.todo('component with anchor insertion', async () => {
244+
const { container, data } = await testHydration(
245+
`
246+
<template><div><span/><components.Child/><span/></div></template>
247+
`,
248+
{
249+
Child: `<template>{{ data }}</template>`,
250+
},
251+
)
252+
expect(container.innerHTML).toMatchInlineSnapshot(
253+
`"<div><span></span>foo<span></span></div>"`,
254+
)
255+
256+
data.value = 'bar'
257+
await nextTick()
258+
expect(container.innerHTML).toMatchInlineSnapshot(
259+
`"<div><span></span>foo<span></span></div>"`,
260+
)
261+
})
262+
242263
test.todo('if')
243264

244265
test.todo('for')

0 commit comments

Comments
 (0)