"""Images inside table cells.
WeasyPrint resolves a percentage width of an image in a table cell against a
width it does not know yet and the image comes out zero wide. It then vanishes
from the PDF without any error, so it is not even reported as a missing asset.
"""
from __future__ import annotations
import pytest
pytest.importorskip("lxml")
from app.pdf.document import SourceDocument # noqa: E402
CELL_IMAGE = """
|
Popis produktu |
"""
FREE_IMAGE = """
"""
WIDTH_ATTRIBUTE = """
"""
def _image_style(document: SourceDocument) -> str:
return document.tree.find(".//img").get("style") or ""
def test_percentage_width_in_a_cell_becomes_auto() -> None:
document = SourceDocument(CELL_IMAGE)
assert document.relax_percentage_image_widths() == 1
style = _image_style(document)
assert "width: auto" in style
# The cap the document asked for has to survive, it is what limits the size now.
assert "max-width: 300px" in style
assert "height: auto" in style
def test_image_outside_a_table_is_left_alone() -> None:
document = SourceDocument(FREE_IMAGE)
assert document.relax_percentage_image_widths() == 0
assert "width: 100%" in _image_style(document)
def test_percentage_width_attribute_is_dropped() -> None:
document = SourceDocument(WIDTH_ATTRIBUTE)
assert document.relax_percentage_image_widths() == 1
image = document.tree.find(".//img")
assert image.get("width") is None
assert "max-width: 100%" in (image.get("style") or "")