mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-08-04 05:17:24 +02:00
Merge pull request #21686 from calixteman/fix/quad-regex-xfa-path
Anchor the regex used to extract the XFA path positions
This commit is contained in:
commit
1da36fb6bf
@ -341,7 +341,8 @@ function lookupNormalRect(arr, fallback) {
|
||||
* each part of the path.
|
||||
*/
|
||||
function parseXFAPath(path) {
|
||||
const positionPattern = /(.+)\[(\d+)\]$/;
|
||||
// Anchoring prevents retrying the match at every character.
|
||||
const positionPattern = /^(.+)\[(\d+)\]$/;
|
||||
return path.split(".").map(component => {
|
||||
const m = component.match(positionPattern);
|
||||
if (m) {
|
||||
|
||||
@ -244,6 +244,32 @@ describe("core_utils", function () {
|
||||
{ name: "BAR", pos: 456 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("should ignore a malformed position", function () {
|
||||
expect(parseXFAPath("foo[].bar[1x].oof[].[3]")).toEqual([
|
||||
{ name: "foo[]", pos: 0 },
|
||||
{ name: "bar[1x]", pos: 0 },
|
||||
{ name: "oof[]", pos: 0 },
|
||||
{ name: "[3]", pos: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
it("should keep the longest name when a component has several brackets", function () {
|
||||
expect(parseXFAPath("foo[1][2]")).toEqual([{ name: "foo[1]", pos: 2 }]);
|
||||
});
|
||||
|
||||
it("should handle a long component efficiently", function () {
|
||||
// Looking for the position with a leading `.+` is quadratic in the
|
||||
// length of a component which doesn't end with one.
|
||||
const name = "a".repeat(200000);
|
||||
|
||||
const startTime = performance.now();
|
||||
const parsedPath = parseXFAPath(name);
|
||||
const duration = performance.now() - startTime;
|
||||
|
||||
expect(parsedPath).toEqual([{ name, pos: 0 }]);
|
||||
expect(duration).toBeLessThan(1000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("recoverJsURL", function () {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user