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 @@ -180,6 +180,20 @@ export function render(_ctx) {
}"
`;

exports[`compiler: transform <slot> outlets > slot outlet with number literal props 1`] = `
"import { createSlot as _createSlot } from 'vue';

export function render(_ctx) {
const n0 = _createSlot("default", {
count: 0,
level: 1,
ratio: 1.5,
str: "1"
})
return n0
}"
`;

exports[`compiler: transform <slot> outlets > slot outlet with scopeId and slotted=false should generate noSlotted 1`] = `
"import { createSlot as _createSlot } from 'vue';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ describe('compiler: transform <slot> outlets', () => {
})
})

test('slot outlet with number literal props', () => {
// slot props are passed along as raw values, so number literals must not
// be stringified the way they are for plain element attributes
const { code } = compileWithSlotsOutlet(
`<slot :count="0" :level="1" :ratio="1.5" :str="'1'" />`,
)
expect(code).toContain(`count: 0`)
expect(code).toContain(`level: 1`)
expect(code).toContain(`ratio: 1.5`)
expect(code).toContain(`str: "1"`)
expect(code).toMatchSnapshot()
})

test('statically named slot outlet with v-bind="obj"', () => {
const { ir, code } = compileWithSlotsOutlet(
`<slot name="foo" foo="bar" v-bind="obj" :baz="qux" />`,
Expand Down
8 changes: 6 additions & 2 deletions packages/compiler-vapor/src/transforms/vBind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ export const transformVBind: DirectiveTransform = (dir, node, context) => {
exp = createSimpleExpression('', true, loc)
}

const isComponent = node.tagType === ElementTypes.COMPONENT
exp = resolveExpression(exp, isComponent)
// component and slot outlet props are passed along as raw values instead of
// being stringified into the template, so number literals must keep their type
const excludeNumber =
node.tagType === ElementTypes.COMPONENT ||
node.tagType === ElementTypes.SLOT
exp = resolveExpression(exp, excludeNumber)
arg = resolveExpression(arg)

if (arg.isStatic && isReservedProp(arg.content)) return
Expand Down
13 changes: 13 additions & 0 deletions packages/runtime-vapor/__tests__/componentSlots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,19 @@ describe('component: slots', () => {
expect(vapor.text).toBe(vdom.text)
})

test('should keep number literal slot props as numbers like vdom', async () => {
const { vdom, vapor } = await renderParity(
{
Child: `<template><slot :count="0" :level="1" :ratio="1.5"/></template>`,
App: `<template><components.Child v-slot="{ count, level, ratio }">{{ typeof count }}/{{ count + 1 }}/{{ count === 0 }}/{{ level === 1 }}/{{ ratio }}</components.Child></template>`,
},
() => ref(0),
() => {},
)
expect(vdom.text).toBe('number/1/true/true/1.5')
expect(vapor.text).toBe(vdom.text)
})

describe('slot fallback boundary', () => {
test('slot fragment insert uses active fallback output', () => {
const container = document.createElement('div')
Expand Down
20 changes: 20 additions & 0 deletions packages/runtime-vapor/__tests__/hydration/slots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ describe('Vapor Mode hydration', () => {
)
})

test('slot with number literal props', async () => {
// the server renders the prop as a number, so the client has to as well,
// otherwise hydration mismatches
const { container } = await testHydration(
`<template>
<components.Child v-slot="{ count }"><span>{{ typeof count }}/{{ count + 1 }}</span></components.Child>
</template>`,
{
Child: `<template><slot :count="0"/></template>`,
},
)
expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
`
"
<!--[--><span>number/1</span><!--]-->
"
`,
)
})

test('dynamic slot outlet update preserves slotted scope id', async () => {
const data = ref({ slotName: 'one' })
const childCode = `<template><slot :name="data.slotName" /></template>`
Expand Down
Loading