Skip to content

Commit 0bee641

Browse files
committed
Avoid to use a used slot when looking for a new page position
1 parent bda7456 commit 0bee641

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/core/editor/pdf_editor.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,15 +616,20 @@ class PDFEditor {
616616
if (newIndex !== -1) {
617617
newPageIndex = newIndex++;
618618
} else {
619+
// Find the first available index in the newPages array.
620+
// This is needed when the pageIndices option is used since the
621+
// pages can be added in any order.
619622
for (
620623
newPageIndex = 0;
621-
this.oldPages[newPageIndex] === undefined;
624+
this.oldPages[newPageIndex] !== undefined;
622625
newPageIndex++
623626
) {
624627
/* empty */
625628
}
626629
}
627630
}
631+
// Reserve the slot immediately because the page fetch is async.
632+
this.oldPages[newPageIndex] = null;
628633
promises.push(
629634
document.getPage(i).then(page => {
630635
this.oldPages[newPageIndex] = new PageData(page, documentData);

test/unit/api_spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6222,6 +6222,44 @@ small scripts as well as for`);
62226222

62236223
await loadingTask.destroy();
62246224
});
6225+
6226+
it("fills missing pageIndices with the first free slots", async function () {
6227+
let loadingTask = getDocument(
6228+
buildGetDocumentParams("tracemonkey.pdf")
6229+
);
6230+
let pdfDoc = await loadingTask.promise;
6231+
const data = await pdfDoc.extractPages([
6232+
{ document: null, includePages: [1, 3, 5], pageIndices: [1] },
6233+
]);
6234+
await loadingTask.destroy();
6235+
6236+
loadingTask = getDocument(data);
6237+
pdfDoc = await loadingTask.promise;
6238+
6239+
expect(pdfDoc.numPages).toEqual(3);
6240+
6241+
// Page 4 in the original document should occupy the first free slot.
6242+
let pdfPage = await pdfDoc.getPage(1);
6243+
let { items: textItems } = await pdfPage.getTextContent();
6244+
expect(mergeText(textItems).includes("3. Trace Trees")).toBeTrue();
6245+
6246+
// Page 2 in the original document keeps its explicit destination slot.
6247+
pdfPage = await pdfDoc.getPage(2);
6248+
({ items: textItems } = await pdfPage.getTextContent());
6249+
expect(
6250+
mergeText(textItems).includes("2. Overview: Example Tracing Run")
6251+
).toBeTrue();
6252+
6253+
// Page 6 in the original document should occupy the remaining free
6254+
// slot.
6255+
pdfPage = await pdfDoc.getPage(3);
6256+
({ items: textItems } = await pdfPage.getTextContent());
6257+
expect(
6258+
mergeText(textItems).includes("4. Nested Trace Tree Formation")
6259+
).toBeTrue();
6260+
6261+
await loadingTask.destroy();
6262+
});
62256263
});
62266264

62276265
describe("AcroForm", function () {

0 commit comments

Comments
 (0)