Fixes issues with special chars in dagre ids
This commit is contained in:
parent
57258c3a27
commit
8764dfb78b
2 changed files with 10 additions and 2 deletions
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"cdr.dev/slog"
|
"cdr.dev/slog"
|
||||||
|
|
@ -257,7 +258,11 @@ func setGraphAttrs(attrs dagreGraphAttrs) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func escapeID(id string) 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
|
// avoid an unescaped \r becoming a \n in the layout result
|
||||||
id = strings.ReplaceAll(id, "\r", `\r`)
|
id = strings.ReplaceAll(id, "\r", `\r`)
|
||||||
return id
|
return id
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,14 @@ import (
|
||||||
func testRegression(t *testing.T) {
|
func testRegression(t *testing.T) {
|
||||||
tcs := []testCase{
|
tcs := []testCase{
|
||||||
{
|
{
|
||||||
name: "dagre_id_with_newline",
|
name: "dagre_special_ids",
|
||||||
script: `
|
script: `
|
||||||
ninety\nnine
|
ninety\nnine
|
||||||
eighty\reight
|
eighty\reight
|
||||||
seventy\r\nseven
|
seventy\r\nseven
|
||||||
|
a\\yode -> there
|
||||||
|
a\\"ode -> there
|
||||||
|
a\\node -> there
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue