Skip to content

Commit 4b659e6

Browse files
fix(runtime-dom): preserve option modifier event names (#8338)
close #8334
1 parent 232f402 commit 4b659e6

2 files changed

Lines changed: 106 additions & 8 deletions

File tree

‎packages/runtime-dom/__tests__/patchEvents.spec.ts‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,102 @@ describe(`runtime-dom: events patching`, () => {
216216
expect(fn2).toHaveBeenCalledTimes(1)
217217
})
218218

219+
// #8334
220+
test('should support once passive capture', async () => {
221+
const el = document.createElement('div')
222+
223+
const onOnce = vi.fn()
224+
const onPassive = vi.fn()
225+
const onCapture = vi.fn()
226+
227+
patchProp(el, 'onOnce', null, onOnce)
228+
patchProp(el, 'onPassive', null, onPassive)
229+
patchProp(el, 'onCapture', null, onCapture)
230+
231+
el.dispatchEvent(new Event('once'))
232+
await timeout()
233+
expect(onOnce).toHaveBeenCalledTimes(1)
234+
235+
el.dispatchEvent(new Event('passive'))
236+
await timeout()
237+
expect(onPassive).toHaveBeenCalledTimes(1)
238+
239+
el.dispatchEvent(new Event('capture'))
240+
await timeout()
241+
expect(onCapture).toHaveBeenCalledTimes(1)
242+
})
243+
244+
test('should support once passive capture in web-components', async () => {
245+
class TestCustomElement extends HTMLElement {
246+
constructor() {
247+
super()
248+
}
249+
}
250+
251+
if (!window.customElements.get('hello-world')) {
252+
window.customElements.define('hello-world', TestCustomElement)
253+
}
254+
255+
const customElement = document.createElement('hello-world')
256+
257+
const onOnce = vi.fn()
258+
const onPassive = vi.fn()
259+
const onCapture = vi.fn()
260+
261+
patchProp(customElement, 'onOnce', null, onOnce)
262+
patchProp(customElement, 'onPassive', null, onPassive)
263+
patchProp(customElement, 'onCapture', null, onCapture)
264+
265+
customElement.dispatchEvent(new CustomEvent('once'))
266+
expect(onOnce).toHaveBeenCalledTimes(1)
267+
268+
customElement.dispatchEvent(new CustomEvent('passive'))
269+
expect(onPassive).toHaveBeenCalledTimes(1)
270+
271+
customElement.dispatchEvent(new CustomEvent('capture'))
272+
expect(onCapture).toHaveBeenCalledTimes(1)
273+
})
274+
275+
test('should support once event with the once modifier', async () => {
276+
const el = document.createElement('div')
277+
278+
const onOnce = vi.fn()
279+
280+
patchProp(el, 'onOnceOnce', null, onOnce)
281+
282+
el.dispatchEvent(new Event('once'))
283+
await timeout()
284+
expect(onOnce).toHaveBeenCalledTimes(1)
285+
286+
el.dispatchEvent(new Event('once'))
287+
await timeout()
288+
expect(onOnce).toHaveBeenCalledTimes(1)
289+
})
290+
291+
test('should support preserved-case option modifier event names', async () => {
292+
const el = document.createElement('div')
293+
294+
const onOnce = vi.fn()
295+
const onPassive = vi.fn()
296+
const onCapture = vi.fn()
297+
298+
patchProp(el, 'on:Once', null, onOnce)
299+
patchProp(el, 'on:Passive', null, onPassive)
300+
patchProp(el, 'on:Capture', null, onCapture)
301+
302+
el.dispatchEvent(new Event('Once'))
303+
await timeout()
304+
expect(onOnce).toHaveBeenCalledTimes(1)
305+
306+
el.dispatchEvent(new Event('Passive'))
307+
await timeout()
308+
expect(onPassive).toHaveBeenCalledTimes(1)
309+
310+
el.dispatchEvent(new Event('Capture'))
311+
await timeout()
312+
expect(onCapture).toHaveBeenCalledTimes(1)
313+
})
314+
219315
it('handles an unknown type', () => {
220316
const el = document.createElement('div')
221317
patchProp(el, 'onClick', null, 'test')

‎packages/runtime-dom/src/modules/events.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,19 @@ export function patchEvent(
6767
}
6868
}
6969

70-
const optionsModifierRE = /(?:Once|Passive|Capture)$/
70+
const optionsModifierRE = /(Once|Passive|Capture)$/
71+
const optionsModifierEventRE = /^on:?(?:Once|Passive|Capture)$/
7172

7273
function parseName(name: string): [string, EventListenerOptions | undefined] {
7374
let options: EventListenerOptions | undefined
74-
if (optionsModifierRE.test(name)) {
75-
options = {}
76-
let m
77-
while ((m = name.match(optionsModifierRE))) {
78-
name = name.slice(0, name.length - m[0].length)
79-
;(options as any)[m[0].toLowerCase()] = true
80-
}
75+
let m
76+
while (
77+
(m = name.match(optionsModifierRE)) &&
78+
!optionsModifierEventRE.test(name)
79+
) {
80+
if (!options) options = {}
81+
name = name.slice(0, name.length - m[1].length)
82+
;(options as any)[m[1].toLowerCase()] = true
8183
}
8284
const event = name[2] === ':' ? name.slice(3) : hyphenate(name.slice(2))
8385
return [event, options]

0 commit comments

Comments
 (0)