Skip to content

fix(runtime-vapor): properly handle consecutive prepend operations with insertionState #13558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: vapor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/runtime-vapor/__tests__/componentSlots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
insert,
prepend,
renderEffect,
setInsertionState,
template,
} from '../src'
import { currentInstance, nextTick, ref } from '@vue/runtime-dom'
Expand Down Expand Up @@ -502,5 +503,35 @@ describe('component: slots', () => {
await nextTick()
expect(host.innerHTML).toBe('<div><h1></h1><!--slot--></div>')
})

test('consecutive slots with insertion state', async () => {
const { component: Child } = define({
setup() {
const n2 = template('<div><div>baz</div></div>', true)() as any
setInsertionState(n2, 0)
createSlot('default', null)
setInsertionState(n2, 0)
createSlot('foo', null)
return n2
},
})

const { html } = define({
setup() {
return createComponent(Child, null, {
default: () => template('default')(),
foo: () => template('foo')(),
})
},
}).render()

expect(html()).toBe(
`<div>` +
`default<!--slot-->` +
`foo<!--slot-->` +
`<div>baz</div>` +
`</div>`,
)
})
})
})
4 changes: 2 additions & 2 deletions packages/runtime-vapor/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ export function isValidBlock(block: Block): boolean {

export function insert(
block: Block,
parent: ParentNode,
parent: ParentNode & { $anchor?: Node | null },
anchor: Node | null | 0 = null, // 0 means prepend
): void {
anchor = anchor === 0 ? parent.firstChild : anchor
anchor = anchor === 0 ? parent.$anchor || parent.firstChild : anchor
if (block instanceof Node) {
if (!isHydrating) {
parent.insertBefore(block, anchor)
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-vapor/src/dom/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export function optimizePropertyLookup(): void {
if (isOptimized) return
isOptimized = true
const proto = Element.prototype as any
proto.$evtclick = undefined
proto.$anchor = proto.$evtclick = undefined
proto.$root = false
proto.$html =
proto.$txt =
Expand Down
13 changes: 12 additions & 1 deletion packages/runtime-vapor/src/insertionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ export let insertionAnchor: Node | 0 | undefined
* (component, slot outlet, if, for) is created. The state is used for actual
* insertion on client-side render, and used for node adoption during hydration.
*/
export function setInsertionState(parent: ParentNode, anchor?: Node | 0): void {
export function setInsertionState(
parent: ParentNode & { $anchor?: Node | null },
anchor?: Node | 0,
): void {
// When setInsertionState(n3, 0) is called consecutively, the first prepend operation
// uses parent.firstChild as the anchor. However, after insertion, parent.firstChild
// changes and cannot serve as the anchor for subsequent prepends. Therefore, we cache
// the original parent.firstChild on the first call for subsequent prepend operations.
if (anchor === 0 && !parent.$anchor) {
parent.$anchor = parent.firstChild
}

insertionParent = parent
insertionAnchor = anchor
}
Expand Down