Skip to content

Commit

Permalink
fix(v-on): capitalize dynamic event names
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 13, 2020
1 parent 576344d commit 9152a89
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
2 changes: 2 additions & 0 deletions packages/compiler-core/src/runtimeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const TO_DISPLAY_STRING = Symbol(__DEV__ ? `toDisplayString` : ``)
export const MERGE_PROPS = Symbol(__DEV__ ? `mergeProps` : ``)
export const TO_HANDLERS = Symbol(__DEV__ ? `toHandlers` : ``)
export const CAMELIZE = Symbol(__DEV__ ? `camelize` : ``)
export const CAPITALIZE = Symbol(__DEV__ ? `capitalize` : ``)
export const SET_BLOCK_TRACKING = Symbol(__DEV__ ? `setBlockTracking` : ``)
export const PUSH_SCOPE_ID = Symbol(__DEV__ ? `pushScopeId` : ``)
export const POP_SCOPE_ID = Symbol(__DEV__ ? `popScopeId` : ``)
Expand Down Expand Up @@ -54,6 +55,7 @@ export const helperNameMap: any = {
[MERGE_PROPS]: `mergeProps`,
[TO_HANDLERS]: `toHandlers`,
[CAMELIZE]: `camelize`,
[CAPITALIZE]: `capitalize`,
[SET_BLOCK_TRACKING]: `setBlockTracking`,
[PUSH_SCOPE_ID]: `pushScopeId`,
[POP_SCOPE_ID]: `popScopeId`,
Expand Down
9 changes: 7 additions & 2 deletions packages/compiler-core/src/transforms/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createCompilerError, ErrorCodes } from '../errors'
import { processExpression } from './transformExpression'
import { validateBrowserExpression } from '../validateExpression'
import { isMemberExpression, hasScopeRef } from '../utils'
import { CAPITALIZE } from '../runtimeHelpers'

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

Expand Down Expand Up @@ -47,12 +48,16 @@ export const transformOn: DirectiveTransform = (
: capitalize(rawName)
eventName = createSimpleExpression(`on${normalizedName}`, true, arg.loc)
} else {
eventName = createCompoundExpression([`"on" + (`, arg, `)`])
eventName = createCompoundExpression([
`"on" + ${context.helperString(CAPITALIZE)}(`,
arg,
`)`
])
}
} else {
// already a compound expression.
eventName = arg
eventName.children.unshift(`"on" + (`)
eventName.children.unshift(`"on" + ${context.helperString(CAPITALIZE)}(`)
eventName.children.push(`)`)
}

Expand Down
14 changes: 1 addition & 13 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,19 +225,7 @@ export {
createCommentVNode,
createStaticVNode
} from './vnode'

// a bit of ceremony to mark these internal only here because we need to include
// them in @vue/shared's typings
import { toDisplayString, camelize } from '@vue/shared'
/**
* @private
*/
const _toDisplayString = toDisplayString
/**
* @private
*/
const _camelize = camelize
export { _toDisplayString as toDisplayString, _camelize as camelize }
export { toDisplayString, camelize, capitalize } from '@vue/shared'

// For test-utils
export { transformVNodeArgs } from './vnode'
Expand Down
9 changes: 9 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,28 @@ const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
}

const camelizeRE = /-(\w)/g
/**
* @private
*/
export const camelize = cacheStringFunction(
(str: string): string => {
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''))
}
)

const hyphenateRE = /\B([A-Z])/g
/**
* @private
*/
export const hyphenate = cacheStringFunction(
(str: string): string => {
return str.replace(hyphenateRE, '-$1').toLowerCase()
}
)

/**
* @private
*/
export const capitalize = cacheStringFunction(
(str: string): string => {
return str.charAt(0).toUpperCase() + str.slice(1)
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/src/toDisplayString.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { isArray, isObject, isPlainObject } from './index'

// For converting {{ interpolation }} values to displayed strings.
/**
* For converting {{ interpolation }} values to displayed strings.
* @private
*/
export const toDisplayString = (val: unknown): string => {
return val == null
? ''
Expand Down

0 comments on commit 9152a89

Please sign in to comment.