Skip to content

Commit cf67c1e

Browse files
committed
Fix the destination names when they're duplicated
1 parent bda7456 commit cf67c1e

4 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/core/editor/pdf_editor.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,17 @@ class PDFEditor {
11471147
*/
11481148
#findDuplicateNamedDestinations() {
11491149
const { namedDestinations } = this;
1150+
const getUniqueDestinationName = name => {
1151+
if (!namedDestinations.has(name)) {
1152+
return name;
1153+
}
1154+
for (let i = 1; ; i++) {
1155+
const dedupedName = `${name}_${i}`;
1156+
if (!namedDestinations.has(dedupedName)) {
1157+
return dedupedName;
1158+
}
1159+
}
1160+
};
11501161
for (let i = 0, ii = this.oldPages.length; i < ii; i++) {
11511162
const page = this.oldPages[i];
11521163
const {
@@ -1179,7 +1190,7 @@ class PDFEditor {
11791190
continue;
11801191
}
11811192
// Create a new unique named destination.
1182-
const newName = `${pointingDest}_p${i + 1}`;
1193+
const newName = getUniqueDestinationName(`${pointingDest}_p${i + 1}`);
11831194
dedupNamedDestinations.set(pointingDest, newName);
11841195
namedDestinations.set(newName, dest);
11851196
}

test/pdfs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@
522522
!issue14048.pdf
523523
!issue11656.pdf
524524
!annotation-fileattachment.pdf
525+
!named_dest_collision_for_editor.pdf
525526
!annotation-text-widget.pdf
526527
!issue7454.pdf
527528
!issue15443.pdf
811 Bytes
Binary file not shown.

test/unit/api_spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5804,6 +5804,41 @@ small scripts as well as for`);
58045804
});
58055805

58065806
describe("Named destinations", function () {
5807+
it("keeps colliding deduplicated destination names unique", async function () {
5808+
let loadingTask = getDocument(
5809+
buildGetDocumentParams("named_dest_collision_for_editor.pdf")
5810+
);
5811+
let pdfDoc = await loadingTask.promise;
5812+
5813+
let destinations = await pdfDoc.getDestinations();
5814+
expect(Object.keys(destinations).sort()).toEqual(["foo", "foo_p2"]);
5815+
5816+
const data = await pdfDoc.extractPages([
5817+
{ document: null },
5818+
{ document: null },
5819+
]);
5820+
await loadingTask.destroy();
5821+
5822+
loadingTask = getDocument(data);
5823+
pdfDoc = await loadingTask.promise;
5824+
5825+
destinations = await pdfDoc.getDestinations();
5826+
expect(Object.keys(destinations).sort()).toEqual([
5827+
"foo",
5828+
"foo_p2",
5829+
"foo_p2_1",
5830+
"foo_p2_p2",
5831+
]);
5832+
5833+
const secondPage = await pdfDoc.getPage(2);
5834+
const annots = await secondPage.getAnnotations();
5835+
expect(annots.length).toEqual(2);
5836+
expect(annots[0].dest).toEqual("foo_p2_1");
5837+
expect(annots[1].dest).toEqual("foo_p2_p2");
5838+
5839+
await loadingTask.destroy();
5840+
});
5841+
58075842
it("extract page and check destinations", async function () {
58085843
let loadingTask = getDocument(buildGetDocumentParams("issue6204.pdf"));
58095844
let pdfDoc = await loadingTask.promise;

0 commit comments

Comments
 (0)