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: unexpected semantic differences compared to text/template #40075

Open
rsc opened this issue Jul 6, 2020 · 3 comments
Open

html/template: unexpected semantic differences compared to text/template #40075

rsc opened this issue Jul 6, 2020 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Jul 6, 2020

I ported the text/template tests over to html/template and discovered some semantic differences between the two that I did not expect.

In html/template:

  • Missing values format as empty strings, while in text/template they format as <no value>. For example:

    - 	{"map .NO", "{{.MSI.NO}}", "<no value>", tVal, true},
    + 	{"map .NO", "{{.MSI.NO}}", "", tVal, true}, // NOTE: <no value> in text/template
    
    - 	{"empty nil", "{{.Empty0}}", "<no value>", tVal, true},
    + 	{"empty nil", "{{.Empty0}}", "", tVal, true}, // NOTE: <no value> in text/template
    
    and so on
    
  • Missing values passed explicitly to the html escaper format as <nil> instead of <no value>:

    - 	{"html untyped nil", `{{html .Empty0}}`, "&lt;no value&gt;", tVal, true},
    + 	{"html untyped nil", `{{html .Empty0}}`, "&lt;nil&gt;", tVal, true}, // NOTE: "&lt;no value&gt;" in text/template
    
  • Given an addressable struct field with a pointer-receiver String method, the String method is not used (in text/template it is):

    - 	{"V{6666}.String()", "-{{.V0}}-", "-<6666>-", tVal, true},
    + 	{"V{6666}.String()", "-{{.V0}}-", "-{6666}-", tVal, true}, //  NOTE: -<6666>- in text/template
    
  • Given an addressable struct field with a pointer-receiver Error method, the Error method is not used (in text/template it is):

    - 	{"W{888}.Error()", "-{{.W0}}-", "-[888]-", tVal, true},
    + 	{"W{888}.Error()", "-{{.W0}}-", "-{888}-", tVal, true}, // NOTE: -[888] in text/template
    
  • TestExecuteOnNewTemplate crashes on all the cases from text/template: zero template.Template no longer safe for use #11379.

  • TestMessageForExecuteEmpty shows a different error message:

    - template: empty: "empty" is an incomplete or empty template
    + template: "empty" is an incomplete or empty template
    
  • TestIssue31810 fails (“check that a parenthesized first argument behaves properly”, for text/template: unintuitive behavior of parenthesized arguments in text/template and html/template #31810).

  • TestTemplateLookUp (for text/template: doesn't return template on lookup #10910, text/template: t.New() doesn't save entry in t.tmpl #10926) fails. Lookup returns non-nil values for undefined templates.

  • Calling Parse("") does not overwrite the text of a previous template parse, as it does in text/template.
    See TestEmptyTemplate, specifically the last case in the table:

    	{[]string{"{{.}}", ""}, "twice", ""},
    

The <no value> change looks like it may possibly be intentional. All the others look like bugs.

/cc @robpike

@rsc rsc added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 6, 2020
@rsc rsc added this to the Backlog milestone Jul 6, 2020
@gopherbot
Copy link

Change https://golang.org/cl/241084 mentions this issue: html/template: add tests from text/template

gopherbot pushed a commit that referenced this issue Jul 14, 2020
Copy and adapt tests from text/template, to exercise more of html/template's copy.

Various differences in behavior are flagged with NOTE comments or t.Skip
and documented in #40075. Many of them are probably bugs.
One clarifying test case added to both text/template and html/template.

No changes to the package itself.

Change-Id: Ifefad83d647db846040d24c2741a0244b00ade82
Reviewed-on: https://go-review.googlesource.com/c/go/+/241084
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
@cfiderer
Copy link

cfiderer commented Sep 9, 2020

Hi Go experts,
is there any news on these behavioral differences?
We'd like to have missing values rendered as "<no value>" in HTML templates, too...

@rsc
Copy link
Contributor Author

rsc commented Sep 10, 2020

@cfiderer My guess is that at this point adding <no value> to HTML templates would break Hugo users quite badly. /cc @bep

In general I think it would make sense to track down why these differences exist before removing them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

3 participants