Check for nil Params in shortcode's Get
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 21 Jul 2016 15:18:55 +0000 (17:18 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 21 Jul 2016 15:22:31 +0000 (17:22 +0200)
Fixes #2294

hugolib/shortcode.go
hugolib/shortcode_test.go

index 8ea09fedae0693f98a37242773a6334eb8c46aae..2de00fa90791c3eb6f97cb0b7ef0af1b88863356 100644 (file)
@@ -66,6 +66,9 @@ func (scp *ShortcodeWithPage) Scratch() *Scratch {
 
 // Get is a convenience method to look up shortcode parameters by its key.
 func (scp *ShortcodeWithPage) Get(key interface{}) interface{} {
+       if scp.Params == nil {
+               return nil
+       }
        if reflect.ValueOf(scp.Params).Len() == 0 {
                return nil
        }
index b30b5b5118563d7252df8c5a16f6735a6647c05a..cd2d9f7a53b21f82aabaf056d5a3a68d4a30711a 100644 (file)
@@ -124,13 +124,6 @@ func TestPositionalParamIndexOutOfBounds(t *testing.T) {
        CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video error: index out of range for positional param at position 1", tem)
 }
 
-// Issue #2294
-func TestPositionalParamNil(t *testing.T) {
-       tem := tpl.New()
-       tem.AddInternalShortcode("div.html", `<div data='{{ .Get 0 }}'>{{ .Inner }}</div>`)
-       CheckShortCodeMatch(t, "{{% div %}}**foo**{{% /div %}}", "<div data=''><strong>foo</strong></div>", tem)
-}
-
 // some repro issues for panics in Go Fuzz testing
 func TestShortcodeGoFuzzRepros(t *testing.T) {
        tt := tpl.New()
@@ -151,6 +144,16 @@ func TestNamedParamSC(t *testing.T) {
        CheckShortCodeMatch(t, `{{< img src = "one" class = "aspen grove" >}}`, `<img src="one" class="aspen grove">`, tem)
 }
 
+// Issue #2294
+func TestNestedNamedMissingParam(t *testing.T) {
+       tem := tpl.New()
+       tem.AddInternalShortcode("acc.html", `<div class="acc">{{ .Inner }}</div>`)
+       tem.AddInternalShortcode("div.html", `<div {{with .Get "class"}} class="{{ . }}"{{ end }}>{{ .Inner }}</div>`)
+       CheckShortCodeMatch(t,
+               `{{% acc %}}{{% div %}}{{% /div %}}{{% /acc %}}`,
+               "<div class=\"acc\"><div ></div>\n</div>", tem)
+}
+
 func TestIsNamedParamsSC(t *testing.T) {
        tem := tpl.New()
        tem.AddInternalShortcode("byposition.html", `<div id="{{ .Get 0 }}">`)