fix: ensure capture events don't call delegated events (#10831)

fixes #10821
pull/10834/head
Simon H 1 year ago committed by GitHub
parent 9ff8029fb2
commit f8c85d525d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: ensure capture events don't call delegated events

@ -17,7 +17,10 @@ export function event(event_name, dom, handler, capture, passive) {
* @this {EventTarget}
*/
function target_handler(/** @type {Event} */ event) {
handle_event_propagation(dom, event);
if (!capture) {
// Only call in the bubble phase, else delegated events would be called before the capturing events
handle_event_propagation(dom, event);
}
if (!event.cancelBubble) {
return handler.call(this, event);
}

@ -0,0 +1,16 @@
import { test } from '../../test';
import { log } from './log.js';
export default test({
before_test() {
log.length = 0;
},
async test({ assert, target }) {
const btn = target.querySelector('button');
btn?.click();
await Promise.resolve();
assert.deepEqual(log, ['div onclickcapture', 'button onclick']);
}
});

@ -0,0 +1,7 @@
<script>
import { log } from "./log";
</script>
<div onclickcapture={() => log.push('div onclickcapture')}>
<button onclick={() => log.push('button onclick')}>main</button>
</div>
Loading…
Cancel
Save