Skip to content

Commit

Permalink
Fixing special case with SVG empty attribute values
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Aug 5, 2022
1 parent 0d4cb26 commit 34ddf55
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
27 changes: 10 additions & 17 deletions fpdf/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,28 +315,21 @@ def apply_styles(stylable, svg_element):

stylable.style.auto_close = False

for svg_attr, converter in svg_attr_map.items():
try:
attr, value = converter(svg_element.attrib[svg_attr])
except KeyError:
pass
else:
setattr(stylable.style, attr, value)
for attr_name, converter in svg_attr_map.items():
value = svg_element.attrib.get(attr_name)
if value:
attr_name, value = converter(value)
setattr(stylable.style, attr_name, value)

# handle this separately for now
try:
opacity = float(svg_element.attrib["opacity"])
except KeyError:
pass
else:
opacity = svg_element.attrib.get("opacity")
if opacity:
opacity = float(opacity)
stylable.style.fill_opacity = opacity
stylable.style.stroke_opacity = opacity

try:
tfstr = svg_element.attrib["transform"]
except KeyError:
pass
else:
tfstr = svg_element.attrib.get("transform")
if tfstr:
stylable.transform = convert_transforms(tfstr)


Expand Down
6 changes: 6 additions & 0 deletions test/svg/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,12 @@ def Gs(**kwargs):
pytest.raises(ValueError),
id="stroke-opacity invalid",
),
pytest.param(
'<path stroke-dasharray="" opacity="" transform=""/>',
Gs(),
no_error(),
id="empty attribute values",
),
)

test_svg_sources = (
Expand Down

0 comments on commit 34ddf55

Please sign in to comment.