Fixes issues with special chars in dagre ids

This commit is contained in:
Júlio César Batista 2022-12-06 13:44:59 -08:00
parent 57258c3a27
commit 8764dfb78b
No known key found for this signature in database
GPG key ID: 10C4B861BF314878
2 changed files with 10 additions and 2 deletions

View file

@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"math"
"regexp"
"strings"
"cdr.dev/slog"
@ -257,7 +258,11 @@ func setGraphAttrs(attrs dagreGraphAttrs) string {
}
func escapeID(id string) string {
id = strings.ReplaceAll(id, `\n`, `\\n`)
// fixes \\
id = strings.ReplaceAll(id, "\\", `\\`)
// replaces \n with \\n whenever \n is not preceded by \ (does not replace \\n)
re := regexp.MustCompile(`[^\\](\n)`)
id = re.ReplaceAllString(id, `\\n`)
// avoid an unescaped \r becoming a \n in the layout result
id = strings.ReplaceAll(id, "\r", `\r`)
return id

View file

@ -7,11 +7,14 @@ import (
func testRegression(t *testing.T) {
tcs := []testCase{
{
name: "dagre_id_with_newline",
name: "dagre_special_ids",
script: `
ninety\nnine
eighty\reight
seventy\r\nseven
a\\yode -> there
a\\"ode -> there
a\\node -> there
`,
},
{