Skip to content

Commit 099da3c

Browse files
committed
fix(pat-ajax): Support anchors without a href attribute and forms without an action attribute.
The URL can still be set via the data-pat-ajax attribute. This fixes a problem where pat-inject enabled forms without an action attribute but a submit button with a formaction attribute would break.
1 parent 9079a47 commit 099da3c

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/pat/ajax/ajax.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ const log = logging.getLogger("pat.ajax");
1414
export const parser = new Parser("ajax");
1515
parser.addArgument("accept", "text/html");
1616
parser.addArgument("url", function ($el) {
17-
return (
18-
$el.is("a") ? $el.attr("href") : $el.is("form") ? $el.attr("action") : ""
19-
).split("#")[0];
17+
var val = $el.is("a") ? $el.attr("href") : $el.is("form") ? $el.attr("action") : "";
18+
return (val || "").split("#")[0];
2019
});
2120
parser.addArgument("browser-cache", "no-cache", ["cache", "no-cache"]); // Cache ajax requests
2221

src/pat/ajax/ajax.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ describe("pat-ajax", function () {
131131
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
132132
expect(ajaxargs.url).toEqual("else.html");
133133
});
134+
it("Does not break with missing anchor-href", async function () {
135+
document.body.innerHTML = `<a class="pat-ajax"/>`;
136+
jest.spyOn(pattern.parser.log, "error");
137+
pattern.parser.parse(document.body.querySelector(".pat-ajax"));
138+
expect(pattern.parser.log.error).not.toHaveBeenCalled();
139+
});
140+
it("Does not break with missing form-action", async function () {
141+
document.body.innerHTML = `<form class="pat-ajax"/>`;
142+
jest.spyOn(pattern.parser.log, "error");
143+
pattern.parser.parse(document.body.querySelector(".pat-ajax"));
144+
expect(pattern.parser.log.error).not.toHaveBeenCalled();
145+
});
134146

135147
// Accept
136148
it("Default accept header", function () {

0 commit comments

Comments
 (0)