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: confusing or incorrect error #2644

Closed
rsc opened this issue Jan 1, 2012 · 11 comments
Closed

html/template: confusing or incorrect error #2644

rsc opened this issue Jan 1, 2012 · 11 comments
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Jan 1, 2012

I have a template that says

<a href="{{if $.Draft}}/draft/{{.Name}}{{else}}/{{.Name}}{{end}}">

which works but is redundant.

If I change it to factor out the final /{{.Name}}, like in

<a href="{{if $.Draft}}/draft/{{end}}{{.Name}}">

then I get this error:

html/template:toc:65: (action: [(command: [F=[Name]])]) appears in an ambiguous URL
context

The error could perhaps say more about what is wrong, or say it
more clearly, but also I don't see why these two should be
different at all.
@gopherbot
Copy link

Comment 1 by mikesamuel:

I agree that the error message needs to be improved.
In the case where $.Draft is truthy, we have
    
where we need to HTML escape .Name but do nothing else.
In the other case, we have
    
where we need to ensure that .Name does not start with "javascript:" or another
disallowed protocol.
The "ambiguous URL context" error message indicates that it is impossible to tell
statically, whether to white-list protocols or not.
In your case, we could probably %-encode any colon, but that is not possible in general
because colons are overloaded and would be bending the rules of RFC 3986.
    
It is ambiguous whether the colon follows a non-hierarchical protocol or precedes a port.
We could factor the interpolation left into the conditional and that may match
programmer intent, but that is not possible across template call boundaries:
     

@adg
Copy link
Contributor

adg commented Jan 4, 2012

Comment 2:

Labels changed: added priority-go1.

@gopherbot
Copy link

Comment 3 by krolaw:

Shouldn't the factored version be:

??

@gopherbot
Copy link

Comment 4 by mikesamuel:

krolaw, I think you're right that the factored form is missing a slash.  If it were
factored to

there would be no error.

@rsc
Copy link
Contributor Author

rsc commented Jan 9, 2012

Comment 5:

wacky.  thanks for the explanation.

@gopherbot
Copy link

Comment 6 by mikesamuel:

Now that you understand the problem, what would html.template ideally have done
differently?

@rsc
Copy link
Contributor Author

rsc commented Jan 9, 2012

Comment 7:

The original error was
(action: [(command: [F=[Name]])]) appears in an ambiguous URL context
The trivial thing is that it would be nice to use the template syntax
in the error, so that it would say
{.Name} appears in an ambiguous URL context
Slightly less trivially, it would be nice to list the contexts in some
friendly way.  For example, in this case,
{.Name} appears in an ambiguous context: either beginning or middle of URL

@robpike
Copy link
Contributor

robpike commented Jan 13, 2012

Comment 8:

Owner changed to builder@golang.org.

@rsc
Copy link
Contributor Author

rsc commented Jan 30, 2012

Comment 9:

Labels changed: added go1-must.

@remyoudompheng
Copy link
Contributor

Comment 10:

I can't see the error with tip. It seems to have been fixed (there are even test cases).

@rsc
Copy link
Contributor Author

rsc commented Feb 24, 2012

Comment 11:

package main
import "html/template"
import "os"
func main() {
    var x struct { Draft bool; Name string }
    t := template.Must(template.New("x").Parse(``))
    if err := t.Execute(os.Stdout, &x); err != nil {
        panic(err.Error())
    }
}
This says
panic: html/template:x:1: {{.Name}} appears in an ambiguous URL context
Close enough for now.

Status changed to Fixed.

@rsc rsc added fixed labels Feb 24, 2012
@rsc rsc added this to the Go1 milestone Apr 10, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants