// Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package html import ( "fmt" ) // Error describes a problem encountered during template Escaping. type Error struct { // ErrorCode describes the kind of error. ErrorCode ErrorCode // Name is the name of the template in which the error was encountered. Name string // Line is the line number of the error in the template source or 0. Line int // Description is a human-readable description of the problem. Description string } // ErrorCode is a code for a kind of error. type ErrorCode int // We define codes for each error that manifests while escaping templates, but // escaped templates may also fail at runtime. // // Output: "ZgotmplZ" // Example: // // where {{.X}} evaluates to `javascript:...` // Discussion: // "ZgotmplZ" is a special value that indicates that unsafe content reached a // CSS or URL context at runtime. The output of the example will be // // If the data comes from a trusted source, use content types to exempt it // from filtering: URL(`javascript:...`). const ( // OK indicates the lack of an error. OK ErrorCode = iota // ErrorAmbigContext: "... appears in an ambiguous URL context" // Example: // // Discussion: // {{.X}} is in an ambiguous URL context since, depending on {{.C}}, // it may be either a URL suffix or a query parameter. // Moving {{.X}} into the condition removes the ambiguity: // ErrAmbigContext // TODO: document ErrBadHTML // ErrBranchEnd: "{{if}} branches end in different contexts" // Example: // {{if .C}}{{template "helper"}} {{end}} // {{define "helper"}} document.write('
{{end}} // {{define "attrs"}}href="{{.URL}}"{{end}} // Discussion: // EscapeSet looks through template calls to compute the context. // Here the {{.URL}} in "attrs" must be treated as a URL when called // from "main", but if "attrs" is not in set when // EscapeSet(&set, "main") is called, this error will arise. ErrNoSuchTemplate // TODO: document ErrOutputContext // ErrPartialCharset: "unfinished JS regexp charset in ..." // Example: // // Discussion: // EscapeSet does not support interpolation into regular expression // literal character sets. ErrPartialCharset // ErrPartialEscape: "unfinished escape sequence in ..." // Example: // // Discussion: // EscapeSet does not support actions following a backslash. // This is usually an error and there are better solutions; for // our example // // should work, and if {{.X}} is a partial escape sequence such as // "xA0", mark the whole sequence as safe content: JSStr(`\xA0`) ErrPartialEscape // ErrRangeLoopReentry: "on range loop re-entry: ..." // Example: // {{range .}}

tag is missing a '>'. // EscapeSet cannot tell whether {{.}} is meant to be an HTML class or // the content of a broken

element and complains because the // second iteration would produce something like // //