-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: don't delegate events on custom elements, solve edge case stopPr…
…opagation issue - don't delegate events on custom elements - still invoke listener for cancelled event on the element where it was cancelled: when you do `stopPropagation`, `event.cancelBubble` becomes `true`. We can't use this as an indicator to not invoke a listener directly, because the listner could be on the element where propagation was cancelled, i.e. it should still run for that listener. Instead, adjust the event propagation algorithm to detect when a delegated event listener caused the event to be cancelled fixes #14704
- Loading branch information
1 parent
c4e9faa
commit c05abb5
Showing
7 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: don't delegate events on custom elements |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: still invoke listener for cancelled event on the element where it was cancelled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
.../svelte/tests/runtime-runes/samples/event-attribute-delegation-custom-elements/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
mode: ['client'], | ||
async test({ assert, target, logs }) { | ||
const [btn1, btn2] = [...target.querySelectorAll('custom-element')].map((c) => | ||
c.shadowRoot?.querySelector('button') | ||
); | ||
|
||
btn1?.click(); | ||
await Promise.resolve(); | ||
assert.deepEqual(logs, ['reached shadow root1']); | ||
|
||
btn2?.click(); | ||
await Promise.resolve(); | ||
assert.deepEqual(logs, ['reached shadow root1', 'reached shadow root2']); | ||
} | ||
}); |
19 changes: 19 additions & 0 deletions
19
...svelte/tests/runtime-runes/samples/event-attribute-delegation-custom-elements/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script> | ||
class CustomElement extends HTMLElement { | ||
constructor() { | ||
super(); | ||
this.attachShadow({ mode: 'open' }); | ||
this.shadowRoot.innerHTML = '<button>click me</button>'; | ||
// Looks weird, but some custom element implementations actually do this | ||
// to prevent unwanted side upwards event propagation | ||
this.addEventListener('click', (e) => e.stopPropagation()); | ||
} | ||
} | ||
customElements.define('custom-element', CustomElement); | ||
</script> | ||
|
||
<div onclick={() => console.log('bubbled beyond shadow root')}> | ||
<custom-element onclick={() => console.log('reached shadow root1')}></custom-element> | ||
<custom-element {...{onclick:() => console.log('reached shadow root2')}}></custom-element> | ||
</div> |