// 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 template import ( "fmt" "text/template/parse" ) // context describes the state an HTML parser must be in when it reaches the // portion of HTML produced by evaluating a particular template node. // // The zero value of type context is the start context for a template that // produces an HTML fragment as defined at // https://www.w3.org/TR/html5/syntax.html#the-end // where the context element is null. type context struct { state state delim delim urlPart urlPart jsCtx jsCtx attr attr element element n parse.Node // for range break/continue err *Error } func (c context) String() string { var err error if c.err != nil { err = c.err } return fmt.Sprintf("{%v %v %v %v %v %v %v}", c.state, c.delim, c.urlPart, c.jsCtx, c.attr, c.element, err) } // eq reports whether two contexts are equal. func (c context) eq(d context) bool { return c.state == d.state && c.delim == d.delim && c.urlPart == d.urlPart && c.jsCtx == d.jsCtx && c.attr == d.attr && c.element == d.element && c.err == d.err } // mangle produces an identifier that includes a suffix that distinguishes it // from template names mangled with different contexts. func (c context) mangle(templateName string) string { // The mangled name for the default context is the input templateName. if c.state == stateText { return templateName } s := templateName + "$htmltemplate_" + c.state.String() if c.delim != delimNone { s += "_" + c.delim.String() } if c.urlPart != urlPartNone { s += "_" + c.urlPart.String() } if c.jsCtx != jsCtxRegexp { s += "_" + c.jsCtx.String() } if c.attr != attrNone { s += "_" + c.attr.String() } if c.element != elementNone { s += "_" + c.element.String() } return s } // state describes a high-level HTML parser state. // // It bounds the top of the element stack, and by extension the HTML insertion // mode, but also contains state that does not correspond to anything in the // HTML5 parsing algorithm because a single token production in the HTML // grammar may contain embedded actions in a template. For instance, the quoted // HTML attribute produced by // //