Skip to content

Commit 3821ca8

Browse files
committed
feat(pat inject): Support submit buttons with child elements.
Send the value of a submit button even if a child element of the submit button was clicked and not the submit button itself.
1 parent 981d71f commit 3821ca8

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/pat/ajax/ajax.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Copyright 2012-2013 Florian Friesdorf
55
* Copyright 2012-2013 Marko Durkovic
66
*/
7+
import "../../core/polyfills"; // SubmitEvent.submitter for Safari < 15.4 and jsDOM
78
import $ from "jquery";
89
import logging from "../../core/logging";
910
import Parser from "../../core/parser";
@@ -53,8 +54,8 @@ const _ = {
5354
$el.off(".pat-ajax");
5455
},
5556
onClickSubmit(event) {
56-
const el = event.target;
57-
const form = el.closest("form");
57+
const el = event.submitter || event.target;
58+
const form = el.form;
5859
const data = {};
5960
if (el.name) {
6061
data[el.name] = el.value;

src/pat/inject/inject.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,18 @@ const inject = {
107107
if ($el[0]?.tagName === "FORM") {
108108
events.add_event_listener(
109109
$el[0],
110-
"click",
111-
"pat-inject--form-submit-click",
110+
"submit",
111+
"pat-inject--form-submit",
112112
(e) => {
113113
if (
114-
e.target.matches(
114+
e.submitter?.matches(
115115
"[type=submit], button:not([type=button]), [type=image]"
116116
)
117117
) {
118118
// make sure the submitting button is sent
119119
// with the form
120120
ajax.onClickSubmit(e);
121121
}
122-
}
123-
);
124-
events.add_event_listener(
125-
$el[0],
126-
"submit",
127-
"pat-inject--form-submit",
128-
(e) => {
129122
this.onTrigger(e);
130123
}
131124
);

src/pat/inject/inject.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,28 @@ describe("pat-inject", function () {
10261026
expect(ajaxargs.data.get("submit")).toContain("label");
10271027
});
10281028

1029+
it("9.2.4.2 - pass submit button value in ajax call as data when clicked on a dom node within the submit button", async function () {
1030+
document.body.innerHTML = `
1031+
<form class="pat-inject" action="test.html#someid" method="post">
1032+
<button name="submit" value="label">
1033+
<span>label</span>
1034+
</button>
1035+
</form>
1036+
`;
1037+
1038+
const form = document.querySelector("form");
1039+
const label = document.querySelector("form button span");
1040+
1041+
pattern.init($(form));
1042+
await utils.timeout(1); // wait a tick for async to settle.
1043+
1044+
label.click();
1045+
1046+
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
1047+
expect($.ajax).toHaveBeenCalled();
1048+
expect(ajaxargs.data.get("submit")).toContain("label");
1049+
});
1050+
10291051
it("9.2.4.3 - Sends submit button form values even if submit button is added after initialization.", async function () {
10301052
document.body.innerHTML = `
10311053
<form class="pat-inject" action="test.cgi">

0 commit comments

Comments
 (0)