Skip to content

Event bubbling order not respected when listeners are added both with addEventListener and attributes #16322

Closed as not planned
@kepzAT

Description

@kepzAT

Describe the bug

I have found a bug where a button whose listener is assigned with onpointerdown attribute is getting the event after the parent div, which has a listener assigned via addEventListener in onMount.

There is no event capturing, so bubbling should work in the correct direction. (button first, div second).

In this example, DIV listener callback always gets triggered first:

Image

Reproduction

<script>
	import {onMount} from 'svelte'
	
	let elementNames = $state([])
	
	const storeElementName = (e) => {
	        elementNames.push(e.currentTarget.nodeName)
	}

	let div
	onMount(() => {
		div.addEventListener('pointerdown', storeElementName)
	})
</script>
<p>Press the button:</p>
<div bind:this={div}>
	<button onpointerdown={storeElementName}>Button should receive the event first.</button>
	Div should receive the event second.
</div>
<p>Order of listeners triggered: <span>{elementNames.join(', ')}</span></p>
<button onclick={() => {elementNames = []}}>Clear</button>
<style>
	div {
		border: 1px solid red;
		padding: 20px;
	}

	span {
		color: orange;
	}
</style>

Playground:
https://svelte.dev/playground/5737a1699e444157afec29b8ed005c7d?version=latest

Logs

System Info

System:
    OS: Linux 6.6 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H
    Memory: 9.34 GB / 15.48 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 22.2.0 - ~/.nvm/versions/node/v22.2.0/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 10.7.0 - ~/.nvm/versions/node/v22.2.0/bin/npm
    pnpm: 10.10.0 - /usr/bin/pnpm
  npmPackages:
    svelte: ^5.34.7 => 5.34.7

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy