Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function render(_ctx) {

exports[`compiler: transform <slot> outlets > default slot outlet with fallback 1`] = `
"import { createSlot as _createSlot, template as _template } from 'vue';
const t0 = _template("<div>", 3)
const t0 = _template("<div>", 2)

export function render(_ctx) {
const n0 = _createSlot("default", null, () => {
Expand All @@ -24,7 +24,7 @@ export function render(_ctx) {

exports[`compiler: transform <slot> outlets > default slot outlet with props & fallback 1`] = `
"import { createSlot as _createSlot, template as _template } from 'vue';
const t0 = _template("<div>", 3)
const t0 = _template("<div>", 2)

export function render(_ctx) {
const n0 = _createSlot("default", { foo: () => (_ctx.bar) }, () => {
Expand All @@ -51,7 +51,7 @@ export function render(_ctx) {
exports[`compiler: transform <slot> outlets > does not mark non-root fallback v-if as slot root 1`] = `
"import { setInsertionState as _setInsertionState, createIf as _createIf, createSlot as _createSlot, template as _template } from 'vue';
const t0 = _template("<span>", 2)
const t1 = _template("<div>", 1)
const t1 = _template("<div>")

export function render(_ctx) {
const n0 = _createSlot("default", null, () => {
Expand Down Expand Up @@ -105,7 +105,7 @@ export function render(_ctx) {

exports[`compiler: transform <slot> outlets > named slot outlet with fallback 1`] = `
"import { createSlot as _createSlot, template as _template } from 'vue';
const t0 = _template("<div>", 3)
const t0 = _template("<div>", 2)

export function render(_ctx) {
const n0 = _createSlot("foo", null, () => {
Expand All @@ -118,7 +118,7 @@ export function render(_ctx) {

exports[`compiler: transform <slot> outlets > named slot outlet with props & fallback 1`] = `
"import { createSlot as _createSlot, template as _template } from 'vue';
const t0 = _template("<div>", 3)
const t0 = _template("<div>", 2)

export function render(_ctx) {
const n0 = _createSlot("foo", { foo: () => (_ctx.bar) }, () => {
Expand Down Expand Up @@ -157,7 +157,7 @@ exports[`compiler: transform <slot> outlets > root dynamic component fallback 1`

export function render(_ctx) {
const n0 = _createSlot("default", null, () => {
const n2 = _createDynamicComponent(() => (_ctx.view), null, null, 1 /* SINGLE_ROOT */)
const n2 = _createDynamicComponent(() => (_ctx.view))
return n2
})
return n0
Expand All @@ -166,7 +166,7 @@ export function render(_ctx) {

exports[`compiler: transform <slot> outlets > root v-if fallback 1`] = `
"import { createIf as _createIf, createSlot as _createSlot, template as _template } from 'vue';
const t0 = _template("<span>", 3)
const t0 = _template("<span>", 2)

export function render(_ctx) {
const n0 = _createSlot("default", null, () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/compiler-vapor/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
import {
isConstantBinding,
isConstantExpression,
isKeepAliveTag,
isTransitionNode,
} from './utils'
import { newBlock, newDynamic } from './transforms/utils'
Expand Down Expand Up @@ -431,7 +432,8 @@ export class TransformContext<T extends AllNode = AllNode> {
return (
this.node.type === NodeTypes.ELEMENT &&
(this.node.tagType === ElementTypes.TEMPLATE ||
isTransitionNode(this.node)) &&
isTransitionNode(this.node) ||
isKeepAliveTag(this.node.tag)) &&
!!this.parent &&
this.isSingleRoot
)
Expand Down
4 changes: 4 additions & 0 deletions packages/compiler-vapor/src/transforms/transformSlotOutlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ function createFallback(
return []
}

// the outlet is a fragment boundary: the component root must not propagate
// into the fallback through the transparent `<template>` wrapper below
context.isSingleRoot = false

context.node = node = extend({}, node, {
type: NodeTypes.ELEMENT,
tag: 'template',
Expand Down
21 changes: 20 additions & 1 deletion packages/runtime-vapor/__tests__/componentAttrs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
setStyle,
template,
} from '../src'
import { compile, makeRender } from './_utils'
import { compile, makeRender, renderParity } from './_utils'
import {
VaporBlockShape,
VaporDynamicComponentFlags,
Expand Down Expand Up @@ -2323,4 +2323,23 @@ describe('attribute fallthrough', () => {
const { host } = define(Parent).render()
expect(host.innerHTML).toBe('<div>child</div>')
})

test('should not pass fallthrough attrs to the fallback of a slot outlet root', async () => {
const cls: Record<string, string> = {}
const { vdom, vapor } = await renderParity(
{
A: `<template><div id="a">A</div></template>`,
Child: `<template><slot><components.A /></slot></template>`,
App: `<template><components.Child class="outer" /></template>`,
},
() => ref(null),
(_data, root, mode) => {
cls[mode] = root.querySelector('#a')!.className
},
)
expect(cls.vdom).toBe('')
expect(cls.vapor).toBe(cls.vdom)
expect(vapor.text).toBe(vdom.text)
expect('Extraneous non-props attributes (class)').toHaveBeenWarned()
})
})
179 changes: 178 additions & 1 deletion packages/runtime-vapor/__tests__/components/KeepAlive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
type VaporComponentInstance,
currentInstance,
} from '../../src/component'
import { compile, ifFlags, makeRender } from '../_utils'
import { compile, ifFlags, makeRender, renderParity } from '../_utils'
import { VaporKeepAlive } from '../../src/components/KeepAlive'
import {
child,
Expand Down Expand Up @@ -4943,4 +4943,181 @@ describe('VaporKeepAlive', () => {
expect(disposed.mock.calls.sort()).toEqual([['A'], ['B']])
},
)

test('should inherit fallthrough attrs through a KeepAlive root', async () => {
const data = ref({ ok: true, title: 'one' })
const A = compile(`<template><div>A</div></template>`, data)
const B = compile(`<template><p>B</p></template>`, data)
const Child = compile(
`<script setup vapor>
const data = _data
const A = _components.A
const B = _components.B
</script>
<template>
<KeepAlive>
<A v-if="data.ok" />
<B v-else />
</KeepAlive>
</template>`,
data,
{ A, B },
)
const App = compile(
`<template><components.Child class="cls" :title="data.title" /></template>`,
data,
{ Child },
)
const { host } = define(App).render()

const a = host.querySelector('div')!
expect(a.className).toBe('cls')
expect(a.getAttribute('title')).toBe('one')

data.value.title = 'two'
await nextTick()
expect(a.getAttribute('title')).toBe('two')

// the other branch is a root too, and it is cached on the way back
data.value.ok = false
await nextTick()
const b = host.querySelector('p')!
expect(b.className).toBe('cls')
expect(b.getAttribute('title')).toBe('two')

data.value.ok = true
await nextTick()
expect(host.querySelector('div')!.getAttribute('title')).toBe('two')
})

test('should merge fallthrough class with the class of a KeepAlive root', async () => {
const data = ref({ outer: 'outer', inner: 'inner' })
const A = compile(
`<template><div class="box" :class="data.inner">A</div></template>`,
data,
)
const Child = compile(
`<script setup vapor>
const A = _components.A
</script>
<template><KeepAlive><A /></KeepAlive></template>`,
data,
{ A },
)
const App = compile(
`<template><components.Child :class="data.outer" /></template>`,
data,
{ Child },
)
const { host } = define(App).render()
const el = host.querySelector('div')!

expect([...el.classList]).toEqual(
expect.arrayContaining(['outer', 'box', 'inner']),
)
expect(el.classList).toHaveLength(3)

data.value.inner = 'inner-next'
data.value.outer = 'outer-next'
await nextTick()
expect([...el.classList]).toEqual(
expect.arrayContaining(['outer-next', 'box', 'inner-next']),
)
expect(el.classList).toHaveLength(3)
})

test('should inherit fallthrough attrs through a KeepAlive root with a dynamic component', async () => {
const out: Record<string, string[]> = {}
await renderParity(
{
A: `<template><div id="a">A</div></template>`,
B: `<template><p id="b">B</p></template>`,
Child: `<template><KeepAlive><component :is="data.view === 'A' ? components.A : components.B" /></KeepAlive></template>`,
App: `<template><components.Child class="outer" :title="data.title" /></template>`,
},
() => ref({ view: 'A', title: 'one' }),
async (data, root, mode) => {
const snap = () => root.querySelector('#a, #b')!.outerHTML
const seen = [snap()]
data.value.view = 'B'
await nextTick()
seen.push(snap())
data.value.title = 'two'
await nextTick()
seen.push(snap())
// A comes back from the cache with the attrs updated meanwhile
data.value.view = 'A'
await nextTick()
seen.push(snap())
out[mode] = seen
},
)
expect(out.vdom).toEqual([
'<div id="a" class="outer" title="one">A</div>',
'<p id="b" class="outer" title="one">B</p>',
'<p id="b" class="outer" title="two">B</p>',
'<div id="a" class="outer" title="two">A</div>',
])
expect(out.vapor).toEqual(out.vdom)
})

test('should pass the attrs of the KeepAlive itself to its child', async () => {
const out: Record<string, string> = {}
await renderParity(
{
A: `<template><div id="a">A</div></template>`,
Child: `<template><KeepAlive class="ka" data-x="1"><components.A /></KeepAlive></template>`,
App: `<template><components.Child /></template>`,
},
() => ref(null),
(_data, root, mode) => {
out[mode] = root.querySelector('#a')!.outerHTML
},
)
expect(out.vdom).toBe('<div id="a" class="ka" data-x="1">A</div>')
expect(out.vapor).toBe(out.vdom)
})

test('should not pass fallthrough attrs to the fallback of a KeepAlive slot', async () => {
const cls: Record<string, string> = {}
const { vdom, vapor } = await renderParity(
{
A: `<template><div id="a">A</div></template>`,
Child: `<template><KeepAlive><slot><components.A /></slot></KeepAlive></template>`,
App: `<template><components.Child class="outer" /></template>`,
},
() => ref(null),
(_data, root, mode) => {
cls[mode] = root.querySelector('#a')!.className
},
)
expect(cls.vdom).toBe('')
expect(cls.vapor).toBe(cls.vdom)
expect(vapor.text).toBe(vdom.text)
expect('Extraneous non-props attributes (class)').toHaveBeenWarned()
})

test('should render provided content of a KeepAlive slot as vdom does', async () => {
const cls: Record<string, string> = {}
const usedFallback: Record<string, boolean> = {}
const { vdom, vapor } = await renderParity(
{
A: `<template><div id="a">A</div></template>`,
B: `<template><span id="b">B</span></template>`,
Child: `<template><KeepAlive><slot><components.A /></slot></KeepAlive></template>`,
App: `<template><components.Child class="outer"><components.B /></components.Child></template>`,
},
() => ref(null),
(_data, root, mode) => {
cls[mode] = root.querySelector('#b')!.className
usedFallback[mode] = !!root.querySelector('#a')
},
)
expect(usedFallback.vdom).toBe(false)
expect(usedFallback.vapor).toBe(usedFallback.vdom)
expect(cls.vdom).toBe('')
expect(cls.vapor).toBe(cls.vdom)
expect(vapor.text).toBe(vdom.text)
expect('Extraneous non-props attributes (class)').toHaveBeenWarned()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,8 @@ describe('Transition', () => {
expect(host.querySelector('#content')).toBeNull()
expect(onEnter).not.toHaveBeenCalled()
expect(onLeave).not.toHaveBeenCalled()
// `visible` is an attr on a slot outlet root: vdom warns too
expect('Extraneous non-props attributes (visible)').toHaveBeenWarned()
})

test('preserves multi-element vdom slot content nested under transition root', () => {
Expand Down
25 changes: 25 additions & 0 deletions packages/runtime-vapor/__tests__/hydration/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,31 @@ describe('Vapor Mode hydration', () => {
)
})

test('fallthrough attrs through a KeepAlive root', async () => {
const data = ref({ ok: true, title: 'one' })
const { container, html } = await testHydration(
`<template><components.Child class="outer" :title="data.title" /></template>`,
{
A: `<template><div class="box">A</div></template>`,
Child: `<template><KeepAlive><components.A v-if="data.ok" /></KeepAlive></template>`,
},
data,
)
expect(html).toBe('<div class="box outer" title="one">A</div>')
const el = () => container.querySelector('div')!
expect(el().outerHTML).toBe(html)

data.value.title = 'two'
await nextTick()
expect(el().getAttribute('title')).toBe('two')

data.value.ok = false
await nextTick()
data.value.ok = true
await nextTick()
expect(el().outerHTML).toBe('<div class="box outer" title="two">A</div>')
})

test('v-if KeepAlive with dynamic component should preserve cached branches', async () => {
const data = ref({
current: 'CompA',
Expand Down
Loading
Loading