Skip to content

Agent skill that turns natural language, code, Terraform/K8s, SQL, OpenAPI, AsyncAPI, Protobuf and GraphQL sources into editable, tested draw.io architecture diagrams: incremental sync, multi-view projection, drift diff, CI architecture tests, whiteboard derasterize, interactive HTML/PPTX/Mermaid exports.

fix(autolayout): escape node ids/edges in DOT and style in mxCell (#38)

Agents365.ai43bc052e26c2
diff --git a/skills/drawio-skill/scripts/autolayout.py b/skills/drawio-skill/scripts/autolayout.py
index 1fd6846..47ce4dc 100644
--- a/skills/drawio-skill/scripts/autolayout.py
+++ b/skills/drawio-skill/scripts/autolayout.py
@@ -73,6 +73,12 @@ def attr(value):
return escape(str(value), {'"': """})
+def dot_quote(value):
+ # Wrap as a DOT double-quoted string, escaping backslash and quote so ids
+ # with those characters can't corrupt the Graphviz input.
+ return '"' + str(value).replace("\\", "\\\\").replace('"', '\\"') + '"'
+
+
def snap(value, grid=10):
# Align to the grid the skill uses everywhere (multiples of 10).
return int(round(value / grid) * grid)
@@ -122,7 +128,7 @@ def build_dot(graph):
lines.append(f'{pad}subgraph cluster_{cidx[p]} {{ margin={GROUP_PAD};')
for c in children.get(p, []):
emit_cluster(c, pad + " ")
- lines.extend(f'{pad} "{m}";' for m in direct.get(p, []))
+ lines.extend(f'{pad} {dot_quote(m)};' for m in direct.get(p, []))
lines.append(pad + "}")
for root in [p for p in ordered if len(p) == 1]:
@@ -131,9 +137,9 @@ def build_dot(graph):
# Pass our pixel sizes to dot as inches so it lays out at the real size.
w = node.get("width", DEFAULT_W) / 72.0
h = node.get("height", DEFAULT_H) / 72.0
- lines.append(f'"{node["id"]}" [width={w:.4f} height={h:.4f}];')
+ lines.append(f'{dot_quote(node["id"])} [width={w:.4f} height={h:.4f}];')
for edge in graph.get("edges", []):
- lines.append(f'"{edge["source"]}" -> "{edge["target"]}";')
+ lines.append(f'{dot_quote(edge["source"])} -> {dot_quote(edge["target"])};')
lines.append("}")
return "\n".join(lines)
@@ -274,7 +280,7 @@ def to_drawio(graph, height, pos, edge_pts, color=True):
style = NODE_STYLE
cells.append(
f' <mxCell id="{attr(nid)}" value="{attr(node.get("label", nid))}" '
- f'style="{style}" vertex="1" parent="{attr(parent)}">\n'
+ f'style="{attr(style)}" vertex="1" parent="{attr(parent)}">\n'
f' <mxGeometry x="{x}" y="{y}" width="{w}" height="{h}" as="geometry"/>\n'
f" </mxCell>"
)
diff --git a/tests/test_scripts.py b/tests/test_scripts.py
index 9c21d3d..2939f8d 100644
--- a/tests/test_scripts.py
+++ b/tests/test_scripts.py
@@ -157,6 +157,23 @@ class TestAutolayoutColor(unittest.TestCase):
self.assertNotIn("fillColor=#d5e8d4", out) # no group tint
self.assertIn("fillColor=#ff0000", out) # explicit style still wins
+ def test_escapes_special_chars(self):
+ # Ids/edges with a quote or backslash must be escaped in the DOT input
+ # (else they corrupt the Graphviz source); a style with a quote must be
+ # XML-escaped in the emitted mxCell.
+ dot = self.m.build_dot({
+ "nodes": [{"id": 'a"x'}, {"id": "b\\y"}],
+ "edges": [{"source": 'a"x', "target": "b\\y"}],
+ })
+ self.assertIn(r'"a\"x"', dot)
+ self.assertIn(r'"b\\y"', dot)
+ out = self.m.to_drawio(
+ {"direction": "TB",
+ "nodes": [{"id": "n", "label": "L", "style": 'fillColor="#fff";'}],
+ "edges": []},
+ 5, {"n": (1, 1)}, {}, color=True)
+ self.assertIn("&quot;", out)
+
class TestValidateCli(unittest.TestCase):
GOOD = ('<mxfile><diagram name="P1"><mxGraphModel><root>'