mirror of https://github.com/sveltejs/svelte
fix: catch delegated events from elements moved outside the container (#10060)
fixes #9777pull/10052/head
parent
8a8505928e
commit
15d6308d60
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: handle delegated events of elements moved outside the container
|
@ -0,0 +1,23 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
// Tests that event delegation still works when the element with the event listener is moved outside the container
|
||||||
|
export default test({
|
||||||
|
async test({ assert, target }) {
|
||||||
|
const btn1 = target.parentElement?.querySelector('button');
|
||||||
|
const btn2 = target.querySelector('button');
|
||||||
|
|
||||||
|
btn1?.click();
|
||||||
|
await Promise.resolve();
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.parentElement?.innerHTML ?? '',
|
||||||
|
'<main><div><button>clicks: 1</button></div></main><button>clicks: 1</button>'
|
||||||
|
);
|
||||||
|
|
||||||
|
btn2?.click();
|
||||||
|
await Promise.resolve();
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.parentElement?.innerHTML ?? '',
|
||||||
|
'<main><div><button>clicks: 2</button></div></main><button>clicks: 2</button>'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,12 @@
|
|||||||
|
<script>
|
||||||
|
let count = $state(0);
|
||||||
|
let el;
|
||||||
|
$effect(() => {
|
||||||
|
document.getElementsByTagName('body')[0].appendChild(el);
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<button bind:this={el} onclick={() => count++}>clicks: {count}</button>
|
||||||
|
<button onclick={() => count++}>clicks: {count}</button>
|
||||||
|
</div>
|
Loading…
Reference in new issue