fix
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
"""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 = """
|
||||
<!DOCTYPE html><html><head><meta charset="utf-8"></head>
|
||||
<body>
|
||||
<table width="100%">
|
||||
<tr>
|
||||
<td width="40%">
|
||||
<img src="https://example.com/foto.jpg"
|
||||
style="width: 100%; max-width: 300px; height: auto; display: block;">
|
||||
</td>
|
||||
<td>Popis produktu</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body></html>
|
||||
"""
|
||||
|
||||
FREE_IMAGE = """
|
||||
<!DOCTYPE html><html><head><meta charset="utf-8"></head>
|
||||
<body>
|
||||
<div><img src="https://example.com/foto.jpg" style="width: 100%; max-width: 300px;"></div>
|
||||
</body></html>
|
||||
"""
|
||||
|
||||
WIDTH_ATTRIBUTE = """
|
||||
<!DOCTYPE html><html><head><meta charset="utf-8"></head>
|
||||
<body><table><tr><td><img src="https://example.com/foto.jpg" width="100%"></td></tr></table></body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
|
||||
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 "")
|
||||
Reference in New Issue
Block a user