Explorar o código

fix(runtime-dom): handle null/undefined handler in withModifiers (#14362)

close #14361
edison hai 2 meses
pai
achega
261de547cd

+ 12 - 0
packages/runtime-dom/__tests__/directives/vOn.spec.ts

@@ -163,4 +163,16 @@ describe('runtime-dom: v-on directive', () => {
     triggerEvent(el2, 'click', e => (e.shiftKey = true))
     expect(fn).toBeCalledTimes(2)
   })
+
+  it('withModifiers should handle null or undefined handler', () => {
+    expect(() => {
+      const handler1 = withModifiers(null as any, ['ctrl'])
+      expect(handler1).toBe(null)
+    }).not.toThrow()
+
+    expect(() => {
+      const handler2 = withModifiers(undefined as any, ['shift'])
+      expect(handler2).toBe(undefined)
+    }).not.toThrow()
+  })
 })

+ 1 - 0
packages/runtime-dom/src/directives/vOn.ts

@@ -55,6 +55,7 @@ export const withModifiers = <
   fn: T & { _withMods?: { [key: string]: T } },
   modifiers: VOnModifiers[],
 ): T => {
+  if (!fn) return fn
   const cache = fn._withMods || (fn._withMods = {})
   const cacheKey = modifiers.join('.')
   return (