Skip to content

Commit

Permalink
Don’t crash when line can be split before trailing spaces
Browse files Browse the repository at this point in the history
We normally don’t break lines before spaces, but some characters (such as line
separators) can force line breaks anywhere.

Fix #1852.
  • Loading branch information
liZe committed Apr 7, 2023
1 parent 74823d3 commit c550a55
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 13 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,19 @@ def test_overflow_wrap_trailing_space(wrap, text, body_width, expected_width):
assert td.width == expected_width


def test_line_break_before_trailing_space():
# Test regression: https://github.com/Kozea/WeasyPrint/issues/1852
page, = render_pages('''
<p style="display: inline-block">test\u2028 </p>a
<p style="display: inline-block">test\u2028</p>a
''')
html, = page.children
body, = html.children
line, = body.children
p1, space1, p2, space2 = line.children
assert p1.width == p2.width


def white_space_lines(width, space):
page, = render_pages('''
<style>
Expand Down
9 changes: 6 additions & 3 deletions weasyprint/layout/preferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,12 @@ def trailing_whitespace_size(context, box):
stripped_box = box.copy_with_text(stripped_text)
stripped_box, resume, _ = split_text_box(
context, stripped_box, None, old_resume)
assert stripped_box is not None
assert resume is None
return old_box.width - stripped_box.width
if stripped_box is None:
# old_box split just before the trailing spaces
return old_box.width
else:
assert resume is None
return old_box.width - stripped_box.width
else:
_, _, _, width, _, _ = split_first_line(
box.text, box.style, context, None, box.justification_spacing)
Expand Down

0 comments on commit c550a55

Please sign in to comment.