Skip to content

Commit

Permalink
Use UTF8 indices instead of unicode indices for line split
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed May 29, 2023
1 parent 21810ba commit 0a03e3d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
28 changes: 28 additions & 0 deletions tests/layout/test_inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,34 @@ def test_breaking_linebox_regression_14():
assert line2.children[1].children[0].text == 'c'


@assert_no_logs
def test_breaking_linebox_regression_15():
# Regression test for https://github.com/ietf-tools/datatracker/issues/5507
page, = render_pages(
'<style>'
' @font-face {src: url(weasyprint.otf); font-family: weasyprint}'
' body {font-family: weasyprint; font-size: 4px}'
' pre {float: left}'
'</style>'
'<pre>ab©\n'
'déf\n'
'ghïj\n'
'klm</pre>')
html, = page.children
body, = html.children
pre, = body.children
line1, line2, line3, line4 = pre.children
assert line1.children[0].text == 'ab©'
assert line2.children[0].text == 'déf'
assert line3.children[0].text == 'ghïj'
assert line4.children[0].text == 'klm'
assert line1.children[0].width == 4 * 3
assert line2.children[0].width == 4 * 3
assert line3.children[0].width == 4 * 4
assert line4.children[0].width == 4 * 3
assert pre.width == 4 * 4


@assert_no_logs
def test_linebox_text():
page, = render_pages('''
Expand Down
10 changes: 5 additions & 5 deletions weasyprint/layout/preferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ def inline_line_widths(context, box, outer, is_line_start, minimum,
else:
(skip, skip_stack), = skip_stack.items()
assert skip_stack is None
child_text = child.text[(skip or 0):]
child_text = child.text.encode()[(skip or 0):]
if is_line_start and space_collapse:
child_text = child_text.lstrip(' ')
if minimum and child_text == ' ':
child_text = child_text.lstrip(b' ')
if minimum and child_text == b' ':
lines = [0, 0]
else:
max_width = 0 if minimum else None
Expand All @@ -302,8 +302,8 @@ def inline_line_widths(context, box, outer, is_line_start, minimum,
resume_index += new_resume_index
_, _, new_resume_index, width, _, _ = (
split_first_line(
child_text[resume_index:], child.style, context,
max_width, child.justification_spacing,
child_text[resume_index:].decode(), child.style,
context, max_width, child.justification_spacing,
is_line_start=is_line_start, minimum=True))
lines.append(width)
if first_line:
Expand Down

0 comments on commit 0a03e3d

Please sign in to comment.