Skip to content

Commit

Permalink
Don't fire submit event from form.submit()
Browse files Browse the repository at this point in the history
Fixes #3435.
  • Loading branch information
jenseng committed Jan 22, 2023
1 parent 79c351b commit ecce8bc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
4 changes: 0 additions & 4 deletions lib/jsdom/living/nodes/HTMLFormElement-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ class HTMLFormElementImpl extends HTMLElementImpl {
}

submit() {
if (!fireAnEvent("submit", this, undefined, { bubbles: true, cancelable: true })) {
return;
}

notImplemented("HTMLFormElement.prototype.submit", this._ownerDocument._defaultView);
}

Expand Down
5 changes: 5 additions & 0 deletions test/web-platform-tests/to-upstream/jsdom-only/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# jsdom-only tests

This directory contains WPT-style tests that are jsdom-specific, and thus should not be run in browsers nor upstreamed (despite living under `to-upstream`). Since jsdom follows the platform wherever possible, these should be extremely rare.

The intended use case is for verifying jsdom behavior that can't (or needn't) be tested in browsers (e.g. validating `HTMLFormElement.prototyope.submit()`'s behavior without actually navigating). If the test will also pass in browsers, then it should be in a different directory.
28 changes: 28 additions & 0 deletions test/web-platform-tests/to-upstream/jsdom-only/form-submit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Form submit event</title>
<link rel="help" href="https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#form-submission-algorithm">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<form><button></button></form>

<body>
<script>
"use strict";

// There's no reliable way to prevent browser navigation on form.submit(), and all browsers get this right, so this
// is a jsdom-only test
test(() => {
let submitEvent;
const form = document.querySelector("form");
form.addEventListener("submit", event => {
event.preventDefault();
submitEvent = event;
});

form.submit();

assert_equals(submitEvent, undefined, "SubmitEvent does not fire");
}, "HTMLFormElement's submit() does not fire a SubmitEvent");
</script>

0 comments on commit ecce8bc

Please sign in to comment.