shithub: hugo

Download patch

ref: 593a546fc6b00d4a34eba3b3f5172fed2c100507
parent: 0bdc0d62d4f5d117032e4c09f2438e9df4a9c18b
author: Bjørn Erik Pedersen <[email protected]>
date: Thu Jul 21 13:18:55 EDT 2016

Check for nil Params in shortcode's Get

Fixes #2294

--- a/hugolib/shortcode.go
+++ b/hugolib/shortcode.go
@@ -66,6 +66,9 @@
 
 // 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
 	}
--- a/hugolib/shortcode_test.go
+++ b/hugolib/shortcode_test.go
@@ -124,13 +124,6 @@
 	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()
@@ -149,6 +142,16 @@
 	CheckShortCodeMatch(t, `{{< img src ="one" >}}`, `<img src="one">`, tem)
 	CheckShortCodeMatch(t, `{{< img src = "one" >}}`, `<img src="one">`, tem)
 	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) {