From 3532b2b0213268a285cacce9b38f806e6af29a61 Mon Sep 17 00:00:00 2001 From: shadowings-zy Date: Sat, 28 Nov 2020 04:24:29 +0800 Subject: [PATCH] fix(runtime-core): fix emit listener check on kebab-case events (#2542) fix #2540 --- .../runtime-core/__tests__/componentEmits.spec.ts | 13 ++++++++++++- packages/runtime-core/src/componentEmits.ts | 7 ++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/runtime-core/__tests__/componentEmits.spec.ts b/packages/runtime-core/__tests__/componentEmits.spec.ts index 53c8d324ea3..2fb1fbb068a 100644 --- a/packages/runtime-core/__tests__/componentEmits.spec.ts +++ b/packages/runtime-core/__tests__/componentEmits.spec.ts @@ -283,12 +283,23 @@ describe('component: emit', () => { }) test('isEmitListener', () => { - const options = { click: null } + const options = { + click: null, + 'test-event': null, + fooBar: null, + FooBaz: null + } expect(isEmitListener(options, 'onClick')).toBe(true) expect(isEmitListener(options, 'onclick')).toBe(false) expect(isEmitListener(options, 'onBlick')).toBe(false) // .once listeners expect(isEmitListener(options, 'onClickOnce')).toBe(true) expect(isEmitListener(options, 'onclickOnce')).toBe(false) + // kebab-case option + expect(isEmitListener(options, 'onTestEvent')).toBe(true) + // camelCase option + expect(isEmitListener(options, 'onFooBar')).toBe(true) + // PascalCase option + expect(isEmitListener(options, 'onFooBaz')).toBe(true) }) }) diff --git a/packages/runtime-core/src/componentEmits.ts b/packages/runtime-core/src/componentEmits.ts index 6b9e505de62..81af34e6708 100644 --- a/packages/runtime-core/src/componentEmits.ts +++ b/packages/runtime-core/src/componentEmits.ts @@ -201,9 +201,10 @@ export function isEmitListener( if (!options || !isOn(key)) { return false } - key = key.replace(/Once$/, '') + key = key.slice(2).replace(/Once$/, '') return ( - hasOwn(options, key[2].toLowerCase() + key.slice(3)) || - hasOwn(options, key.slice(2)) + hasOwn(options, key[0].toLowerCase() + key.slice(1)) || + hasOwn(options, hyphenate(key)) || + hasOwn(options, key) ) }