func (e *escaper) computeOutCtx(c context, t *template.Template) context {
	c1, ok := e.escapeTemplateBody(c, t)
	if !ok {
		if c2, ok2 := e.escapeTemplateBody(c1, t); ok2 {
			c1, ok = c2, true
		}
	}
	if !ok && c1.state != stateError {
		return context{
			state: stateError,
			err:   errorf(ErrOutputContext, t.Tree.Root, 0, "cannot compute output context for template %s", t.Name()),
		}
	}
	return c1
}
computeOutCtx takes a template and its start context and computes the output
context while storing any inferences in e.
package template
import (
	"bytes"
	"fmt"
	"html"
	"reflect"
	"strings"
	"text/template"
	"text/template/parse"
)
func escapeTemplate(tmpl *Template, node parse.Node, name string) error {
	c, _ := tmpl.esc.escapeTree(context{}, node, name, 0)
	var err error
	if c.err != nil {
		err, c.err.Name = c.err, name
	} else if c.state != stateText {
		err = &Error{ErrEndContext, nil, name, 0, fmt.Sprintf("ends in a non-text context: %+v", c)}
	}
	if err != nil {
		if t := tmpl.set[name]; t != nil {
			t.escapeErr = err
			t.text.Tree = nil
			t.Tree = nil
		}
		return err
	}
	tmpl.esc.commit()
	if t := tmpl.set[name]; t != nil {
		t.escapeErr = errEscapeOK
		t.Tree = t.text.Tree
	}
	return nil
}
func evalArgs(args ...interface{}) string {
	if len(args) == 1 {
		if s, ok := args[0].(string); ok {
			return s
		}
	}
	for i, arg := range args {
		args[i] = indirectToStringerOrError(arg)
	}
	return fmt.Sprint(args...)
}
type escaper struct {
	ns *nameSpace
	output map[string]context
	derived map[string]*template.Template
	called map[string]bool
	actionNodeEdits   map[*parse.ActionNode][]string
	templateNodeEdits map[*parse.TemplateNode]string
	textNodeEdits     map[*parse.TextNode][]byte
}
func makeEscaper(n *nameSpace) escaper {
	return escaper{
		n,
		map[string]context{},
		map[string]*template.Template{},
		map[string]bool{},
		map[*parse.ActionNode][]string{},
		map[*parse.TemplateNode]string{},
		map[*parse.TextNode][]byte{},
	}
}
func (e *escaper) escape(c context, n parse.Node) context {
	switch n := n.(type) {
	case *parse.ActionNode:
		return e.escapeAction(c, n)
	case *parse.IfNode:
		return e.escapeBranch(c, &n.BranchNode, "if")
	case *parse.ListNode:
		return e.escapeList(c, n)
	case *parse.RangeNode:
		return e.escapeBranch(c, &n.BranchNode, "range")
	case *parse.TemplateNode:
		return e.escapeTemplate(c, n)
	case *parse.TextNode:
		return e.escapeText(c, n)
	case *parse.WithNode:
		return e.escapeBranch(c, &n.BranchNode, "with")
	}
	panic("escaping " + n.String() + " is unimplemented")
}
func (e *escaper) escapeAction(c context, n *parse.ActionNode) context {
	if len(n.Pipe.Decl) != 0 {
		return c
	}
	c = nudge(c)
	for pos, idNode := range n.Pipe.Cmds {
		node, ok := idNode.Args[0].(*parse.IdentifierNode)
		if !ok {
			continue
		}
		ident := node.Ident
		if _, ok := predefinedEscapers[ident]; ok {
			if pos < len(n.Pipe.Cmds)-1 ||
				c.state == stateAttr && c.delim == delimSpaceOrTagEnd && ident == "html" {
				return context{
					state: stateError,
					err:   errorf(ErrPredefinedEscaper, n, n.Line, "predefined escaper %q disallowed in template", ident),
				}
			}
		}
	}
	switch c.state {
	case stateError:
		return c
	case stateAttrName, stateTag:
		c.state = stateAttrName
	}
	s, err := sanitizerForContext(c)
	if err != nil {
		return context{
			state: stateError,
			err: errorf(ErrEscapeAction, n, n.Line, "cannot escape action %v: %s", n, err),
		}
	}
	e.editActionNode(n, s)
	return c
}
func ensurePipelineContains(p *parse.PipeNode, s []string) {
	if len(s) == 0 {
		return
	}
	pipelineLen := len(p.Cmds)
	if pipelineLen > 0 {
		lastCmd := p.Cmds[pipelineLen-1]
		if idNode, ok := lastCmd.Args[0].(*parse.IdentifierNode); ok {
			if esc := idNode.Ident; predefinedEscapers[esc] {
				if len(p.Cmds) == 1 && len(lastCmd.Args) > 1 {
					lastCmd.Args[0] = parse.NewIdentifier(evalArgsFuncName).SetTree(nil).SetPos(lastCmd.Args[0].Position())
					p.Cmds = append(p.Cmds, newIdentCmd(esc, p.Position()))
					pipelineLen++
				}
				dup := false
				for i, escaper := range s {
					if escFnsEq(esc, escaper) {
						s[i] = idNode.Ident
						dup = true
					}
				}
				if dup {
					pipelineLen--
				}
			}
		}
	}
	newCmds := make([]*parse.CommandNode, pipelineLen, pipelineLen+len(s))
	copy(newCmds, p.Cmds)
	for _, name := range s {
		newCmds = append(newCmds, newIdentCmd(name, p.Position()))
	}
	p.Cmds = newCmds
}
var predefinedEscapers = map[string]bool{
	"html":     true,
	"urlquery": true,
}
var equivEscapers = map[string]string{
	sanitizeHTMLFuncName:   "html",
	sanitizeRCDATAFuncName: "html",
	queryEscapeURLFuncName: "urlquery",
	normalizeURLFuncName: "urlquery",
}
func escFnsEq(a, b string) bool {
	return normalizeEscFn(a) == normalizeEscFn(b)
}
func normalizeEscFn(e string) string {
	if norm := equivEscapers[e]; norm != "" {
		return norm
	}
	return e
}
func newIdentCmd(identifier string, pos parse.Pos) *parse.CommandNode {
	return &parse.CommandNode{
		NodeType: parse.NodeCommand,
		Args:     []parse.Node{parse.NewIdentifier(identifier).SetTree(nil).SetPos(pos)},  
		Pos:      pos,
	}
}
func nudge(c context) context {
	switch c.state {
	case stateTag:
		c.state = stateAttrName
	case stateBeforeValue:
		c.state, c.delim = stateAttr, delimSpaceOrTagEnd
	case stateAfterName:
		c.state = stateAttrName
	}
	return c
}
func join(a, b context, node parse.Node, nodeName string) context {
	if a.state == stateError {
		return a
	}
	if b.state == stateError {
		return b
	}
	a.element.names = joinNames(a.element.name, b.element.name, a.element.names, b.element.names)
	a.attr.names = joinNames(a.attr.name, b.attr.name, a.attr.names, b.attr.names)
	if a.attr.value != b.attr.value {
		a.attr.ambiguousValue = true
	}
	if a.eq(b) {
		return a
	}
	c := a
	c.element.name = b.element.name
	if c.eq(b) {
		return c
	}
	c = a
	c.attr.name = b.attr.name
	if c.eq(b) {
		return c
	}
	if c, d := nudge(a), nudge(b); !(c.eq(a) && d.eq(b)) {
		if e := join(c, d, node, nodeName); e.state != stateError {
			return e
		}
	}
	return context{
		state: stateError,
		err:   errorf(ErrBranchEnd, node, 0, "{{%s}} branches end in different contexts: %v, %v", nodeName, a, b),
	}
}
func joinNames(aName, bName string, aNames, bNames []string) []string {
	var ret []string
	if aName != bName {
		ret = append(ret, aName, bName)
	}
	aNamesSet := make(map[string]bool)
	for _, name := range aNames {
		aNamesSet[name] = true
	}
	for _, name := range bNames {
		if !aNamesSet[name] {
			ret = append(ret, name)
		}
	}
	return ret
}
func (e *escaper) escapeBranch(c context, n *parse.BranchNode, nodeName string) context {
	c0 := e.escapeList(c, n.List)
	if nodeName == "range" && c0.state != stateError {
		c1, _ := e.escapeListConditionally(c0, n.List, nil)
		c0 = join(c0, c1, n, nodeName)
		if c0.state == stateError {
			c0.err.Line = n.Line
			c0.err.Description = "on range loop re-entry: " + c0.err.Description
			return c0
		}
	}
	c1 := e.escapeList(c, n.ElseList)
	return join(c0, c1, n, nodeName)
}
func (e *escaper) escapeList(c context, n *parse.ListNode) context {
	if n == nil {
		return c
	}
	for _, m := range n.Nodes {
		c = e.escape(c, m)
	}
	return c
}
func (e *escaper) escapeListConditionally(c context, n *parse.ListNode, filter func(*escaper, context) bool) (context, bool) {
	e1 := makeEscaper(e.ns)
	for k, v := range e.output {
		e1.output[k] = v
	}
	c = e1.escapeList(c, n)
	ok := filter != nil && filter(&e1, c)
	if ok {
		for k, v := range e1.output {
			e.output[k] = v
		}
		for k, v := range e1.derived {
			e.derived[k] = v
		}
		for k, v := range e1.called {
			e.called[k] = v
		}
		for k, v := range e1.actionNodeEdits {
			e.editActionNode(k, v)
		}
		for k, v := range e1.templateNodeEdits {
			e.editTemplateNode(k, v)
		}
		for k, v := range e1.textNodeEdits {
			e.editTextNode(k, v)
		}
	}
	return c, ok
}
func (e *escaper) escapeTemplate(c context, n *parse.TemplateNode) context {
	c, name := e.escapeTree(c, n, n.Name, n.Line)
	if name != n.Name {
		e.editTemplateNode(n, name)
	}
	return c
}
func mangle(c context, templateName string) string {
	if c.state == stateText {
		return templateName
	}
	s := templateName + "$htmltemplate_" + c.state.String()
	if c.delim != 0 {
		s += "_" + c.delim.String()
	}
	if c.attr.name != "" {
		s += "_" + c.attr.String()
	}
	if c.element.name != "" {
		s += "_" + c.element.String()
	}
	return s
}
func (e *escaper) escapeTree(c context, node parse.Node, name string, line int) (context, string) {
	dname := mangle(c, name)
	e.called[dname] = true
	if out, ok := e.output[dname]; ok {
		return out, dname
	}
	t := e.template(name)
	if t == nil {
		if e.ns.set[name] != nil {
			return context{
				state: stateError,
				err:   errorf(ErrNoSuchTemplate, node, line, "%q is an incomplete or empty template", name),
			}, dname
		}
		return context{
			state: stateError,
			err:   errorf(ErrNoSuchTemplate, node, line, "no such template %q", name),
		}, dname
	}
	if dname != name {
		dt := e.template(dname)
		if dt == nil {
			dt = template.New(dname)
			dt.Tree = t.Tree.Copy()
			dt.Tree.Name = dname
			e.derived[dname] = dt
		}
		t = dt
	}
	return e.computeOutCtx(c, t), dname
}