Skip to content
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
6 changes: 5 additions & 1 deletion src/scripting_api/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,11 @@ class Util extends PDFObject {
function (match, patternElement) {
const { pattern, action } = handlers[patternElement];
actions.push(action);
return pattern;
// If the format is "Hm", then /\d{1,2}\d{1,2}/ is ambiguous so we use
// a lookahead to ensure that we match the longest possible sequence.
return pattern.includes(",")
? `(?=${pattern})\\${actions.length}`
: pattern;
}
);

Expand Down
9 changes: 9 additions & 0 deletions test/unit/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ describe("Scripting", function () {
value = await myeval(`util.scand("mmddyyyy", "07a15b2007").toString()`);
expect(new Date(value)).toEqual(new Date("07/15/2007 12:00:00"));
});

it("should handle a format with repeated specifiers", async () => {
const cFormat = "Hm".repeat(40);
const cDate = `${"1".repeat(84)}x`;
const value = await myeval(
`util.scand("${cFormat}", "${cDate}")?.toString() ?? "null"`
);
expect(value).toEqual("null");
});
});

describe("printf", function () {
Expand Down