Skip to content

Commit 8cae5d1

Browse files
Merge pull request #20917 from calixteman/fix_dup_name_dest
Fix the destination names when they're duplicated
2 parents 746e6b4 + cf67c1e commit 8cae5d1

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
@@ -5800,6 +5800,41 @@ small scripts as well as for`);
58005800
});
58015801

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

0 commit comments

Comments
 (0)