Source file src/html/template/context.go
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. 4 5 package template 6 7 import ( 8 "fmt" 9 "text/template/parse" 10 ) 11 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. 14 // 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. 19 type context struct { 20 state state 21 delim delim 22 urlPart urlPart 23 jsCtx jsCtx 24 attr attr 25 element element 26 n parse.Node // for range break/continue 27 err *Error 28 } 29 30 func (c context) String() string { 31 var err error 32 if c.err != nil { 33 err = c.err 34 } 35 return fmt.Sprintf("{%v %v %v %v %v %v %v}", c.state, c.delim, c.urlPart, c.jsCtx, c.attr, c.element, err) 36 } 37 38 // eq reports whether two contexts are equal. 39 func (c context) eq(d context) bool { 40 return c.state == d.state && 41 c.delim == d.delim && 42 c.urlPart == d.urlPart && 43 c.jsCtx == d.jsCtx && 44 c.attr == d.attr && 45 c.element == d.element && 46 c.err == d.err 47 } 48 49 // mangle produces an identifier that includes a suffix that distinguishes it 50 // from template names mangled with different contexts. 51 func (c context) mangle(templateName string) string { 52 // The mangled name for the default context is the input templateName. 53 if c.state == stateText { 54 return templateName 55 } 56 s := templateName + "$htmltemplate_" + c.state.String() 57 if c.delim != delimNone { 58 s += "_" + c.delim.String() 59 } 60 if c.urlPart != urlPartNone { 61 s += "_" + c.urlPart.String() 62 } 63 if c.jsCtx != jsCtxRegexp { 64 s += "_" + c.jsCtx.String() 65 } 66 if c.attr != attrNone { 67 s += "_" + c.attr.String() 68 } 69 if c.element != elementNone { 70 s += "_" + c.element.String() 71 } 72 return s 73 } 74 75 // state describes a high-level HTML parser state. 76 // 77 // It bounds the top of the element stack, and by extension the HTML insertion 78 // mode, but also contains state that does not correspond to anything in the 79 // HTML5 parsing algorithm because a single token production in the HTML 80 // grammar may contain embedded actions in a template. For instance, the quoted 81 // HTML attribute produced by 82 // 83 // <div title="Hello {{.World}}"> 84 // 85 // is a single token in HTML's grammar but in a template spans several nodes. 86 type state uint8 87 88 //go:generate stringer -type state 89 90 const ( 91 // stateText is parsed character data. An HTML parser is in 92 // this state when its parse position is outside an HTML tag, 93 // directive, comment, and special element body. 94 stateText state = iota 95 // stateTag occurs before an HTML attribute or the end of a tag. 96 stateTag 97 // stateAttrName occurs inside an attribute name. 98 // It occurs between the ^'s in ` ^name^ = value`. 99 stateAttrName 100 // stateAfterName occurs after an attr name has ended but before any 101 // equals sign. It occurs between the ^'s in ` name^ ^= value`. 102 stateAfterName 103 // stateBeforeValue occurs after the equals sign but before the value. 104 // It occurs between the ^'s in ` name =^ ^value`. 105 stateBeforeValue 106 // stateHTMLCmt occurs inside an <!-- HTML comment -->. 107 stateHTMLCmt 108 // stateRCDATA occurs inside an RCDATA element (<textarea> or <title>) 109 // as described at https://www.w3.org/TR/html5/syntax.html#elements-0 110 stateRCDATA 111 // stateAttr occurs inside an HTML attribute whose content is text. 112 stateAttr 113 // stateURL occurs inside an HTML attribute whose content is a URL. 114 stateURL 115 // stateSrcset occurs inside an HTML srcset attribute. 116 stateSrcset 117 // stateJS occurs inside an event handler or script element. 118 stateJS 119 // stateJSDqStr occurs inside a JavaScript double quoted string. 120 stateJSDqStr 121 // stateJSSqStr occurs inside a JavaScript single quoted string. 122 stateJSSqStr 123 // stateJSBqStr occurs inside a JavaScript back quoted string. 124 stateJSBqStr 125 // stateJSRegexp occurs inside a JavaScript regexp literal. 126 stateJSRegexp 127 // stateJSBlockCmt occurs inside a JavaScript /* block comment */. 128 stateJSBlockCmt 129 // stateJSLineCmt occurs inside a JavaScript // line comment. 130 stateJSLineCmt 131 // stateJSHTMLOpenCmt occurs inside a JavaScript <!-- HTML-like comment. 132 stateJSHTMLOpenCmt 133 // stateJSHTMLCloseCmt occurs inside a JavaScript --> HTML-like comment. 134 stateJSHTMLCloseCmt 135 // stateCSS occurs inside a <style> element or style attribute. 136 stateCSS 137 // stateCSSDqStr occurs inside a CSS double quoted string. 138 stateCSSDqStr 139 // stateCSSSqStr occurs inside a CSS single quoted string. 140 stateCSSSqStr 141 // stateCSSDqURL occurs inside a CSS double quoted url("..."). 142 stateCSSDqURL 143 // stateCSSSqURL occurs inside a CSS single quoted url('...'). 144 stateCSSSqURL 145 // stateCSSURL occurs inside a CSS unquoted url(...). 146 stateCSSURL 147 // stateCSSBlockCmt occurs inside a CSS /* block comment */. 148 stateCSSBlockCmt 149 // stateCSSLineCmt occurs inside a CSS // line comment. 150 stateCSSLineCmt 151 // stateError is an infectious error state outside any valid 152 // HTML/CSS/JS construct. 153 stateError 154 // stateDead marks unreachable code after a {{break}} or {{continue}}. 155 stateDead 156 ) 157 158 // isComment is true for any state that contains content meant for template 159 // authors & maintainers, not for end-users or machines. 160 func isComment(s state) bool { 161 switch s { 162 case stateHTMLCmt, stateJSBlockCmt, stateJSLineCmt, stateJSHTMLOpenCmt, stateJSHTMLCloseCmt, stateCSSBlockCmt, stateCSSLineCmt: 163 return true 164 } 165 return false 166 } 167 168 // isInTag return whether s occurs solely inside an HTML tag. 169 func isInTag(s state) bool { 170 switch s { 171 case stateTag, stateAttrName, stateAfterName, stateBeforeValue, stateAttr: 172 return true 173 } 174 return false 175 } 176 177 // isInScriptLiteral returns true if s is one of the literal states within a 178 // <script> tag, and as such occurances of "<!--", "<script", and "</script" 179 // need to be treated specially. 180 func isInScriptLiteral(s state) bool { 181 // Ignore the comment states (stateJSBlockCmt, stateJSLineCmt, 182 // stateJSHTMLOpenCmt, stateJSHTMLCloseCmt) because their content is already 183 // omitted from the output. 184 switch s { 185 case stateJSDqStr, stateJSSqStr, stateJSBqStr, stateJSRegexp: 186 return true 187 } 188 return false 189 } 190 191 // delim is the delimiter that will end the current HTML attribute. 192 type delim uint8 193 194 //go:generate stringer -type delim 195 196 const ( 197 // delimNone occurs outside any attribute. 198 delimNone delim = iota 199 // delimDoubleQuote occurs when a double quote (") closes the attribute. 200 delimDoubleQuote 201 // delimSingleQuote occurs when a single quote (') closes the attribute. 202 delimSingleQuote 203 // delimSpaceOrTagEnd occurs when a space or right angle bracket (>) 204 // closes the attribute. 205 delimSpaceOrTagEnd 206 ) 207 208 // urlPart identifies a part in an RFC 3986 hierarchical URL to allow different 209 // encoding strategies. 210 type urlPart uint8 211 212 //go:generate stringer -type urlPart 213 214 const ( 215 // urlPartNone occurs when not in a URL, or possibly at the start: 216 // ^ in "^http://auth/path?k=v#frag". 217 urlPartNone urlPart = iota 218 // urlPartPreQuery occurs in the scheme, authority, or path; between the 219 // ^s in "h^ttp://auth/path^?k=v#frag". 220 urlPartPreQuery 221 // urlPartQueryOrFrag occurs in the query portion between the ^s in 222 // "http://auth/path?^k=v#frag^". 223 urlPartQueryOrFrag 224 // urlPartUnknown occurs due to joining of contexts both before and 225 // after the query separator. 226 urlPartUnknown 227 ) 228 229 // jsCtx determines whether a '/' starts a regular expression literal or a 230 // division operator. 231 type jsCtx uint8 232 233 //go:generate stringer -type jsCtx 234 235 const ( 236 // jsCtxRegexp occurs where a '/' would start a regexp literal. 237 jsCtxRegexp jsCtx = iota 238 // jsCtxDivOp occurs where a '/' would start a division operator. 239 jsCtxDivOp 240 // jsCtxUnknown occurs where a '/' is ambiguous due to context joining. 241 jsCtxUnknown 242 ) 243 244 // element identifies the HTML element when inside a start tag or special body. 245 // Certain HTML element (for example <script> and <style>) have bodies that are 246 // treated differently from stateText so the element type is necessary to 247 // transition into the correct context at the end of a tag and to identify the 248 // end delimiter for the body. 249 type element uint8 250 251 //go:generate stringer -type element 252 253 const ( 254 // elementNone occurs outside a special tag or special element body. 255 elementNone element = iota 256 // elementScript corresponds to the raw text <script> element 257 // with JS MIME type or no type attribute. 258 elementScript 259 // elementStyle corresponds to the raw text <style> element. 260 elementStyle 261 // elementTextarea corresponds to the RCDATA <textarea> element. 262 elementTextarea 263 // elementTitle corresponds to the RCDATA <title> element. 264 elementTitle 265 ) 266 267 //go:generate stringer -type attr 268 269 // attr identifies the current HTML attribute when inside the attribute, 270 // that is, starting from stateAttrName until stateTag/stateText (exclusive). 271 type attr uint8 272 273 const ( 274 // attrNone corresponds to a normal attribute or no attribute. 275 attrNone attr = iota 276 // attrScript corresponds to an event handler attribute. 277 attrScript 278 // attrScriptType corresponds to the type attribute in script HTML element 279 attrScriptType 280 // attrStyle corresponds to the style attribute whose value is CSS. 281 attrStyle 282 // attrURL corresponds to an attribute whose value is a URL. 283 attrURL 284 // attrSrcset corresponds to a srcset attribute. 285 attrSrcset 286 ) 287