Skip to content

Commit

Permalink
Merge pull request #1091 from alankan886/master
Browse files Browse the repository at this point in the history
compiler: Compiler error when Link is set and Tooltip is an URL
  • Loading branch information
alixander authored Mar 28, 2023
2 parents 50e3d75 + 848b8ea commit 4924a8b
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

#### Improvements 🧹

- prevent `tooltip` to be an URL when `link` is already set. [#1091](https://github.com/terrastruct/d2/pull/1091)

#### Bugfixes ⛑️
7 changes: 7 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,13 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
attrs.Constraint.Value = scalar.ScalarString()
attrs.Constraint.MapKey = f.LastPrimaryKey()
}

if attrs.Link != nil && attrs.Tooltip != nil {
_, err := url.ParseRequestURI(attrs.Tooltip.Value)
if err == nil {
c.errorf(scalar, "Tooltip cannot be set to URL when link is also set (for security)")
}
}
}

func (c *compiler) compileStyle(attrs *d2graph.Attributes, m *d2ir.Map) {
Expand Down
34 changes: 34 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,40 @@ x -> y: {
}
},
},
{
name: "url_tooltip",
text: `x: {tooltip: https://google.com}`,
assertions: func(t *testing.T, g *d2graph.Graph) {
if len(g.Objects) != 1 {
t.Fatal(g.Objects)
}

if g.Objects[0].Attributes.Tooltip.Value != "https://google.com" {
t.Fatal(g.Objects[0].Attributes.Tooltip.Value)
}
},
},
{
name: "no_url_link_and_url_tooltip_concurrently",
text: `x: {link: https://not-google.com; tooltip: https://google.com}`,
expErr: `d2/testdata/d2compiler/TestCompile/no_url_link_and_url_tooltip_concurrently.d2:1:44: Tooltip cannot be set to URL when link is also set (for security)`,
},
{
name: "url_link_and_not_url_tooltip_concurrently",
text: `x: {link: https://google.com; tooltip: hello world}`,
assertions: func(t *testing.T, g *d2graph.Graph) {
if len(g.Objects) != 1 {
t.Fatal(g.Objects)
}
if g.Objects[0].Attributes.Link.Value != "https://google.com" {
t.Fatal(g.Objects[0].Attributes.Link.Value)
}

if g.Objects[0].Attributes.Tooltip.Value != "hello world" {
t.Fatal(g.Objects[0].Attributes.Tooltip.Value)
}
},
},
{
name: "nil_scope_obj_regression",

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4924a8b

Please sign in to comment.