fix dagre layout with \r in IDs

This commit is contained in:
Gavin Nishizawa 2022-12-05 11:40:21 -08:00
parent e83eb998cc
commit 53bceb8578
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
3 changed files with 24 additions and 4 deletions

View file

@ -1351,6 +1351,17 @@ y -> x.style
b`, g.Objects[0].Attributes.Label.Value)
},
},
{
name: "unescaped_id_cr",
text: `b\rb`,
assertions: func(t *testing.T, g *d2graph.Graph) {
if len(g.Objects) != 1 {
t.Fatal(g.Objects)
}
assert.String(t, "b\rb", g.Objects[0].Attributes.Label.Value)
},
},
{
name: "class_style",

View file

@ -259,15 +259,20 @@ func setGraphAttrs(attrs dagreGraphAttrs) string {
)
}
func escapeID(id string) string {
return strings.ReplaceAll(id, "\r", "\\r")
}
func generateAddNodeLine(id string, width, height int) string {
id = escapeID(id)
return fmt.Sprintf("g.setNode(`%s`, { id: `%s`, width: %d, height: %d });\n", id, id, width, height)
}
func generateAddParentLine(childID, parentID string) string {
return fmt.Sprintf("g.setParent(`%s`, `%s`);\n", childID, parentID)
return fmt.Sprintf("g.setParent(`%s`, `%s`);\n", escapeID(childID), escapeID(parentID))
}
func generateAddEdgeLine(fromID, toID, edgeID string) string {
// in dagre v is from, w is to, name is to uniquely identify
return fmt.Sprintf("g.setEdge({v:`%s`, w:`%s`, name:`%s` });\n", fromID, toID, edgeID)
return fmt.Sprintf("g.setEdge({v:`%s`, w:`%s`, name:`%s` });\n", escapeID(fromID), escapeID(toID), escapeID(edgeID))
}

View file

@ -7,8 +7,12 @@ import (
func testRegression(t *testing.T) {
tcs := []testCase{
{
name: "dagre_id_with_newline",
script: `ninety\nnine`,
name: "dagre_id_with_newline",
script: `
ninety\nnine
eighty\reight
seventy\r\nseven
`,
},
}