Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dispatchEvent fired events don't bubble #1661

Open
raystubbs opened this issue Jan 3, 2025 · 10 comments · Fixed by #1679
Open

dispatchEvent fired events don't bubble #1661

raystubbs opened this issue Jan 3, 2025 · 10 comments · Fixed by #1679
Assignees
Labels
bug Something isn't working

Comments

@raystubbs
Copy link

raystubbs commented Jan 3, 2025

Describe the bug
Events fired with dispatchEvent don't propagate up the DOM.

To Reproduce
Steps to reproduce the behavior:

  1. Create a DOM like <div id="my-div"><button id="my-button"></button></div>
  2. document.querySelector("#my-div").addEventListener("my-event", event => console.log(event))
  3. document.querySelector("#my-button").dispatchEvent(new Event("my-event", {bubbles: true}))

Expected behavior
The event should bubble up to #my-div, and be printed by the listener. But that doesn't happen.

Device:

  • OS: Macbook Pro, M1
  • Version 15.11.7
@raystubbs raystubbs added the bug Something isn't working label Jan 3, 2025
@capricorn86
Copy link
Owner

Thank you for reporting @raystubbs! 🙂

I can't reproduce this issue in the latest version of Happy DOM.

Could the problem be that the console used is the default VirtualConsole, and therefore it is not printed out to the Node.js console?

You can change to use the Node.js console like this:

    const window = new Window({ console: globalThis.console });

If this doesn't solve your problem, please try with the latest version v16.3.0

@capricorn86
Copy link
Owner

Please re-open if you believe that the issue hasn't been solved.

@raystubbs
Copy link
Author

Huh, odd, I'll investigate further and get back to you.

@raystubbs
Copy link
Author

raystubbs commented Jan 8, 2025

This is still definitely happening on my end. I'm on node 18, just downgraded from 21 to see if it was a version issue. But seeing this in both of them.

Here's my repro code:

const { Window } = require('happy-dom');
const window = new Window();

window.document.body.innerHTML = "<div><button>Click Me!</button></div>";

const div = window.document.querySelector("div");
const btn = window.document.querySelector("button");

for(const node of [div, btn]) {
  node.addEventListener("click", () => {
    console.log("click", node.nodeName);
  });

  node.addEventListener("a", () => {
    console.log("a", node.nodeName);
  });

  node.addEventListener("b", () => {
    console.log("b", node.nodeName);
  });

  node.addEventListener("c", () => {
    console.log("c", node.nodeName);
  });
}


btn.click();
btn.dispatchEvent(new Event("a", {bubbles: true}));
btn.dispatchEvent(new Event("b", {bubbles: true}));
btn.dispatchEvent(new Event("c", {bubbles: true}));

console.log("done");

And the output:

click BUTTON
click DIV
a BUTTON
b BUTTON
c BUTTON
done

And my yarn.lock:

# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


happy-dom@^16.5.1:
  version "16.5.2"
  resolved "https://registry.yarnpkg.com/happy-dom/-/happy-dom-16.5.2.tgz#0521876d462d014f4f42db2ce95bf
8096a5ac9e0"
  integrity sha512-XWXjbbNjog3Me13Q9FQ5L1D7p4bCUdxrJndLVdsCIsCZgHpKz7HUQzAy+R2TidjKEz2Spe3gaqNpPPxOOkep
yg==
  dependencies:
    webidl-conversions "^7.0.0"
    whatwg-mimetype "^3.0.0"

webidl-conversions@^7.0.0:
  version "7.0.0"
  resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-7.0.0.tgz#256b4e1882be
7debbf01d05f0aa2039778ea080a"
  integrity sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu
6g==

whatwg-mimetype@^3.0.0:
  version "3.0.0"
  resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz#5fa1a7623867ff1af6
ca3dc72ad6b8a4208beba7"
  integrity sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o
/Q==

@raystubbs
Copy link
Author

I don't think Github will let me reopen this once it's been closed by the repo owner @capricorn86.

@capricorn86 capricorn86 reopened this Jan 8, 2025
@capricorn86
Copy link
Owner

Re-opened 😊

@raystubbs
Copy link
Author

Thanks 🙏

@capricorn86
Copy link
Owner

capricorn86 commented Jan 8, 2025

I'm still not able to reproduce the problem. I'm not sure if I have missed something. You are sure that you are running with a newer version of Happy DOM?

I have added your test as a unit test:
https://github.com/capricorn86/happy-dom/blob/master/packages/happy-dom/test/nodes/node/Node.test.ts#L1139

The output there is:

  1. click:BUTTON
  2. click:DIV
  3. a:BUTTON
  4. a:DIV
  5. b:BUTTON
  6. b:DIV
  7. c:BUTTON
  8. c:DIV

Which seem to be correct.

By the way, thank you for sponsoring me and Happy DOM! 🙏

capricorn86 added a commit that referenced this issue Jan 8, 2025
…s in HTML (#1680)

* chore: [#1661] Adds unit test for testing bubbling of events

* fix: [#1661] Fixes problem with encoding and decoding attribute values in HTML

* chore: [#1678] Fixes problem with query selector
@raystubbs
Copy link
Author

raystubbs commented Jan 8, 2025

I'm still not able to reproduce the problem. I'm not sure if I have missed something. You are sure that you are running with a newer version of Happy DOM?

Huh... that's very odd. That output seems right to me... which seems to indicate that whatever's going on is specific to my setup. I'll try cloning and running all the unit tests. See if they break on my system.

By the way, thank you for sponsoring me and Happy DOM! 🙏

My absolute pleasure! Isn't much. But I use Happy DOM quite a bit these days, and think it's an awesome project. Wanted to give what I could to show some appreciation.

@raystubbs
Copy link
Author

Eh, my bad 😅 sorry for wasting your time and mine; I was dispatching instances of the wrong event class. Event instead of window.Event. Can't believe how long it too me to spot it 😬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants