Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made picosvg ignore <?xpacket?> tags. #288

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist/
.eggs/
src/picosvg/_version.py
.tox/
.*.swp
26 changes: 22 additions & 4 deletions src/picosvg/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ def _opacity(el: etree.Element) -> float:
return _clamp(float(el.attrib.get("opacity", 1.0)))


def _is_redundant(tag):
return tag is etree.Comment or tag is etree.ProcessingInstruction


def _is_removable_group(el):
"""
Groups with:
Expand All @@ -201,7 +205,7 @@ def _is_removable_group(el):
# no attributes makes a group meaningless
if len(el.attrib) == 0:
return True
num_children = sum(1 for e in el if e.tag is not etree.Comment)
num_children = sum(1 for e in el if not _is_redundant(e.tag))

return num_children <= 1 or _opacity(el) in {0.0, 1.0}

Expand All @@ -224,7 +228,7 @@ def _try_remove_group(group_el, push_opacity=True):
_replace_el(group_el, list(group_el))
if push_opacity:
for child in children:
if child.tag is etree.Comment:
if _is_redundant(child.tag):
continue
_inherit_attrib({"opacity": opacity}, child)
else:
Expand Down Expand Up @@ -604,7 +608,7 @@ def _traverse(self, next_fn, append_fn, resolve_clip_paths=True):
child_idxs = defaultdict(int)
new_entries = []
for child in context.element:
if child.tag is etree.Comment:
if _is_redundant(child.tag):
continue
transform = _element_transform(child, context.transform)
clips = context.clips
Expand Down Expand Up @@ -982,6 +986,19 @@ def remove_nonsvg_content(self, inplace=False):

return self

def remove_processing_instructions(self, inplace=False):
if not inplace:
svg = SVG(copy.deepcopy(self.svg_root))
svg.remove_processing_instructions(inplace=True)
return svg

self._update_etree()

for el in self.xpath("//processing-instruction()"):
el.getparent().remove(el)

return self

def remove_comments(self, inplace=False):
if not inplace:
svg = SVG(copy.deepcopy(self.svg_root))
Expand Down Expand Up @@ -1073,7 +1090,7 @@ def _iter_nested_svgs(
frontier = deque(root)
while frontier:
el = frontier.popleft()
if el.tag is etree.Comment:
if _is_redundant(el.tag):
continue
if strip_ns(el.tag) == "svg":
yield el
Expand Down Expand Up @@ -1317,6 +1334,7 @@ def topicosvg(self, *, ndigits=3, inplace=False):
# Discard useless content
self.remove_nonsvg_content(inplace=True)
self.remove_comments(inplace=True)
self.remove_processing_instructions(inplace=True)
self.remove_anonymous_symbols(inplace=True)
self.remove_title_meta_desc(inplace=True)

Expand Down
7 changes: 7 additions & 0 deletions tests/svg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,10 @@ def test_topicosvg_ndigits(inplace):
'<path d="M60.5,30 L100.1,30 L100.1,70 L60.5,70 Z"/>'
"</svg>"
)


def test_xpacket():
xpacket_svg = load_test_svg("xpacket.svg")
assert "xpacket" in xpacket_svg.tostring()
pico_svg = xpacket_svg.topicosvg()
assert "xpacket" not in pico_svg.tostring()
12 changes: 12 additions & 0 deletions tests/xpacket.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.