Catch incomplete shortcode error
authorsatotake <doublequotation@gmail.com>
Mon, 24 May 2021 12:59:02 +0000 (21:59 +0900)
committerGitHub <noreply@github.com>
Mon, 24 May 2021 12:59:02 +0000 (14:59 +0200)
Currently, no name shortcodes (`{{< >}}`) enter unexpected branch and
throw `BUG: template info not set`. This patch checks if shortcode has
name or not earlier and throws specific error.

Closes #6866

hugolib/shortcode.go
hugolib/shortcode_test.go

index 483fad2e1e21afc1ffd3401a028fa4bd97bcf2a7..21d65de32f0688f92ef15ab84be15a9100391b5c 100644 (file)
@@ -33,8 +33,6 @@ import (
        "github.com/gohugoio/hugo/parser/pageparser"
        "github.com/gohugoio/hugo/resources/page"
 
-       _errors "github.com/pkg/errors"
-
        "github.com/gohugoio/hugo/common/maps"
        "github.com/gohugoio/hugo/common/text"
        "github.com/gohugoio/hugo/common/urls"
@@ -310,7 +308,7 @@ func renderShortcode(
                        var found bool
                        tmpl, found = s.TextTmpl().Lookup(templName)
                        if !found {
-                               return "", false, _errors.Errorf("no earlier definition of shortcode %q found", sc.name)
+                               return "", false, errors.Errorf("no earlier definition of shortcode %q found", sc.name)
                        }
                }
        } else {
@@ -417,7 +415,7 @@ func (s *shortcodeHandler) renderShortcodesForPage(p *pageState, f output.Format
        for _, v := range s.shortcodes {
                s, more, err := renderShortcode(0, s.s, tplVariants, v, nil, p)
                if err != nil {
-                       err = p.parseError(_errors.Wrapf(err, "failed to render shortcode %q", v.name), p.source.parsed.Input(), v.pos)
+                       err = p.parseError(errors.Wrapf(err, "failed to render shortcode %q", v.name), p.source.parsed.Input(), v.pos)
                        return nil, false, err
                }
                hasVariants = hasVariants || more
@@ -460,6 +458,10 @@ Loop:
                switch {
                case currItem.IsLeftShortcodeDelim():
                        next := pt.Peek()
+                       if next.IsRightShortcodeDelim() {
+                               // no name: {{< >}} or {{% %}}
+                               return sc, errors.New("shortcode has no name")
+                       }
                        if next.IsShortcodeClose() {
                                continue
                        }
@@ -506,7 +508,7 @@ Loop:
                                                // return that error, more specific
                                                continue
                                        }
-                                       return sc, fail(_errors.Errorf("shortcode %q has no .Inner, yet a closing tag was provided", next.Val), next)
+                                       return sc, fail(errors.Errorf("shortcode %q has no .Inner, yet a closing tag was provided", next.Val), next)
                                }
                        }
                        if next.IsRightShortcodeDelim() {
@@ -536,7 +538,7 @@ Loop:
                        // Used to check if the template expects inner content.
                        templs := s.s.Tmpl().LookupVariants(sc.name)
                        if templs == nil {
-                               return nil, _errors.Errorf("template for shortcode %q not found", sc.name)
+                               return nil, errors.Errorf("template for shortcode %q not found", sc.name)
                        }
 
                        sc.info = templs[0].(tpl.Info)
@@ -637,7 +639,7 @@ func renderShortcodeWithPage(h tpl.TemplateHandler, tmpl tpl.Template, data *Sho
 
        err := h.Execute(tmpl, buffer, data)
        if err != nil {
-               return "", _errors.Wrap(err, "failed to process shortcode")
+               return "", errors.Wrap(err, "failed to process shortcode")
        }
        return buffer.String(), nil
 }
index 41f4eddb53cec745e9d3355b924f05e52d4af6e8..51187a00332c857bfd27c2281e038a0ce4df06cb 100644 (file)
@@ -65,8 +65,9 @@ title: "Title"
                t.Fatalf("Shortcode rendered error %s.", err)
        }
 
-       if err == nil && expectError {
-               t.Fatalf("No error from shortcode")
+       if expectError {
+               c.Assert(err, qt.ErrorMatches, expected)
+               return
        }
 
        h := b.H
@@ -341,6 +342,12 @@ func TestShortcodeWrappedInPIssue(t *testing.T) {
 `, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", wt)
 }
 
+// #6866
+func TestShortcodeIncomplete(t *testing.T) {
+       t.Parallel()
+       CheckShortCodeMatchAndError(t, `{{<          >}}`, ".*shortcode has no name.*", nil, true)
+}
+
 func TestExtractShortcodes(t *testing.T) {
        b := newTestSitesBuilder(t).WithSimpleConfigFile()