diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md
index 4457be55c..07a9d41ed 100644
--- a/ci/release/changelogs/next.md
+++ b/ci/release/changelogs/next.md
@@ -9,4 +9,5 @@
#### Bugfixes ⛑️
- Restricts where `near` key constant values can be used, with good error messages, instead of erroring (e.g. setting `near: top-center` on a container would cause bad layouts or error). [#538](https://github.com/terrastruct/d2/pull/538)
+- Fixes an error during ELK layout when images had empty labels. [#555](https://github.com/terrastruct/d2/pull/555)
- Fixes rendering classes and tables with empty headers. [#498](https://github.com/terrastruct/d2/pull/498)
diff --git a/d2layouts/d2elklayout/layout.go b/d2layouts/d2elklayout/layout.go
index 6dd37f828..ada0ca0e7 100644
--- a/d2layouts/d2elklayout/layout.go
+++ b/d2layouts/d2elklayout/layout.go
@@ -147,8 +147,10 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) {
walk(g.Root, nil, func(obj, parent *d2graph.Object) {
height := obj.Height
- if obj.Attributes.Shape.Value == d2target.ShapeImage || obj.Attributes.Icon != nil {
- height += float64(*obj.LabelHeight) + label.PADDING
+ if obj.LabelWidth != nil && obj.LabelHeight != nil {
+ if obj.Attributes.Shape.Value == d2target.ShapeImage || obj.Attributes.Icon != nil {
+ height += float64(*obj.LabelHeight) + label.PADDING
+ }
}
n := &ELKNode{
diff --git a/e2etests/regression_test.go b/e2etests/regression_test.go
index f823b03c2..a3698795b 100644
--- a/e2etests/regression_test.go
+++ b/e2etests/regression_test.go
@@ -248,6 +248,20 @@ code: |go
b := a + 7
fmt.Printf("%d", b)
|
+`,
+ },
+ {
+ name: "elk_img_empty_label_panic",
+ script: `
+img: {
+ label: ""
+ shape: image
+ icon: https://icons.terrastruct.com/infra/019-network.svg
+}
+ico: {
+ label: ""
+ icon: https://icons.terrastruct.com/infra/019-network.svg
+}
`,
},
}
diff --git a/e2etests/testdata/regression/elk_img_empty_label_panic/dagre/board.exp.json b/e2etests/testdata/regression/elk_img_empty_label_panic/dagre/board.exp.json
new file mode 100644
index 000000000..f1b566716
--- /dev/null
+++ b/e2etests/testdata/regression/elk_img_empty_label_panic/dagre/board.exp.json
@@ -0,0 +1,107 @@
+{
+ "name": "",
+ "fontFamily": "SourceSansPro",
+ "shapes": [
+ {
+ "id": "img",
+ "type": "image",
+ "pos": {
+ "x": 0,
+ "y": 0
+ },
+ "width": 128,
+ "height": 128,
+ "opacity": 1,
+ "strokeDash": 0,
+ "strokeWidth": 2,
+ "borderRadius": 0,
+ "fill": "#FFFFFF",
+ "stroke": "#0D32B2",
+ "shadow": false,
+ "3d": false,
+ "multiple": false,
+ "tooltip": "",
+ "link": "",
+ "icon": {
+ "Scheme": "https",
+ "Opaque": "",
+ "User": null,
+ "Host": "icons.terrastruct.com",
+ "Path": "/infra/019-network.svg",
+ "RawPath": "",
+ "ForceQuery": false,
+ "RawQuery": "",
+ "Fragment": "",
+ "RawFragment": ""
+ },
+ "iconPosition": "INSIDE_MIDDLE_CENTER",
+ "blend": false,
+ "fields": null,
+ "methods": null,
+ "columns": null,
+ "label": "",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "",
+ "color": "#0A0F25",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 0,
+ "labelHeight": 0,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "ico",
+ "type": "",
+ "pos": {
+ "x": 188,
+ "y": 14
+ },
+ "width": 100,
+ "height": 100,
+ "opacity": 1,
+ "strokeDash": 0,
+ "strokeWidth": 2,
+ "borderRadius": 0,
+ "fill": "#F7F8FE",
+ "stroke": "#0D32B2",
+ "shadow": false,
+ "3d": false,
+ "multiple": false,
+ "tooltip": "",
+ "link": "",
+ "icon": {
+ "Scheme": "https",
+ "Opaque": "",
+ "User": null,
+ "Host": "icons.terrastruct.com",
+ "Path": "/infra/019-network.svg",
+ "RawPath": "",
+ "ForceQuery": false,
+ "RawQuery": "",
+ "Fragment": "",
+ "RawFragment": ""
+ },
+ "iconPosition": "INSIDE_MIDDLE_CENTER",
+ "blend": false,
+ "fields": null,
+ "methods": null,
+ "columns": null,
+ "label": "",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "",
+ "color": "#0A0F25",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 0,
+ "labelHeight": 0,
+ "zIndex": 0,
+ "level": 1
+ }
+ ],
+ "connections": []
+}
diff --git a/e2etests/testdata/regression/elk_img_empty_label_panic/dagre/sketch.exp.svg b/e2etests/testdata/regression/elk_img_empty_label_panic/dagre/sketch.exp.svg
new file mode 100644
index 000000000..3726c5c2d
--- /dev/null
+++ b/e2etests/testdata/regression/elk_img_empty_label_panic/dagre/sketch.exp.svg
@@ -0,0 +1,24 @@
+
+
\ No newline at end of file
diff --git a/e2etests/testdata/regression/elk_img_empty_label_panic/elk/board.exp.json b/e2etests/testdata/regression/elk_img_empty_label_panic/elk/board.exp.json
new file mode 100644
index 000000000..e37d0a2b3
--- /dev/null
+++ b/e2etests/testdata/regression/elk_img_empty_label_panic/elk/board.exp.json
@@ -0,0 +1,107 @@
+{
+ "name": "",
+ "fontFamily": "SourceSansPro",
+ "shapes": [
+ {
+ "id": "img",
+ "type": "image",
+ "pos": {
+ "x": 12,
+ "y": 12
+ },
+ "width": 128,
+ "height": 128,
+ "opacity": 1,
+ "strokeDash": 0,
+ "strokeWidth": 2,
+ "borderRadius": 0,
+ "fill": "#FFFFFF",
+ "stroke": "#0D32B2",
+ "shadow": false,
+ "3d": false,
+ "multiple": false,
+ "tooltip": "",
+ "link": "",
+ "icon": {
+ "Scheme": "https",
+ "Opaque": "",
+ "User": null,
+ "Host": "icons.terrastruct.com",
+ "Path": "/infra/019-network.svg",
+ "RawPath": "",
+ "ForceQuery": false,
+ "RawQuery": "",
+ "Fragment": "",
+ "RawFragment": ""
+ },
+ "iconPosition": "INSIDE_MIDDLE_CENTER",
+ "blend": false,
+ "fields": null,
+ "methods": null,
+ "columns": null,
+ "label": "",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "",
+ "color": "#0A0F25",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 0,
+ "labelHeight": 0,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "ico",
+ "type": "",
+ "pos": {
+ "x": 160,
+ "y": 26
+ },
+ "width": 100,
+ "height": 100,
+ "opacity": 1,
+ "strokeDash": 0,
+ "strokeWidth": 2,
+ "borderRadius": 0,
+ "fill": "#F7F8FE",
+ "stroke": "#0D32B2",
+ "shadow": false,
+ "3d": false,
+ "multiple": false,
+ "tooltip": "",
+ "link": "",
+ "icon": {
+ "Scheme": "https",
+ "Opaque": "",
+ "User": null,
+ "Host": "icons.terrastruct.com",
+ "Path": "/infra/019-network.svg",
+ "RawPath": "",
+ "ForceQuery": false,
+ "RawQuery": "",
+ "Fragment": "",
+ "RawFragment": ""
+ },
+ "iconPosition": "INSIDE_MIDDLE_CENTER",
+ "blend": false,
+ "fields": null,
+ "methods": null,
+ "columns": null,
+ "label": "",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "",
+ "color": "#0A0F25",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 0,
+ "labelHeight": 0,
+ "zIndex": 0,
+ "level": 1
+ }
+ ],
+ "connections": []
+}
diff --git a/e2etests/testdata/regression/elk_img_empty_label_panic/elk/sketch.exp.svg b/e2etests/testdata/regression/elk_img_empty_label_panic/elk/sketch.exp.svg
new file mode 100644
index 000000000..81e2051b4
--- /dev/null
+++ b/e2etests/testdata/regression/elk_img_empty_label_panic/elk/sketch.exp.svg
@@ -0,0 +1,24 @@
+
+
\ No newline at end of file