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