Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit button fixes #1175

Merged
merged 5 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 30 additions & 32 deletions src/core/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ class ArgumentParser {
if (this.parameters[original]) {
this.parameters[original].alias = alias;
} else {
throw (
'Attempted to add an alias "' +
alias +
'" for a non-existing parser argument "' +
original +
'".'
);
throw `Attempted to add an alias "${alias}" for a non-existing parser argument "${original}".`;
}
}

Expand Down Expand Up @@ -166,19 +160,19 @@ class ArgumentParser {
} else if (typeof value === "number") {
value = !!value;
} else {
throw "Cannot convert value for " + name + " to boolean";
throw `Cannot convert value for ${name} to boolean.`;
}
break;
case "number":
if (typeof value === "string") {
value = parseInt(value, 10);
if (isNaN(value)) {
throw "Cannot convert value for " + name + " to number";
throw `Cannot convert value for ${name} to number.`;
}
} else if (typeof value === "boolean") {
value = value + 0;
} else {
throw "Cannot convert value for " + name + " to number";
throw `Cannot convert value for ${name} to number.`;
}
break;
case "string":
Expand All @@ -188,28 +182,25 @@ class ArgumentParser {
case "undefined":
break;
default:
throw (
"Do not know how to convert value for " +
name +
" to " +
throw `Do not know how to convert value for ${name} of type ${typeof value} to ${
spec.type
);
}.`;
}
} catch (e) {
this.log.warn(e);
return null;
}

if (spec.choices && spec.choices.indexOf(value) === -1) {
this.log.warn("Illegal value for " + name + ": " + value);
this.log.warn(`Illegal value for ${name}: ${value}.`);
return null;
}
return value;
}

_set(opts, name, value) {
if (!(name in this.parameters)) {
this.log.debug("Ignoring value for unknown argument " + name);
this.log.debug(`Ignoring value for unknown argument: ${name}.`);
return;
}
const spec = this.parameters[name];
Expand Down Expand Up @@ -261,7 +252,7 @@ class ArgumentParser {
}
const matches = part.match(this.named_param_pattern);
if (!matches) {
this.log.warn("Invalid parameter: " + part + ": " + argstring);
this.log.warn(`Invalid parameter: ${part}: ${argstring}.`);
continue;
}
const name = matches[1];
Expand All @@ -280,7 +271,7 @@ class ArgumentParser {
this._set(opts, name + "-" + field, subopt[field]);
}
} else {
this.log.warn("Unknown named parameter " + matches[1]);
this.log.warn(`Unknown named parameter: ${matches[1]}.`);
continue;
}
}
Expand Down Expand Up @@ -320,7 +311,7 @@ class ArgumentParser {
break;
}
}
if (parts.length) this.log.warn("Ignore extra arguments: " + parts.join(" "));
if (parts.length) this.log.warn(`Ignore extra arguments: ${parts.join(" ")}.`);
return opts;
}

Expand All @@ -332,7 +323,7 @@ class ArgumentParser {
try {
return JSON.parse(parameter);
} catch (e) {
this.log.warn("Invalid JSON argument found: " + parameter);
this.log.warn(`Invalid JSON argument found: ${parameter}.`);
}
}
if (parameter.match(this.named_param_pattern)) {
Expand All @@ -352,41 +343,50 @@ class ArgumentParser {

_defaults($el) {
const result = {};
for (const name in this.parameters)
if (typeof this.parameters[name].value === "function")
for (const name in this.parameters) {
if (typeof this.parameters[name].value === "function") {
try {
result[name] = this.parameters[name].value($el, name);
this.parameters[name].type = typeof result[name];
} catch (e) {
this.log.error("Default function for " + name + " failed.");
this.log.error(`Default function for ${name} failed.`);
}
else result[name] = this.parameters[name].value;
} else {
result[name] = this.parameters[name].value;
}
}
return result;
}

_cleanupOptions(options, group_options = true) {
// Resolve references
for (const name of Object.keys(options)) {
const spec = this.parameters[name];
if (spec === undefined) continue;
if (spec === undefined) {
continue;
}

if (
options[name] === spec.value &&
typeof spec.value === "string" &&
spec.value.slice(0, 1) === "$"
)
) {
options[name] = options[spec.value.slice(1)];
}
}
if (group_options) {
// Move options into groups and do renames
for (const name of Object.keys(options)) {
const spec = this.parameters[name];
let target;
if (spec === undefined) continue;
if (spec === undefined) {
continue;
}

if (spec.group) {
if (typeof options[spec.group] !== "object")
if (typeof options[spec.group] !== "object") {
options[spec.group] = {};
}
target = options[spec.group];
} else {
target = options;
Expand Down Expand Up @@ -430,9 +430,7 @@ class ArgumentParser {
) {
$possible_config_providers = $el;
} else {
$possible_config_providers = $el
.parents("[" + this.attribute + "]")
.addBack();
$possible_config_providers = $el.parents(`[${this.attribute}]`).addBack();
}

for (const provider of $possible_config_providers) {
Expand Down
11 changes: 8 additions & 3 deletions src/pat/ajax/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ const log = logging.getLogger("pat.ajax");
export const parser = new Parser("ajax");
parser.addArgument("accept", "text/html");
parser.addArgument("url", function ($el) {
return (
$el.is("a") ? $el.attr("href") : $el.is("form") ? $el.attr("action") : ""
).split("#")[0];
const el = $el[0];
const value =
el.tagName === "A"
? el.getAttribute("href")
: el.tagName === "FORM"
? el.getAttribute("action")
: "";
return (value || "").split("#")[0];
});
parser.addArgument("browser-cache", "no-cache", ["cache", "no-cache"]); // Cache ajax requests

Expand Down
12 changes: 12 additions & 0 deletions src/pat/ajax/ajax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ describe("pat-ajax", function () {
const ajaxargs = $.ajax.mock.calls[$.ajax.mock.calls.length - 1][0];
expect(ajaxargs.url).toEqual("else.html");
});
it("Does not break with missing anchor-href", async function () {
document.body.innerHTML = `<a class="pat-ajax"/>`;
jest.spyOn(pattern.parser.log, "error");
pattern.parser.parse(document.body.querySelector(".pat-ajax"));
expect(pattern.parser.log.error).not.toHaveBeenCalled();
});
it("Does not break with missing form-action", async function () {
document.body.innerHTML = `<form class="pat-ajax"/>`;
jest.spyOn(pattern.parser.log, "error");
pattern.parser.parse(document.body.querySelector(".pat-ajax"));
expect(pattern.parser.log.error).not.toHaveBeenCalled();
});

// Accept
it("Default accept header", function () {
Expand Down
11 changes: 9 additions & 2 deletions src/pat/inject/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,17 @@ const inject = {
// setup event handlers
if ($el.is("form")) {
$el.on("submit.pat-inject", this.onTrigger.bind(this))
.on("click.pat-inject", "[type=submit]", ajax.onClickSubmit)
.on(
"click.pat-inject",
"[type=submit][formaction], [type=image][formaction]",
`[type=submit]:not([formaction]),
button:not([formaction]):not([type=button])`,
ajax.onClickSubmit
)
.on(
"click.pat-inject",
`[type=submit][formaction],
[type=image][formaction],
button[formaction]:not([type=button])`,
this.onFormActionSubmit.bind(this)
);
} else if ($el.is(".pat-subform")) {
Expand Down
63 changes: 63 additions & 0 deletions src/pat/inject/inject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,69 @@ describe("pat-inject", function () {
"other"
);
});

it("9.2.5.10 - does not call ajax.onClickSubmit twice.", async function () {
document.body.innerHTML = `
<form class="pat-inject">
<button type="submit" formaction="test.cgi"/>
</form>
`;

const pat_ajax = (await import("../ajax/ajax.js")).default;
jest.spyOn(pat_ajax, "onClickSubmit");

const form = document.querySelector("form");
const button = form.querySelector("button");

pattern.init($(form));
button.click();

expect(pat_ajax.onClickSubmit).toHaveBeenCalledTimes(1);
});
});

describe("9.2.6 - Support submit buttons without type attribute ...", () => {
it("9.2.6.1 - ... without a formaction atttribute.", async function () {
document.body.innerHTML = `
<form class="pat-inject" action="test.cgi">
<button/>
</form>
`;

const pat_ajax = (await import("../ajax/ajax.js")).default;
jest.spyOn(pat_ajax, "onClickSubmit");
jest.spyOn(pattern, "onFormActionSubmit");

const form = document.querySelector("form");
const button = form.querySelector("button");

pattern.init($(form));
button.click();

expect(pat_ajax.onClickSubmit).toHaveBeenCalledTimes(1);
expect(pattern.onFormActionSubmit).toHaveBeenCalledTimes(0);
});

it("9.2.6.1 - ... with a formaction atttribute.", async function () {
document.body.innerHTML = `
<form class="pat-inject">
<button formaction="test.cgi"/>
</form>
`;

const pat_ajax = (await import("../ajax/ajax.js")).default;
jest.spyOn(pat_ajax, "onClickSubmit");
jest.spyOn(pattern, "onFormActionSubmit");

const form = document.querySelector("form");
const button = form.querySelector("button");

pattern.init($(form));
button.click();

expect(pat_ajax.onClickSubmit).toHaveBeenCalledTimes(1);
expect(pattern.onFormActionSubmit).toHaveBeenCalledTimes(1);
});
});
});
});
Expand Down