Catch incomplete shortcode error

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
This commit is contained in:
satotake 2021-05-24 21:59:02 +09:00 committed by GitHub
parent 10f60de89a
commit 845a7ba4fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 9 deletions

View file

@ -33,8 +33,6 @@ import (
"github.com/gohugoio/hugo/parser/pageparser" "github.com/gohugoio/hugo/parser/pageparser"
"github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/page"
_errors "github.com/pkg/errors"
"github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/text" "github.com/gohugoio/hugo/common/text"
"github.com/gohugoio/hugo/common/urls" "github.com/gohugoio/hugo/common/urls"
@ -310,7 +308,7 @@ func renderShortcode(
var found bool var found bool
tmpl, found = s.TextTmpl().Lookup(templName) tmpl, found = s.TextTmpl().Lookup(templName)
if !found { 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 { } else {
@ -417,7 +415,7 @@ func (s *shortcodeHandler) renderShortcodesForPage(p *pageState, f output.Format
for _, v := range s.shortcodes { for _, v := range s.shortcodes {
s, more, err := renderShortcode(0, s.s, tplVariants, v, nil, p) s, more, err := renderShortcode(0, s.s, tplVariants, v, nil, p)
if err != nil { 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 return nil, false, err
} }
hasVariants = hasVariants || more hasVariants = hasVariants || more
@ -460,6 +458,10 @@ Loop:
switch { switch {
case currItem.IsLeftShortcodeDelim(): case currItem.IsLeftShortcodeDelim():
next := pt.Peek() next := pt.Peek()
if next.IsRightShortcodeDelim() {
// no name: {{< >}} or {{% %}}
return sc, errors.New("shortcode has no name")
}
if next.IsShortcodeClose() { if next.IsShortcodeClose() {
continue continue
} }
@ -506,7 +508,7 @@ Loop:
// return that error, more specific // return that error, more specific
continue 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() { if next.IsRightShortcodeDelim() {
@ -536,7 +538,7 @@ Loop:
// Used to check if the template expects inner content. // Used to check if the template expects inner content.
templs := s.s.Tmpl().LookupVariants(sc.name) templs := s.s.Tmpl().LookupVariants(sc.name)
if templs == nil { 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) 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) err := h.Execute(tmpl, buffer, data)
if err != nil { if err != nil {
return "", _errors.Wrap(err, "failed to process shortcode") return "", errors.Wrap(err, "failed to process shortcode")
} }
return buffer.String(), nil return buffer.String(), nil
} }

View file

@ -65,8 +65,9 @@ title: "Title"
t.Fatalf("Shortcode rendered error %s.", err) t.Fatalf("Shortcode rendered error %s.", err)
} }
if err == nil && expectError { if expectError {
t.Fatalf("No error from shortcode") c.Assert(err, qt.ErrorMatches, expected)
return
} }
h := b.H h := b.H
@ -341,6 +342,12 @@ func TestShortcodeWrappedInPIssue(t *testing.T) {
`, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", wt) `, "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) { func TestExtractShortcodes(t *testing.T) {
b := newTestSitesBuilder(t).WithSimpleConfigFile() b := newTestSitesBuilder(t).WithSimpleConfigFile()