diff --git a/dag/marshal.go b/dag/marshal.go index ebb8a0a63..0ad45e8cb 100644 --- a/dag/marshal.go +++ b/dag/marshal.go @@ -108,9 +108,14 @@ func newMarshalVertex(v Vertex) *marshalVertex { dn = nil } + // the name will be quoted again later, so we need to ensure it's properly + // escaped without quotes. + name := strconv.Quote(VertexName(v)) + name = name[1 : len(name)-1] + return &marshalVertex{ ID: marshalVertexID(v), - Name: VertexName(v), + Name: name, Attrs: make(map[string]string), graphNodeDotter: dn, } diff --git a/dag/marshal_test.go b/dag/marshal_test.go index a7e468dc1..79f1c648e 100644 --- a/dag/marshal_test.go +++ b/dag/marshal_test.go @@ -32,6 +32,21 @@ func TestGraphDot_basic(t *testing.T) { } } +func TestGraphDot_quoted(t *testing.T) { + var g Graph + quoted := `name["with-quotes"]` + other := `other` + g.Add(quoted) + g.Add(other) + g.Connect(BasicEdge(quoted, other)) + + actual := strings.TrimSpace(string(g.Dot(nil))) + expected := strings.TrimSpace(testGraphDotQuotedStr) + if actual != expected { + t.Fatalf("\ngot: %q\nwanted %q\n", actual, expected) + } +} + func TestGraphDot_attrs(t *testing.T) { var g Graph g.Add(&testGraphNodeDotter{ @@ -53,6 +68,14 @@ type testGraphNodeDotter struct{ Result *DotNode } func (n *testGraphNodeDotter) Name() string { return n.Result.Name } func (n *testGraphNodeDotter) DotNode(string, *DotOpts) *DotNode { return n.Result } +const testGraphDotQuotedStr = `digraph { + compound = "true" + newrank = "true" + subgraph "root" { + "[root] name[\"with-quotes\"]" -> "[root] other" + } +}` + const testGraphDotBasicStr = `digraph { compound = "true" newrank = "true"