Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c7505c

Browse files
committed
fix(runtime-vapor): re-sync whole slot fallback carrier block
1 parent 5cc1732 commit 9c7505c

3 files changed

Lines changed: 69 additions & 3 deletions

File tree

‎packages/runtime-vapor/__tests__/componentSlots.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,38 @@ describe('component: slots', () => {
418418
expect(fallback.onUpdated).toHaveLength(1)
419419
})
420420

421+
test('slot fallback controller re-syncs the whole carrier block order', async () => {
422+
const container = document.createElement('div')
423+
const carrierA = document.createTextNode('x')
424+
const carrierB = document.createTextNode('y')
425+
const marker = document.createTextNode('!')
426+
const slotAnchor = document.createComment('slot')
427+
const fallback = new VaporFragment([
428+
document.createTextNode('a'),
429+
document.createTextNode('b'),
430+
])
431+
const controller = new SlotFallbackController({
432+
getParentBoundary: () => null,
433+
getLocalFallback: () => () => fallback,
434+
getContent: () => [carrierA, carrierB],
435+
getParentNode: () => container,
436+
getAnchor: () => slotAnchor,
437+
runWithRenderCtx: fn => fn(),
438+
isContentValid: () => false,
439+
onValidityChange: vi.fn(),
440+
})
441+
442+
container.append(carrierA, marker, carrierB, slotAnchor)
443+
controller.recheck()
444+
445+
expect(container.innerHTML).toBe('abx!y<!--slot-->')
446+
447+
controller.syncActiveFallback()
448+
await nextTick()
449+
450+
expect(container.innerHTML).toBe('abxy!<!--slot-->')
451+
})
452+
421453
test('slot fallback controller defaults to idle when isBusy is omitted', () => {
422454
const fallback = document.createTextNode('fallback')
423455
const controller = new SlotFallbackController({

‎packages/runtime-vapor/src/apiCreateFor.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ export const createFor = (
176176
) {
177177
setCurrentHydrationNode(hydrationStart)
178178
}
179-
queuePostFlushCb(() =>
180-
anchor.parentNode!.insertBefore(parentAnchor, anchor),
181-
)
179+
queuePostFlushCb(() => {
180+
const parentNode = anchor.parentNode
181+
if (parentNode) parentNode.insertBefore(parentAnchor, anchor)
182+
})
182183
} else {
183184
const close = locateHydrationBoundaryClose(currentHydrationNode!)
184185
parentAnchor = markHydrationAnchor(close)

‎packages/runtime-vapor/src/fragment.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,39 @@ function collectBlockNodes(
698698
return nodes
699699
}
700700

701+
function collectSlotFallbackCarrierNodes(
702+
block: Block,
703+
nodes: Node[] = [],
704+
includeComments: boolean = false,
705+
): Node[] {
706+
if (block instanceof Node) {
707+
if (includeComments || !(block instanceof Comment)) {
708+
nodes.push(block)
709+
}
710+
return nodes
711+
}
712+
713+
if (isVaporComponent(block)) {
714+
if (block.block) {
715+
collectSlotFallbackCarrierNodes(block.block, nodes, includeComments)
716+
}
717+
return nodes
718+
}
719+
720+
if (isArray(block)) {
721+
for (const child of block) {
722+
collectSlotFallbackCarrierNodes(child, nodes, includeComments)
723+
}
724+
return nodes
725+
}
726+
727+
collectSlotFallbackCarrierNodes(block.nodes, nodes, true)
728+
if (block.anchor) {
729+
nodes.push(block.anchor)
730+
}
731+
return nodes
732+
}
733+
701734
export function insertSlotFallbackCarrier(
702735
block: Block,
703736
parent: ParentNode,

0 commit comments

Comments
 (0)