Skip to content

Commit

Permalink
fix(runtime-dom): support native onxxx handlers
Browse files Browse the repository at this point in the history
close #927
  • Loading branch information
yyx990803 committed Apr 7, 2020
1 parent cb504c2 commit 2302dea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
40 changes: 29 additions & 11 deletions packages/runtime-dom/__tests__/modules/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe(`events`, () => {
const el = document.createElement('div')
const event = new Event('click')
const fn = jest.fn()
patchEvent(el, 'click', null, fn, null)
patchEvent(el, 'onClick', null, fn, null)
el.dispatchEvent(event)
await timeout()
el.dispatchEvent(event)
Expand All @@ -22,9 +22,9 @@ describe(`events`, () => {
const event = new Event('click')
const prevFn = jest.fn()
const nextFn = jest.fn()
patchEvent(el, 'click', null, prevFn, null)
patchEvent(el, 'onClick', null, prevFn, null)
el.dispatchEvent(event)
patchEvent(el, 'click', prevFn, nextFn, null)
patchEvent(el, 'onClick', prevFn, nextFn, null)
await timeout()
el.dispatchEvent(event)
await timeout()
Expand All @@ -39,7 +39,7 @@ describe(`events`, () => {
const event = new Event('click')
const fn1 = jest.fn()
const fn2 = jest.fn()
patchEvent(el, 'click', null, [fn1, fn2], null)
patchEvent(el, 'onClick', null, [fn1, fn2], null)
el.dispatchEvent(event)
await timeout()
expect(fn1).toHaveBeenCalledTimes(1)
Expand All @@ -50,8 +50,8 @@ describe(`events`, () => {
const el = document.createElement('div')
const event = new Event('click')
const fn = jest.fn()
patchEvent(el, 'click', null, fn, null)
patchEvent(el, 'click', fn, null, null)
patchEvent(el, 'onClick', null, fn, null)
patchEvent(el, 'onClick', fn, null, null)
el.dispatchEvent(event)
await timeout()
expect(fn).not.toHaveBeenCalled()
Expand All @@ -67,7 +67,7 @@ describe(`events`, () => {
once: true
}
}
patchEvent(el, 'click', null, nextValue, null)
patchEvent(el, 'onClick', null, nextValue, null)
el.dispatchEvent(event)
await timeout()
el.dispatchEvent(event)
Expand All @@ -86,8 +86,8 @@ describe(`events`, () => {
once: true
}
}
patchEvent(el, 'click', null, prevFn, null)
patchEvent(el, 'click', prevFn, nextValue, null)
patchEvent(el, 'onClick', null, prevFn, null)
patchEvent(el, 'onClick', prevFn, nextValue, null)
el.dispatchEvent(event)
await timeout()
el.dispatchEvent(event)
Expand All @@ -106,12 +106,30 @@ describe(`events`, () => {
once: true
}
}
patchEvent(el, 'click', null, nextValue, null)
patchEvent(el, 'click', nextValue, null, null)
patchEvent(el, 'onClick', null, nextValue, null)
patchEvent(el, 'onClick', nextValue, null, null)
el.dispatchEvent(event)
await timeout()
el.dispatchEvent(event)
await timeout()
expect(fn).not.toHaveBeenCalled()
})

it('should assign native onclick attribute', async () => {
const el = document.createElement('div')
const event = new Event('click')
const fn = ((window as any)._nativeClickSpy = jest.fn())

patchEvent(el, 'onclick', null, '_nativeClickSpy()' as any)
el.dispatchEvent(event)
await timeout()
expect(fn).toHaveBeenCalledTimes(1)

const fn2 = jest.fn()
patchEvent(el, 'onclick', null, fn2)
el.dispatchEvent(event)
await timeout()
expect(fn).toHaveBeenCalledTimes(1)
expect(fn2).toHaveBeenCalledTimes(1)
})
})
15 changes: 13 additions & 2 deletions packages/runtime-dom/src/modules/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EMPTY_OBJ } from '@vue/shared'
import { EMPTY_OBJ, isString } from '@vue/shared'
import {
ComponentInternalInstance,
callWithAsyncErrorHandling
Expand Down Expand Up @@ -66,11 +66,22 @@ export function removeEventListener(

export function patchEvent(
el: Element,
name: string,
rawName: string,
prevValue: EventValueWithOptions | EventValue | null,
nextValue: EventValueWithOptions | EventValue | null,
instance: ComponentInternalInstance | null = null
) {
// support native onxxx handlers
if (rawName in el) {
if (isString(nextValue)) {
el.setAttribute(rawName, nextValue)
} else {
;(el as any)[rawName] = nextValue
}
return
}

const name = rawName.slice(2).toLowerCase()
const prevOptions = prevValue && 'options' in prevValue && prevValue.options
const nextOptions = nextValue && 'options' in nextValue && nextValue.options
const invoker = prevValue && prevValue.invoker
Expand Down
8 changes: 1 addition & 7 deletions packages/runtime-dom/src/patchProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ export const patchProp: RendererOptions<Node, Element>['patchProp'] = (
if (isOn(key)) {
// ignore v-model listeners
if (key.indexOf('onUpdate:') < 0) {
patchEvent(
el,
key.slice(2).toLowerCase(),
prevValue,
nextValue,
parentComponent
)
patchEvent(el, key, prevValue, nextValue, parentComponent)
}
} else if (!isSVG && key in el) {
patchDOMProp(
Expand Down

0 comments on commit 2302dea

Please sign in to comment.