fix(custom-element): fix event listeners with capital letter event names on custom elements

close https://github.com/vuejs/docs/issues/1708
close https://github.com/vuejs/docs/pull/1890
This commit is contained in:
Evan You
2022-08-30 14:07:35 +08:00
parent 9f8f07ed38
commit 0739f8909a
7 changed files with 99 additions and 12 deletions

View File

@@ -314,6 +314,37 @@ describe('compiler: element transform', () => {
)
expect(root.helpers).toContain(MERGE_PROPS)
expect(node.props).toMatchObject({
type: NodeTypes.JS_CALL_EXPRESSION,
callee: MERGE_PROPS,
arguments: [
createObjectMatcher({
id: 'foo'
}),
{
type: NodeTypes.JS_CALL_EXPRESSION,
callee: TO_HANDLERS,
arguments: [
{
type: NodeTypes.SIMPLE_EXPRESSION,
content: `obj`
},
`true`
]
},
createObjectMatcher({
class: 'bar'
})
]
})
})
test('v-on="obj" on component', () => {
const { root, node } = parseWithElementTransform(
`<Foo id="foo" v-on="obj" class="bar" />`
)
expect(root.helpers).toContain(MERGE_PROPS)
expect(node.props).toMatchObject({
type: NodeTypes.JS_CALL_EXPRESSION,
callee: MERGE_PROPS,
@@ -358,7 +389,8 @@ describe('compiler: element transform', () => {
{
type: NodeTypes.SIMPLE_EXPRESSION,
content: `handlers`
}
},
`true`
]
},
{

View File

@@ -647,7 +647,7 @@ export function buildProps(
type: NodeTypes.JS_CALL_EXPRESSION,
loc,
callee: context.helper(TO_HANDLERS),
arguments: [exp]
arguments: isComponent ? [exp] : [exp, `true`]
})
}
} else {

View File

@@ -47,12 +47,17 @@ export const transformOn: DirectiveTransform = (
if (rawName.startsWith('vue:')) {
rawName = `vnode-${rawName.slice(4)}`
}
// for all event listeners, auto convert it to camelCase. See issue #2249
eventName = createSimpleExpression(
toHandlerKey(camelize(rawName)),
true,
arg.loc
)
const eventString =
node.tagType === ElementTypes.COMPONENT ||
rawName.startsWith('vnode') ||
!/[A-Z]/.test(rawName)
? // for component and vnode lifecycle event listeners, auto convert
// it to camelCase. See issue #2249
toHandlerKey(camelize(rawName))
// preserve case for plain element listeners that have uppercase
// letters, as these may be custom elements' custom events
: `on:${rawName}`
eventName = createSimpleExpression(eventString, true, arg.loc)
} else {
// #2388
eventName = createCompoundExpression([