From 3db70bb31d3dfc0737b8c9802ca0a6a265ad160f Mon Sep 17 00:00:00 2001 From: Bernard Xie Date: Thu, 1 Jun 2023 18:42:43 -0700 Subject: [PATCH 1/7] Update compile.go --- d2compiler/compile.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 8c60316cd..1c83edf99 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -270,6 +270,9 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) { // TODO: Delete instead. attrs.Label.Value = scalar.ScalarString() case *d2ast.BlockString: + if scalar.ScalarString() == "" { + c.errorf(f.LastPrimaryKey(), "block string cannot be empty") + } attrs.Language = scalar.Tag fullTag, ok := ShortToFullLanguageAliases[scalar.Tag] if ok { From 7d05c395930fa935754ecaf2b85aefdf78cb77cf Mon Sep 17 00:00:00 2001 From: Bernard Xie Date: Thu, 1 Jun 2023 18:45:15 -0700 Subject: [PATCH 2/7] Update next.md --- ci/release/changelogs/next.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 9fcfa6163..274a93921 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -18,3 +18,4 @@ - Fixes panic using reserved keywords as containers [#1358](https://github.com/terrastruct/d2/pull/1358) - When multiple classes are applied changing different attributes of arrowheads, they are all applied instead of only the last one [#1362](https://github.com/terrastruct/d2/pull/1362) +- Prevent empty blocks strings [#1364](https://github.com/terrastruct/d2/pull/1364) From 1a6b1a4084fc6d0b26622fed313bd30da9164911 Mon Sep 17 00:00:00 2001 From: Bernard Xie Date: Thu, 1 Jun 2023 18:48:37 -0700 Subject: [PATCH 3/7] add test --- d2compiler/compile_test.go | 5 +++++ .../TestCompile/no_empty_block_strings.exp.json | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 testdata/d2compiler/TestCompile/no_empty_block_strings.exp.json diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 78dbd5365..c7c6267da 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -610,6 +610,11 @@ x: { expErr: `d2/testdata/d2compiler/TestCompile/md_block_string_err.d2:4:19: unexpected text after md block string. See https://d2lang.com/tour/text#advanced-block-strings. d2/testdata/d2compiler/TestCompile/md_block_string_err.d2:5:1: block string must be terminated with |`, }, + { + name: "no_empty_block_strings", + text: `Text: |md |`, + expErr: `d2/testdata/d2compiler/TestCompile/no_empty_block_strings.d2:1:1: block string cannot be empty`, + }, { name: "underscore_edge_existing", diff --git a/testdata/d2compiler/TestCompile/no_empty_block_strings.exp.json b/testdata/d2compiler/TestCompile/no_empty_block_strings.exp.json new file mode 100644 index 000000000..be534202f --- /dev/null +++ b/testdata/d2compiler/TestCompile/no_empty_block_strings.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/no_empty_block_strings.d2,0:0:0-0:11:11", + "errmsg": "d2/testdata/d2compiler/TestCompile/no_empty_block_strings.d2:1:1: block string cannot be empty" + } + ] + } +} From 107a47e9e338566139dba2b5f290b5a591fbc28e Mon Sep 17 00:00:00 2001 From: Bernard Xie Date: Thu, 1 Jun 2023 18:49:02 -0700 Subject: [PATCH 4/7] Update next.md --- ci/release/changelogs/next.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 274a93921..0735e46d5 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -18,4 +18,4 @@ - Fixes panic using reserved keywords as containers [#1358](https://github.com/terrastruct/d2/pull/1358) - When multiple classes are applied changing different attributes of arrowheads, they are all applied instead of only the last one [#1362](https://github.com/terrastruct/d2/pull/1362) -- Prevent empty blocks strings [#1364](https://github.com/terrastruct/d2/pull/1364) +- Prevent empty block strings [#1364](https://github.com/terrastruct/d2/pull/1364) From cd09aba54ff82b5632922355c1491da6466363a9 Mon Sep 17 00:00:00 2001 From: Bernard Xie Date: Fri, 2 Jun 2023 10:42:44 -0700 Subject: [PATCH 5/7] trim space --- d2compiler/compile.go | 2 +- d2compiler/compile_test.go | 17 +++++++++++++++-- .../TestCompile/no_empty_block_string.exp.json | 12 ++++++++++++ .../no_new_lines_only_block_string.exp.json | 12 ++++++++++++ .../no_white_spaces_only_block_string.exp.json | 12 ++++++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 testdata/d2compiler/TestCompile/no_empty_block_string.exp.json create mode 100644 testdata/d2compiler/TestCompile/no_new_lines_only_block_string.exp.json create mode 100644 testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.exp.json diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 1c83edf99..2e85a27be 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -270,7 +270,7 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) { // TODO: Delete instead. attrs.Label.Value = scalar.ScalarString() case *d2ast.BlockString: - if scalar.ScalarString() == "" { + if strings.TrimSpace(scalar.ScalarString()) == "" { c.errorf(f.LastPrimaryKey(), "block string cannot be empty") } attrs.Language = scalar.Tag diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index c7c6267da..3a73a9b08 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -611,9 +611,22 @@ x: { d2/testdata/d2compiler/TestCompile/md_block_string_err.d2:5:1: block string must be terminated with |`, }, { - name: "no_empty_block_strings", + name: "no_empty_block_string", text: `Text: |md |`, - expErr: `d2/testdata/d2compiler/TestCompile/no_empty_block_strings.d2:1:1: block string cannot be empty`, + expErr: `d2/testdata/d2compiler/TestCompile/no_empty_block_string.d2:1:1: block string cannot be empty`, + }, + { + name: "no_white_spaces_only_block_string", + text: `Text: |md |`, + expErr: `d2/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.d2:1:1: block string cannot be empty`, + }, + { + name: "no_new_lines_only_block_string", + text: `Text: |md + + +|`, + expErr: `d2/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.d2:1:1: block string cannot be empty`, }, { name: "underscore_edge_existing", diff --git a/testdata/d2compiler/TestCompile/no_empty_block_string.exp.json b/testdata/d2compiler/TestCompile/no_empty_block_string.exp.json new file mode 100644 index 000000000..bf0a8c3a8 --- /dev/null +++ b/testdata/d2compiler/TestCompile/no_empty_block_string.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/no_empty_block_string.d2,0:0:0-0:11:11", + "errmsg": "d2/testdata/d2compiler/TestCompile/no_empty_block_string.d2:1:1: block string cannot be empty" + } + ] + } +} diff --git a/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.exp.json b/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.exp.json new file mode 100644 index 000000000..2dff86029 --- /dev/null +++ b/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.d2,0:0:0-3:1:13", + "errmsg": "d2/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.d2:1:1: block string cannot be empty" + } + ] + } +} diff --git a/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.exp.json b/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.exp.json new file mode 100644 index 000000000..ab68a6cc0 --- /dev/null +++ b/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.d2,0:0:0-0:16:16", + "errmsg": "d2/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.d2:1:1: block string cannot be empty" + } + ] + } +} From 9f581bd78ab5fa531c6087f62893f2541d4628ce Mon Sep 17 00:00:00 2001 From: Bernard Xie Date: Fri, 2 Jun 2023 10:50:58 -0700 Subject: [PATCH 6/7] Update regression_test.go --- e2etests/regression_test.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/e2etests/regression_test.go b/e2etests/regression_test.go index e0453a15e..be778d758 100644 --- a/e2etests/regression_test.go +++ b/e2etests/regression_test.go @@ -929,19 +929,6 @@ cf many required: { style.stroke-width: 8 } } -`, - }, - { - name: "empty_md_measurement", - script: ` -a -b: |md - - -| -c -d -a -> b -> c `, }, loadFromFile(t, "slow_grid"), From 98c78c1f1955cd13da82796e4ca816020ed99c52 Mon Sep 17 00:00:00 2001 From: Bernard Xie Date: Fri, 2 Jun 2023 10:57:40 -0700 Subject: [PATCH 7/7] Update measured_test.go --- e2etests/measured_test.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/e2etests/measured_test.go b/e2etests/measured_test.go index 3c33c5c24..777fda482 100644 --- a/e2etests/measured_test.go +++ b/e2etests/measured_test.go @@ -26,14 +26,6 @@ func testMeasured(t *testing.T) { name: "empty-sql_table", mtexts: []*d2target.MText{}, script: `a: "" { shape: sql_table } -`, - }, - { - name: "empty-markdown", - mtexts: []*d2target.MText{}, - script: `a: |md -` + " " + ` -| `, }, }