Skip to content

Commit

Permalink
Add SubmitEvent's submitter
Browse files Browse the repository at this point in the history
Fixes #3117.
  • Loading branch information
jenseng committed Jan 10, 2023
1 parent 8a43fd5 commit 79c351b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
13 changes: 13 additions & 0 deletions lib/jsdom/living/events/SubmitEvent-impl.js
@@ -0,0 +1,13 @@
"use strict";

const EventImpl = require("./Event-impl").implementation;

const SubmitEventInit = require("../generated/SubmitEventInit");

// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#the-submitevent-interface
class SubmitEventImpl extends EventImpl {}
SubmitEventImpl.defaultInit = SubmitEventInit.convert(undefined, undefined);

module.exports = {
implementation: SubmitEventImpl
};
11 changes: 11 additions & 0 deletions lib/jsdom/living/events/SubmitEvent.webidl
@@ -0,0 +1,11 @@
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#the-submitevent-interface
[Exposed=Window]
interface SubmitEvent : Event {
constructor(DOMString type, optional SubmitEventInit eventInitDict = {});

readonly attribute HTMLElement? submitter;
};

dictionary SubmitEventInit : EventInit {
HTMLElement? submitter = null;
};
1 change: 1 addition & 0 deletions lib/jsdom/living/interfaces.js
Expand Up @@ -130,6 +130,7 @@ const generatedInterfaces = {
StorageEvent: require("./generated/StorageEvent"),
ProgressEvent: require("./generated/ProgressEvent"),
PageTransitionEvent: require("./generated/PageTransitionEvent"),
SubmitEvent: require("./generated/SubmitEvent"),

UIEvent: require("./generated/UIEvent"),
FocusEvent: require("./generated/FocusEvent"),
Expand Down
7 changes: 4 additions & 3 deletions lib/jsdom/living/nodes/HTMLFormElement-impl.js
Expand Up @@ -9,6 +9,7 @@ const { formOwner, isListed, isSubmittable, isSubmitButton } = require("../helpe
const HTMLFormControlsCollection = require("../generated/HTMLFormControlsCollection");
const notImplemented = require("../../browser/not-implemented");
const { parseURLToResultingURLRecord } = require("../helpers/document-base-url");
const SubmitEvent = require("../generated/SubmitEvent");

const encTypes = new Set([
"application/x-www-form-urlencoded",
Expand Down Expand Up @@ -87,8 +88,8 @@ class HTMLFormElementImpl extends HTMLElementImpl {
notImplemented("HTMLFormElement.prototype.submit", this._ownerDocument._defaultView);
}

requestSubmit(submitter = undefined) {
if (submitter !== undefined) {
requestSubmit(submitter = null) {
if (submitter !== null) {
if (!isSubmitButton(submitter)) {
throw new TypeError("The specified element is not a submit button");
}
Expand All @@ -106,7 +107,7 @@ class HTMLFormElementImpl extends HTMLElementImpl {
return;
}

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

Expand Down
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>HTML Test: 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>

<body>
<script>
"use strict";

let submitEvent;
function prepareForm() {
submitEvent = undefined;
const form = document.createElement("FORM");
const submitButton = document.createElement("BUTTON");
form.appendChild(submitButton);
form.addEventListener("submit", event => {
event.preventDefault();
submitEvent = event;
});
document.body.appendChild(form);
return { form, submitButton };
}

test(() => {
const { submitButton } = prepareForm();
submitButton.click();
assert_equals(submitEvent.submitter, submitButton, "Submitter is set");
}, "Clicking a submit button sets the submitter");

test(() => {
const { form, submitButton } = prepareForm();
form.requestSubmit(submitButton);
assert_equals(submitEvent.submitter, submitButton, "Submitter is set");
}, "Requesting submit with a submitter sets the submitter");

test(() => {
const { form } = prepareForm();
form.requestSubmit();
assert_equals(submitEvent.submitter, null, "Submitter is not set");
}, "Requesting submit without a submitter does not set the submitter");
</script>

0 comments on commit 79c351b

Please sign in to comment.