Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overflow-wrap, support word-break: break-all #1534

Merged
merged 2 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,42 @@ def test_overflow_wrap(wrap, text, test, full_text):
assert full_text == lines_full_text


@assert_no_logs
@pytest.mark.parametrize('span_css, expected_lines', (
# overflow-wrap: anywhere and break-word are only allowed to break a word
# "if there are no otherwise-acceptable break points in the line", which
# means they should not split a word if it fits cleanly into the next line.
# This can be done accidentally if it is in its own inline element.
('overflow-wrap: anywhere', ['aaa', 'bbb']),
('overflow-wrap: break-word', ['aaa', 'bbb']),

# On the other hand, word-break: break-all mandates a break anywhere at the
# end of a line, even if the word could fit cleanly onto the next line.
('word-break: break-all', ['aaa b', 'bb']),
))
def test_wrap_overflow_word_break(span_css, expected_lines):
page, = render_pages('''
<style>
@font-face {src: url(weasyprint.otf); font-family: weasyprint}
body {width: 80px; overflow: hidden; font-family: weasyprint}
span {%s}
</style>
<body>
<span>aaa </span><span>bbb
''' % span_css)
html, = page.children
body, = html.children
lines = body.children
lines = []
print(body.children)
for line in body.children:
line_text = ''
for span_box in line.children:
line_text += span_box.children[0].text
lines.append(line_text)
assert lines == expected_lines


@assert_no_logs
@pytest.mark.parametrize('wrap, text, body_width, expected_width', (
('anywhere', 'aaaaaa', 10, 20),
Expand Down
2 changes: 2 additions & 0 deletions weasyprint/css/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
'text_indent': Dimension(0, 'px'),
'text_transform': 'none',
'white_space': 'normal',
'word_break': 'normal',
'word_spacing': 0, # computed value for 'normal'

# Transforms 1 (CR): https://www.w3.org/TR/css-transforms-1/
Expand Down Expand Up @@ -270,6 +271,7 @@
'visibility',
'white_space',
'widows',
'word_break',
'word_spacing',
}

Expand Down
7 changes: 7 additions & 0 deletions weasyprint/css/validation/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,13 @@ def overflow_wrap(keyword):
return keyword in ('anywhere', 'normal', 'break-word')


@property()
@single_keyword
def word_break(keyword):
"""``word-break`` property validation."""
return keyword in ('normal', 'break-all')


@property()
@single_token
def flex_basis(token):
Expand Down
9 changes: 6 additions & 3 deletions weasyprint/layout/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,10 @@ def split_inline_level(context, box, position_x, max_x, bottom_space,
skip = skip or 0
assert skip_stack is None

is_line_start = len(line_children) == 0
new_box, skip, preserved_line_break = split_text_box(
context, box, max_x - position_x, skip)
context, box, max_x - position_x, skip,
is_line_start=is_line_start)

if skip is None:
resume_at = None
Expand Down Expand Up @@ -882,7 +884,7 @@ def split_inline_box(context, box, position_x, max_x, bottom_space, skip_stack,
float_widths)


def split_text_box(context, box, available_width, skip):
def split_text_box(context, box, available_width, skip, is_line_start=True):
"""Keep as much text as possible from a TextBox in a limited width.

Try not to overflow but always have some text in ``new_box``.
Expand All @@ -900,7 +902,8 @@ def split_text_box(context, box, available_width, skip):
if font_size == 0 or not text:
return None, None, False
layout, length, resume_index, width, height, baseline = split_first_line(
text, box.style, context, available_width, box.justification_spacing)
text, box.style, context, available_width, box.justification_spacing,
is_line_start=is_line_start)
assert resume_index != 0

# Convert ``length`` and ``resume_at`` from UTF-8 indexes in text
Expand Down
2 changes: 1 addition & 1 deletion weasyprint/layout/preferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def inline_line_widths(context, box, outer, is_line_start, minimum,
split_first_line(
child_text[resume_index:], child.style, context,
max_width, child.justification_spacing,
minimum=True))
is_line_start=is_line_start, minimum=True))
lines.append(width)
if first_line:
break
Expand Down
10 changes: 7 additions & 3 deletions weasyprint/text/line_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def create_layout(text, style, context, max_width, justification_spacing):


def split_first_line(text, style, context, max_width, justification_spacing,
minimum=False):
is_line_start=True, minimum=False):
"""Fit as much as possible in the available width for one line of text.

Return ``(layout, length, resume_index, width, height, baseline)``.
Expand Down Expand Up @@ -545,8 +545,12 @@ def split_first_line(text, style, context, max_width, justification_spacing,
first_line_width, _ = line_size(first_line, style)
space = max_width - first_line_width
# If we can break words and the first line is too long
if space < 0 and (overflow_wrap == 'anywhere' or
(overflow_wrap == 'break-word' and not minimum)):
if space < 0 and ((style['word_break'] == 'break-all') or
(is_line_start and
(
overflow_wrap == 'anywhere' or
(overflow_wrap == 'break-word' and not minimum)
))):
# Is it really OK to remove hyphenation for word-break ?
hyphenated = False
# TODO: Modify code to preserve W3C condition:
Expand Down