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

Improve date parsing in the js sandbox #18234

Merged
merged 1 commit into from
Jun 14, 2024
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
19 changes: 17 additions & 2 deletions src/scripting_api/aform.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,26 @@ class AForm {
return date;
}

_parseDate(cFormat, cDate) {
_parseDate(cFormat, cDate, strict = false) {
let date = null;
try {
date = this._util.scand(cFormat, cDate);
} catch {}
if (!date) {
if (strict) {
return null;
}
let format = cFormat;
if (/mm(?!m)/.test(format)) {
format = format.replace("mm", "m");
}
if (/dd(?!d)/.test(format)) {
format = format.replace("dd", "d");
}
try {
date = this._util.scand(format, cDate);
} catch {}
}
if (!date) {
date = Date.parse(cDate);
date = isNaN(date)
Expand Down Expand Up @@ -379,7 +394,7 @@ class AForm {
return;
}

if (this._parseDate(cFormat, value) === null) {
if (this._parseDate(cFormat, value, /* strict = */ true) === null) {
const invalid = GlobalConstants.IDS_INVALID_DATE;
const invalid2 = GlobalConstants.IDS_INVALID_DATE2;
const err = `${invalid} ${this._mkTargetName(
Expand Down
4 changes: 4 additions & 0 deletions test/unit/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,10 @@ describe("Scripting", function () {
await check("12", "mm", "2000/12/01");
await check("2022", "yyyy", "2022/01/01");
await check("a1$9bbbb21", "dd/mm/yyyy", "2021/09/01");
await check("1/2/2024", "dd/mm/yyyy", "2024/02/01");
await check("01/2/2024", "dd/mm/yyyy", "2024/02/01");
await check("1/02/2024", "dd/mm/yyyy", "2024/02/01");
await check("01/02/2024", "dd/mm/yyyy", "2024/02/01");

// The following test isn't working as expected because
// the quickjs date parser has been replaced by the browser one
Expand Down