mirror of https://github.com/sveltejs/svelte
Merge 34689e1b66 into 020242d6be
commit
6bfa239914
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: fire delegated event handlers when the element was disabled mid-dispatch
|
||||
@ -0,0 +1,26 @@
|
||||
import { ok, test } from '../../assert';
|
||||
|
||||
export default test({
|
||||
// the test performs a real mouse click so the event is trusted
|
||||
// (`isTrusted === true`), which cannot be faked from inside the page
|
||||
real_click: true,
|
||||
|
||||
async test({ assert, target, window, waitUntil }) {
|
||||
const span = target.querySelector('span');
|
||||
ok(span);
|
||||
const rect = span.getBoundingClientRect();
|
||||
|
||||
// a real user click: the browser flushes effects at the microtask
|
||||
// checkpoint between listeners, so the `{@attach}` handler's state
|
||||
// write disables the button mid-dispatch, before the event reaches
|
||||
// the delegated `onclick` at the root (#18070)
|
||||
await window.__real_click(rect.x + rect.width / 2, rect.y + rect.height / 2);
|
||||
|
||||
await waitUntil(() => target.querySelector('p')?.textContent === 'attach: 1, onclick: 1');
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`<button disabled=""><span>click me</span></button><p>attach: 1, onclick: 1</p>`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,33 @@
|
||||
<script>
|
||||
import { on } from 'svelte/events';
|
||||
|
||||
let attach_count = $state(0);
|
||||
let onclick_count = $state(0);
|
||||
|
||||
// throttle, as in https://github.com/sveltejs/svelte/issues/18070
|
||||
function throttle(node) {
|
||||
let throttled = $state(false);
|
||||
|
||||
const off = on(node, 'click', () => {
|
||||
attach_count += 1;
|
||||
throttled = true;
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (throttled) {
|
||||
node.disabled = true;
|
||||
return () => {
|
||||
node.disabled = false;
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return () => off();
|
||||
}
|
||||
</script>
|
||||
|
||||
<button {@attach throttle} onclick={() => (onclick_count += 1)}>
|
||||
<span>click me</span>
|
||||
</button>
|
||||
|
||||
<p>attach: {attach_count}, onclick: {onclick_count}</p>
|
||||
Loading…
Reference in new issue