1
1
// vim: set fdm=marker:
2
- var EXPORTED_SYMBOLS = [ "raw" , "safehtml" , "tmpl" , " xml", "cooked" ] ;
2
+ var EXPORTED_SYMBOLS = [ "xml" , "TemplateSupportsXML" ] ;
3
3
4
4
// {{{ escape function
5
5
//var obj1 = {};
@@ -60,308 +60,6 @@ function TemplateXML(s) {this.value = s;}
60
60
TemplateXML . prototype = new TemplateSupportsXML ;
61
61
TemplateXML . prototype . toString = function ( ) this . value ;
62
62
63
- function TemplateTmpl ( s ) { this . value = s ; } //{{{
64
- TemplateTmpl . prototype = new TemplateSupportsXML ;
65
- TemplateTmpl . prototype . toString = function ( ) this . value ;
66
- TemplateTmpl . prototype . toXMLString = function ( ) {
67
- var str = this . value ;
68
- const whiteSpace = / ^ \s * / y;
69
- const tagToken = / ^ ( [ ^ ` " ' , \s \[ \] \{ \} \( \) ] + ) \s * ( \[ ) ? / y;
70
- const attrToken = / ^ \s * ( [ ^ \s = ] + ) \s * = \s * ( \S ) / y;
71
- const attrSep = / ^ \s * ( \s | ] ) / y;
72
- const STATE = 0 , NAME = 1 , OPEN = 2 ;
73
- const TAG = { } , ROOT = { } , GROUP = { } ;
74
- var offset = 0 ;
75
- var start = 0 ;
76
- var res = "" ;
77
- var stack = [ ] ;
78
- var depth = 0 ;
79
- var state = [ ROOT ] ;
80
- var m ;
81
- label_root: while ( 1 ) {
82
- whiteSpace . lastIndex = offset ;
83
- m = whiteSpace . exec ( str ) ;
84
- if ( ! m ) throw SyntaxError ( "ws" ) ;
85
- offset = whiteSpace . lastIndex ;
86
- c = str [ offset ++ ] ;
87
- if ( ! c ) break ;
88
- switch ( c ) {
89
- case "{" :
90
- if ( state [ STATE ] === TAG && state [ OPEN ] ) {
91
- res += ">" ;
92
- state [ OPEN ] = false ;
93
- }
94
- start = offset ;
95
- while ( c = str [ offset ++ ] ) {
96
- if ( c === "\\" ) {
97
- res += str . substring ( start , offset - 1 ) ;
98
- start = offset ++ ;
99
- }
100
- else if ( c === "}" ) {
101
- res += escapeHTML ( str . substring ( start , offset - 1 ) ) ;
102
- continue label_root;
103
- }
104
- }
105
- throw SyntaxError ( "text" ) ;
106
- case "`" :
107
- if ( state [ STATE ] === TAG && state [ OPEN ] ) {
108
- res += ">" ;
109
- state [ OPEN ] = false ;
110
- }
111
- start = offset ;
112
- while ( c = str [ offset ++ ] ) {
113
- if ( c === "\\" ) {
114
- res += str . substring ( start , offset - 1 ) ;
115
- start = offset ++ ;
116
- } else if ( c === "`" ) {
117
- res += str . substring ( start , offset - 1 ) ;
118
- continue label_root;
119
- }
120
- }
121
- throw SyntaxError ( "xml" ) ;
122
- case "(" :
123
- if ( state [ STATE ] === TAG && state [ OPEN ] ) {
124
- res += ">" ;
125
- state [ OPEN ] = false ;
126
- }
127
- stack [ depth ++ ] = state ;
128
- state = [ GROUP ] ;
129
- break ;
130
- case ")" :
131
- do {
132
- switch ( state [ STATE ] ) {
133
- case TAG :
134
- if ( state [ OPEN ] ) {
135
- res += "/>" ;
136
- state [ OPEN ] = false ;
137
- } else {
138
- res += "</" + state [ NAME ] + ">" ;
139
- }
140
- break ;
141
- case GROUP :
142
- state = stack [ -- depth ] ;
143
- continue label_root;
144
- case ROOT :
145
- throw SyntaxError ( ")" ) ;
146
- }
147
- } while ( state = stack [ -- depth ] ) ;
148
- throw SyntaxError ( ")" ) ;
149
- case "," :
150
- do {
151
- switch ( state [ STATE ] ) {
152
- case TAG :
153
- if ( state [ OPEN ] ) {
154
- res += "/>" ;
155
- state [ OPEN ] = false ;
156
- } else {
157
- res += "</" + state [ NAME ] + ">" ;
158
- }
159
- break ;
160
- case GROUP :
161
- case ROOT :
162
- continue label_root;
163
- }
164
- } while ( state = stack [ -- depth ] ) ;
165
- throw SyntaxError ( ")" ) ;
166
- default :
167
- if ( state [ STATE ] === TAG && state [ OPEN ] ) {
168
- res += ">" ;
169
- state [ OPEN ] = false ;
170
- }
171
- tagToken . lastIndex = offset - 1 ;
172
- m = tagToken . exec ( str ) ;
173
- if ( ! m ) throw SyntaxError ( "tag" ) ;
174
- offset = tagToken . lastIndex ;
175
- res += "<" + m [ 1 ] ;
176
- stack [ depth ++ ] = state ;
177
- state = [ TAG , m [ 1 ] , true ] ;
178
- if ( m [ 2 ] ) {
179
- label_attr: while ( 1 ) {
180
- attrToken . lastIndex = offset ;
181
- m = attrToken . exec ( str ) ;
182
- if ( ! m ) throw new SyntaxError ( "attr" ) ;
183
- offset = attrToken . lastIndex ;
184
- res += " " + m [ 1 ] + "=" ;
185
- start = offset ;
186
- close = m [ 2 ] ;
187
- while ( c = str [ offset ++ ] ) {
188
- if ( c === close ) {
189
- res += close + str . substring ( start , offset - 1 ) + close ;
190
- attrSep . lastIndex = offset ;
191
- m = attrSep . exec ( str ) ;
192
- if ( ! m ) throw SyntaxError ( "attr sep" ) ;
193
- offset = attrSep . lastIndex ;
194
- if ( m [ 1 ] === "]" ) {
195
- break label_attr;
196
- }
197
- continue label_attr;
198
- }
199
- }
200
- }
201
- }
202
- break ;
203
- }
204
- }
205
- if ( state [ STATE ] === TAG ) res += state [ OPEN ] ? "/>" : "</" + state [ NAME ] + ">" ;
206
- while ( state = stack [ -- depth ] ) {
207
- if ( state [ STATE ] === TAG )
208
- res += "</" + state [ NAME ] + ">" ;
209
- }
210
- return res ;
211
- } ; //}}}
212
-
213
- function templateTmpl ( portion , args ) // {{{
214
- {
215
- var res = "" ;
216
-
217
- const BODY = { } , ATTR1 = { } , ATTR2 = { } , TEXT = { } , XML = { } ;
218
-
219
- var c ;
220
- var raw = portion . raw ;
221
- var i = 0 , j = args . length ;
222
-
223
- var depth = 0 ;
224
-
225
- var state = BODY , offset = 0 ;
226
- var str = raw [ 0 ] , arg = args [ 0 ] ;
227
- var res = str ;
228
- label_root: while ( 1 ) {
229
- switch ( state ) {
230
- case BODY :
231
- while ( c = str [ offset ++ ] ) {
232
- switch ( c ) {
233
- case "[" :
234
- state = ATTR1 ;
235
- continue label_root;
236
- case "`" :
237
- state = XML ;
238
- continue label_root;
239
- case "{" :
240
- state = TEXT ;
241
- continue label_root;
242
- case "(" :
243
- depth ++ ;
244
- break ;
245
- case ")" :
246
- if ( -- depth < 0 ) throw SyntaxError ( "depth" ) ;
247
- break ;
248
- }
249
- }
250
- if ( i >= j ) break label_root;
251
- else if ( arg instanceof TemplateTmpl ) {
252
- res += "(" + arg . value + ")" ;
253
- } else if ( arg instanceof TemplateXML ) {
254
- res += "`" + arg . value . replace ( / ( [ \\ ` ] ) / g, "\\$1" , "g" ) + "`" ;
255
- } else {
256
- res += "{" + String . replace ( arg , / ( [ \\ } ] ) / g, "\\$1" ) + "}" ;
257
- }
258
- break ;
259
- case ATTR1 :
260
- while ( c = str [ offset ++ ] ) {
261
- if ( c === "=" ) {
262
- state = ATTR2 ;
263
- continue label_root;
264
- } else if ( c === "]" ) {
265
- state = BODY ;
266
- continue label_root;
267
- }
268
- }
269
- if ( i >= j ) throw SyntaxError ( "attr left" ) ;
270
- arg = String ( arg ) ;
271
- if ( / [ = \[ \] ! " # $ % & ' ] / . test ( arg ) ) throw SyntaxError ( "substitude:" + i ) ;
272
- res += arg ;
273
- break ;
274
- case ATTR2 :
275
- c1 = str [ offset ++ ] ;
276
- if ( ! c1 ) {
277
- if ( i >= j ) throw SyntaxError ( "attr right" ) ;
278
- arg = String ( arg ) ;
279
- res += '"' + String . replace ( arg , / " / g, '\\"' , "g" ) + '"' ;
280
- state = ATTR1 ;
281
- break ;
282
- }
283
- else if ( c1 === "{" ) c1 = "}" ;
284
- while ( c = str [ offset ++ ] ) {
285
- if ( c === "\\" ) offset ++ ;
286
- else if ( c === c1 ) {
287
- state = ATTR1 ;
288
- continue label_root;
289
- }
290
- }
291
- // do not support attribute's value nesting
292
- throw SyntaxError ( "attr2" ) ;
293
- break ;
294
- case TEXT :
295
- while ( c = str [ offset ++ ] ) {
296
- if ( c === "\\" ) offset ++ ;
297
- else if ( c === "}" ) {
298
- state = BODY ;
299
- continue label_root;
300
- }
301
- }
302
- if ( i >= j ) throw SyntaxError ( "text" ) ;
303
- arg = String ( arg ) ;
304
- res += '{' + String . replace ( arg , / } / g, '\\}' , "g" ) + '}' ;
305
- break ;
306
- case XML :
307
- while ( c = str [ offset ++ ] ) {
308
- if ( c === "\\" ) offset ++ ;
309
- else if ( c === "`" ) {
310
- state = BODY ;
311
- continue label_root;
312
- }
313
- }
314
- // do not support xml nesting
315
- throw SyntaxError ( "xml" ) ;
316
- break ;
317
- default :
318
- throw SyntaxError ( "unknown state" ) ;
319
- }
320
-
321
- str = raw [ ++ i ] ;
322
- arg = args [ i ] ;
323
- res += str ;
324
- offset = 0 ;
325
- }
326
- if ( depth !== 0 ) throw SyntaxError ( "depth" ) ;
327
- return new TemplateTmpl ( res ) ;
328
- }
329
-
330
- // xxx: no check
331
- templateTmpl . raw = function templateTmplRaw ( portion , args ) {
332
- return new TemplateTmpl ( templateRaw ( portion , args ) ) ;
333
- } ;
334
- templateTmpl . map = function templateTmplMap ( data , fn ) {
335
- var val , res = "" ;
336
- for ( var i = 0 , j = data . length ; i < j ; i ++ ) {
337
- val = fn ( data [ i ] ) ;
338
- if ( val instanceof TemplateTmpl )
339
- res += "(" + val . value + ")" ;
340
- else if ( val instanceof TemplateXML )
341
- res += encodeTmplXml ( val . value ) ;
342
- else
343
- res += encodeTmplText ( val ) ;
344
- }
345
- return new TemplateTmpl ( res ) ;
346
- } ;
347
- templateTmpl . is = function is ( obj ) {
348
- return obj instanceof TemplateTmpl ;
349
- } ;
350
- templateTmpl . isEmpty = function isEmpty ( ) {
351
- return ( value instanceof TemplateXML || value instanceof TemplateTmpl ) && value . value === "" ;
352
- } ;
353
- templateTmpl [ "+=" ] = function ( self , value ) {
354
- if ( ! ( self instanceof TemplateTmpl ) ) throw SyntaxError ( ) ;
355
- else if ( value instanceof TemplateTmpl )
356
- self . value += "(" + value . value + ")" ;
357
- else if ( value instanceof TemplateXML )
358
- self . value += "`" + value . value . replace ( / ` / g, "\\`" ) + "`" ;
359
- else
360
- self . value = "{" + String ( value ) . replace ( / } / g, "\\}" ) + "}" ;
361
- return self ;
362
- } ;
363
- //}}}
364
-
365
63
function templateXML ( portion , args ) // {{{
366
64
{
367
65
var res = "" ;
@@ -414,9 +112,7 @@ function templateXML(portion, args) // {{{
414
112
}
415
113
}
416
114
if ( i >= j ) break label_root;
417
- else if ( arg instanceof TemplateTmpl ) {
418
- res += arg . toXMLString ( ) ;
419
- } else if ( arg instanceof TemplateXML ) {
115
+ else if ( arg instanceof TemplateXML ) {
420
116
res += arg . value ;
421
117
} else if ( arg instanceof TemplateSupportsXML ) {
422
118
res += ( arg . toXMLString || arg . toString ) ( ) ;
@@ -515,9 +211,7 @@ templateXML.map = function templateXmlMap(data, fn) {
515
211
var val , res = "" ;
516
212
for ( var i = 0 , j = data . length ; i < j ; i ++ ) {
517
213
val = fn ( data [ i ] ) ;
518
- if ( val instanceof TemplateTmpl )
519
- res += val . toXMLString ( ) ;
520
- else if ( val instanceof TemplateXML )
214
+ if ( val instanceof TemplateXML )
521
215
res += val . value ;
522
216
else
523
217
res += val ;
@@ -540,8 +234,6 @@ templateXML.cdata = function templateXmlCDATA(portion, args) {
540
234
} ;
541
235
templateXML [ "+=" ] = function ( self , value ) {
542
236
if ( ! ( self instanceof TemplateXML ) ) throw SyntaxError ( ) ;
543
- else if ( value instanceof TemplateTmpl )
544
- self . value += value . toXMLString ( ) ;
545
237
else if ( value instanceof TemplateXML )
546
238
self . value += value . value ;
547
239
else
@@ -552,7 +244,7 @@ templateXML.is = function is(obj) {
552
244
return obj instanceof TemplateXML ;
553
245
} ;
554
246
templateXML . isEmpty = function ( value ) {
555
- return ( value instanceof TemplateXML || value instanceof TemplateTmpl ) && value . value === "" ;
247
+ return ( value instanceof TemplateXML ) && value . value === "" ;
556
248
} ;
557
249
//}}}
558
250
@@ -566,31 +258,5 @@ function templateRaw(portion, args) {
566
258
return res ;
567
259
}
568
260
569
- function templateCooked ( portion , args ) {
570
- var str = portion . cooked ;
571
- var i = 0 , j = args . length , res = str [ 0 ] ;
572
- while ( i < j ) {
573
- res += args [ i ++ ] ;
574
- res += str [ i ] ;
575
- }
576
- return res ;
577
- }
578
-
579
- function templateSafeHtml ( portion , args ) {
580
- var raw = portion . raw ;
581
- var i = 0 , j = args . length , res = raw [ 0 ] ;
582
- while ( i < j ) {
583
- res += escapeHTML ( args [ i ++ ] ) ;
584
- res += raw [ i ] ;
585
- }
586
- return res ;
587
- }
588
-
589
- //}}}
590
-
591
- var tmpl = templateTmpl ;
592
261
var xml = templateXML ;
593
- var raw = templateRaw ;
594
- var cooked = templateCooked ;
595
- var safehtml = templateSafeHtml ;
596
262
0 commit comments