Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

html/template: script type attribute: {{if}} branches end in different contexts #57136

Closed
melato opened this issue Dec 7, 2022 · 5 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@melato
Copy link

melato commented Dec 7, 2022

The following template:

<script {{if .type}}type="{{.type}}"{{end}}></script>

causes the following error when executed (regardless of the value of .type):

tml/template:test:1:13: {{if}} branches end in different contexts: {stateTag delimNone urlPartNone jsCtxRegexp attrNone elementNone <nil>}, {stateTag delimNone urlPartNone jsCtxRegexp attrNone elementScript <nil>}

This behavior is specific to the script type attribute. It executes as expected if "type=" is replaced with "xtype="

What version of Go are you using (go version)?

go version go1.19.3 linux/amd64
go version go1.19.4 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

debian/11 amd64

go env Output
$ go env

What did you do?

https://go.dev/play/p/F4f3aMmjMVw

What did you expect to see?

<script type="module"></script>

What did you see instead?

html/template:test:1:13: {{if}} branches end in different contexts: {stateTag delimNone urlPartNone jsCtxRegexp attrNone elementNone }, {stateTag delimNone urlPartNone jsCtxRegexp attrNone elementScript }

@prattmic
Copy link
Member

prattmic commented Dec 7, 2022

cc @empijei @kele

@prattmic prattmic added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 7, 2022
@seankhliao
Copy link
Member

Working as intended, from the documentation of the error:

// ErrBranchEnd: "{{if}} branches end in different contexts"
// Example:
// {{if .C}}<a href="{{end}}{{.X}}
// Discussion:
// Package html/template statically examines each path through an
// {{if}}, {{range}}, or {{with}} to escape any following pipelines.
// The example is ambiguous since {{.X}} might be an HTML text node,
// or a URL prefix in an HTML attribute. The context of {{.X}} is
// used to figure out how to escape it, but that context depends on
// the run-time value of {{.C}} which is not statically known.

{{.type}} may contain a " which ends the context early.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 7, 2022
@melato
Copy link
Author

melato commented Dec 7, 2022

The example in the documentation has an {{if}} outside the tag and its {{end}} inside the tag (as far as we can tell). Hence the "different context". In my case, both {{if}} and {{end}} are inside the tag. {{if} branches are allowed and are useful around attributes. How else would you place attributes conditionally in a tag? The problem is with one specific attribute ("type") in the <script> element. I do not get the error for other <script> attributes or for a "type" attribute in other elements.

@melato
Copy link
Author

melato commented Dec 7, 2022

I am looking at the code (escape.go) now and I see the special treatment of the "type" attribute inside a <script> tag:
// If this is a non-JS "type" attribute inside "script" tag, do not treat the contents as JS.
but <script type="module"> is a valid javascript tag. See here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

@melato
Copy link
Author

melato commented Dec 7, 2022

This no longer affects me. If I want to add type="module" conditionally, I can do it differently. This works, for example, and it is better because the type attribute cannot have arbitrary values:

<script {{if .module}}type="module"{{end}}></script>

It turns out I don't need this conditional at all. JavaScript modules are imported differently in HTML than other JavaScript.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants