Skip to content

Commit

Permalink
Transform text-align into shorthand
Browse files Browse the repository at this point in the history
text-align is now a shorthand, setting text-align-all and text-align-last.

Related to #1389.
  • Loading branch information
liZe committed Aug 24, 2021
1 parent 412b589 commit bb29eca
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 18 deletions.
18 changes: 9 additions & 9 deletions tests/test_presentational_hints.py
Expand Up @@ -64,12 +64,12 @@ def test_ph_flow():
body, = html.children
pre, center, div1, div2, div3, div4, div5 = body.children
assert pre.style['white_space'] == 'pre-wrap'
assert center.style['text_align'] == 'center'
assert div1.style['text_align'] == 'center'
assert div2.style['text_align'] == 'center'
assert div3.style['text_align'] == 'left'
assert div4.style['text_align'] == 'right'
assert div5.style['text_align'] == 'justify'
assert center.style['text_align_all'] == 'center'
assert div1.style['text_align_all'] == 'center'
assert div2.style['text_align_all'] == 'center'
assert div3.style['text_align_all'] == 'left'
assert div4.style['text_align_all'] == 'right'
assert div5.style['text_align_all'] == 'justify'


@assert_no_logs
Expand Down Expand Up @@ -222,11 +222,11 @@ def test_ph_tables():
assert td.style['border_top_width'] == 1
assert td.style['border_top_style'] == 'inset'
h1, p = td.children
assert h1.style['text_align'] == 'right'
assert p.style['text_align'] == 'center'
assert h1.style['text_align_all'] == 'right'
assert p.style['text_align_all'] == 'center'
foot, = foot_group.children
tr, = foot.children
assert tr.style['text_align'] == 'justify'
assert tr.style['text_align_all'] == 'justify'


@assert_no_logs
Expand Down
80 changes: 80 additions & 0 deletions tests/test_text.py
Expand Up @@ -244,6 +244,86 @@ def test_text_align_justify():
assert image_5.position_x == 0


@assert_no_logs
def test_text_align_justify_all():
page, = render_pages('''
<style>
@page { size: 300px 1000px }
body { text-align: justify-all }
</style>
<p><img src="pattern.png" style="width: 40px">
<strong>
<img src="pattern.png" style="width: 60px">
<img src="pattern.png" style="width: 10px">
<img src="pattern.png" style="width: 100px"
></strong><img src="pattern.png" style="width: 200px">
<img src="pattern.png" style="width: 10px">''')
html, = page.children
body, = html.children
paragraph, = body.children
line_1, line_2 = paragraph.children
image_1, space_1, strong = line_1.children
image_2, space_2, image_3, space_3, image_4 = strong.children
image_5, space_4, image_6 = line_2.children
assert space_1.text == ' '
assert space_2.text == ' '
assert space_3.text == ' '
assert space_4.text == ' '

assert image_1.position_x == 0
assert space_1.position_x == 40
assert strong.position_x == 70
assert image_2.position_x == 70
assert space_2.position_x == 130
assert image_3.position_x == 160
assert space_3.position_x == 170
assert image_4.position_x == 200
assert strong.width == 230

assert image_5.position_x == 0
assert space_4.position_x == 200
assert image_6.position_x == 290


@assert_no_logs
def test_text_align_all_last():
page, = render_pages('''
<style>
@page { size: 300px 1000px }
body { text-align-all: justify; text-align-last: right }
</style>
<p><img src="pattern.png" style="width: 40px">
<strong>
<img src="pattern.png" style="width: 60px">
<img src="pattern.png" style="width: 10px">
<img src="pattern.png" style="width: 100px"
></strong><img src="pattern.png" style="width: 200px"
><img src="pattern.png" style="width: 10px">''')
html, = page.children
body, = html.children
paragraph, = body.children
line_1, line_2 = paragraph.children
image_1, space_1, strong = line_1.children
image_2, space_2, image_3, space_3, image_4 = strong.children
image_5, image_6 = line_2.children
assert space_1.text == ' '
assert space_2.text == ' '
assert space_3.text == ' '

assert image_1.position_x == 0
assert space_1.position_x == 40
assert strong.position_x == 70
assert image_2.position_x == 70
assert space_2.position_x == 130
assert image_3.position_x == 160
assert space_3.position_x == 170
assert image_4.position_x == 200
assert strong.width == 230

assert image_5.position_x == 90
assert image_6.position_x == 290


@assert_no_logs
def test_text_align_not_enough_space():
page, = render_pages('''
Expand Down
4 changes: 2 additions & 2 deletions weasyprint/css/properties.py
Expand Up @@ -147,7 +147,7 @@
'hyphens': 'manual',
'letter_spacing': 'normal',
'tab_size': 8,
'text_align': 'start',
'text_align_all': 'start',
'text_align_last': 'auto',
'text_indent': Dimension(0, 'px'),
'text_transform': 'none',
Expand Down Expand Up @@ -258,7 +258,7 @@
'overflow_wrap',
'quotes',
'tab_size',
'text_align',
'text_align_all',
'text_align_last',
'text_decoration_line',
'text_decoration_color',
Expand Down
13 changes: 13 additions & 0 deletions weasyprint/css/validation/expanders.py
Expand Up @@ -639,3 +639,16 @@ def expand_line_clamp(base_url, name, tokens):
raise InvalidValues
else:
raise InvalidValues


@expander('text-align')
def expand_text_align(base_url, name, tokens):
"""Expand the ``text-align`` property."""
if len(tokens) == 1:
keyword = get_single_keyword(tokens)
align_all = 'justify' if keyword == 'justify-all' else keyword
yield 'text_align_all', align_all
align_last = 'start' if keyword == 'justify' else align_all
yield 'text_align_last', align_last
else:
raise InvalidValues
4 changes: 2 additions & 2 deletions weasyprint/css/validation/properties.py
Expand Up @@ -1096,8 +1096,8 @@ def table_layout(keyword):

@property()
@single_keyword
def text_align(keyword):
"""``text-align`` property validation."""
def text_align_all(keyword):
"""``text-align-all`` property validation."""
return keyword in ('left', 'right', 'center', 'justify', 'start', 'end')


Expand Down
7 changes: 2 additions & 5 deletions weasyprint/layout/inlines.py
Expand Up @@ -1315,13 +1315,10 @@ def text_align(context, line, available_width, last):
if line.width >= available_width:
return 0

align = line.style['text_align']
align = line.style['text_align_all']
if last:
align_last = line.style['text_align_last']
if align_last != 'auto':
align = align_last
elif align == 'justify':
align = 'start'
align = align if align_last == 'auto' else align_last
space_collapse = line.style['white_space'] in (
'normal', 'nowrap', 'pre-line')
if align in ('left', 'right'):
Expand Down

0 comments on commit bb29eca

Please sign in to comment.