summaryrefslogtreecommitdiff
path: root/src/html
Commit message (Collapse)AuthorAgeFilesLines
* html/template: lock in application/json as valid JS testEmmanuel Odeke2016-12-041-0/+1
| | | | | | | | | | | | | | | CL https://go-review.googlesource.com/33899 added application/json as a mimeType for valid JS. Let's lock that fix in with a test. Updates #18159 Change-Id: Ic4dfd8929aebfc5410f796688f081ca06630f672 Reviewed-on: https://go-review.googlesource.com/33901 TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Nodir Turakulov <nodir@google.com> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
* html/template: escape JS in application/json script tagNodir Turakulov2016-12-041-2/+4
| | | | | | | | | | | | | | | Since ffd1c781b77aab542713b66ef387fa9307e4060b HTML templates check MIME type in the "type" attribute of "script" tag to decide if contents should be escaped as JavaScript. The whitelist of MIME types did not include application/json. Include it in this CL. Fixes #18159 Change-Id: I17a8a38f2b7789b4b7e941d14279de222eaf2b6a Reviewed-on: https://go-review.googlesource.com/33899 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* html/template: fix multiple Clones of redefined templateCaleb Spare2016-11-152-4/+23
| | | | | | | | | | | | | | This change redoes the fix for #16101 (CL 31092) in a different way by making t.Clone return the template associated with the t.Name() while allowing for the case that a template of the same name is define-d. Fixes #17735. Change-Id: I1e69672390a4c81aa611046a209008ae4a3bb723 Reviewed-on: https://go-review.googlesource.com/33210 Run-TryBot: Caleb Spare <cespare@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
* html/template: typo fixMarcel Edmund Franke2016-11-141-1/+1
| | | | | | | | comment on unexported function starts with wrong functionname Change-Id: Ib16c2fe42b5a8d4606ed719f620923c17839d091 Reviewed-on: https://go-review.googlesource.com/33203 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* all: spell "marshal" and "unmarshal" consistentlyDmitri Shuralyov2016-11-122-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The tree is inconsistent about single l vs double l in those words in documentation, test messages, and one error value text. $ git grep -E '[Mm]arshall(|s|er|ers|ed|ing)' | wc -l 42 $ git grep -E '[Mm]arshal(|s|er|ers|ed|ing)' | wc -l 1694 Make it consistently a single l, per earlier decisions. This means contributors won't be confused by misleading precedence, and it helps consistency. Change the spelling in one error value text in newRawAttributes of crypto/x509 package to be consistent. This change was generated with: perl -i -npe 's,([Mm]arshal)l(|s|er|ers|ed|ing),$1$2,' $(git grep -l -E '[Mm]arshall' | grep -v AUTHORS | grep -v CONTRIBUTORS) Updates #12431. Follows https://golang.org/cl/14150. Change-Id: I85d28a2d7692862ccb02d6a09f5d18538b6049a2 Reviewed-on: https://go-review.googlesource.com/33017 Run-TryBot: Minux Ma <minux@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* html/template, text/template: drop defined template list from errorsRuss Cox2016-10-281-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | The report in #17414 points out that if you have many many templates, then this is an overwhelming list and just hurts the signal-to-noise ratio of the error. Even the test of the old behavior also supports the idea that this is noise: template: empty: "empty" is an incomplete or empty template; defined templates are: "secondary" The chance that someone mistyped "secondary" as "empty" is slim at best. Similarly, the compiler does not augment an error like 'unknown variable x' by dumping the full list of all the known variables. For all these reasons, drop the list. Fixes #17414. Change-Id: I78f92d2c591df7218385fe723a4abc497913acf8 Reviewed-on: https://go-review.googlesource.com/32116 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
* html/template: add test case for unbounded template expansionRuss Cox2016-10-241-0/+12
| | | | | | | | | | | | | Fixed by CL 31092 already, but that change is a few steps away from the problem observed here, so add an explicit test. Fixes #17019. Change-Id: If4ece1418e6596b1976961347889ce12c5969637 Reviewed-on: https://go-review.googlesource.com/31466 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Quentin Smith <quentin@golang.org>
* html/template, text/template: docs and fixes for template redefinitionRuss Cox2016-10-242-20/+174
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | All prior versions of Go have allowed redefining empty templates to become non-empty. Unfortunately, that has never consistently taken effect in html/template after the first execution: // define and execute t := template.New("root") t.Parse(`{{define "T"}}{{end}}<a href="{{template "T"}}">`) t.Execute(w, nil) // <a href=""> // redefine t.Parse(`{{define "T"}}my.url{{end}}`) // succeeds, but ignored t.Execute(w, nil) // <a href=""> When Go 1.6 added {{block...}} to text/template, that loosened the redefinition rules to allow redefinition at any time. The loosening was undone a bit in html/template, although inconsistently: // define and execute t := template.New("root") t.Parse(`{{define "T"}}body{{end}}`) t.Lookup("T").Execute(ioutil.Discard, nil) // attempt to redefine t.Parse(`{{define "T"}}body{{end}}`) // rejected in all Go versions t.Lookup("T").Parse("body") // OK as of Go 1.6, likely unintentionally Like in the empty->non-empty case, whether future execution takes notice of a redefinition basically can't be explained without going into the details of the template escape analysis. Address both the original inconsistencies in whether a redefinition would have any effect and the new inconsistencies about whether a redefinition is allowed by adopting a new rule: no parsing or modifying any templates after the first execution of any template in the same set. Template analysis begins at first execution, and once template analysis has begun, we simply don't have the right logic to update the analysis for incremental modifications (and never have). If this new rule breaks existing uses of templates that we decide need to be supported, we can try to invalidate all escape analysis for the entire set after any modifications. But let's wait on that until we know we need to and why. Also fix documentation of text/template redefinition policy (redefinition is always OK). Fixes #15761. Change-Id: I7d58d7c08a7d9df2440ee0d651a5b2ecaff3006c Reviewed-on: https://go-review.googlesource.com/31464 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
* html/template: adjust ambiguous URL context textRuss Cox2016-10-193-3/+3
| | | | | | | | | | | | | | | | Before: ... appears in an ambiguous URL context. After: ... appears in an ambiguous context within a URL. It's a minor point, but it's confused multiple people. Try to make clearer that the ambiguity is "where exactly inside the URL?" Fixes #17319. Change-Id: Id834868d1275578036c1b00c2bdfcd733d9d2b7b Reviewed-on: https://go-review.googlesource.com/31465 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* html/template, text/template: clarify template redefinition behaviorRuss Cox2016-10-191-10/+14
| | | | | | | | | | | | | | | | | | | Make two important points clearer: - Giving a template definition containing nothing but spaces has no effect. - Giving a template definition containing non-spaces can only be done once per template. Fixes #16912. Fixes #16913. Fixes #17360. Change-Id: Ie3971b83ab148b7c8bb800fe4a21579566378e3e Reviewed-on: https://go-review.googlesource.com/31459 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
* html/template: fix Clone so that t.Lookup(t.Name()) yields tCaleb Spare2016-10-172-0/+38
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Template.escape makes the assumption that t.Lookup(t.Name()) is t (escapeTemplate looks up the associated template by name and sets escapeErr appropriately). This assumption did not hold for a Cloned template, because the template associated with t.Name() was a second copy of the original. Add a test for the assumption that t.Lookup(t.Name()) == t. One effect of this broken assumption was #16101: parallel Executes racily accessed the template namespace because each Execute call saw t.escapeErr == nil and re-escaped the template concurrently with read accesses occurring outside the namespace mutex. Add a test for this race. Related to #12996 and CL 16104. Fixes #16101 Change-Id: I59831d0847abbabb4ef9135f2912c6ce982f9837 Reviewed-on: https://go-review.googlesource.com/31092 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
* html/template: check "type" attribute in <script>Nodir Turakulov2016-09-297-17/+148
| | | | | | | | | | | | | | | | Currently any script tag is treated as a javascript container, although <script type="text/template"> must not be. Check "type" attribute of "script" tag. If it is present and it is not a JS MIME type, do not transition to elementScript state. Fixes #12149, where // inside text template was treated as regexp. Fixes #6701 Change-Id: I8fc9e504f7280bdd800f40383c061853665ac8a2 Reviewed-on: https://go-review.googlesource.com/14336 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
* html/template: update security model linkIan Lance Taylor2016-06-231-1/+1
| | | | | | | | | | Fixes #16148. Change-Id: Ifab773e986b768602476824d005eea9200761236 Reviewed-on: https://go-review.googlesource.com/24327 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
* html/template: mention risks of the CSS, HTML, JS, etc. typesAndrew Gerrand2016-05-181-0/+29
| | | | | | | | | | Fixes #15399 Change-Id: I5b9645cb9ddede6981ce0a005e0c6fdd8a751c6f Reviewed-on: https://go-review.googlesource.com/22824 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Minux Ma <minux@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
* html/template, text/template: clarify Parse{Files,Glob} semanticsAndrew Gerrand2016-04-221-0/+14
| | | | | | | | | | | Document the subtle property that files with equivalent base names will overwrite extant templates with those same names. Fixes golang/go#14320 Change-Id: Ie9ace1b08e6896ea599836e31582123169aa7a25 Reviewed-on: https://go-review.googlesource.com/21824 Reviewed-by: Rob Pike <r@golang.org>
* html/template: add examples of loading templates from filesEmmanuel Odeke2016-04-131-0/+226
| | | | | | | | | | | | | | | | | | | | | | Adds examples showing loading templates from files and executing them. Shows examples: - Using ParseGlob. - Using ParseFiles. - Using helper functions to share and use templates in different contexts by adding them to an existing bundle of templates. - Using a group of driver templates with distinct sets of helper templates. Almost all of the code was directly copied from text/template. Fixes #8500 Change-Id: Ic3d91d5232afc5a1cd2d8cd3d9a5f3b754c64225 Reviewed-on: https://go-review.googlesource.com/21854 Reviewed-by: Andrew Gerrand <adg@golang.org>
* all: use bytes.Equal, bytes.Contains and strings.Contains, againDominik Honnef2016-04-111-1/+1
| | | | | | | | | The previous cleanup was done with a buggy tool, missing some potential rewrites. Change-Id: I333467036e355f999a6a493e8de87e084f374e26 Reviewed-on: https://go-review.googlesource.com/21378 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* html: fix typo in UnescapeString string docsBrad Fitzpatrick2016-04-101-1/+1
| | | | | | | | Fixes #15221 Change-Id: I9e927a2f604213338b4572f1a32d0247c58bdc60 Reviewed-on: https://go-review.googlesource.com/21798 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* all: replace magic 0x80 with named constant utf8.RuneSelfMartin Möhrmann2016-04-101-1/+1
| | | | | | | | Change-Id: Id1c2e8e9d60588de866e8b6ca59cc83dd28f848f Reviewed-on: https://go-review.googlesource.com/21756 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* html/template: prefix the internally-used FuncMap values with an underscoreRob Pike2016-04-041-46/+46
| | | | | | | | | | | | This makes these names even less likely to collide with a real user-defined function. Fixes #13852. Change-Id: If5a8562c6797ced19c355c7ab2c86fc4401a8674 Reviewed-on: https://go-review.googlesource.com/21490 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
* all: use bytes.Equal, bytes.Contains and strings.ContainsDominik Honnef2016-04-012-2/+2
| | | | | | | Change-Id: Iba82a5bd3846f7ab038cc10ec72ff6bcd2c0b484 Reviewed-on: https://go-review.googlesource.com/21377 Run-TryBot: Dave Cheney <dave@cheney.net> Reviewed-by: Dave Cheney <dave@cheney.net>
* all: delete dead non-test codeDominik Honnef2016-03-251-4/+0
| | | | | | | | | | | | | | | | | | | | | | | This change removes a lot of dead code. Some of the code has never been used, not even when it was first commited. The rest shouldn't have survived refactors. This change doesn't remove unused routines helpful for debugging, nor does it remove code that's used in commented out blocks of code that are only unused temporarily. Furthermore, unused constants weren't removed when they were part of a set of constants from specifications. One noteworthy omission from this CL are about 1000 lines of unused code in cmd/fix, 700 lines of which are the typechecker, which hasn't been used ever since the pre-Go 1 fixes have been removed. I wasn't sure if this code should stick around for future uses of cmd/fix or be culled as well. Change-Id: Ib714bc7e487edc11ad23ba1c3222d1fd02e4a549 Reviewed-on: https://go-review.googlesource.com/20926 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* all: single space after period.Brad Fitzpatrick2016-03-022-3/+3
| | | | | | | | | | | | | | | | | | | | The tree's pretty inconsistent about single space vs double space after a period in documentation. Make it consistently a single space, per earlier decisions. This means contributors won't be confused by misleading precedence. This CL doesn't use go/doc to parse. It only addresses // comments. It was generated with: $ perl -i -npe 's,^(\s*// .+[a-z]\.) +([A-Z]),$1 $2,' $(git grep -l -E '^\s*//(.+\.) +([A-Z])') $ go test go/doc -update Change-Id: Iccdb99c37c797ef1f804a94b22ba5ee4b500c4f7 Reviewed-on: https://go-review.googlesource.com/20022 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Dave Day <djd@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* all: fix typos and spellingMartin Möhrmann2016-02-241-1/+1
| | | | | | | | Change-Id: Icd06d99c42b8299fd931c7da821e1f418684d913 Reviewed-on: https://go-review.googlesource.com/19829 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* text/template,html/template: correct comment in DefinedTemplatesRob Pike2015-11-241-1/+1
| | | | | | | | The prefix includes a semicolon. Change-Id: I4bdb79aa9931e835e297f3ea2c46a001cd123d56 Reviewed-on: https://go-review.googlesource.com/17200 Reviewed-by: Andrew Gerrand <adg@golang.org>
* html/template: add DefinedTemplates to html/templateRob Pike2015-11-231-1/+8
| | | | | | | | | | | | | | It is not important to add, since it's only used for creating an error message, but for consistency in the API between text/template and html/template it should be provided here. The implementation just calls the one in text/template. Fixes #13349. Change-Id: I0882849e06a58f1e38b00eb89d79ac39777309b2 Reviewed-on: https://go-review.googlesource.com/17172 Reviewed-by: Andrew Gerrand <adg@golang.org>
* html/template: Add missing error check to package example.Dmitri Shuralyov2015-11-221-0/+1
| | | | | | | | | | | | | This appears to be an unintended omission. The check func is declared just above, and the err value from template.Parse is captured rather than discarded via blank identifier. All following calls that similarly return err are checked, so it can't be that this example elides error checking for brevity. Finally, if you look at Example_autoescaping, it does check err from template.Parse and its code is very similar. Change-Id: I076e1846302d5f2cdb1d027ed85ca0db85e33ace Reviewed-on: https://go-review.googlesource.com/17170 Reviewed-by: Andrew Gerrand <adg@golang.org>
* html/template, encoding/asn1: fix test errorsDominik Honnef2015-11-101-1/+1
| | | | | | | | | | Change-Id: I1da1d718609eb6a7b78d29b173ec780bde22c687 Reviewed-on: https://go-review.googlesource.com/16422 Reviewed-by: Ralph Corderoy <ralph@inputplus.co.uk> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
* html/template: include itself while cloningNodir Turakulov2015-10-202-0/+30
| | | | | | | | | | | | | | template.Clone() initialized template set incorrectly: it didn't include itself. * include itself in template set while cloning * add a test Fixes #12996 Change-Id: I932530e4f7f1bbebf833e12b000a5ce052bc9223 Reviewed-on: https://go-review.googlesource.com/16104 Reviewed-by: Andrew Gerrand <adg@golang.org>
* text/template: change IsTrue to take interface{} instead of reflect.Value.David Symonds2015-10-011-2/+1
| | | | | | | | | | | This is a follow-up to a326c3e to avoid reflect being in the API. Fixes #12801. Change-Id: Ic4c2e592e2c35b5911f75d88f1d9c44787c80f30 Reviewed-on: https://go-review.googlesource.com/15240 Run-TryBot: David Symonds <dsymonds@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
* text/template, html/template: fix block example nameAndrew Gerrand2015-09-291-1/+1
| | | | | | Change-Id: I004a43842430201296363a9745480bee94920041 Reviewed-on: https://go-review.googlesource.com/15084 Reviewed-by: Andrew Gerrand <adg@golang.org>
* text/template, html/template: add block keyword and permit template redefinitionAndrew Gerrand2015-09-283-3/+49
| | | | | | | | | | | | | | | | | | This change adds a new "block" keyword that permits the definition of templates inline inside existing templates, and loosens the restriction on template redefinition. Templates may now be redefined, but in the html/template package they may only be redefined before the template is executed (and therefore escaped). The intention is that such inline templates can be redefined by subsequent template definitions, permitting a kind of template "inheritance" or "overlay". (See the example for details.) Fixes #3812 Change-Id: I733cb5332c1c201c235f759cc64333462e70dc27 Reviewed-on: https://go-review.googlesource.com/14005 Reviewed-by: Rob Pike <r@golang.org>
* text/template: export isTrueRob Pike2015-09-171-0/+8
| | | | | | | | | | | | The definition of 'truth' used by if etc. is not trivial to compute, so publish the implementation to allow custom template functions to have the same definition as the template language itself. Fixes #12033. Change-Id: Icdfd6039722d7d3f984ba0905105eb3253e14831 Reviewed-on: https://go-review.googlesource.com/14593 Reviewed-by: Andrew Gerrand <adg@golang.org>
* html/template: preserve attr in stateBeforeValueNodir Turakulov2015-09-093-62/+67
| | | | | | | | | | | | | | | Context: #12149. The problem there is that contents of <script type="text/template"> are treated as JS, and thus // is treated as regexp. Preserve context.attr while we are in the attribute, in particular in stateBeforeValue, so we have attr when reading attr value. Next CL will actually fix the bug. Change-Id: I99add2237b0885ecdcc08b4f7c25d0af99173e53 Reviewed-on: https://go-review.googlesource.com/14335 Reviewed-by: Rob Pike <r@golang.org>
* html: speed up UnescapeStringIngo Oeser2015-08-222-31/+49
| | | | | | | | | | | | | | | | | | | | | | | | | | Add benchmarks for for sparsely escaped and densely escaped strings. Then speed up the sparse unescaping part heavily by using IndexByte and copy to skip the parts containing no escaping very fast. Unescaping densely escaped strings slower because of the new function call overhead. But sparsely encoded strings are seen more often in the utf8 enabled web. We win part of the speed back by looking up entityName differently. benchmark old ns/op new ns/op delta BenchmarkEscape 31680 31396 -0.90% BenchmarkEscapeNone 6507 6872 +5.61% BenchmarkUnescape 36481 48298 +32.39% BenchmarkUnescapeNone 332 325 -2.11% BenchmarkUnescapeSparse 8836 3221 -63.55% BenchmarkUnescapeDense 30639 32224 +5.17% Change-Id: If606cb01897a40eefe35ba98f2ff23bb25251606 Reviewed-on: https://go-review.googlesource.com/10172 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* html/template: add examples to package and functionsCarlos C2015-07-271-0/+122
| | | | | | Change-Id: Ib4fb8256863d908580a07e6f2e1c92ea109ea989 Reviewed-on: https://go-review.googlesource.com/11249 Reviewed-by: Russ Cox <rsc@golang.org>
* all: link to https instead of httpBrad Fitzpatrick2015-07-111-1/+1
| | | | | | | | | | | | | The one in misc/makerelease/makerelease.go is particularly bad and probably warrants rotating our keys. I didn't update old weekly notes, and reverted some changes involving test code for now, since we're late in the Go 1.5 freeze. Otherwise, the rest are all auto-generated changes, and all manually reviewed. Change-Id: Ia2753576ab5d64826a167d259f48a2f50508792d Reviewed-on: https://go-review.googlesource.com/12048 Reviewed-by: Rob Pike <r@golang.org>
* html/template: prevent panic while escaping pipelinesDidier Spezia2015-06-272-1/+23
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | AFAIK, the documentation does not explicitly state whether variables can store a callable entity or not. I believe the current implementation in text/template assumes they cannot though. The call builtin function is supposed to be used for this purpose. Template "{{0|$}}" should generate an error at runtime, instead of a panic. Similarly, template "{{0|(nil)}}" should not generate a panic. This CL aborts the sanitization process for a given pipeline when no identifier can be derived from the selected node. It happens with malformed pipelines. We now have the following errors: {{ 0 | $ }} template: foo:1:10: executing "foo" at <$>: can't give argument to non-function $ {{ 0 | (nil) }} template: foo:1:11: executing "foo" at <nil>: nil is not a command Fixes #11118 Fixes #11356 Change-Id: Idae52f806849f4c9ab7aca1b4bb4b59a74723d0e Reviewed-on: https://go-review.googlesource.com/10823 Reviewed-by: Rob Pike <r@golang.org>
* html: add examples to the functionsCarlos C2015-06-191-0/+22
| | | | | | Change-Id: I129d70304ae4e4694d9217826b18b341e3834d3c Reviewed-on: https://go-review.googlesource.com/11201 Reviewed-by: Andrew Gerrand <adg@golang.org>
* html/template: prevent panic when escaping actions involving chain nodesDidier Spezia2015-06-012-1/+15
| | | | | | | | | | | | | | | | | | | | | | | | The current escape code panics when an action involves chain nodes. Such nodes can be seen in the following situation: {{ . | AAA.B }} - AAA being a registered function The above expression is actually valid, because AAA could return a map containing a B key. The tests in text/template explicitly demonstrate this case. Fix allIdents to cover also chain nodes. While I was investigating this issue, I realized that the tests introduced in similar CL 9621 were incorrect. Parse errors were caught as expected, but for the wrong reason. Fixed them as well. No changes in text/template code itself. Fixes #10801 Change-Id: Ic9fe43b63669298ca52c3f499e2725dd2bb818a8 Reviewed-on: https://go-review.googlesource.com/10340 Reviewed-by: Rob Pike <r@golang.org>
* html/template: fix string iteration in replacement operationsDidier Spezia2015-05-194-55/+51
| | | | | | | | | | | | | | | | | | | | | | | | | | | In css, js, and html, the replacement operations are implemented by iterating on strings (rune by rune). The for/range statement is used. The length of the rune is required and added to the index to properly slice the string. This is potentially wrong because there is a discrepancy between the result of utf8.RuneLen and the increment of the index (set by the for/range statement). For invalid strings, utf8.RuneLen('\ufffd') == 3, while the index is incremented only by 1 byte. htmlReplacer triggers a panic at slicing time for some invalid strings. Use a more robust iteration mechanism based on utf8.DecodeRuneInString, and make sure the same pattern is used for all similar functions in this package. Fixes #10799 Change-Id: Ibad3857b2819435d9fa564f06fc2ca8774102841 Reviewed-on: https://go-review.googlesource.com/10105 Reviewed-by: Rob Pike <r@golang.org>
* html: simplify and optimize escape/unescapeDidier Spezia2015-05-082-47/+50
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The html package uses some specific code to escape special characters. Actually, the strings.Replacer can be used instead, and is much more efficient. The converse operation is more complex but can still be slightly optimized. Credits to Ken Bloom (kabloom@google.com), who first submitted a similar patch at https://codereview.appspot.com/141930043 Added benchmarks and slightly optimized UnescapeString. benchmark old ns/op new ns/op delta BenchmarkEscape-4 118713 19825 -83.30% BenchmarkEscapeNone-4 87653 3784 -95.68% BenchmarkUnescape-4 24888 23417 -5.91% BenchmarkUnescapeNone-4 14423 157 -98.91% benchmark old allocs new allocs delta BenchmarkEscape-4 9 2 -77.78% BenchmarkEscapeNone-4 0 0 +0.00% BenchmarkUnescape-4 2 2 +0.00% BenchmarkUnescapeNone-4 0 0 +0.00% benchmark old bytes new bytes delta BenchmarkEscape-4 24800 12288 -50.45% BenchmarkEscapeNone-4 0 0 +0.00% BenchmarkUnescape-4 10240 10240 +0.00% BenchmarkUnescapeNone-4 0 0 +0.00% Fixes #8697 Change-Id: I208261ed7cbe9b3dee6317851f8c0cf15528bce4 Reviewed-on: https://go-review.googlesource.com/9808 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* html/template: fix pipeline sanitizationDidier Spezia2015-05-082-3/+13
| | | | | | | | | | | | | | | | | | Pipelines are altered by inserting sanitizers if they are not already present. The code makes the assumption that the first operands of each commands are function identifiers. This is wrong, since they can also be methods. It results in a panic with templates such as {{1|print 2|.f 3}} Adds an extra type assertion to make sure only identifiers are compared with sanitizers. Fixes #10673 Change-Id: I3eb820982675231dbfa970f197abc5ef335ce86b Reviewed-on: https://go-review.googlesource.com/9801 Reviewed-by: Rob Pike <r@golang.org>
* html/template: fix quadratic performance with special tagsDidier Spezia2015-04-302-6/+96
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The current implementation of the tSpecialTagEnd function is inefficient since it generates plenty of memory allocations and converts the whole buffer to lowercase at each call. If the number of special tags increases linearly with the template size, the complexity becomes quadratic. This CL provides an alternative implementation. While the algorithm is probably still not optimal, it avoids the quadratic behavior and the memory allocations. benchmark old ns/op new ns/op delta BenchmarkTemplateSpecialTags-4 19326431 532190 -97.25% benchmark old allocs new allocs delta BenchmarkTemplateSpecialTags-4 2650 190 -92.83% benchmark old bytes new bytes delta BenchmarkTemplateSpecialTags-4 4106460 46568 -98.87% While we are there, make sure we respect the HTML tokenization algorithm. An end tag needs to be followed by a space, tab, CR, FF, /, or > as described in https://html.spec.whatwg.org/multipage/syntax.html#tokenization Explicitly add this check. Fixes #10605 Change-Id: Ia33ddee164ab608a69ac4183e16ec506bbeaa54c Reviewed-on: https://go-review.googlesource.com/9502 Reviewed-by: Rob Pike <r@golang.org>
* text/template: provide a mechanism for optionsRob Pike2015-04-031-0/+23
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Add one option, which is the motivating example, a way to control what happens when a map is indexed with a key that is not in the map. Rather than do something specific for that case, we provide a simple general option mechanism to avoid adding API if something else comes up. This general approach also makes it easy for html/template to track (and adapt, should that become important). New method: Option(option string...). The option strings are key=value pairs or just simple strings (no =). New option: missingkey: Control the behavior during execution if a map is indexed with a key that is not present in the map. "missingkey=default" or "missingkey=invalid" The default behavior: Do nothing and continue execution. If printed, the result of the index operation is the string "<no value>". "missingkey=zero" The operation returns the zero value for the map type's element. "missingkey=error" Execution stops immediately with an error. Fixes #6288. Change-Id: Id811e2b99dc05aff324d517faac113ef3c25293a Reviewed-on: https://go-review.googlesource.com/8462 Reviewed-by: Robert Griesemer <gri@golang.org>
* html/template: fix crash when escaping incomplete templateRob Pike2015-03-202-0/+18
| | | | | | | | | | | | | | text/template turned this into an error but html/template crashed. Refactor text/template.Execute to export a new function, text/template.DefinedTemplates, so html/template can get the same helpful error message in this case, and invoke it when there is no definition for a template being escaped. Fixes #10204. Change-Id: I1d04e9e7ebca829bc08509caeb65e75da969711f Reviewed-on: https://go-review.googlesource.com/7855 Reviewed-by: Russ Cox <rsc@golang.org>
* all: don't refer to code.google.com/p/go{,-wiki}/Péter Surányi2015-02-062-2/+2
| | | | | | | | | | Only documentation / comment changes. Update references to point to golang.org permalinks or go.googlesource.com/go. References in historical release notes under doc are left as is. Change-Id: Icfc14e4998723e2c2d48f9877a91c5abef6794ea Reviewed-on: https://go-review.googlesource.com/4060 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* html/template: fix example codeAndrew Gerrand2015-01-211-1/+1
| | | | | | | | Fixes #9651 Change-Id: I987833b6263482a402e58fcd9eeb0e42401599b5 Reviewed-on: https://go-review.googlesource.com/3073 Reviewed-by: Robert Griesemer <gri@golang.org>
* html/template: fix build after encoding/js escaping changeAndrew Gerrand2014-10-281-1/+1
| | | | | | | TBR=rsc R=golang-codereviews CC=golang-codereviews https://golang.org/cl/159590043
* build: move package sources from src/pkg to srcRuss Cox2014-09-0823-0/+9484
Preparation was in CL 134570043. This CL contains only the effect of 'hg mv src/pkg/* src'. For more about the move, see golang.org/s/go14nopkg.