Skip to content

Commit 9152a89

Browse files
committed
fix(v-on): capitalize dynamic event names
1 parent 576344d commit 9152a89

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

packages/compiler-core/src/runtimeHelpers.ts

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const TO_DISPLAY_STRING = Symbol(__DEV__ ? `toDisplayString` : ``)
2222
export const MERGE_PROPS = Symbol(__DEV__ ? `mergeProps` : ``)
2323
export const TO_HANDLERS = Symbol(__DEV__ ? `toHandlers` : ``)
2424
export const CAMELIZE = Symbol(__DEV__ ? `camelize` : ``)
25+
export const CAPITALIZE = Symbol(__DEV__ ? `capitalize` : ``)
2526
export const SET_BLOCK_TRACKING = Symbol(__DEV__ ? `setBlockTracking` : ``)
2627
export const PUSH_SCOPE_ID = Symbol(__DEV__ ? `pushScopeId` : ``)
2728
export const POP_SCOPE_ID = Symbol(__DEV__ ? `popScopeId` : ``)
@@ -54,6 +55,7 @@ export const helperNameMap: any = {
5455
[MERGE_PROPS]: `mergeProps`,
5556
[TO_HANDLERS]: `toHandlers`,
5657
[CAMELIZE]: `camelize`,
58+
[CAPITALIZE]: `capitalize`,
5759
[SET_BLOCK_TRACKING]: `setBlockTracking`,
5860
[PUSH_SCOPE_ID]: `pushScopeId`,
5961
[POP_SCOPE_ID]: `popScopeId`,

packages/compiler-core/src/transforms/vOn.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { createCompilerError, ErrorCodes } from '../errors'
1414
import { processExpression } from './transformExpression'
1515
import { validateBrowserExpression } from '../validateExpression'
1616
import { isMemberExpression, hasScopeRef } from '../utils'
17+
import { CAPITALIZE } from '../runtimeHelpers'
1718

1819
const fnExpRE = /^([\w$_]+|\([^)]*?\))\s*=>|^function(?:\s+[\w$]+)?\s*\(/
1920

@@ -47,12 +48,16 @@ export const transformOn: DirectiveTransform = (
4748
: capitalize(rawName)
4849
eventName = createSimpleExpression(`on${normalizedName}`, true, arg.loc)
4950
} else {
50-
eventName = createCompoundExpression([`"on" + (`, arg, `)`])
51+
eventName = createCompoundExpression([
52+
`"on" + ${context.helperString(CAPITALIZE)}(`,
53+
arg,
54+
`)`
55+
])
5156
}
5257
} else {
5358
// already a compound expression.
5459
eventName = arg
55-
eventName.children.unshift(`"on" + (`)
60+
eventName.children.unshift(`"on" + ${context.helperString(CAPITALIZE)}(`)
5661
eventName.children.push(`)`)
5762
}
5863

packages/runtime-core/src/index.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,7 @@ export {
225225
createCommentVNode,
226226
createStaticVNode
227227
} from './vnode'
228-
229-
// a bit of ceremony to mark these internal only here because we need to include
230-
// them in @vue/shared's typings
231-
import { toDisplayString, camelize } from '@vue/shared'
232-
/**
233-
* @private
234-
*/
235-
const _toDisplayString = toDisplayString
236-
/**
237-
* @private
238-
*/
239-
const _camelize = camelize
240-
export { _toDisplayString as toDisplayString, _camelize as camelize }
228+
export { toDisplayString, camelize, capitalize } from '@vue/shared'
241229

242230
// For test-utils
243231
export { transformVNodeArgs } from './vnode'

packages/shared/src/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,28 @@ const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
9494
}
9595

9696
const camelizeRE = /-(\w)/g
97+
/**
98+
* @private
99+
*/
97100
export const camelize = cacheStringFunction(
98101
(str: string): string => {
99102
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
100103
}
101104
)
102105

103106
const hyphenateRE = /\B([A-Z])/g
107+
/**
108+
* @private
109+
*/
104110
export const hyphenate = cacheStringFunction(
105111
(str: string): string => {
106112
return str.replace(hyphenateRE, '-$1').toLowerCase()
107113
}
108114
)
109115

116+
/**
117+
* @private
118+
*/
110119
export const capitalize = cacheStringFunction(
111120
(str: string): string => {
112121
return str.charAt(0).toUpperCase() + str.slice(1)

packages/shared/src/toDisplayString.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { isArray, isObject, isPlainObject } from './index'
22

3-
// For converting {{ interpolation }} values to displayed strings.
3+
/**
4+
* For converting {{ interpolation }} values to displayed strings.
5+
* @private
6+
*/
47
export const toDisplayString = (val: unknown): string => {
58
return val == null
69
? ''

0 commit comments

Comments
 (0)