Skip to content

Commit b875946

Browse files
committed
pat inject: Fix formaction button injections with target self.
Fixes: quaive/ploneintranet.prototype#1164
1 parent 7241dec commit b875946

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797

9898
### Fixes
9999

100+
- pat inject: Fix formaction button injections with target ``self``. Fixes: https://github.com/quaive/ploneintranet.prototype/issues/1164
100101
- pat calendar: Fix language loading error "Error: Cannot find module './en.js'"
101102
- pat depends, pat auto suggest: Fix a problem with initialization of ``pat-auto-suggest`` which occurred after the lazy loading changes.
102103
- pat checklist: Also dispatch standard ``change`` event when de/selecting all items.

src/pat/inject/inject.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ const inject = {
193193
const $button = $(e.target);
194194
const formaction = $button.attr("formaction");
195195
const $form = $button.parents(".pat-inject").first();
196-
const opts = { url: formaction };
196+
const opts = {
197+
url: formaction,
198+
$formaction_button: $button,
199+
};
197200
const $cfg_node = $button.closest("[data-pat-inject]");
198201
const cfgs = this.extractConfig($cfg_node, opts);
199202

@@ -239,6 +242,15 @@ const inject = {
239242
$el.parents("form").attr("action") ||
240243
"";
241244

245+
if (
246+
opts.$formaction_button &&
247+
(cfg.target || "").startsWith("self")
248+
) {
249+
// In case of button formaction submit with target ``self``,
250+
// set it. Otherwise the ``form`` will be used as target.
251+
cfg.$target = opts.$formaction_button;
252+
}
253+
242254
// separate selector from url
243255
const urlparts = cfg.url.split("#");
244256
cfg.url = urlparts[0];
@@ -311,9 +323,10 @@ const inject = {
311323
* cfg.$target.
312324
*/
313325
// make sure target exist
314-
if (cfg.target === "none")
326+
if (cfg.target === "none") {
315327
// special case, we don't want to inject anything
316328
return true;
329+
}
317330
cfg.$target =
318331
cfg.$target || (cfg.target === "self" ? $el : $(cfg.target));
319332
if (cfg.$target.length === 0) {

src/pat/inject/inject.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,43 @@ describe("pat-inject", function () {
962962
expect($target1.html()).toBe("some");
963963
expect($target2.html()).toBe("other");
964964
});
965+
966+
it("formaction which replaces itself", async (done) => {
967+
answer(`
968+
<html>
969+
<body>
970+
<div id="someid">some</div>
971+
<div id="otherid">other</div>
972+
</body>
973+
</html>
974+
`);
975+
976+
document.body.innerHTML = `
977+
<div id="oha">form inject target</div>
978+
<form
979+
class="pat-inject"
980+
data-pat-inject="target:#oha;source:#id1">
981+
<button
982+
type="submit"
983+
formaction="test.cgi"
984+
class="pat-inject"
985+
data-pat-inject="target:self::element;source:#otherid"
986+
/>
987+
</form>
988+
`;
989+
990+
pattern.init($("form"));
991+
await utils.timeout(1); // wait a tick for async to settle.
992+
993+
document.querySelector("form button").click();
994+
await utils.timeout(1); // wait a tick for async to settle.
995+
996+
expect(
997+
document.querySelector("form").innerHTML.trim()
998+
).toBe("other");
999+
1000+
done();
1001+
});
9651002
});
9661003
});
9671004
});

0 commit comments

Comments
 (0)