capricorn86 / happy-dom

A JavaScript implementation of a web browser without its graphical user interface
MIT License
3.4k stars 206 forks source link

Click events inside wrapped <label> elements do not cascade #1614

Open EvHaus opened 5 days ago

EvHaus commented 5 days ago

Describe the bug

The behavior of happy-dom v13.6.2 and v15.10.2 differs in that in v13 when you click on elements inside a <label> tag, it follows the browser spec and triggers the onChange events for the wrapped input element. However, in v15 this behavior is no longer working. I'm not sure where between 13.6.2 and 15.10.2 this broke.

To Reproduce

Given the following DOM structure rendered via happy-dom:

<label htmlFor="foo">
  <input id="foo" onChange={() => console.log('howdy')} type="checkbox" />
  <span id="description">Description</span>
</label>

And the following JS code

document.querySelector('#description').click();
// "howdy" should be printed

Expected behavior

If you click on the span#description element, it should trigger the onChange event on the input element. In v13.6.2 it does. In browsers it does. But in v15.10.2 it doesn't.

Screenshots N/A

Device: N/A

Additional context N/A

cythrawll commented 2 days ago

This caught us off guard too, when random checkbox tests started to fail after happy-dom update. Looking into it the developer was using testing-librarysgetByTextinstead ofgetByLabelText`. So it was selecting the div node we had in the label, so our click events were no longer triggering the output.

We had to fix the tests by using getByLabelText as a work around.