1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-05 00:21:15 +03:00

Fixed #2: Crash in PDF outlines with malformed bookmark-level sequence.

This commit is contained in:
Simon Sapin 2012-07-23 16:12:45 +02:00
parent 47e1289472
commit 30533879b2
2 changed files with 28 additions and 3 deletions

View File

@ -304,6 +304,11 @@ def process_bookmarks(raw_bookmarks):
root = {'Count': 0}
bookmark_list = []
# At one point in the document, for each "output" level (ie. depth in the
# PDF outline tree), how much to add to get the source level (CSS values
# of bookmark-level).
# Eg. with <h1> then <h3>, level_shifts == [0, 1]
# 1 means that <h3> has depth 3 - 1 = 2 in the output.
level_shifts = []
last_by_level = [root]
indices_by_level = [0]
@ -314,9 +319,12 @@ def process_bookmarks(raw_bookmarks):
if level > previous_level:
level_shifts.append(level - previous_level - 1)
else:
k = 0
while k < previous_level - level:
k += 1 + level_shifts.pop()
temp_level = level
while temp_level < previous_level:
temp_level += 1 + level_shifts.pop()
if temp_level > previous_level:
# The last popd value was too big
level_shifts.append(temp_level - previous_level - 1)
# Resolve level inconsistencies
level -= sum(level_shifts)

View File

@ -84,6 +84,23 @@ def test_bookmarks():
Warning: the PDF output of this structure is not tested.
"""
root, bookmarks = get_bookmarks('''
<h1>a</h1> #
<h4>b</h4> ####
<h3>c</h3> ###
<h2>d</h2> ##
<h1>e</h1> #
''', structure_only=True)
assert root == dict(Count=5, First=1, Last=5)
import pprint
pprint.pprint(bookmarks, width=150)
assert bookmarks == [
dict(Count=3, First=2, Last=4, Next=5, Parent=0, Prev=None),
dict(Count=0, First=None, Last=None, Next=3, Parent=1, Prev=None),
dict(Count=0, First=None, Last=None, Next=4, Parent=1, Prev=2),
dict(Count=0, First=None, Last=None, Next=None, Parent=1, Prev=3),
dict(Count=0, First=None, Last=None, Next=None, Parent=0, Prev=1)]
root, bookmarks = get_bookmarks('<body>')
assert root == dict(Count=0)
assert bookmarks == []