| 1 |
747 |
jeremybenn |
// Copyright 2011 The Go Authors. All rights reserved.
|
| 2 |
|
|
// Use of this source code is governed by a BSD-style
|
| 3 |
|
|
// license that can be found in the LICENSE file.
|
| 4 |
|
|
|
| 5 |
|
|
package template
|
| 6 |
|
|
|
| 7 |
|
|
import (
|
| 8 |
|
|
"fmt"
|
| 9 |
|
|
)
|
| 10 |
|
|
|
| 11 |
|
|
// context describes the state an HTML parser must be in when it reaches the
|
| 12 |
|
|
// portion of HTML produced by evaluating a particular template node.
|
| 13 |
|
|
//
|
| 14 |
|
|
// The zero value of type context is the start context for a template that
|
| 15 |
|
|
// produces an HTML fragment as defined at
|
| 16 |
|
|
// http://www.w3.org/TR/html5/the-end.html#parsing-html-fragments
|
| 17 |
|
|
// where the context element is null.
|
| 18 |
|
|
type context struct {
|
| 19 |
|
|
state state
|
| 20 |
|
|
delim delim
|
| 21 |
|
|
urlPart urlPart
|
| 22 |
|
|
jsCtx jsCtx
|
| 23 |
|
|
attr attr
|
| 24 |
|
|
element element
|
| 25 |
|
|
err *Error
|
| 26 |
|
|
}
|
| 27 |
|
|
|
| 28 |
|
|
func (c context) String() string {
|
| 29 |
|
|
return fmt.Sprintf("{%v %v %v %v %v %v %v}", c.state, c.delim, c.urlPart, c.jsCtx, c.attr, c.element, c.err)
|
| 30 |
|
|
}
|
| 31 |
|
|
|
| 32 |
|
|
// eq returns whether two contexts are equal.
|
| 33 |
|
|
func (c context) eq(d context) bool {
|
| 34 |
|
|
return c.state == d.state &&
|
| 35 |
|
|
c.delim == d.delim &&
|
| 36 |
|
|
c.urlPart == d.urlPart &&
|
| 37 |
|
|
c.jsCtx == d.jsCtx &&
|
| 38 |
|
|
c.attr == d.attr &&
|
| 39 |
|
|
c.element == d.element &&
|
| 40 |
|
|
c.err == d.err
|
| 41 |
|
|
}
|
| 42 |
|
|
|
| 43 |
|
|
// mangle produces an identifier that includes a suffix that distinguishes it
|
| 44 |
|
|
// from template names mangled with different contexts.
|
| 45 |
|
|
func (c context) mangle(templateName string) string {
|
| 46 |
|
|
// The mangled name for the default context is the input templateName.
|
| 47 |
|
|
if c.state == stateText {
|
| 48 |
|
|
return templateName
|
| 49 |
|
|
}
|
| 50 |
|
|
s := templateName + "$htmltemplate_" + c.state.String()
|
| 51 |
|
|
if c.delim != 0 {
|
| 52 |
|
|
s += "_" + c.delim.String()
|
| 53 |
|
|
}
|
| 54 |
|
|
if c.urlPart != 0 {
|
| 55 |
|
|
s += "_" + c.urlPart.String()
|
| 56 |
|
|
}
|
| 57 |
|
|
if c.jsCtx != 0 {
|
| 58 |
|
|
s += "_" + c.jsCtx.String()
|
| 59 |
|
|
}
|
| 60 |
|
|
if c.attr != 0 {
|
| 61 |
|
|
s += "_" + c.attr.String()
|
| 62 |
|
|
}
|
| 63 |
|
|
if c.element != 0 {
|
| 64 |
|
|
s += "_" + c.element.String()
|
| 65 |
|
|
}
|
| 66 |
|
|
return s
|
| 67 |
|
|
}
|
| 68 |
|
|
|
| 69 |
|
|
// state describes a high-level HTML parser state.
|
| 70 |
|
|
//
|
| 71 |
|
|
// It bounds the top of the element stack, and by extension the HTML insertion
|
| 72 |
|
|
// mode, but also contains state that does not correspond to anything in the
|
| 73 |
|
|
// HTML5 parsing algorithm because a single token production in the HTML
|
| 74 |
|
|
// grammar may contain embedded actions in a template. For instance, the quoted
|
| 75 |
|
|
// HTML attribute produced by
|
| 76 |
|
|
//
|
| 77 |
|
|
// is a single token in HTML's grammar but in a template spans several nodes.
|
| 78 |
|
|
type state uint8
|
| 79 |
|
|
|
| 80 |
|
|
const (
|
| 81 |
|
|
// stateText is parsed character data. An HTML parser is in
|
| 82 |
|
|
// this state when its parse position is outside an HTML tag,
|
| 83 |
|
|
// directive, comment, and special element body.
|
| 84 |
|
|
stateText state = iota
|
| 85 |
|
|
// stateTag occurs before an HTML attribute or the end of a tag.
|
| 86 |
|
|
stateTag
|
| 87 |
|
|
// stateAttrName occurs inside an attribute name.
|
| 88 |
|
|
// It occurs between the ^'s in ` ^name^ = value`.
|
| 89 |
|
|
stateAttrName
|
| 90 |
|
|
// stateAfterName occurs after an attr name has ended but before any
|
| 91 |
|
|
// equals sign. It occurs between the ^'s in ` name^ ^= value`.
|
| 92 |
|
|
stateAfterName
|
| 93 |
|
|
// stateBeforeValue occurs after the equals sign but before the value.
|
| 94 |
|
|
// It occurs between the ^'s in ` name =^ ^value`.
|
| 95 |
|
|
stateBeforeValue
|
| 96 |
|
|
// stateHTMLCmt occurs inside an .
|
| 97 |
|
|
stateHTMLCmt
|
| 98 |
|
|
// stateRCDATA occurs inside an RCDATA element (
|
| 99 |
|
|
// as described at http://dev.w3.org/html5/spec/syntax.html#elements-0
|
| 100 |
|
|
stateRCDATA
|
| 101 |
|
|
// stateAttr occurs inside an HTML attribute whose content is text.
|
| 102 |
|
|
stateAttr
|
| 103 |
|
|
// stateURL occurs inside an HTML attribute whose content is a URL.
|
| 104 |
|
|
stateURL
|
| 105 |
|
|
// stateJS occurs inside an event handler or script element.
|
| 106 |
|
|
stateJS
|
| 107 |
|
|
// stateJSDqStr occurs inside a JavaScript double quoted string.
|
| 108 |
|
|
stateJSDqStr
|
| 109 |
|
|
// stateJSSqStr occurs inside a JavaScript single quoted string.
|
| 110 |
|
|
stateJSSqStr
|
| 111 |
|
|
// stateJSRegexp occurs inside a JavaScript regexp literal.
|
| 112 |
|
|
stateJSRegexp
|
| 113 |
|
|
// stateJSBlockCmt occurs inside a JavaScript /* block comment */.
|
| 114 |
|
|
stateJSBlockCmt
|
| 115 |
|
|
// stateJSLineCmt occurs inside a JavaScript // line comment.
|
| 116 |
|
|
stateJSLineCmt
|
| 117 |
|
|
// stateCSS occurs inside a
|