aboutsummaryrefslogtreecommitdiff
path: root/src/text
AgeCommit message (Collapse)Author
2016-02-19all: replace strings.Index with strings.Contains where possibleNathan VanBenschoten
Change-Id: Ia613f1c37bfce800ece0533a5326fca91d99a66a Reviewed-on: https://go-review.googlesource.com/18120 Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org>
2016-02-18text/scanner: mention package when reporting errors to stderrRobert Griesemer
Fixes #14166. Change-Id: I325b283a1d53e73a6d862611c446820ab94a161c Reviewed-on: https://go-review.googlesource.com/19622 Reviewed-by: Damien Neil <dneil@google.com>
2016-01-21text/template: fix documentation for pipelinesRob Pike
The header was in the wrong place, so the definition of a pipeline was not in the section labeled "Pipelines". Fixes #13972 Change-Id: Ibca791a4511ca112047b57091c391f6e959fdd78 Reviewed-on: https://go-review.googlesource.com/18775 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-11-24text/template,html/template: correct comment in DefinedTemplatesRob Pike
The prefix includes a semicolon. Change-Id: I4bdb79aa9931e835e297f3ea2c46a001cd123d56 Reviewed-on: https://go-review.googlesource.com/17200 Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-10-28text/template: remove redundant indirectNodir Turakulov
Change-Id: I8cc9783fd044bed48347824dcf973c61c78275a5 Reviewed-on: https://go-review.googlesource.com/15833 Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-10-15text/template: resolve non-empty interfaceNodir Turakulov
Read what a non-empty interface points to. The deleted lines were added in https://codereview.appspot.com/4810060/, which attempted to break an infinite loop. That was a long time ago. If I just delete these lines with current codebase, the test "bug1" (added in that CL) does not fail. All new tests fail without this fix. Fixes #12924 Change-Id: I9370ca44facd6af3019850aa065b936e5a482d37 Reviewed-on: https://go-review.googlesource.com/15809 Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-10-08text/template: fix formatting calls in testsAlexander Morozov
Change-Id: I8e94fa57482149f6ea8f13d02ddcc82d6764ddb8 Reviewed-on: https://go-review.googlesource.com/15496 Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-10-01text/template: change IsTrue to take interface{} instead of reflect.Value.David Symonds
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>
2015-09-29text/template, html/template: fix block example nameAndrew Gerrand
Change-Id: I004a43842430201296363a9745480bee94920041 Reviewed-on: https://go-review.googlesource.com/15084 Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-09-28text/template, html/template: add block keyword and permit template redefinitionAndrew Gerrand
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>
2015-09-17text/template: export isTrueRob Pike
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>
2015-09-15text/template: verify that names in FuncMap are valid identifiersRob Pike
There was no verification in Funcs that the map had valid names, which meant that the error could only be caught when parsing the template that tried to use them. Fix this by validating the names in Funcs and panicking before parsing if there is a bad name. This is arguably an API change, since it didn't trigger a panic before, but Funcs did already panic if the function itself was no good, so I argue it's an acceptable change to add more sanity checks. Fixes #9685. Change-Id: Iabf1d0602c49d830f3ed71ca1ccc7eb9a5521ff5 Reviewed-on: https://go-review.googlesource.com/14562 Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-09-09text/template: perform value validity checksNodir Turakulov
Check reflect.Value.IsValid() before calling other reflect.Value methods that panic on zero values. Added tests for cases with untyped nils. They panicked without these fixes. Removed a TODO. Fixes #12356 Change-Id: I9b5cbed26db09a0a7c36d99a93f8b9729899d51e Reviewed-on: https://go-review.googlesource.com/14340 Reviewed-by: Rob Pike <r@golang.org>
2015-09-09text/template: provide a way to trim leading and trailing space between actionsRob Pike
Borrowing a suggestion from the issue listed below, we modify the lexer to trim spaces at the beginning (end) of a block of text if the action immediately before (after) is marked with a minus sign. To avoid parsing/lexing ambiguity, we require an ASCII space between the minus sign and the rest of the action. Thus: {{23 -}} < {{- 45}} produces the output 23<45 All the work is done in the lexer. The modification is invisible to the parser or any outside package (except I guess for noticing some gaps in the input if one tracks error positions). Thus it slips in without worry in text/template and html/template both. Fixes long-requested issue #9969. Change-Id: I3774be650bfa6370cb993d0899aa669c211de7b2 Reviewed-on: https://go-review.googlesource.com/14391 Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-09-09all: minor documentation tweaks for constantsKonstantin Shaposhnikov
Block comments appear after a block in the HTML documentation generated by godoc. Words like "following" should be avoided. Change-Id: Iedfad67f4b8b9c84f128b98b9b06fa76919af388 Reviewed-on: https://go-review.googlesource.com/14357 Reviewed-by: Rob Pike <r@golang.org>
2015-08-27text/template: add ExecError type and return it from Execute on errorRob Pike
Useful to discriminate evaluation errors from write errors. Fixes #11898. Change-Id: I907d339a3820e887872d78e0e2d8fd011451fd19 Reviewed-on: https://go-review.googlesource.com/13957 Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-06-24text/template: make zero Template work againRuss Cox
Fixes #11379. Change-Id: Idbb5c3faad472b77e9867dd2d4551fef5e4ac5f1 Reviewed-on: https://go-review.googlesource.com/11421 Reviewed-by: Rob Pike <r@golang.org>
2015-06-24text/scanner: add runnable example for packageCarlos C
Change-Id: I42a952b04a56fb888fa7d5d9c2b56cbdd3434034 Reviewed-on: https://go-review.googlesource.com/11246 Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-06-23text/template: explain better the semantics of ParseFilesRob Pike
Documentation change only. Fixes #11247. Change-Id: Ib412de2d643292dbe42b56dee955bdb877aee81b Reviewed-on: https://go-review.googlesource.com/11329 Reviewed-by: David Symonds <dsymonds@golang.org>
2015-06-18all: switch to the new deprecation conventionShenghou Ma
While we're at it, move some misplaced comment blocks around. Change-Id: I1847d7f1ca1dbb8e5de737203c4ed6c66e112508 Reviewed-on: https://go-review.googlesource.com/10188 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2015-06-03text/template: refactor code to accomodate bi-state requirement for templatesAamir Khan
This is follow-up to CL10607 - Refactor AddParseTree() to use t.associate() - Refactor Parse() to use AddParseTree() to put entries into common structure - Clone() should not put entry in t.tmpl for undefined template - Clarify documentation for Templates() - Clarify documentation for AddParseTree() to include the error case Updates #10910 Uodates #10926 Includes test cases for most of the above changes Change-Id: I25b2fce6f9651272866f881acf44e4dbca04a4a8 Reviewed-on: https://go-review.googlesource.com/10622 Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-06-02text/template: clarify the documentation around template definitionsRob Pike
Due to the requirements of parsing template definitions that mention other templates that are not yet defined, a Template can be in two states: defined and undefined. Thus, although one calls New, the resulting template has no definition even though it exists as a data structure. Thus, for example, will return nil for a template that is named but not yet defined. Fixes #10910 Fixes #10926 Clarify the documentation a little to explain this, Also tidy up the code a little and remove a spurious call to init. Change-Id: I22cc083291500bca424e83dc12807e0de7b00b7a Reviewed-on: https://go-review.googlesource.com/10641 Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-06-01text/template: fix variadic function call corner caseDidier Spezia
Executing a template involving variadic functions featuring a []interface{} slice (such as printf) could result in a panic in reflect.Value.Call, due to incorrect type checking. The following expressions failed (with a panic): {{true|printf}} {{1|printf}} {{1.1|printf}} {{'x'|printf}} {{1+2i|printf}} Implemented proper type checks for the fixed parameters of the variadic functions. Fixes #10946 Change-Id: Ia75333f651f73b3d2e024cb0c47cc30d90cb6852 Reviewed-on: https://go-review.googlesource.com/10403 Reviewed-by: Rob Pike <r@golang.org>
2015-06-01html/template: prevent panic when escaping actions involving chain nodesDidier Spezia
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>
2015-06-01text/template: template must be initialized at the time of creationAamir Khan
t.init() should be called at the time of template creation i.e, template.New() and t.New() instead of later in the process. - Removed calls of t.init() from t.Parse(), t.Execute(), t.Funcs() - Also got rid of t.common != nil checks as it should never be nil Fixes #10879 Change-Id: I1b7ac812f02c841ae80037babce7e2b0a2df13e8 Reviewed-on: https://go-review.googlesource.com/10240 Reviewed-by: Rob Pike <r@golang.org>
2015-05-16text/template: fix race condition on function mapsDidier Spezia
The Template objects are supposed to be goroutine-safe once they have been parsed. This includes the text and html ones. For html/template, the escape mechanism is triggered at execution time. It may alter the internal structures of the template, so a mutex protects them against concurrent accesses. The text/template package is free of any synchronization primitive. A race condition may occur when nested templates are escaped: the escape algorithm alters the function maps of the associated text templates, while a concurrent template execution may access the function maps in read mode. The less invasive fix I have found is to introduce a RWMutex in text/template to protect the function maps. This is unfortunate but it should be effective. Fixes #9945 Change-Id: I1edb73c0ed0f1fcddd2f1516230b548b92ab1269 Reviewed-on: https://go-review.googlesource.com/10101 Reviewed-by: Rob Pike <r@golang.org>
2015-05-14text/template: need to validate type when an argument is a function callRob Pike
Missed a case; just need to call validateType. Fixes #10800. Change-Id: I81997ca7a9feb1be31c8b47e631b32712d7ffb86 Reviewed-on: https://go-review.googlesource.com/10031 Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-05-13text/scanner: avoid further reads after EOFRobert Griesemer
Fixes #10735. Change-Id: I5c6e424653657c89da176136ac56597c7565abe5 Reviewed-on: https://go-review.googlesource.com/10039 Reviewed-by: Rob Pike <r@golang.org>
2015-05-07text/template: delete obsolete nil checkRob Pike
This was added during testing but is unnecessary. Thanks to gravis on GitHub for catching it. See #10574. Change-Id: I4a8f76d237e67f5a0ea189a0f3cadddbf426778a Reviewed-on: https://go-review.googlesource.com/9841 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-05-05text/template: check for malformed pipelinesDidier Spezia
Catch some malformed pipelines at parsing time. The current code accepts pipelines such as: {{12|.}} {{"hello"|print|false}} {{.|"blah blah"}} Such pipelines generate panic in html/template at execution time. Add an extra check to verify all the commands of the pipeline are executable (except for the first one). Fixes #10610 Change-Id: Id72236ba8f76a59fa284fe3d4c2cb073e50b51f1 Reviewed-on: https://go-review.googlesource.com/9626 Reviewed-by: Rob Pike <r@golang.org>
2015-05-05text/template: shut down lexing goroutine on errorRob Pike
When a parse error occurred, the lexing goroutine would lay idle. It's not likely a problem but if the program is for some reason accepting badly formed data repeatedly, it's wasteful. The solution is easy: Just drain the input on error. We know this will succeed because the input is always a string and is therefore guaranteed finite. With debugging prints in the package tests I've shown this is effective, shutting down 79 goroutines that would otherwise linger, out of 123 total. Fixes #10574. Change-Id: I8aa536e327b219189a7e7f604a116fa562ae1c39 Reviewed-on: https://go-review.googlesource.com/9658 Reviewed-by: Russ Cox <rsc@golang.org>
2015-05-04text/template: ensures code consistency in lexerAymerick
At the end of lexInsideAction(), we return lexInsideAction: this is the default behaviour when we are still parsing an action. But some switch branches return lexInsideAction too. So let's ensure code consistency by always reaching the end of the lexInsideAction function when needed. Change-Id: I7e9d8d6e51f29ecd6db6bdd63b36017845d95368 Reviewed-on: https://go-review.googlesource.com/9441 Reviewed-by: Rob Pike <r@golang.org>
2015-05-03text/template/parse: huge integers are not floatsRob Pike
Ideal constants in the template package are a little different from Go. This is a case that slipped through the cracks: A huge integer number was accepted as a floating-point number, but this loses precision and is confusing. Also, the code in the template package (as opposed to the parse package) wasn't expecting it. Root this out at the source: If an integer doesn't fit an int64 or uint64, complain right away. Change-Id: I375621e6f5333c4d53f053a3c84a9af051711b7a Reviewed-on: https://go-review.googlesource.com/9651 Reviewed-by: Russ Cox <rsc@golang.org>
2015-05-02text/template: check for literals in chain of termsDidier Spezia
The current parser ignores obvious errors such as: {{0.1.E}} {{true.any}} {{"hello".wrong}} {{nil.E}} The common problem is that a chain is built from a literal value. It then panics at execution time. Furthermore, a double dot triggers the same behavior: {{..E}} Addresses a TODO left in Tree.operand to catch these errors at parsing time. Note that identifiers can include a '.', and pipelines could return an object which a field can be derived from (like a variable), so they are excluded from the check. Fixes #10615 Change-Id: I903706d1c17861b5a8354632c291e73c9c0bc4e1 Reviewed-on: https://go-review.googlesource.com/9621 Reviewed-by: Rob Pike <r@golang.org>
2015-05-01text/template: detect unmatched else at parsing timeDidier Spezia
An unmatched {{else}} should trigger a parsing error. The top level parser is able to issue an error in case of unmatched {{end}}. It does it a posteriori (i.e. after having parsed the action). Extend this behavior to also check for unmatched {{else}} Fixes #10611 Change-Id: I1d4f433cc64e11bea5f4d61419ccc707ac01bb1d Reviewed-on: https://go-review.googlesource.com/9620 Reviewed-by: Rob Pike <r@golang.org>
2015-05-01text/template: allow newlines in raw quotesRob Pike
This was disallowed for error-checking reasons but people ask for it, it's easy, and it's clear what it all means. Fixes #7323. Change-Id: I26542f5ac6519e45b335ad789713a4d9e356279b Reviewed-on: https://go-review.googlesource.com/9537 Reviewed-by: Russ Cox <rsc@golang.org>
2015-04-03text/template: provide a mechanism for optionsRob Pike
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>
2015-04-01text/scanner: Fix EOF reporting on strange ReadersEvan Phoenix
Currently, scanner uses -1 to represent 2 different states: 1. I haven't yet scanned anything, call it "Beginning of File" 2. I've reached the end of the input, ie EOF The result of this behavior is that calling Peek() when next() has detected the end of the input and set s.ch to scanner.EOF, is that Peek() things "oh, s.ch is < 0, which to me means that I haven't scanned any next yet, let me try and clear the BOM marker." When this behavior is run on a typical IO, next() will issue a Read and get (0, io.EOF) back for the second time without blocking and Peek() will return scanner.EOF. The bug comes into play when, inside a terminal, hitting Control-D. This causes the terminal to return a EOF condition to the reader but it does not actually close the fd. So, combining these 2 situations, we arrive at the bug: What is expected: hitting Control-D in a terminal will make Peek() return scanner.EOF instantly. What actually happens: 0. Code waiting in Next() 1. User hits Control-D 2. fd returns EOF condition 3. EOF bubbles it's way out to line 249 in scanner.go 4. next() returns scanner.EOF 5. Next() saves the scanner.EOF to s.ch and returns the previous value 6. Peek() runs, sees s.ch < 0, mistakenly thinks it hasn't run yet and tries to read the BOM marker. 7. next() sees the buffer is empty and tries to fill it again, blocking on line 249. The fix is simple: use a different code to indicate that no data has been scanned. Change-Id: Iee8f4da5881682c4d4c36b93b9bf397ac5798179 Reviewed-on: https://go-review.googlesource.com/7913 Reviewed-by: Robert Griesemer <gri@golang.org>
2015-03-20html/template: fix crash when escaping incomplete templateRob Pike
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>
2015-03-18all: use "reports whether" in place of "returns true if(f)"Josh Bleecher Snyder
Comment changes only. Change-Id: I56848814564c4aa0988b451df18bebdfc88d6d94 Reviewed-on: https://go-review.googlesource.com/7721 Reviewed-by: Rob Pike <r@golang.org>
2015-03-16text/template: protect against explicit nil in field chainsRob Pike
An explicit nil in an expression like nil.Foo caused a panic because the evaluator attempted to reflect on the nil. A typeless nil like this cannot be used to do anything, so just error out. Fixes #9426 Change-Id: Icd2c9c7533dda742748bf161eced163991a12f54 Reviewed-on: https://go-review.googlesource.com/7643 Reviewed-by: David Symonds <dsymonds@golang.org>
2014-10-18text/template: fix bug in pipelined variadicsRob Pike
Simple bug in argument processing: The final arg may be the pipeline value, in which case it gets bound to the fixed argument section. The code got that wrong. Easy to fix. Fixes #8950. LGTM=bradfitz R=golang-codereviews, bradfitz CC=golang-codereviews https://golang.org/cl/161750043
2014-09-22text/template: type-check chained node as argumentRob Pike
Was just a missing case (literally) in the type checker. Fixes #8473. LGTM=adg R=golang-codereviews, adg CC=golang-codereviews https://golang.org/cl/142460043
2014-09-22text/template: allow comparison functions to work between any integersRob Pike
Previously, signed and unsigned integers could not be compared, but this has problems with things like comparing 'x' with a byte in a string. Since signed and unsigned integers have a well-defined ordering, even though their types are different, and since we already allow comparison regardless of the size of the integers, why not allow it regardless of the sign? Integers only, a fine place to draw the line. Fixes #7489. LGTM=adg R=golang-codereviews, adg CC=golang-codereviews https://golang.org/cl/149780043
2014-09-08build: move package sources from src/pkg to srcRuss Cox
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.