Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: add_named_destination() maintains named destination list sort order #1930

Merged
merged 2 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1985,10 +1985,10 @@ def add_named_destination(
)

dest_ref = self._add_object(dest)
nd = self.get_named_dest_root()
if not isinstance(title, TextStringObject):
title = TextStringObject(str(title))
nd.extend([title, dest_ref])

self.add_named_destination_array(title, dest_ref)
return dest_ref

def addNamedDestination(
Expand Down
26 changes: 26 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,32 @@ def test_add_named_destination(pdf_file_path):
writer.write(output_stream)


def test_add_named_destination_sort_order(pdf_file_path):
"""
Issue #1927 does not appear.

add_named_destination() maintains the named destination list sort order
"""
writer = PdfWriter()

assert writer.get_named_dest_root() == []

writer.add_blank_page(200, 200)
writer.add_named_destination("b", 0)
# "a" should be moved before "b" on insert
writer.add_named_destination("a", 0)

root = writer.get_named_dest_root()

assert len(root) == 4
assert root[0] == "a", '"a" was not inserted before "b" in the named destination root'
assert root[2] == "b"

# write "output" to pypdf-output.pdf
with open(pdf_file_path, "wb") as output_stream:
writer.write(output_stream)


def test_add_uri(pdf_file_path):
reader = PdfReader(RESOURCE_ROOT / "pdflatex-outline.pdf")
writer = PdfWriter()
Expand Down