Skip to content

Commit

Permalink
Process whitespaces as soon as box children are set
Browse files Browse the repository at this point in the history
This early process enables properties with <content-list> values (string-set,
bookmark-label, etc.) to be defined using whitespace-processed strings.

Fix #607.
  • Loading branch information
liZe committed Oct 13, 2021
1 parent 8ff7ed9 commit e5beff6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
10 changes: 4 additions & 6 deletions tests/test_boxes.py
Expand Up @@ -64,7 +64,7 @@ def test_inline_in_block_1():
('div', 'Text', 'Hello, '),
('em', 'Inline', [
('em', 'Text', 'World')]),
('div', 'Text', '!\n')])]),
('div', 'Text', '! ')])]),
('p', 'Block', [
('p', 'Line', [
('p', 'Text', 'Lipsum.')])])])]
Expand All @@ -86,7 +86,7 @@ def test_inline_in_block_2():
('div', 'Text', 'Hello, '),
('em', 'Inline', [
('em', 'Text', 'World')]),
('div', 'Text', '!\n')])])])]
('div', 'Text', '! ')])])])]
box = parse(source)
box = build.inline_in_block(box)
assert_tree(box, expected)
Expand Down Expand Up @@ -151,8 +151,7 @@ def test_block_in_inline():
('span', 'Block', [ # This block is "pulled up"
('span', 'Line', [
('span', 'Text', 'sit')])]),
# No whitespace processing here.
('strong', 'Text', '\n '),
('strong', 'Text', ' '),
('span', 'Block', [ # This block is "pulled up"
('span', 'Line', [
('span', 'Text', 'amet,')])])]),
Expand Down Expand Up @@ -180,8 +179,7 @@ def test_block_in_inline():
('p', 'Line', [
('em', 'Inline', [
('strong', 'Inline', [
# Whitespace processing not done yet.
('strong', 'Text', '\n ')])])])]),
('strong', 'Text', ' ')])])])]),
('span', 'Block', [
('span', 'Line', [
('span', 'Text', 'amet,')])]),
Expand Down
19 changes: 19 additions & 0 deletions tests/test_pdf.py
Expand Up @@ -279,6 +279,25 @@ def test_bookmarks_13():
assert re.findall(b'/Title \\((.*)\\)', pdf) == [b'a']


@assert_no_logs
def test_bookmarks_14():
pdf = FakeHTML(string='''
<h1>a</h1>
<h1> b c d </h1>
<h1> e
f </h1>
<h1> g <span> h </span> i </h1>
''').write_pdf()
# a
# |_ b
# |_ c
# L_ d
# e
assert re.findall(b'/Count ([0-9-]*)', pdf)[-1] == b'4'
assert re.findall(b'/Title \\((.*)\\)', pdf) == [
b'a', b'b c d', b'e f', b'g h i']


@assert_no_logs
def test_links_none():
pdf = FakeHTML(string='<body>').write_pdf()
Expand Down
2 changes: 1 addition & 1 deletion weasyprint/formatting_structure/build.py
Expand Up @@ -72,7 +72,6 @@ def root_style_for(element, pseudo_type=None):

box.is_for_root_element = True
# If this is changed, maybe update weasy.layout.page.make_margin_boxes()
process_whitespace(box)
box = anonymous_table_boxes(box)
box = flex_boxes(box)
box = inline_in_block(box)
Expand Down Expand Up @@ -190,6 +189,7 @@ def element_to_box(element, style_for, get_image_from_uri, base_url,
counter_values.pop(name)

box.children = children
process_whitespace(box)
# calculate string-set and bookmark-label
set_content_lists(
element, box, style, counter_values, target_collector, counter_style)
Expand Down

0 comments on commit e5beff6

Please sign in to comment.