1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-04 07:57:52 +03:00
WeasyPrint/tests/test_stacking.py

78 lines
2.2 KiB
Python
Raw Normal View History

2022-03-25 13:47:27 +03:00
"""Test CSS stacking contexts."""
2018-03-15 02:40:06 +03:00
import pytest
2024-03-17 13:34:03 +03:00
2020-12-06 22:19:59 +03:00
from weasyprint.stacking import StackingContext
2018-03-15 02:40:06 +03:00
2021-04-19 18:15:53 +03:00
from .testing_utils import assert_no_logs, render_pages, serialize
2020-01-08 01:55:39 +03:00
z_index_source = '''
<style>
@page { size: 10px }
div, div * { width: 10px; height: 10px; position: absolute }
article { background: red; z-index: %s }
section { background: blue; z-index: %s }
nav { background: lime; z-index: %s }
</style>
<div>
<article></article>
<section></section>
<nav></nav>
</div>'''
def serialize_stacking(context):
return (
context.box.element_tag,
[b.element_tag for b in context.blocks_and_cells],
2018-03-15 02:40:06 +03:00
[serialize_stacking(c) for c in context.zero_z_contexts])
@assert_no_logs
2018-03-15 02:40:06 +03:00
@pytest.mark.parametrize('source, contexts', (
('''
<p id=lorem></p>
<div style="position: relative">
<p id=lipsum></p>
</div>''',
('html', ['body', 'p'], [('div', ['p'], [])])),
('''
<div style="position: relative">
<p style="position: relative"></p>
</div>''',
('html', ['body'], [('div', [], []), ('p', [], [])])),
))
def test_nested(source, contexts):
page, = render_pages(source)
html, = page.children
assert serialize_stacking(StackingContext.from_box(html, page)) == contexts
@assert_no_logs
def test_image_contexts():
2018-03-14 03:32:01 +03:00
page, = render_pages('''
2018-03-20 00:35:15 +03:00
<body>Some text: <img style="position: relative" src=pattern.png>''')
html, = page.children
context = StackingContext.from_box(html, page)
# The image is *not* in this context:
assert serialize([context.box]) == [
('html', 'Block', [
('body', 'Block', [
('body', 'Line', [
('body', 'Text', 'Some text: ')])])])]
# ... but in a sub-context:
assert serialize(c.box for c in context.zero_z_contexts) == [
('img', 'InlineReplaced', '<replaced>')]
2020-01-08 01:55:39 +03:00
@assert_no_logs
@pytest.mark.parametrize('z_indexes, color', (
((3, 2, 1), 'R'),
((1, 2, 3), 'G'),
((1, 2, -3), 'B'),
((1, 2, 'auto'), 'B'),
((-1, 'auto', -2), 'B'),
))
def test_z_index(assert_pixels, z_indexes, color):
assert_pixels('\n'.join([color * 10] * 10), z_index_source % z_indexes)