Skip to content

Commit

Permalink
models: SVG: tree parse to support netsted groups
Browse files Browse the repository at this point in the history
Instead of looking only at direct childs of a node,
search the complete tree starting at the node.

This is particularly useful when we add fancy attributes,
since they tend to add encompassing groups to the graph.

It is pretty easy to test for the bug and how it can be reproduced.
It's tracked in: hbmartin#45

I re-created a minimal fail case from
hbmartin#45 (comment)

```
digraph {
    Pitas
    Pizza [ tooltip=Delicious ]
    Pitas -> Pizza
}
```

Previous HEAD failed to parse this.
  • Loading branch information
laurierloi committed Sep 14, 2022
1 parent 7519935 commit f384e3a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions graphviz2drawio/models/SVG.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
def svg_tag(tag):
return "{http://www.w3.org/2000/svg}" + tag


def get_first(g, tag):
return g.findall("./{http://www.w3.org/2000/svg}" + tag)[0]
target = svg_tag(tag)
for i in g.iter():
if i.tag == target:
return i
raise RuntimeError(f"Failed to find tag ({tag}) in {g}, contains {[i for i in get_all(g)]}")


def get_title(g):
return get_first(g, "title").text


def is_tag(g, tag):
return g.tag == "{http://www.w3.org/2000/svg}" + tag
return g.tag == svg_tag(tag)


def has(g, tag):
return len(g.findall("./{http://www.w3.org/2000/svg}" + tag)) > 0
try:
get_first(g, tag)
except RuntimeError:
return False
else:
return True

0 comments on commit f384e3a

Please sign in to comment.