1 // 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.
12 // context describes the state an HTML parser must be in when it reaches the
13 // portion of HTML produced by evaluating a particular template node.
15 // The zero value of type context is the start context for a template that
16 // produces an HTML fragment as defined at
17 // https://www.w3.org/TR/html5/syntax.html#the-end
18 // where the context element is null.
28 n parse.Node // for range break/continue
32 func (c context) String() string {
37 return fmt.Sprintf("{%v %v %v %v %v %v %v}", c.state, c.delim, c.urlPart, c.jsCtx, c.attr, c.element, err)
40 // eq reports whether two contexts are equal.
41 func (c context) eq(d context) bool {
42 return c.state == d.state &&
44 c.urlPart == d.urlPart &&
47 c.element == d.element &&
51 // mangle produces an identifier that includes a suffix that distinguishes it
52 // from template names mangled with different contexts.
53 func (c context) mangle(templateName string) string {
54 // The mangled name for the default context is the input templateName.
55 if c.state == stateText {
58 s := templateName + "$htmltemplate_" + c.state.String()
59 if c.delim != delimNone {
60 s += "_" + c.delim.String()
62 if c.urlPart != urlPartNone {
63 s += "_" + c.urlPart.String()
65 if c.jsCtx != jsCtxRegexp {
66 s += "_" + c.jsCtx.String()
68 if c.attr != attrNone {
69 s += "_" + c.attr.String()
71 if c.element != elementNone {
72 s += "_" + c.element.String()
77 // state describes a high-level HTML parser state.
79 // It bounds the top of the element stack, and by extension the HTML insertion
80 // mode, but also contains state that does not correspond to anything in the
81 // HTML5 parsing algorithm because a single token production in the HTML
82 // grammar may contain embedded actions in a template. For instance, the quoted
83 // HTML attribute produced by
85 // <div title="Hello {{.World}}">
87 // is a single token in HTML's grammar but in a template spans several nodes.
90 //go:generate stringer -type state
93 // stateText is parsed character data. An HTML parser is in
94 // this state when its parse position is outside an HTML tag,
95 // directive, comment, and special element body.
96 stateText state = iota
97 // stateTag occurs before an HTML attribute or the end of a tag.
99 // stateAttrName occurs inside an attribute name.
100 // It occurs between the ^'s in ` ^name^ = value`.
102 // stateAfterName occurs after an attr name has ended but before any
103 // equals sign. It occurs between the ^'s in ` name^ ^= value`.
105 // stateBeforeValue occurs after the equals sign but before the value.
106 // It occurs between the ^'s in ` name =^ ^value`.
108 // stateHTMLCmt occurs inside an <!-- HTML comment -->.
110 // stateRCDATA occurs inside an RCDATA element (<textarea> or <title>)
111 // as described at https://www.w3.org/TR/html5/syntax.html#elements-0
113 // stateAttr occurs inside an HTML attribute whose content is text.
115 // stateURL occurs inside an HTML attribute whose content is a URL.
117 // stateSrcset occurs inside an HTML srcset attribute.
119 // stateJS occurs inside an event handler or script element.
121 // stateJSDqStr occurs inside a JavaScript double quoted string.
123 // stateJSSqStr occurs inside a JavaScript single quoted string.
125 // stateJSTmplLit occurs inside a JavaScript back quoted string.
127 // stateJSRegexp occurs inside a JavaScript regexp literal.
129 // stateJSBlockCmt occurs inside a JavaScript /* block comment */.
131 // stateJSLineCmt occurs inside a JavaScript // line comment.
133 // stateJSHTMLOpenCmt occurs inside a JavaScript <!-- HTML-like comment.
135 // stateJSHTMLCloseCmt occurs inside a JavaScript --> HTML-like comment.
137 // stateCSS occurs inside a <style> element or style attribute.
139 // stateCSSDqStr occurs inside a CSS double quoted string.
141 // stateCSSSqStr occurs inside a CSS single quoted string.
143 // stateCSSDqURL occurs inside a CSS double quoted url("...").
145 // stateCSSSqURL occurs inside a CSS single quoted url('...').
147 // stateCSSURL occurs inside a CSS unquoted url(...).
149 // stateCSSBlockCmt occurs inside a CSS /* block comment */.
151 // stateCSSLineCmt occurs inside a CSS // line comment.
153 // stateError is an infectious error state outside any valid
154 // HTML/CSS/JS construct.
156 // stateDead marks unreachable code after a {{break}} or {{continue}}.
160 // isComment is true for any state that contains content meant for template
161 // authors & maintainers, not for end-users or machines.
162 func isComment(s state) bool {
164 case stateHTMLCmt, stateJSBlockCmt, stateJSLineCmt, stateJSHTMLOpenCmt, stateJSHTMLCloseCmt, stateCSSBlockCmt, stateCSSLineCmt:
170 // isInTag return whether s occurs solely inside an HTML tag.
171 func isInTag(s state) bool {
173 case stateTag, stateAttrName, stateAfterName, stateBeforeValue, stateAttr:
179 // isInScriptLiteral returns true if s is one of the literal states within a
180 // <script> tag, and as such occurrences of "<!--", "<script", and "</script"
181 // need to be treated specially.
182 func isInScriptLiteral(s state) bool {
183 // Ignore the comment states (stateJSBlockCmt, stateJSLineCmt,
184 // stateJSHTMLOpenCmt, stateJSHTMLCloseCmt) because their content is already
185 // omitted from the output.
187 case stateJSDqStr, stateJSSqStr, stateJSTmplLit, stateJSRegexp:
193 // delim is the delimiter that will end the current HTML attribute.
196 //go:generate stringer -type delim
199 // delimNone occurs outside any attribute.
200 delimNone delim = iota
201 // delimDoubleQuote occurs when a double quote (") closes the attribute.
203 // delimSingleQuote occurs when a single quote (') closes the attribute.
205 // delimSpaceOrTagEnd occurs when a space or right angle bracket (>)
206 // closes the attribute.
210 // urlPart identifies a part in an RFC 3986 hierarchical URL to allow different
211 // encoding strategies.
214 //go:generate stringer -type urlPart
217 // urlPartNone occurs when not in a URL, or possibly at the start:
218 // ^ in "^http://auth/path?k=v#frag".
219 urlPartNone urlPart = iota
220 // urlPartPreQuery occurs in the scheme, authority, or path; between the
221 // ^s in "h^ttp://auth/path^?k=v#frag".
223 // urlPartQueryOrFrag occurs in the query portion between the ^s in
224 // "http://auth/path?^k=v#frag^".
226 // urlPartUnknown occurs due to joining of contexts both before and
227 // after the query separator.
231 // jsCtx determines whether a '/' starts a regular expression literal or a
232 // division operator.
235 //go:generate stringer -type jsCtx
238 // jsCtxRegexp occurs where a '/' would start a regexp literal.
239 jsCtxRegexp jsCtx = iota
240 // jsCtxDivOp occurs where a '/' would start a division operator.
242 // jsCtxUnknown occurs where a '/' is ambiguous due to context joining.
246 // element identifies the HTML element when inside a start tag or special body.
247 // Certain HTML element (for example <script> and <style>) have bodies that are
248 // treated differently from stateText so the element type is necessary to
249 // transition into the correct context at the end of a tag and to identify the
250 // end delimiter for the body.
253 //go:generate stringer -type element
256 // elementNone occurs outside a special tag or special element body.
257 elementNone element = iota
258 // elementScript corresponds to the raw text <script> element
259 // with JS MIME type or no type attribute.
261 // elementStyle corresponds to the raw text <style> element.
263 // elementTextarea corresponds to the RCDATA <textarea> element.
265 // elementTitle corresponds to the RCDATA <title> element.
269 //go:generate stringer -type attr
271 // attr identifies the current HTML attribute when inside the attribute,
272 // that is, starting from stateAttrName until stateTag/stateText (exclusive).
276 // attrNone corresponds to a normal attribute or no attribute.
278 // attrScript corresponds to an event handler attribute.
280 // attrScriptType corresponds to the type attribute in script HTML element
282 // attrStyle corresponds to the style attribute whose value is CSS.
284 // attrURL corresponds to an attribute whose value is a URL.
286 // attrSrcset corresponds to a srcset attribute.