// these short codes aren't used until after Page render,
// but processed here to avoid coupling
- tmpContent, tmpContentShortCodes := extractAndRenderShortcodes(string(p.rawContent), p, t)
+ tmpContent, tmpContentShortCodes, _ := extractAndRenderShortcodes(string(p.rawContent), p, t)
p.rawContent = []byte(tmpContent)
p.contentShortCodes = tmpContentShortCodes
// HandleShortcodes does all in one go: extract, render and replace
// only used for testing
func HandleShortcodes(stringToParse string, page *Page, t tpl.Template) (string, error) {
- tmpContent, tmpShortcodes := extractAndRenderShortcodes(stringToParse, page, t)
+ tmpContent, tmpShortcodes, err := extractAndRenderShortcodes(stringToParse, page, t)
+
+ if err != nil {
+ return "", err
+ }
if len(tmpShortcodes) > 0 {
tmpContentWithTokensReplaced, err := replaceShortcodeTokens([]byte(tmpContent), shortcodePlaceholderPrefix, true, tmpShortcodes)
return renderShortcodeWithPage(tmpl, data)
}
-func extractAndRenderShortcodes(stringToParse string, p *Page, t tpl.Template) (string, map[string]string) {
+func extractAndRenderShortcodes(stringToParse string, p *Page, t tpl.Template) (string, map[string]string, error) {
content, shortcodes, err := extractShortcodes(stringToParse, p, t)
renderedShortcodes := make(map[string]string)
}
}
- return content, renderedShortcodes
+ return content, renderedShortcodes, err
}
}
}
+func TestShortcodeGoFuzzReports(t *testing.T) {
+ tem := tpl.New()
+
+ tem.AddInternalShortcode("sc.html", `foo`)
+ p, _ := pageFromString(SIMPLE_PAGE, "simple.md")
+
+ for i, this := range []struct {
+ data string
+ expectErr bool
+ }{
+ {"{{</*/", true},
+ } {
+ output, err := HandleShortcodes(this.data, p, tem)
+
+ if this.expectErr && err == nil {
+ t.Errorf("[%d] should have errored", i)
+ }
+
+ if !this.expectErr && err != nil {
+ t.Errorf("[%d] should not have errored: %s", i, err)
+ }
+
+ if !this.expectErr && err == nil && len(output) == 0 {
+ t.Errorf("[%d] empty result", i)
+ }
+ }
+
+}
+
func TestNonSC(t *testing.T) {
tem := tpl.New()
// notice the syntax diff from 0.12, now comment delims must be added
func lexShortcodeComment(l *pagelexer) stateFunc {
posRightComment := strings.Index(l.input[l.pos:], rightComment)
- if posRightComment < 0 {
+ if posRightComment <= 1 {
return l.errorf("comment must be closed")
}
// we emit all as text, except the comment markers