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
46 changes: 45 additions & 1 deletion packages/runtime-vapor/__tests__/componentAttrs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3370,7 +3370,7 @@ describe('attribute fallthrough', () => {
'constructor',
'value',
]) {
expect(attrs[key]).toBeUndefined()
expect(attrs[key]).toBe((Object.prototype as any)[key])
expect(key in attrs).toBe(false)
}
expect(Object.keys(attrs)).toEqual(['title'])
Expand Down Expand Up @@ -3509,3 +3509,47 @@ describe('checkbox model value bindings', () => {
)
})
})

test('attrs object supports hasOwnProperty like vdom', async () => {
const { vdom, vapor } = await renderParity(
{
Comp: `<script setup>
import { useAttrs } from 'vue'
const attrs = useAttrs()
</script>
<template><p>{{ attrs.hasOwnProperty('id') }}-{{ $attrs.hasOwnProperty('title') }}-{{ attrs.hasOwnProperty('missing') }}</p></template>`,
App: `<template><components.Comp v-bind="data" /></template>`,
},
() => ref<any>({ id: 'a', title: 't' }),
async (data, root) => {
expect(root.textContent).toBe('true-true-false')
data.value = { id: 'a' }
},
)
expect(vdom.text).toBe('true-false-false')
expect(vapor.after).toBe(vdom.after)
})

test('attrs with an undefined value shadow Object.prototype until removed', async () => {
const { vdom, vapor } = await renderParity(
{
Comp: `<script setup>
import { useAttrs } from 'vue'
defineOptions({ inheritAttrs: false })
const attrs = useAttrs()
</script>
<template><p>{{ attrs.toString === undefined }}-{{ attrs.toString === Object.prototype.toString }}</p></template>`,
App: `<template><components.Comp v-bind="data" /></template>`,
},
() => ref<any>({}),
async (data, root) => {
expect(root.textContent).toBe('false-true')
data.value = { toString: undefined }
await nextTick()
expect(root.textContent).toBe('true-false')
data.value = {}
},
)
expect(vdom.text).toBe('false-true')
expect(vapor.after).toBe(vdom.after)
})
16 changes: 16 additions & 0 deletions packages/runtime-vapor/__tests__/componentProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2779,3 +2779,19 @@ describe('component: fallthrough props parity', () => {
},
)
})

test('props object supports hasOwnProperty like vdom', async () => {
const { vdom, vapor } = await renderParity(
{
Comp: `<script setup>
const props = defineProps(['foo', 'bar', 'constructor'])
</script>
<template><p>{{ props.hasOwnProperty('foo') }}-{{ props.hasOwnProperty('bar') }}-{{ props.hasOwnProperty('baz') }}-{{ props.hasOwnProperty('constructor') }}-{{ props.constructor === undefined }}</p></template>`,
App: `<template><components.Comp foo="1" baz="2" :constructor="undefined" /></template>`,
},
() => ref(null),
() => {},
)
expect(vdom.text).toBe('true-true-false-true-true')
expect(vapor.after).toBe(vdom.after)
})
32 changes: 32 additions & 0 deletions packages/runtime-vapor/__tests__/componentSlots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8384,3 +8384,35 @@ test('caches native dynamic slot descriptors across reads', async () => {
app.unmount()
}
})

test.each(['id="a"', 'v-bind="data"'])(
'scoped slot props support hasOwnProperty like vdom (%s)',
async binding => {
const { vdom, vapor } = await renderParity(
{
Comp: `<template><slot ${binding} /></template>`,
App: `<template><components.Comp v-slot="props"><p>{{ props.hasOwnProperty('id') }}-{{ props.hasOwnProperty('missing') }}-{{ props.hasOwnProperty('toString') }}-{{ Object.keys(props).join(',') }}</p></components.Comp></template>`,
},
() => ref({ id: 'a' }),
() => {},
)
expect(vdom.text).toBe('true-false-false-id')
expect(vapor.text).toBe(vdom.text)
},
)

test('scoped slot props with an undefined value shadow Object.prototype', async () => {
const { vdom, vapor } = await renderParity(
{
Comp: `<template><slot v-bind="data" /></template>`,
App: `<template><components.Comp v-slot="props"><p>{{ props.toString === undefined }}-{{ props.toString === Object.prototype.toString }}</p></components.Comp></template>`,
},
() => ref<any>({ toString: undefined }),
(data, root) => {
expect(root.textContent).toBe('true-false')
data.value = {}
},
)
expect(vdom.text).toBe('false-true')
expect(vapor.text).toBe(vdom.text)
})
71 changes: 71 additions & 0 deletions packages/runtime-vapor/__tests__/vdomInterop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14153,3 +14153,74 @@ describe('vdom component template refs', () => {
expect(seen.vapor).toEqual(seen.vdom)
})
})

test.each([true, false])(
'props and attrs support hasOwnProperty (parent vapor: %s)',
async vapor => {
const data = ref<any>({ id: 'a' })
const Comp = compile(
`<script setup>
import { useAttrs } from 'vue'
defineOptions({ inheritAttrs: false })
const props = defineProps(['foo', 'bar'])
const attrs = useAttrs()
</script>
<template><p>{{ props.hasOwnProperty('foo') }}-{{ props.hasOwnProperty('bar') }}-{{ attrs.hasOwnProperty('id') }}-{{ attrs.hasOwnProperty('constructor') }}</p></template>`,
data,
{},
{ vapor: !vapor },
)
const App = compile(
`<script setup>
const data = _data
const components = _components
</script>
<template><components.Comp foo="1" v-bind="data" /></template>`,
data,
{ Comp },
{ vapor },
)
const { host, app } = define(App).render()
try {
expect(host.textContent).toBe('true-true-true-false')
data.value = {}
await nextTick()
expect(host.textContent).toBe('true-true-false-false')
} finally {
app.unmount()
}
},
)

test.each([true, false])(
'scoped slot props support hasOwnProperty (parent vapor: %s)',
async vapor => {
const data = ref<any>({ id: 'a' })
const Comp = compile(
`<script setup>const data = _data</script>
<template><slot v-bind="data" /></template>`,
data,
{},
{ vapor: !vapor },
)
const App = compile(
`<script setup>const components = _components</script>
<template><components.Comp v-slot="props"><p>{{ props.hasOwnProperty('id') }}-{{ props.hasOwnProperty('toString') }}-{{ props.toString === undefined }}</p></components.Comp></template>`,
data,
{ Comp },
{ vapor },
)
const { host, app } = define(App).render()
try {
expect(host.textContent).toBe('true-false-false')
data.value = { id: 'a', toString: undefined }
await nextTick()
expect(host.textContent).toBe('true-true-true')
data.value = {}
await nextTick()
expect(host.textContent).toBe('false-false-false')
} finally {
app.unmount()
}
},
)
7 changes: 6 additions & 1 deletion packages/runtime-vapor/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ export function getPropsProxyHandlers(
trackPropsValue(instance, key)
return instance.propsValues[key]
}
// fall back to Object.prototype so `props.hasOwnProperty()` works like vdom
return (Object.prototype as any)[key]
}

const propsHandlers = propsOptions
Expand Down Expand Up @@ -273,8 +275,10 @@ export function getPropsProxyHandlers(
const getAttr = (instance: VaporComponentInstance, key: string | symbol) => {
if (isAttr(key)) {
trackPropsValue(instance, key)
return instance.propsValues[key]
if (hasOwn(instance.propsValues, key)) return instance.propsValues[key]
}
// fall back to Object.prototype so `attrs.hasOwnProperty()` works like vdom
return (Object.prototype as any)[key]
}
const hasAttr = (instance: VaporComponentInstance, key: string | symbol) => {
if (!isAttr(key)) return false
Expand Down Expand Up @@ -347,6 +351,7 @@ export function getAttrFromRawProps(rawProps: RawProps, key: string): unknown {
merged.reverse()
return isEvent ? merged.reduce(mergeEventHandlers) : merged
}
return (Object.prototype as any)[key]
}

function mergeEventHandlers(existing: unknown, incoming: unknown): unknown {
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-vapor/src/vdomInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import {
ShapeFlags,
VaporSlotFlags,
extend,
hasOwn,
isArray,
isForwardedSlot,
isFunction,
Expand Down Expand Up @@ -1017,7 +1018,7 @@ const vaporSlotPropsProxyHandler: ProxyHandler<
return Reflect.ownKeys(target.value)
},
getOwnPropertyDescriptor(target, key: any) {
if (key in target.value) {
if (hasOwn(target.value, key)) {
return {
enumerable: true,
configurable: true,
Expand Down
Loading