diff --git a/tests/test_pdf.py b/tests/test_pdf.py index 40597006..ac103c87 100644 --- a/tests/test_pdf.py +++ b/tests/test_pdf.py @@ -334,6 +334,18 @@ def test_links(): assert rects.pop(0) == [0, TOP, RIGHT, TOP - 30] +@assert_no_logs +def test_sorted_links(): + # Regression test for https://github.com/Kozea/WeasyPrint/issues/1352 + pdf = FakeHTML(string=''' +

zzz

+

aaa

+ z + a + ''', base_url=resource_filename('')).write_pdf() + assert b'(zzz) [' in pdf.split(b'(aaa) [')[-1] + + @assert_no_logs def test_relative_links_no_height(): # 100% wide (block), 0pt high diff --git a/weasyprint/document.py b/weasyprint/document.py index e0e34d57..3dd1717a 100644 --- a/weasyprint/document.py +++ b/weasyprint/document.py @@ -469,7 +469,8 @@ def add_hyperlinks(links, anchors, matrix, pdf, page, names): page['Annots'] = pydyf.Array() page['Annots'].append(annot.reference) - for anchor in anchors: + # Anchors are name trees that have to be sorted + for anchor in sorted(anchors): anchor_name, x, y = anchor x, y = matrix.transform_point(x, y) names.append(pydyf.String(anchor_name))