capricorn86 / happy-dom

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

`preventDefault()` will not prevent navigation on a form submission #1459

Open LukasBombach opened 3 weeks ago

LukasBombach commented 3 weeks ago

Describe the bug

When you try to prevent a form element from element from submitting by attaching an event listener and preventing the default, it will still submit the form:

const form = document.querySelector("form");

form.addEventListener('submit', e => e.preventDefault());

/* click submit */

it should not submit the form and no navigation should happen. But it does.

To Reproduce

I did create a minimal reproducible example here:

https://github.com/LukasBombach/happy-dom-form-prevent-default-bug

  1. clone
  2. install (I used yarn yarn install )
  3. run "test" npm script yarn test

Expected behavior

Using preventDefault on a submit event of a form should stop it from navigating.

After submitting a form in happy-dom there will be a navigation nonetheless.

Screenshots

--

Device:

Additional context

I can see this using vitest 1.6.0 happy-dom 14.12.0