github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/html/template/context.go (about) 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 // stateCSS occurs inside a <style> element or style attribute. 132 stateCSS 133 // stateCSSDqStr occurs inside a CSS double quoted string. 134 stateCSSDqStr 135 // stateCSSSqStr occurs inside a CSS single quoted string. 136 stateCSSSqStr 137 // stateCSSDqURL occurs inside a CSS double quoted url("..."). 138 stateCSSDqURL 139 // stateCSSSqURL occurs inside a CSS single quoted url('...'). 140 stateCSSSqURL 141 // stateCSSURL occurs inside a CSS unquoted url(...). 142 stateCSSURL 143 // stateCSSBlockCmt occurs inside a CSS /* block comment */. 144 stateCSSBlockCmt 145 // stateCSSLineCmt occurs inside a CSS // line comment. 146 stateCSSLineCmt 147 // stateError is an infectious error state outside any valid 148 // HTML/CSS/JS construct. 149 stateError 150 // stateDead marks unreachable code after a {{break}} or {{continue}}. 151 stateDead 152 ) 153 154 // isComment is true for any state that contains content meant for template 155 // authors & maintainers, not for end-users or machines. 156 func isComment(s state) bool { 157 switch s { 158 case stateHTMLCmt, stateJSBlockCmt, stateJSLineCmt, stateCSSBlockCmt, stateCSSLineCmt: 159 return true 160 } 161 return false 162 } 163 164 // isInTag return whether s occurs solely inside an HTML tag. 165 func isInTag(s state) bool { 166 switch s { 167 case stateTag, stateAttrName, stateAfterName, stateBeforeValue, stateAttr: 168 return true 169 } 170 return false 171 } 172 173 // delim is the delimiter that will end the current HTML attribute. 174 type delim uint8 175 176 //go:generate stringer -type delim 177 178 const ( 179 // delimNone occurs outside any attribute. 180 delimNone delim = iota 181 // delimDoubleQuote occurs when a double quote (") closes the attribute. 182 delimDoubleQuote 183 // delimSingleQuote occurs when a single quote (') closes the attribute. 184 delimSingleQuote 185 // delimSpaceOrTagEnd occurs when a space or right angle bracket (>) 186 // closes the attribute. 187 delimSpaceOrTagEnd 188 ) 189 190 // urlPart identifies a part in an RFC 3986 hierarchical URL to allow different 191 // encoding strategies. 192 type urlPart uint8 193 194 //go:generate stringer -type urlPart 195 196 const ( 197 // urlPartNone occurs when not in a URL, or possibly at the start: 198 // ^ in "^http://auth/path?k=v#frag". 199 urlPartNone urlPart = iota 200 // urlPartPreQuery occurs in the scheme, authority, or path; between the 201 // ^s in "h^ttp://auth/path^?k=v#frag". 202 urlPartPreQuery 203 // urlPartQueryOrFrag occurs in the query portion between the ^s in 204 // "http://auth/path?^k=v#frag^". 205 urlPartQueryOrFrag 206 // urlPartUnknown occurs due to joining of contexts both before and 207 // after the query separator. 208 urlPartUnknown 209 ) 210 211 // jsCtx determines whether a '/' starts a regular expression literal or a 212 // division operator. 213 type jsCtx uint8 214 215 //go:generate stringer -type jsCtx 216 217 const ( 218 // jsCtxRegexp occurs where a '/' would start a regexp literal. 219 jsCtxRegexp jsCtx = iota 220 // jsCtxDivOp occurs where a '/' would start a division operator. 221 jsCtxDivOp 222 // jsCtxUnknown occurs where a '/' is ambiguous due to context joining. 223 jsCtxUnknown 224 ) 225 226 // element identifies the HTML element when inside a start tag or special body. 227 // Certain HTML element (for example <script> and <style>) have bodies that are 228 // treated differently from stateText so the element type is necessary to 229 // transition into the correct context at the end of a tag and to identify the 230 // end delimiter for the body. 231 type element uint8 232 233 //go:generate stringer -type element 234 235 const ( 236 // elementNone occurs outside a special tag or special element body. 237 elementNone element = iota 238 // elementScript corresponds to the raw text <script> element 239 // with JS MIME type or no type attribute. 240 elementScript 241 // elementStyle corresponds to the raw text <style> element. 242 elementStyle 243 // elementTextarea corresponds to the RCDATA <textarea> element. 244 elementTextarea 245 // elementTitle corresponds to the RCDATA <title> element. 246 elementTitle 247 ) 248 249 //go:generate stringer -type attr 250 251 // attr identifies the current HTML attribute when inside the attribute, 252 // that is, starting from stateAttrName until stateTag/stateText (exclusive). 253 type attr uint8 254 255 const ( 256 // attrNone corresponds to a normal attribute or no attribute. 257 attrNone attr = iota 258 // attrScript corresponds to an event handler attribute. 259 attrScript 260 // attrScriptType corresponds to the type attribute in script HTML element 261 attrScriptType 262 // attrStyle corresponds to the style attribute whose value is CSS. 263 attrStyle 264 // attrURL corresponds to an attribute whose value is a URL. 265 attrURL 266 // attrSrcset corresponds to a srcset attribute. 267 attrSrcset 268 )