Skip to content

Commit 6245bb2

Browse files
Merge pull request #20915 from calixteman/fix_pageindice
Avoid to use a used slot when looking for a new page position
2 parents 8cae5d1 + 0bee641 commit 6245bb2

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
@@ -6253,6 +6253,44 @@ small scripts as well as for`);
62536253

62546254
await loadingTask.destroy();
62556255
});
6256+
6257+
it("fills missing pageIndices with the first free slots", async function () {
6258+
let loadingTask = getDocument(
6259+
buildGetDocumentParams("tracemonkey.pdf")
6260+
);
6261+
let pdfDoc = await loadingTask.promise;
6262+
const data = await pdfDoc.extractPages([
6263+
{ document: null, includePages: [1, 3, 5], pageIndices: [1] },
6264+
]);
6265+
await loadingTask.destroy();
6266+
6267+
loadingTask = getDocument(data);
6268+
pdfDoc = await loadingTask.promise;
6269+
6270+
expect(pdfDoc.numPages).toEqual(3);
6271+
6272+
// Page 4 in the original document should occupy the first free slot.
6273+
let pdfPage = await pdfDoc.getPage(1);
6274+
let { items: textItems } = await pdfPage.getTextContent();
6275+
expect(mergeText(textItems).includes("3. Trace Trees")).toBeTrue();
6276+
6277+
// Page 2 in the original document keeps its explicit destination slot.
6278+
pdfPage = await pdfDoc.getPage(2);
6279+
({ items: textItems } = await pdfPage.getTextContent());
6280+
expect(
6281+
mergeText(textItems).includes("2. Overview: Example Tracing Run")
6282+
).toBeTrue();
6283+
6284+
// Page 6 in the original document should occupy the remaining free
6285+
// slot.
6286+
pdfPage = await pdfDoc.getPage(3);
6287+
({ items: textItems } = await pdfPage.getTextContent());
6288+
expect(
6289+
mergeText(textItems).includes("4. Nested Trace Tree Formation")
6290+
).toBeTrue();
6291+
6292+
await loadingTask.destroy();
6293+
});
62566294
});
62576295

62586296
describe("AcroForm", function () {

0 commit comments

Comments
 (0)