]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl: Fix stray quotes from partial decorator in script context
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 3 Apr 2026 14:04:54 +0000 (16:04 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 3 Apr 2026 16:30:26 +0000 (18:30 +0200)
Use template.JS for the falsy return value of _PopPartialDecorator
so Go's html/template JS escaper doesn't wrap the empty string in
quotes inside <script> tags.

Fixes #14711

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
tpl/templates/decorator_falsy_test.go
tpl/templates/templates.go

index ede758a0b6ec0f9ab4c6872a07250bc4d35d2768..fad5cbf96631da97ef5c2cf215ca59f820baad5c 100644 (file)
@@ -61,3 +61,30 @@ title: "Page 1"
 
        b.AssertFileContent("public/index.html", "d:truthy", "$")
 }
+
+// Issue 14711
+// When using {{ with partial "name" . }} inside a <script> tag,
+// the injected else block's empty string return was being JS-escaped to "".
+func TestDecoratorPartialFalsyReturnInScript(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+disableKinds = ["section", "taxonomy", "term", "sitemap", "RSS"]
+-- content/p1.md --
+---
+title: "Page 1"
+---
+-- layouts/_partials/empty.html --
+{{ return "" }}
+-- layouts/home.html --
+<script>
+  {{- with partial "empty" . }}
+  "this-should-be-skipped": "{{ . }}"
+  {{- end }}
+</script>
+`
+       b := hugolib.Test(t, files)
+
+       b.AssertFileContent("public/index.html", "! \"\"")
+}
index 77efcdf62c82a7b47e35de3e70adb1e575130b2d..8428ff2525eee7a795a3524aa4fb3aaf9be4644a 100644 (file)
@@ -17,6 +17,7 @@ package templates
 import (
        "context"
        "fmt"
+       htmltemplate "html/template"
        "strconv"
        "sync/atomic"
 
@@ -113,7 +114,10 @@ func (ns *Namespace) _PopPartialDecorator(ctx context.Context, id string) any {
        }
        if !top.Bool {
                // Prevents anything being rendered if inner was not called.
-               return ""
+               // Use template.JS to avoid Go's html/template JS escaper
+               // wrapping an empty string in quotes inside <script> tags.
+               // See https://github.com/gohugoio/hugo/issues/14711
+               return htmltemplate.JS("")
        }
        return top.Bool // return whether inner exists in the wrapped partial.
 }