Skip to content

Commit

Permalink
Merge pull request #73 from e10v/dev
Browse files Browse the repository at this point in the history
Encode svg with base64
  • Loading branch information
e10v authored Jun 13, 2023
2 parents ef87729 + efefc4d commit 461e488
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 51 deletions.
25 changes: 9 additions & 16 deletions src/rico/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,22 +264,15 @@ def __init__(
"""
super().__init__(class_)

if format == "svg":
if isinstance(data, bytes):
data = data.decode()

for element in rico._html.parse_html(data):
self.container.append(element)
else:
if isinstance(data, str):
data = data.encode()
encoded_image = base64.b64encode(data).decode()

element = ET.Element(
"img",
attrib={"src": f"data:image/{format};base64,{encoded_image}"},
)
self.container.append(element)
if isinstance(data, str):
data = data.encode()
encoded_image = base64.b64encode(data).decode()

element = ET.Element(
"img",
attrib={"src": f"data:image/{format};base64,{encoded_image}"},
)
self.container.append(element)


class Chart(ContentBase):
Expand Down
52 changes: 17 additions & 35 deletions tests/test__content.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ def test_markdown_import_error():
def test_image_svg(data: str | bytes):
content = rico._content.Image(data, format="svg", class_="row")

if isinstance(data, str):
data = data.encode()
encoded_image = base64.b64encode(data).decode()

div = content.container
assert isinstance(div, ET.Element)
assert div.tag == "div"
Expand All @@ -325,30 +329,13 @@ def test_image_svg(data: str | bytes):
assert div.tail is None
assert len(div) == 1

svg = tuple(div)[0]
assert isinstance(svg, ET.Element)
assert svg.tag == "svg"
assert svg.attrib == {
"xmlns": "http://www.w3.org/2000/svg",
"xmlns:xlink": "http://www.w3.org/1999/xlink",
"width": "16",
"height": "16",
"fill": "currentColor",
"class": "bi bi-dash",
}
assert svg.text is None
assert svg.tail is None
assert len(svg) == 1

path = tuple(svg)[0]
assert isinstance(path, ET.Element)
assert path.tag == "path"
assert path.attrib == {
"d": "M4 8a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7A.5.5 0 0 1 4 8z",
}
assert path.text is None
assert path.tail is None
assert len(path) == 0
img = tuple(div)[0]
assert isinstance(img, ET.Element)
assert img.tag == "img"
assert img.attrib == {"src": f"data:image/svg;base64,{encoded_image}"}
assert img.text is None
assert img.tail is None
assert len(img) == 0


@pytest.mark.parametrize("data", [svg_data, svg_data.encode()], ids=["str", "bytes"])
Expand Down Expand Up @@ -408,14 +395,9 @@ def test_chart_complete(chart: Any, format: Literal["svg", "png"] | None): # no
assert div.tail is None
assert len(div) == 1

if format is None:
svg = tuple(div)[0]
assert isinstance(svg, ET.Element)
assert svg.tag == "svg"
else:
img = tuple(div)[0]
assert isinstance(img, ET.Element)
assert img.tag == "img"
img = tuple(div)[0]
assert isinstance(img, ET.Element)
assert img.tag == "img"


@pytest.mark.parametrize(
Expand Down Expand Up @@ -468,9 +450,9 @@ def _repr_html_(self) -> str:
assert p.tail is None
assert len(p) == 0

svg = tuple(div)[2]
assert isinstance(svg, ET.Element)
assert svg.tag == "svg"
img = tuple(div)[2]
assert isinstance(img, ET.Element)
assert img.tag == "img"


@pytest.mark.parametrize("defer", [True, False], ids=["defer", "not defer"])
Expand Down

0 comments on commit 461e488

Please sign in to comment.