Skip to content

Commit

Permalink
fix(runtime-dom): invoker value is actually unknown at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Aug 10, 2023
1 parent 3be4e3c commit 58491dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
7 changes: 7 additions & 0 deletions packages/runtime-dom/__tests__/patchEvents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,11 @@ describe(`runtime-dom: events patching`, () => {
testElement.dispatchEvent(new CustomEvent('foobar'))
expect(fn2).toHaveBeenCalledTimes(1)
})

it('handles an unknown type', () => {
const el = document.createElement('div')
patchProp(el, 'onClick', null, 'test')
el.dispatchEvent(new Event('click'))
expect('[Vue warn]: Wrong type passed to the event invoker, did you maybe forget @ or : in front of your prop? Received test').toHaveBeenWarned()
})
})
21 changes: 16 additions & 5 deletions packages/runtime-dom/src/modules/events.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { hyphenate, isArray } from '@vue/shared'
import { hyphenate, isArray, isString, isFunction } from '@vue/shared'
import {
ErrorCodes,
ComponentInternalInstance,
callWithAsyncErrorHandling
callWithAsyncErrorHandling, warn
} from '@vue/runtime-core'

interface Invoker extends EventListener {
Expand Down Expand Up @@ -81,7 +81,7 @@ const getNow = () =>
cachedNow || (p.then(() => (cachedNow = 0)), (cachedNow = Date.now()))

function createInvoker(
initialValue: EventValue,
initialValue: EventValue | unknown,
instance: ComponentInternalInstance | null
) {
const invoker: Invoker = (e: Event & { _vts?: number }) => {
Expand Down Expand Up @@ -109,11 +109,22 @@ function createInvoker(
[e]
)
}
invoker.value = initialValue
invoker.value = sanitizeEventValue(initialValue)
invoker.attached = getNow()
return invoker
}

function sanitizeEventValue(value: unknown): EventValue {
if (isFunction(value) || isArray(value)) {
return value as EventValue
}

if (__DEV__) {
warn('Wrong type passed to the event invoker, did you maybe forget @ or : in front of your prop? Received ' + (isString(value) ? value : typeof value))
}
return () => {}
}

function patchStopImmediatePropagation(
e: Event,
value: EventValue
Expand All @@ -124,7 +135,7 @@ function patchStopImmediatePropagation(
originalStop.call(e)
;(e as any)._stopped = true
}
return value.map(fn => (e: Event) => !(e as any)._stopped && fn && fn(e))
return (value as Function[]).map(fn => (e: Event) => !(e as any)._stopped && fn && fn(e))
} else {
return value
}
Expand Down

0 comments on commit 58491dc

Please sign in to comment.