Skip to content

Commit 593a546

Browse files
committed
Check for nil Params in shortcode's Get
Fixes gohugoio#2294
1 parent 0bdc0d6 commit 593a546

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

hugolib/shortcode.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func (scp *ShortcodeWithPage) Scratch() *Scratch {
6666

6767
// Get is a convenience method to look up shortcode parameters by its key.
6868
func (scp *ShortcodeWithPage) Get(key interface{}) interface{} {
69+
if scp.Params == nil {
70+
return nil
71+
}
6972
if reflect.ValueOf(scp.Params).Len() == 0 {
7073
return nil
7174
}

hugolib/shortcode_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,6 @@ func TestPositionalParamIndexOutOfBounds(t *testing.T) {
124124
CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video error: index out of range for positional param at position 1", tem)
125125
}
126126

127-
// Issue #2294
128-
func TestPositionalParamNil(t *testing.T) {
129-
tem := tpl.New()
130-
tem.AddInternalShortcode("div.html", `<div data='{{ .Get 0 }}'>{{ .Inner }}</div>`)
131-
CheckShortCodeMatch(t, "{{% div %}}**foo**{{% /div %}}", "<div data=''><strong>foo</strong></div>", tem)
132-
}
133-
134127
// some repro issues for panics in Go Fuzz testing
135128
func TestShortcodeGoFuzzRepros(t *testing.T) {
136129
tt := tpl.New()
@@ -151,6 +144,16 @@ func TestNamedParamSC(t *testing.T) {
151144
CheckShortCodeMatch(t, `{{< img src = "one" class = "aspen grove" >}}`, `<img src="one" class="aspen grove">`, tem)
152145
}
153146

147+
// Issue #2294
148+
func TestNestedNamedMissingParam(t *testing.T) {
149+
tem := tpl.New()
150+
tem.AddInternalShortcode("acc.html", `<div class="acc">{{ .Inner }}</div>`)
151+
tem.AddInternalShortcode("div.html", `<div {{with .Get "class"}} class="{{ . }}"{{ end }}>{{ .Inner }}</div>`)
152+
CheckShortCodeMatch(t,
153+
`{{% acc %}}{{% div %}}{{% /div %}}{{% /acc %}}`,
154+
"<div class=\"acc\"><div ></div>\n</div>", tem)
155+
}
156+
154157
func TestIsNamedParamsSC(t *testing.T) {
155158
tem := tpl.New()
156159
tem.AddInternalShortcode("byposition.html", `<div id="{{ .Get 0 }}">`)

0 commit comments

Comments
 (0)