1
2
3
4
5 package template
6
7 import (
8 "fmt"
9 )
10
11
12
13
14
15
16
17
18 type context struct {
19 state state
20 delim delim
21 urlPart urlPart
22 jsCtx jsCtx
23 attr attr
24 element element
25 err *Error
26 }
27
28 func (c context) String() string {
29 return fmt.Sprintf("{%v %v %v %v %v %v %v}", c.state, c.delim, c.urlPart, c.jsCtx, c.attr, c.element, c.err)
30 }
31
32
33 func (c context) eq(d context) bool {
34 return c.state == d.state &&
35 c.delim == d.delim &&
36 c.urlPart == d.urlPart &&
37 c.jsCtx == d.jsCtx &&
38 c.attr == d.attr &&
39 c.element == d.element &&
40 c.err == d.err
41 }
42
43
44
45 func (c context) mangle(templateName string) string {
46
47 if c.state == stateText {
48 return templateName
49 }
50 s := templateName + "$htmltemplate_" + c.state.String()
51 if c.delim != 0 {
52 s += "_" + c.delim.String()
53 }
54 if c.urlPart != 0 {
55 s += "_" + c.urlPart.String()
56 }
57 if c.jsCtx != 0 {
58 s += "_" + c.jsCtx.String()
59 }
60 if c.attr != 0 {
61 s += "_" + c.attr.String()
62 }
63 if c.element != 0 {
64 s += "_" + c.element.String()
65 }
66 return s
67 }
68
69
70
71
72
73
74
75
76
77
78 type state uint8
79
80 const (
81
82
83
84 stateText state = iota
85
86 stateTag
87
88
89 stateAttrName
90
91
92 stateAfterName
93
94
95 stateBeforeValue
96
97 stateHTMLCmt
98
99
100 stateRCDATA
101
102 stateAttr
103
104 stateURL
105
106 stateSrcset
107
108 stateJS
109
110 stateJSDqStr
111
112 stateJSSqStr
113
114 stateJSRegexp
115
116 stateJSBlockCmt
117
118 stateJSLineCmt
119
120 stateCSS
121
122 stateCSSDqStr
123
124 stateCSSSqStr
125
126 stateCSSDqURL
127
128 stateCSSSqURL
129
130 stateCSSURL
131
132 stateCSSBlockCmt
133
134 stateCSSLineCmt
135
136
137 stateError
138 )
139
140 var stateNames = [...]string{
141 stateText: "stateText",
142 stateTag: "stateTag",
143 stateAttrName: "stateAttrName",
144 stateAfterName: "stateAfterName",
145 stateBeforeValue: "stateBeforeValue",
146 stateHTMLCmt: "stateHTMLCmt",
147 stateRCDATA: "stateRCDATA",
148 stateAttr: "stateAttr",
149 stateURL: "stateURL",
150 stateSrcset: "stateSrcset",
151 stateJS: "stateJS",
152 stateJSDqStr: "stateJSDqStr",
153 stateJSSqStr: "stateJSSqStr",
154 stateJSRegexp: "stateJSRegexp",
155 stateJSBlockCmt: "stateJSBlockCmt",
156 stateJSLineCmt: "stateJSLineCmt",
157 stateCSS: "stateCSS",
158 stateCSSDqStr: "stateCSSDqStr",
159 stateCSSSqStr: "stateCSSSqStr",
160 stateCSSDqURL: "stateCSSDqURL",
161 stateCSSSqURL: "stateCSSSqURL",
162 stateCSSURL: "stateCSSURL",
163 stateCSSBlockCmt: "stateCSSBlockCmt",
164 stateCSSLineCmt: "stateCSSLineCmt",
165 stateError: "stateError",
166 }
167
168 func (s state) String() string {
169 if int(s) < len(stateNames) {
170 return stateNames[s]
171 }
172 return fmt.Sprintf("illegal state %d", int(s))
173 }
174
175
176
177 func isComment(s state) bool {
178 switch s {
179 case stateHTMLCmt, stateJSBlockCmt, stateJSLineCmt, stateCSSBlockCmt, stateCSSLineCmt:
180 return true
181 }
182 return false
183 }
184
185
186 func isInTag(s state) bool {
187 switch s {
188 case stateTag, stateAttrName, stateAfterName, stateBeforeValue, stateAttr:
189 return true
190 }
191 return false
192 }
193
194
195 type delim uint8
196
197 const (
198
199 delimNone delim = iota
200
201 delimDoubleQuote
202
203 delimSingleQuote
204
205
206 delimSpaceOrTagEnd
207 )
208
209 var delimNames = [...]string{
210 delimNone: "delimNone",
211 delimDoubleQuote: "delimDoubleQuote",
212 delimSingleQuote: "delimSingleQuote",
213 delimSpaceOrTagEnd: "delimSpaceOrTagEnd",
214 }
215
216 func (d delim) String() string {
217 if int(d) < len(delimNames) {
218 return delimNames[d]
219 }
220 return fmt.Sprintf("illegal delim %d", int(d))
221 }
222
223
224
225 type urlPart uint8
226
227 const (
228
229
230 urlPartNone urlPart = iota
231
232
233 urlPartPreQuery
234
235
236 urlPartQueryOrFrag
237
238
239 urlPartUnknown
240 )
241
242 var urlPartNames = [...]string{
243 urlPartNone: "urlPartNone",
244 urlPartPreQuery: "urlPartPreQuery",
245 urlPartQueryOrFrag: "urlPartQueryOrFrag",
246 urlPartUnknown: "urlPartUnknown",
247 }
248
249 func (u urlPart) String() string {
250 if int(u) < len(urlPartNames) {
251 return urlPartNames[u]
252 }
253 return fmt.Sprintf("illegal urlPart %d", int(u))
254 }
255
256
257
258 type jsCtx uint8
259
260 const (
261
262 jsCtxRegexp jsCtx = iota
263
264 jsCtxDivOp
265
266 jsCtxUnknown
267 )
268
269 func (c jsCtx) String() string {
270 switch c {
271 case jsCtxRegexp:
272 return "jsCtxRegexp"
273 case jsCtxDivOp:
274 return "jsCtxDivOp"
275 case jsCtxUnknown:
276 return "jsCtxUnknown"
277 }
278 return fmt.Sprintf("illegal jsCtx %d", int(c))
279 }
280
281
282
283
284
285
286 type element uint8
287
288 const (
289
290 elementNone element = iota
291
292
293 elementScript
294
295 elementStyle
296
297 elementTextarea
298
299 elementTitle
300 )
301
302 var elementNames = [...]string{
303 elementNone: "elementNone",
304 elementScript: "elementScript",
305 elementStyle: "elementStyle",
306 elementTextarea: "elementTextarea",
307 elementTitle: "elementTitle",
308 }
309
310 func (e element) String() string {
311 if int(e) < len(elementNames) {
312 return elementNames[e]
313 }
314 return fmt.Sprintf("illegal element %d", int(e))
315 }
316
317
318
319 type attr uint8
320
321 const (
322
323 attrNone attr = iota
324
325 attrScript
326
327 attrScriptType
328
329 attrStyle
330
331 attrURL
332
333 attrSrcset
334 )
335
336 var attrNames = [...]string{
337 attrNone: "attrNone",
338 attrScript: "attrScript",
339 attrScriptType: "attrScriptType",
340 attrStyle: "attrStyle",
341 attrURL: "attrURL",
342 attrSrcset: "attrSrcset",
343 }
344
345 func (a attr) String() string {
346 if int(a) < len(attrNames) {
347 return attrNames[a]
348 }
349 return fmt.Sprintf("illegal attr %d", int(a))
350 }
351
View as plain text