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 cfb2e28
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 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()
})
})
25 changes: 18 additions & 7 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,23 +109,34 @@ 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
value: EventValue | unknown
): EventValue {
if (isArray(value)) {
const originalStop = e.stopImmediatePropagation
e.stopImmediatePropagation = () => {
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
return sanitizeEventValue(value)
}
}

0 comments on commit cfb2e28

Please sign in to comment.