From 251157944eefe20a927fdf664a1956792ad91b6f Mon Sep 17 00:00:00 2001 From: "Mohammed Y. Alnajdi" Date: Sat, 3 Oct 2020 20:41:59 +0300 Subject: [PATCH 1/6] added rtl flexbox test --- weasyprint/tests/test_layout/test_flex.py | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/weasyprint/tests/test_layout/test_flex.py b/weasyprint/tests/test_layout/test_flex.py index e0c3be1cd..674f8dfce 100644 --- a/weasyprint/tests/test_layout/test_flex.py +++ b/weasyprint/tests/test_layout/test_flex.py @@ -37,6 +37,31 @@ def test_flex_direction_row(): assert div_1.position_x < div_2.position_x < div_3.position_x +@assert_no_logs +def test_flex_direction_row_rtl(): + page, = render_pages(''' +
+
A
+
B
+
C
+
+ ''') + html, = page.children + body, = html.children + article, = body.children + div_1, div_2, div_3 = article.children + assert div_1.children[0].children[0].text == 'A' + assert div_2.children[0].children[0].text == 'B' + assert div_3.children[0].children[0].text == 'C' + assert ( + div_1.position_y == + div_2.position_y == + div_3.position_y == + article.position_y) + assert div_1.position_x == article.width - div_1.padding_width() + assert div_1.position_x > div_2.position_x > div_3.position_x + + @assert_no_logs def test_flex_direction_row_reverse(): page, = render_pages(''' @@ -64,6 +89,35 @@ def test_flex_direction_row_reverse(): assert div_1.position_x < div_2.position_x < div_3.position_x +@assert_no_logs +def test_flex_direction_row_reverse_rtl(): + page, = render_pages(''' +
+
A
+
B
+
C
+
+ ''') + html, = page.children + body, = html.children + article, = body.children + div_1, div_2, div_3 = article.children + assert div_1.children[0].children[0].text == 'C' + assert div_2.children[0].children[0].text == 'B' + assert div_3.children[0].children[0].text == 'A' + assert ( + div_1.position_y == + div_2.position_y == + div_3.position_y == + article.position_y) + # below assert needs to be fixed + assert ( + div_3.position_x + div_3.width == + article.position_x + article.width) + assert div_1.position_x > div_2.position_x > div_3.position_x + + @assert_no_logs def test_flex_direction_column(): page, = render_pages(''' From 1e35ab3c5e499146d7ada78b2bb427af4e4236af Mon Sep 17 00:00:00 2001 From: "Mohammed Y. Alnajdi" Date: Sat, 3 Oct 2020 20:42:36 +0300 Subject: [PATCH 2/6] flexbox: initial attempt to support flex with rtl --- weasyprint/layout/flex.py | 74 +++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/weasyprint/layout/flex.py b/weasyprint/layout/flex.py index 3d610a50f..5fc95dc63 100644 --- a/weasyprint/layout/flex.py +++ b/weasyprint/layout/flex.py @@ -650,33 +650,61 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block, child.margin_bottom = free_space free_space = 0 - if justify_content == 'flex-end': - position_axis += free_space - elif justify_content == 'center': - position_axis += free_space / 2 - elif justify_content == 'space-around': - position_axis += free_space / len(line) / 2 - elif justify_content == 'space-evenly': - position_axis += free_space / (len(line) + 1) + if box.style['direction'] == 'rtl': + if justify_content == 'flex-end': + position_axis -= free_space + elif justify_content == 'center': + position_axis -= free_space / 2 + elif justify_content == 'space-around': + position_axis -= free_space / len(line) / 2 + elif justify_content == 'space-evenly': + position_axis -= free_space / (len(line) - 1) - for i, child in line: - if axis == 'width': - child.position_x = position_axis - if justify_content == 'stretch': - child.width += free_space / len(line) - else: - child.position_y = position_axis - position_axis += ( - child.margin_width() if axis == 'width' - else child.margin_height()) - if justify_content == 'space-around': - position_axis += free_space / len(line) - elif justify_content == 'space-between': - if len(line) > 1: - position_axis += free_space / (len(line) - 1) + for i, child in line: + if axis == 'width': + child.position_x = position_axis + if justify_content == 'stretch': + child.width -= free_space / len(line) + else: + child.position_y = position_axis + position_axis -= ( + child.margin_width() if axis == 'width' + else child.margin_height()) + if justify_content == 'space-around': + position_axis -= free_space / len(line) + elif justify_content == 'space-between': + if len(line) > 1: + position_axis -= free_space / (len(line) + 1) + elif justify_content == 'space-evenly': + position_axis -= free_space / (len(line) - 1) + else: + if justify_content == 'flex-end': + position_axis += free_space + elif justify_content == 'center': + position_axis += free_space / 2 + elif justify_content == 'space-around': + position_axis += free_space / len(line) / 2 elif justify_content == 'space-evenly': position_axis += free_space / (len(line) + 1) + for i, child in line: + if axis == 'width': + child.position_x = position_axis + if justify_content == 'stretch': + child.width += free_space / len(line) + else: + child.position_y = position_axis + position_axis += ( + child.margin_width() if axis == 'width' + else child.margin_height()) + if justify_content == 'space-around': + position_axis += free_space / len(line) + elif justify_content == 'space-between': + if len(line) > 1: + position_axis += free_space / (len(line) - 1) + elif justify_content == 'space-evenly': + position_axis += free_space / (len(line) + 1) + # Step 13 position_cross = ( box.content_box_y() if cross == 'height' From ecb97b18547a87d99f6ce6aadfe02114e48e5141 Mon Sep 17 00:00:00 2001 From: "Mohammed Y. Alnajdi" Date: Sat, 10 Oct 2020 21:45:45 +0300 Subject: [PATCH 3/6] Kozea/WeasyPrint#601: handle flex-direction column --- weasyprint/layout/flex.py | 51 ++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/weasyprint/layout/flex.py b/weasyprint/layout/flex.py index 5fc95dc63..0a58c2782 100644 --- a/weasyprint/layout/flex.py +++ b/weasyprint/layout/flex.py @@ -650,33 +650,34 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block, child.margin_bottom = free_space free_space = 0 - if box.style['direction'] == 'rtl': - if justify_content == 'flex-end': - position_axis -= free_space - elif justify_content == 'center': - position_axis -= free_space / 2 - elif justify_content == 'space-around': - position_axis -= free_space / len(line) / 2 - elif justify_content == 'space-evenly': - position_axis -= free_space / (len(line) - 1) - - for i, child in line: - if axis == 'width': - child.position_x = position_axis - if justify_content == 'stretch': - child.width -= free_space / len(line) - else: - child.position_y = position_axis - position_axis -= ( - child.margin_width() if axis == 'width' - else child.margin_height()) - if justify_content == 'space-around': - position_axis -= free_space / len(line) - elif justify_content == 'space-between': - if len(line) > 1: - position_axis -= free_space / (len(line) + 1) + if (box.style['direction'] == 'rtl' and + box.style['flex_direction'].startswith('row')): + if justify_content == 'flex-end': + position_axis -= free_space + elif justify_content == 'center': + position_axis -= free_space / 2 + elif justify_content == 'space-around': + position_axis -= free_space / len(line) / 2 elif justify_content == 'space-evenly': position_axis -= free_space / (len(line) - 1) + + for i, child in line: + if axis == 'width': + child.position_x = position_axis + if justify_content == 'stretch': + child.width -= free_space / len(line) + else: + child.position_y = position_axis + position_axis -= ( + child.margin_width() if axis == 'width' + else child.margin_height()) + if justify_content == 'space-around': + position_axis -= free_space / len(line) + elif justify_content == 'space-between': + if len(line) > 1: + position_axis -= free_space / (len(line) + 1) + elif justify_content == 'space-evenly': + position_axis -= free_space / (len(line) - 1) else: if justify_content == 'flex-end': position_axis += free_space From 2a01aefeb58cbd6ce15d7056150a4bb0b207c0a0 Mon Sep 17 00:00:00 2001 From: "Mohammed Y. Alnajdi" Date: Sat, 10 Oct 2020 22:04:49 +0300 Subject: [PATCH 4/6] Kozea/WeasyPrint#601: add flex rtl tests row & col --- weasyprint/tests/test_layout/test_flex.py | 63 +++++++++++++++++++++-- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/weasyprint/tests/test_layout/test_flex.py b/weasyprint/tests/test_layout/test_flex.py index 674f8dfce..18e75bc5b 100644 --- a/weasyprint/tests/test_layout/test_flex.py +++ b/weasyprint/tests/test_layout/test_flex.py @@ -58,7 +58,9 @@ def test_flex_direction_row_rtl(): div_2.position_y == div_3.position_y == article.position_y) - assert div_1.position_x == article.width - div_1.padding_width() + assert ( + div_1.position_x + div_1.width == + article.position_x + article.width) assert div_1.position_x > div_2.position_x > div_3.position_x @@ -111,10 +113,7 @@ def test_flex_direction_row_reverse_rtl(): div_2.position_y == div_3.position_y == article.position_y) - # below assert needs to be fixed - assert ( - div_3.position_x + div_3.width == - article.position_x + article.width) + assert div_3.position_x == article.position_x assert div_1.position_x > div_2.position_x > div_3.position_x @@ -143,6 +142,32 @@ def test_flex_direction_column(): assert div_1.position_y < div_2.position_y < div_3.position_y +@assert_no_logs +def test_flex_direction_column_rtl(): + page, = render_pages(''' +
+
A
+
B
+
C
+
+ ''') + html, = page.children + body, = html.children + article, = body.children + div_1, div_2, div_3 = article.children + assert div_1.children[0].children[0].text == 'A' + assert div_2.children[0].children[0].text == 'B' + assert div_3.children[0].children[0].text == 'C' + assert ( + div_1.position_x == + div_2.position_x == + div_3.position_x == + article.position_x) + assert div_1.position_y == article.position_y + assert div_1.position_y < div_2.position_y < div_3.position_y + + @assert_no_logs def test_flex_direction_column_reverse(): page, = render_pages(''' @@ -170,6 +195,34 @@ def test_flex_direction_column_reverse(): assert div_1.position_y < div_2.position_y < div_3.position_y +@assert_no_logs +def test_flex_direction_column_reverse_rtl(): + page, = render_pages(''' +
+
A
+
B
+
C
+
+ ''') + html, = page.children + body, = html.children + article, = body.children + div_1, div_2, div_3 = article.children + assert div_1.children[0].children[0].text == 'C' + assert div_2.children[0].children[0].text == 'B' + assert div_3.children[0].children[0].text == 'A' + assert ( + div_1.position_x == + div_2.position_x == + div_3.position_x == + article.position_x) + assert ( + div_3.position_y + div_3.height == + article.position_y + article.height) + assert div_1.position_y < div_2.position_y < div_3.position_y + + @assert_no_logs def test_flex_row_wrap(): page, = render_pages(''' From 0c2e7b23410aeac4c8ac15bd8abd3f558552ce56 Mon Sep 17 00:00:00 2001 From: "Mohammed Y. Alnajdi" Date: Sat, 10 Oct 2020 23:14:21 +0300 Subject: [PATCH 5/6] Kozea/WeasyPrint#601: remove TODO:handle RTL --- weasyprint/layout/flex.py | 1 - 1 file changed, 1 deletion(-) diff --git a/weasyprint/layout/flex.py b/weasyprint/layout/flex.py index 0a58c2782..717f99eee 100644 --- a/weasyprint/layout/flex.py +++ b/weasyprint/layout/flex.py @@ -593,7 +593,6 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block, # else: Cross size has been set by step 7 # Step 12 - # TODO: handle rtl original_position_axis = ( box.content_box_x() if axis == 'width' else box.content_box_y()) From a81a82dc4c5d46aec0a924287354356528bf6fb4 Mon Sep 17 00:00:00 2001 From: "Mohammed Y. Alnajdi" Date: Sun, 11 Oct 2020 08:49:57 +0300 Subject: [PATCH 6/6] Kozea/WeasyPrint#601: fix indentation --- weasyprint/layout/flex.py | 48 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/weasyprint/layout/flex.py b/weasyprint/layout/flex.py index 717f99eee..7a6579321 100644 --- a/weasyprint/layout/flex.py +++ b/weasyprint/layout/flex.py @@ -651,32 +651,32 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block, if (box.style['direction'] == 'rtl' and box.style['flex_direction'].startswith('row')): - if justify_content == 'flex-end': - position_axis -= free_space - elif justify_content == 'center': - position_axis -= free_space / 2 - elif justify_content == 'space-around': - position_axis -= free_space / len(line) / 2 + if justify_content == 'flex-end': + position_axis -= free_space + elif justify_content == 'center': + position_axis -= free_space / 2 + elif justify_content == 'space-around': + position_axis -= free_space / len(line) / 2 + elif justify_content == 'space-evenly': + position_axis -= free_space / (len(line) - 1) + + for i, child in line: + if axis == 'width': + child.position_x = position_axis + if justify_content == 'stretch': + child.width -= free_space / len(line) + else: + child.position_y = position_axis + position_axis -= ( + child.margin_width() if axis == 'width' + else child.margin_height()) + if justify_content == 'space-around': + position_axis -= free_space / len(line) + elif justify_content == 'space-between': + if len(line) > 1: + position_axis -= free_space / (len(line) + 1) elif justify_content == 'space-evenly': position_axis -= free_space / (len(line) - 1) - - for i, child in line: - if axis == 'width': - child.position_x = position_axis - if justify_content == 'stretch': - child.width -= free_space / len(line) - else: - child.position_y = position_axis - position_axis -= ( - child.margin_width() if axis == 'width' - else child.margin_height()) - if justify_content == 'space-around': - position_axis -= free_space / len(line) - elif justify_content == 'space-between': - if len(line) > 1: - position_axis -= free_space / (len(line) + 1) - elif justify_content == 'space-evenly': - position_axis -= free_space / (len(line) - 1) else: if justify_content == 'flex-end': position_axis += free_space