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