From 3821ca8793557ede2e42e20caee7908a9f466b62 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Sat, 4 Nov 2023 23:34:10 +0100 Subject: [PATCH] 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. --- src/pat/ajax/ajax.js | 5 +++-- src/pat/inject/inject.js | 13 +++---------- src/pat/inject/inject.test.js | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/pat/ajax/ajax.js b/src/pat/ajax/ajax.js index 06454fe03..041532c8d 100644 --- a/src/pat/ajax/ajax.js +++ b/src/pat/ajax/ajax.js @@ -4,6 +4,7 @@ * Copyright 2012-2013 Florian Friesdorf * Copyright 2012-2013 Marko Durkovic */ +import "../../core/polyfills"; // SubmitEvent.submitter for Safari < 15.4 and jsDOM import $ from "jquery"; import logging from "../../core/logging"; import Parser from "../../core/parser"; @@ -53,8 +54,8 @@ const _ = { $el.off(".pat-ajax"); }, onClickSubmit(event) { - const el = event.target; - const form = el.closest("form"); + const el = event.submitter || event.target; + const form = el.form; const data = {}; if (el.name) { data[el.name] = el.value; diff --git a/src/pat/inject/inject.js b/src/pat/inject/inject.js index c3e2f0e62..27160b2cf 100644 --- a/src/pat/inject/inject.js +++ b/src/pat/inject/inject.js @@ -107,11 +107,11 @@ const inject = { if ($el[0]?.tagName === "FORM") { events.add_event_listener( $el[0], - "click", - "pat-inject--form-submit-click", + "submit", + "pat-inject--form-submit", (e) => { if ( - e.target.matches( + e.submitter?.matches( "[type=submit], button:not([type=button]), [type=image]" ) ) { @@ -119,13 +119,6 @@ const inject = { // with the form ajax.onClickSubmit(e); } - } - ); - events.add_event_listener( - $el[0], - "submit", - "pat-inject--form-submit", - (e) => { this.onTrigger(e); } ); diff --git a/src/pat/inject/inject.test.js b/src/pat/inject/inject.test.js index 6e618923a..eaa0b4820 100644 --- a/src/pat/inject/inject.test.js +++ b/src/pat/inject/inject.test.js @@ -1026,6 +1026,28 @@ describe("pat-inject", function () { expect(ajaxargs.data.get("submit")).toContain("label"); }); + 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 () { + document.body.innerHTML = ` +
+ +
+ `; + + const form = document.querySelector("form"); + const label = document.querySelector("form button span"); + + pattern.init($(form)); + await utils.timeout(1); // wait a tick for async to settle. + + label.click(); + + const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0]; + expect($.ajax).toHaveBeenCalled(); + expect(ajaxargs.data.get("submit")).toContain("label"); + }); + it("9.2.4.3 - Sends submit button form values even if submit button is added after initialization.", async function () { document.body.innerHTML = `