Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87f194b

Browse files
committed
Sync Go template package to 1.26.3
See #14897
1 parent 5f01b06 commit 87f194b

20 files changed

Lines changed: 160 additions & 286 deletions

File tree

scripts/fork_go_templates/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
func main() {
18-
// The current is built with e87b10ea2a2c6c65b80c4374af42b9c02ac9fb20 go1.26.1
18+
// The current is built with 2dc996f71b0ebafb77e64433e58333e049488a3c go1.26.3
1919
// TODO(bep) preserve the staticcheck.conf file.
2020
fmt.Println("Forking ...")
2121
defer fmt.Println("Done ...")

tpl/internal/go_templates/htmltemplate/clone_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build go1.13 && !windows
6-
// +build go1.13,!windows
5+
//go:build !windows
6+
// +build !windows
77

88
package template
99

tpl/internal/go_templates/htmltemplate/content_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build go1.13 && !windows
6-
// +build go1.13,!windows
5+
//go:build !windows
6+
// +build !windows
77

88
package template
99

@@ -428,7 +428,7 @@ func TestStringer(t *testing.T) {
428428
if err := tmpl.Execute(b, s); err != nil {
429429
t.Fatal(err)
430430
}
431-
var expect = "string=3"
431+
expect := "string=3"
432432
if b.String() != expect {
433433
t.Errorf("expected %q got %q", expect, b.String())
434434
}

tpl/internal/go_templates/htmltemplate/context.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package template
66

77
import (
88
"fmt"
9+
"slices"
910

1011
"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
1112
)
@@ -38,7 +39,7 @@ func (c context) String() string {
3839
if c.err != nil {
3940
err = c.err
4041
}
41-
return fmt.Sprintf("{%v %v %v %v %v %v %v}", c.state, c.delim, c.urlPart, c.jsCtx, c.attr, c.element, err)
42+
return fmt.Sprintf("{%v %v %v %v %v %v %v %v}", c.state, c.delim, c.urlPart, c.jsCtx, c.jsBraceDepth, c.attr, c.element, err)
4243
}
4344

4445
// eq reports whether two contexts are equal.
@@ -47,6 +48,7 @@ func (c context) eq(d context) bool {
4748
c.delim == d.delim &&
4849
c.urlPart == d.urlPart &&
4950
c.jsCtx == d.jsCtx &&
51+
slices.Equal(c.jsBraceDepth, d.jsBraceDepth) &&
5052
c.attr == d.attr &&
5153
c.element == d.element &&
5254
c.err == d.err
@@ -69,6 +71,9 @@ func (c context) mangle(templateName string) string {
6971
if c.jsCtx != jsCtxRegexp {
7072
s += "_" + c.jsCtx.String()
7173
}
74+
if c.jsBraceDepth != nil {
75+
s += fmt.Sprintf("_jsBraceDepth(%v)", c.jsBraceDepth)
76+
}
7277
if c.attr != attrNone {
7378
s += "_" + c.attr.String()
7479
}
@@ -78,6 +83,13 @@ func (c context) mangle(templateName string) string {
7883
return s
7984
}
8085

86+
// clone returns a copy of c with the same field values.
87+
func (c context) clone() context {
88+
clone := c
89+
clone.jsBraceDepth = slices.Clone(c.jsBraceDepth)
90+
return clone
91+
}
92+
8193
// state describes a high-level HTML parser state.
8294
//
8395
// It bounds the top of the element stack, and by extension the HTML insertion

tpl/internal/go_templates/htmltemplate/css_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build go1.13 && !windows
6-
// +build go1.13,!windows
5+
//go:build !windows
6+
// +build !windows
77

88
package template
99

tpl/internal/go_templates/htmltemplate/escape.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"fmt"
1010
"html"
11+
//"internal/godebug"
1112
"io"
1213
"maps"
1314
"regexp"
@@ -164,7 +165,9 @@ func (e *escaper) escape(c context, n parse.Node) context {
164165
panic("escaping " + n.String() + " is unimplemented")
165166
}
166167

167-
var htmlmetacontenturlescape = true // godebug.New("htmlmetacontenturlescape")
168+
//var debugAllowActionJSTmpl = godebug.New("jstmpllitinterp")
169+
170+
var htmlmetacontenturlescape = true //godebug.New("htmlmetacontenturlescape")
168171

169172
// escapeAction escapes an action template node.
170173
func (e *escaper) escapeAction(c context, n *parse.ActionNode) context {
@@ -228,12 +231,6 @@ func (e *escaper) escapeAction(c context, n *parse.ActionNode) context {
228231
case stateMetaContentURL:
229232
if htmlmetacontenturlescape {
230233
s = append(s, "_html_template_urlfilter")
231-
} else {
232-
// We don't have a great place to increment this, since it's hard to
233-
// know if we actually escape any urls in _html_template_urlfilter,
234-
// since it has no information about what context it is being
235-
// executed in etc. This is probably the best we can do.
236-
// htmlmetacontenturlescape.IncNonDefault()
237234
}
238235
case stateJS:
239236
s = append(s, "_html_template_jsvalescaper")
@@ -521,7 +518,7 @@ func (e *escaper) escapeBranch(c context, n *parse.BranchNode, nodeName string)
521518
if nodeName == "range" {
522519
e.rangeContext = &rangeContext{outer: e.rangeContext}
523520
}
524-
c0 := e.escapeList(c, n.List)
521+
c0 := e.escapeList(c.clone(), n.List)
525522
if nodeName == "range" {
526523
if c0.state != stateError {
527524
c0 = joinRange(c0, e.rangeContext)
@@ -552,7 +549,7 @@ func (e *escaper) escapeBranch(c context, n *parse.BranchNode, nodeName string)
552549
return c0
553550
}
554551
}
555-
c1 := e.escapeList(c, n.ElseList)
552+
c1 := e.escapeList(c.clone(), n.ElseList)
556553
return join(c0, c1, n, nodeName)
557554
}
558555

tpl/internal/go_templates/htmltemplate/escape_test.go

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build go1.13 && !windows
6-
// +build go1.13,!windows
5+
//go:build !windows
6+
// +build !windows
77

88
package template
99

@@ -236,6 +236,21 @@ func TestEscape(t *testing.T) {
236236
"<script>alert({{.A}})</script>",
237237
`<script>alert(["\u003ca\u003e","\u003cb\u003e"])</script>`,
238238
},
239+
{
240+
"scriptTypeSpace",
241+
"<script type=\" \">{{.H}}</script>",
242+
"<script type=\" \">\"\\u003cHello\\u003e\"</script>",
243+
},
244+
{
245+
"scriptTypeTab",
246+
"<script type=\"\t\">{{.H}}</script>",
247+
"<script type=\"\t\">\"\\u003cHello\\u003e\"</script>",
248+
},
249+
{
250+
"scriptTypeEmpty",
251+
"<script type=\"\">{{.H}}</script>",
252+
"<script type=\"\">\"\\u003cHello\\u003e\"</script>",
253+
},
239254
{
240255
"jsObjValueNotOverEscaped",
241256
"<button onclick='alert({{.A | html}})'>",
@@ -749,6 +764,26 @@ func TestEscape(t *testing.T) {
749764
`<meta http-equiv="refresh" content="{{"asd: 123"}}">`,
750765
`<meta http-equiv="refresh" content="asd: 123">`,
751766
},
767+
{
768+
"meta content url with whitespace before equals",
769+
`<meta http-equiv="refresh" content="0;url ={{"javascript:alert(1)"}}">`,
770+
`<meta http-equiv="refresh" content="0;url =#ZgotmplZ">`,
771+
},
772+
{
773+
"meta content url with tab before equals",
774+
"<meta http-equiv=\"refresh\" content=\"0;url\t={{\"javascript:alert(1)\"}}\">",
775+
"<meta http-equiv=\"refresh\" content=\"0;url\t=#ZgotmplZ\">",
776+
},
777+
{
778+
"meta content url with space after equals",
779+
`<meta http-equiv="refresh" content="0;url= {{"javascript:alert(1)"}}">`,
780+
`<meta http-equiv="refresh" content="0;url= #ZgotmplZ">`,
781+
},
782+
{
783+
"meta content url with whitespace both sides of equals",
784+
"<meta http-equiv=\"refresh\" content=\"0;url \t= {{\"javascript:alert(1)\"}}\">",
785+
"<meta http-equiv=\"refresh\" content=\"0;url \t= #ZgotmplZ\">",
786+
},
752787
}
753788

754789
for _, test := range tests {
@@ -1189,6 +1224,18 @@ func TestErrors(t *testing.T) {
11891224
// html is allowed since it is the last command in the pipeline, but urlquery is not.
11901225
`predefined escaper "urlquery" disallowed in template`,
11911226
},
1227+
{
1228+
"<script>var a = `{{if .X}}`{{end}}",
1229+
`{{if}} branches end in different contexts`,
1230+
},
1231+
{
1232+
"<script>var a = `{{if .X}}a{{else}}`{{end}}",
1233+
`{{if}} branches end in different contexts`,
1234+
},
1235+
{
1236+
"<script>var a = `{{if .X}}a{{else}}b{{end}}`</script>",
1237+
``,
1238+
},
11921239
}
11931240
for _, test := range tests {
11941241
buf := new(bytes.Buffer)
@@ -1759,43 +1806,43 @@ func TestEscapeText(t *testing.T) {
17591806
},
17601807
{
17611808
"<script>var a = `${",
1762-
context{state: stateJS, element: elementScript},
1809+
context{state: stateJS, element: elementScript, jsBraceDepth: []int{0}},
17631810
},
17641811
{
17651812
"<script>var a = `${}",
17661813
context{state: stateJSTmplLit, element: elementScript},
17671814
},
17681815
{
17691816
"<script>var a = `${`",
1770-
context{state: stateJSTmplLit, element: elementScript},
1817+
context{state: stateJSTmplLit, element: elementScript, jsBraceDepth: []int{0}},
17711818
},
17721819
{
17731820
"<script>var a = `${var a = \"",
1774-
context{state: stateJSDqStr, element: elementScript},
1821+
context{state: stateJSDqStr, element: elementScript, jsBraceDepth: []int{0}},
17751822
},
17761823
{
17771824
"<script>var a = `${var a = \"`",
1778-
context{state: stateJSDqStr, element: elementScript},
1825+
context{state: stateJSDqStr, element: elementScript, jsBraceDepth: []int{0}},
17791826
},
17801827
{
17811828
"<script>var a = `${var a = \"}",
1782-
context{state: stateJSDqStr, element: elementScript},
1829+
context{state: stateJSDqStr, element: elementScript, jsBraceDepth: []int{0}},
17831830
},
17841831
{
17851832
"<script>var a = `${``",
1786-
context{state: stateJS, element: elementScript},
1833+
context{state: stateJS, element: elementScript, jsBraceDepth: []int{0}},
17871834
},
17881835
{
17891836
"<script>var a = `${`}",
1790-
context{state: stateJSTmplLit, element: elementScript},
1837+
context{state: stateJSTmplLit, element: elementScript, jsBraceDepth: []int{0}},
17911838
},
17921839
{
17931840
"<script>`${ {} } asd`</script><script>`${ {} }",
17941841
context{state: stateJSTmplLit, element: elementScript},
17951842
},
17961843
{
17971844
"<script>var foo = `${ (_ => { return \"x\" })() + \"${",
1798-
context{state: stateJSDqStr, element: elementScript},
1845+
context{state: stateJSDqStr, element: elementScript, jsBraceDepth: []int{0}},
17991846
},
18001847
{
18011848
"<script>var a = `${ {</script><script>var b = `${ x }",
@@ -1823,23 +1870,23 @@ func TestEscapeText(t *testing.T) {
18231870
},
18241871
{
18251872
"<script>`${ { `` }",
1826-
context{state: stateJS, element: elementScript},
1873+
context{state: stateJS, element: elementScript, jsBraceDepth: []int{0}},
18271874
},
18281875
{
18291876
"<script>`${ { }`",
1830-
context{state: stateJSTmplLit, element: elementScript},
1877+
context{state: stateJSTmplLit, element: elementScript, jsBraceDepth: []int{0}},
18311878
},
18321879
{
18331880
"<script>var foo = `${ foo({ a: { c: `${",
1834-
context{state: stateJS, element: elementScript},
1881+
context{state: stateJS, element: elementScript, jsBraceDepth: []int{2, 0}},
18351882
},
18361883
{
18371884
"<script>var foo = `${ foo({ a: { c: `${ {{.}} }` }, b: ",
1838-
context{state: stateJS, element: elementScript},
1885+
context{state: stateJS, element: elementScript, jsBraceDepth: []int{1}},
18391886
},
18401887
{
18411888
"<script>`${ `}",
1842-
context{state: stateJSTmplLit, element: elementScript},
1889+
context{state: stateJSTmplLit, element: elementScript, jsBraceDepth: []int{0}},
18431890
},
18441891
}
18451892

tpl/internal/go_templates/htmltemplate/html_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build go1.13 && !windows
6-
// +build go1.13,!windows
5+
//go:build !windows
6+
// +build !windows
77

88
package template
99

tpl/internal/go_templates/htmltemplate/js.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ func isJSType(mimeType string) bool {
463463
mimeType = strings.TrimSpace(mimeType)
464464
switch mimeType {
465465
case
466+
"",
466467
"application/ecmascript",
467468
"application/javascript",
468469
"application/json",

tpl/internal/go_templates/htmltemplate/js_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build go1.13 && !windows
6-
// +build go1.13,!windows
5+
//go:build !windows
6+
// +build !windows
77

88
package template
99

@@ -221,7 +221,8 @@ func TestJSStrEscaper(t *testing.T) {
221221
{"<!--", `\u003c!--`},
222222
{"-->", `--\u003e`},
223223
// From https://code.google.com/p/doctype/wiki/ArticleUtf7
224-
{"+ADw-script+AD4-alert(1)+ADw-/script+AD4-",
224+
{
225+
"+ADw-script+AD4-alert(1)+ADw-/script+AD4-",
225226
`\u002bADw-script\u002bAD4-alert(1)\u002bADw-\/script\u002bAD4-`,
226227
},
227228
// Invalid UTF-8 sequence

0 commit comments

Comments
 (0)