forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jstemplate_compiled.js
1182 lines (1065 loc) · 31.7 KB
/
jstemplate_compiled.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileoverview This file contains miscellaneous basic functionality.
*
*/
/**
* Creates a DOM element with the given tag name in the document of the
* owner element.
*
* @param {String} tagName The name of the tag to create.
* @param {Element} owner The intended owner (i.e., parent element) of
* the created element.
* @param {Point} opt_position The top-left corner of the created element.
* @param {Size} opt_size The size of the created element.
* @param {Boolean} opt_noAppend Do not append the new element to the owner.
* @return {Element} The newly created element node.
*/
function createElement(tagName, owner, opt_position, opt_size, opt_noAppend) {
var element = ownerDocument(owner).createElement(tagName);
if (opt_position) {
setPosition(element, opt_position);
}
if (opt_size) {
setSize(element, opt_size);
}
if (owner && !opt_noAppend) {
appendChild(owner, element);
}
return element;
}
/**
* Creates a text node with the given value.
*
* @param {String} value The text to place in the new node.
* @param {Element} owner The owner (i.e., parent element) of the new
* text node.
* @return {Text} The newly created text node.
*/
function createTextNode(value, owner) {
var element = ownerDocument(owner).createTextNode(value);
if (owner) {
appendChild(owner, element);
}
return element;
}
/**
* Returns the document owner of the given element. In particular,
* returns window.document if node is null or the browser does not
* support ownerDocument.
*
* @param {Node} node The node whose ownerDocument is required.
* @returns {Document|Null} The owner document or null if unsupported.
*/
function ownerDocument(node) {
return (node ? node.ownerDocument : null) || document;
}
/**
* Wrapper function to create CSS units (pixels) string
*
* @param {Number} numPixels Number of pixels, may be floating point.
* @returns {String} Corresponding CSS units string.
*/
function px(numPixels) {
return round(numPixels) + "px";
}
/**
* Sets the left and top of the given element to the given point.
*
* @param {Element} element The dom element to manipulate.
* @param {Point} point The desired position.
*/
function setPosition(element, point) {
var style = element.style;
style.position = "absolute";
style.left = px(point.x);
style.top = px(point.y);
}
/**
* Sets the width and height style attributes to the given size.
*
* @param {Element} element The dom element to manipulate.
* @param {Size} size The desired size.
*/
function setSize(element, size) {
var style = element.style;
style.width = px(size.width);
style.height = px(size.height);
}
/**
* Sets display to none. Doing this as a function saves a few bytes for
* the 'style.display' property and the 'none' literal.
*
* @param {Element} node The dom element to manipulate.
*/
function displayNone(node) {
node.style.display = 'none';
}
/**
* Sets display to default.
*
* @param {Element} node The dom element to manipulate.
*/
function displayDefault(node) {
node.style.display = '';
}
/**
* Appends the given child to the given parent in the DOM
*
* @param {Element} parent The parent dom element.
* @param {Node} child The new child dom node.
*/
function appendChild(parent, child) {
parent.appendChild(child);
}
/**
* Wrapper for the eval() builtin function to evaluate expressions and
* obtain their value. It wraps the expression in parentheses such
* that object literals are really evaluated to objects. Without the
* wrapping, they are evaluated as block, and create syntax
* errors. Also protects against other syntax errors in the eval()ed
* code and returns null if the eval throws an exception.
*
* @param {String} expr
* @return {Object|Null}
*/
function jsEval(expr) {
try {
return eval('[' + expr + '][0]');
} catch (e) {
return null;
}
}
/**
* Wrapper for the eval() builtin function to execute statements. This
* guards against exceptions thrown, but doesn't return a
* value. Still, mostly for testability, it returns a boolean to
* indicate whether execution was successful. NOTE:
* javascript's eval semantics is murky in that it confounds
* expression evaluation and statement execution into a single
* construct. Cf. jsEval().
*
* @param {String} stmt
* @return {Boolean}
*/
function jsExec(stmt) {
try {
eval(stmt);
return true;
} catch (e) {
return false;
}
}
/**
* Wrapper for eval with a context. NOTE: The style guide
* deprecates eval, so this is the exception that proves the
* rule. Notice also that since the value of the expression is
* returned rather than assigned to a local variable, one major
* objection aganist the use of the with() statement, namely that
* properties of the with() target override local variables of the
* same name, is void here.
*
* @param {String} expr
* @param {Object} context
* @return {Object|Null}
*/
function jsEvalWith(expr, context) {
try {
with (context) {
return eval('[' + expr + '][0]');
}
} catch (e) {
return null;
}
}
var DOM_ELEMENT_NODE = 1;
var DOM_ATTRIBUTE_NODE = 2;
var DOM_TEXT_NODE = 3;
var DOM_CDATA_SECTION_NODE = 4;
var DOM_ENTITY_REFERENCE_NODE = 5;
var DOM_ENTITY_NODE = 6;
var DOM_PROCESSING_INSTRUCTION_NODE = 7;
var DOM_COMMENT_NODE = 8;
var DOM_DOCUMENT_NODE = 9;
var DOM_DOCUMENT_TYPE_NODE = 10;
var DOM_DOCUMENT_FRAGMENT_NODE = 11;
var DOM_NOTATION_NODE = 12;
/**
* Traverses the element nodes in the DOM tree underneath the given
* node and finds the first node with elemId, or null if there is no such
* element. Traversal is in depth-first order.
*
* NOTE: The reason this is not combined with the elem() function is
* that the implementations are different.
* elem() is a wrapper for the built-in document.getElementById() function,
* whereas this function performs the traversal itself.
* Modifying elem() to take an optional root node is a possibility,
* but the in-built function would perform better than using our own traversal.
*
* @param {Element} node Root element of subtree to traverse.
* @param {String} elemId The id of the element to search for.
* @return {Element|Null} The corresponding element, or null if not found.
*/
function nodeGetElementById(node, elemId) {
for (var c = node.firstChild; c; c = c.nextSibling) {
if (c.id == elemId) {
return c;
}
if (c.nodeType == DOM_ELEMENT_NODE) {
var n = arguments.callee.call(this, c, elemId);
if (n) {
return n;
}
}
}
return null;
}
/**
* Get an attribute from the DOM. Simple redirect, exists to compress code.
*
* @param {Element} node Element to interrogate.
* @param {String} name Name of parameter to extract.
* @return {String} Resulting attribute.
*/
function domGetAttribute(node, name) {
return node.getAttribute(name);
}
/**
* Set an attribute in the DOM. Simple redirect to compress code.
*
* @param {Element} node Element to interrogate.
* @param {String} name Name of parameter to set.
* @param {String} value Set attribute to this value.
*/
function domSetAttribute(node, name, value) {
node.setAttribute(name, value);
}
/**
* Remove an attribute from the DOM. Simple redirect to compress code.
*
* @param {Element} node Element to interrogate.
* @param {String} name Name of parameter to remove.
*/
function domRemoveAttribute(node, name) {
node.removeAttribute(name);
}
/**
* Clone a node in the DOM.
*
* @param {Node} node Node to clone.
* @return {Node} Cloned node.
*/
function domCloneNode(node) {
return node.cloneNode(true);
}
/**
* Return a safe string for the className of a node.
* If className is not a string, returns "".
*
* @param {Element} node DOM element to query.
* @return {String}
*/
function domClassName(node) {
return node.className ? "" + node.className : "";
}
/**
* Adds a class name to the class attribute of the given node.
*
* @param {Element} node DOM element to modify.
* @param {String} className Class name to add.
*/
function domAddClass(node, className) {
var name = domClassName(node);
if (name) {
var cn = name.split(/\s+/);
var found = false;
for (var i = 0; i < jsLength(cn); ++i) {
if (cn[i] == className) {
found = true;
break;
}
}
if (!found) {
cn.push(className);
}
node.className = cn.join(' ');
} else {
node.className = className;
}
}
/**
* Removes a class name from the class attribute of the given node.
*
* @param {Element} node DOM element to modify.
* @param {String} className Class name to remove.
*/
function domRemoveClass(node, className) {
var c = domClassName(node);
if (!c || c.indexOf(className) == -1) {
return;
}
var cn = c.split(/\s+/);
for (var i = 0; i < jsLength(cn); ++i) {
if (cn[i] == className) {
cn.splice(i--, 1);
}
}
node.className = cn.join(' ');
}
/**
* Checks if a node belongs to a style class.
*
* @param {Element} node DOM element to test.
* @param {String} className Class name to check for.
* @return {Boolean} Node belongs to style class.
*/
function domTestClass(node, className) {
var cn = domClassName(node).split(/\s+/);
for (var i = 0; i < jsLength(cn); ++i) {
if (cn[i] == className) {
return true;
}
}
return false;
}
/**
* Inserts a new child before a given sibling.
*
* @param {Node} newChild Node to insert.
* @param {Node} oldChild Sibling node.
* @return {Node} Reference to new child.
*/
function domInsertBefore(newChild, oldChild) {
return oldChild.parentNode.insertBefore(newChild, oldChild);
}
/**
* Appends a new child to the specified (parent) node.
*
* @param {Element} node Parent element.
* @param {Node} child Child node to append.
* @return {Node} Newly appended node.
*/
function domAppendChild(node, child) {
return node.appendChild(child);
}
/**
* Remove a new child from the specified (parent) node.
*
* @param {Element} node Parent element.
* @param {Node} child Child node to remove.
* @return {Node} Removed node.
*/
function domRemoveChild(node, child) {
return node.removeChild(child);
}
/**
* Replaces an old child node with a new child node.
*
* @param {Node} newChild New child to append.
* @param {Node} oldChild Old child to remove.
* @return {Node} Replaced node.
*/
function domReplaceChild(newChild, oldChild) {
return oldChild.parentNode.replaceChild(newChild, oldChild);
}
/**
* Removes a node from the DOM.
*
* @param {Node} node The node to remove.
* @return {Node} The removed node.
*/
function domRemoveNode(node) {
return domRemoveChild(node.parentNode, node);
}
/**
* Creates a new text node in the given document.
*
* @param {Document} doc Target document.
* @param {String} text Text composing new text node.
* @return {Text} Newly constructed text node.
*/
function domCreateTextNode(doc, text) {
return doc.createTextNode(text);
}
/**
* Creates a new node in the given document
*
* @param {Document} doc Target document.
* @param {String} name Name of new element (i.e. the tag name)..
* @return {Element} Newly constructed element.
*/
function domCreateElement(doc, name) {
return doc.createElement(name);
}
/**
* Creates a new attribute in the given document.
*
* @param {Document} doc Target document.
* @param {String} name Name of new attribute.
* @return {Attr} Newly constructed attribute.
*/
function domCreateAttribute(doc, name) {
return doc.createAttribute(name);
}
/**
* Creates a new comment in the given document.
*
* @param {Document} doc Target document.
* @param {String} text Comment text.
* @return {Comment} Newly constructed comment.
*/
function domCreateComment(doc, text) {
return doc.createComment(text);
}
/**
* Creates a document fragment.
*
* @param {Document} doc Target document.
* @return {DocumentFragment} Resulting document fragment node.
*/
function domCreateDocumentFragment(doc) {
return doc.createDocumentFragment();
}
/**
* Redirect to document.getElementById
*
* @param {Document} doc Target document.
* @param {String} id Id of requested node.
* @return {Element|Null} Resulting element.
*/
function domGetElementById(doc, id) {
return doc.getElementById(id);
}
/**
* Redirect to window.setInterval
*
* @param {Window} win Target window.
* @param {Function} fun Callback function.
* @param {Number} time Time in milliseconds.
* @return {Object} Contract id.
*/
function windowSetInterval(win, fun, time) {
return win.setInterval(fun, time);
}
/**
* Redirect to window.clearInterval
*
* @param {Window} win Target window.
* @param {object} id Contract id.
* @return {any} NOTE: Return type unknown?
*/
function windowClearInterval(win, id) {
return win.clearInterval(id);
}
/**
* Determines whether one node is recursively contained in another.
* @param parent The parent node.
* @param child The node to look for in parent.
* @return parent recursively contains child
*/
function containsNode(parent, child) {
while (parent != child && child.parentNode) {
child = child.parentNode;
}
return parent == child;
};
/**
* @fileoverview This file contains javascript utility functions that
* do not depend on anything defined elsewhere.
*
*/
/**
* Returns the value of the length property of the given object. Used
* to reduce compiled code size.
*
* @param {Array | String} a The string or array to interrogate.
* @return {Number} The value of the length property.
*/
function jsLength(a) {
return a.length;
}
var min = Math.min;
var max = Math.max;
var ceil = Math.ceil;
var floor = Math.floor;
var round = Math.round;
var abs = Math.abs;
/**
* Copies all properties from second object to the first. Modifies to.
*
* @param {Object} to The target object.
* @param {Object} from The source object.
*/
function copyProperties(to, from) {
foreachin(from, function(p) {
to[p] = from[p];
});
}
/**
* Iterates over the array, calling the given function for each
* element.
*
* @param {Array} array
* @param {Function} fn
*/
function foreach(array, fn) {
var I = jsLength(array);
for (var i = 0; i < I; ++i) {
fn(array[i], i);
}
}
/**
* Safely iterates over all properties of the given object, calling
* the given function for each property. If opt_all isn't true, uses
* hasOwnProperty() to assure the property is on the object, not on
* its prototype.
*
* @param {Object} object
* @param {Function} fn
* @param {Boolean} opt_all If true, also iterates over inherited properties.
*/
function foreachin(object, fn, opt_all) {
for (var i in object) {
if (opt_all || !object.hasOwnProperty || object.hasOwnProperty(i)) {
fn(i, object[i]);
}
}
}
/**
* Appends the second array to the first, copying its elements.
* Optionally only a slice of the second array is copied.
*
* @param {Array} a1 Target array (modified).
* @param {Array} a2 Source array.
* @param {Number} opt_begin Begin of slice of second array (optional).
* @param {Number} opt_end End (exclusive) of slice of second array (optional).
*/
function arrayAppend(a1, a2, opt_begin, opt_end) {
var i0 = opt_begin || 0;
var i1 = opt_end || jsLength(a2);
for (var i = i0; i < i1; ++i) {
a1.push(a2[i]);
}
}
/**
* Trim whitespace from begin and end of string.
*
* @see testStringTrim();
*
* @param {String} str Input string.
* @return {String} Trimmed string.
*/
function stringTrim(str) {
return stringTrimRight(stringTrimLeft(str));
}
/**
* Trim whitespace from beginning of string.
*
* @see testStringTrimLeft();
*
* @param {String} str Input string.
* @return {String} Trimmed string.
*/
function stringTrimLeft(str) {
return str.replace(/^\s+/, "");
}
/**
* Trim whitespace from end of string.
*
* @see testStringTrimRight();
*
* @param {String} str Input string.
* @return {String} Trimmed string.
*/
function stringTrimRight(str) {
return str.replace(/\s+$/, "");
}
/**
* Jscompiler wrapper for parseInt() with base 10.
*
* @param {String} s String repersentation of a number.
*
* @return {Number} The integer contained in s, converted on base 10.
*/
function parseInt10(s) {
return parseInt(s, 10);
}
/**
* @fileoverview A simple formatter to project JavaScript data into
* HTML templates. The template is edited in place. I.e. in order to
* instantiate a template, clone it from the DOM first, and then
* process the cloned template. This allows for updating of templates:
* If the templates is processed again, changed values are merely
* updated.
*
* NOTE: IE DOM doesn't have importNode().
*
* NOTE: The property name "length" must not be used in input
* data, see comment in jstSelect_().
*/
/**
* Names of jstemplate attributes. These attributes are attached to
* normal HTML elements and bind expression context data to the HTML
* fragment that is used as template.
*/
var ATT_select = 'jsselect';
var ATT_instance = 'jsinstance';
var ATT_display = 'jsdisplay';
var ATT_values = 'jsvalues';
var ATT_eval = 'jseval';
var ATT_transclude = 'transclude';
var ATT_content = 'jscontent';
/**
* Names of special variables defined by the jstemplate evaluation
* context. These can be used in js expression in jstemplate
* attributes.
*/
var VAR_index = '$index';
var VAR_this = '$this';
/**
* Context for processing a jstemplate. The context contains a context
* object, whose properties can be referred to in jstemplate
* expressions, and it holds the locally defined variables.
*
* @param {Object} opt_data The context object. Null if no context.
*
* @param {Object} opt_parent The parent context, from which local
* variables are inherited. Normally the context object of the parent
* context is the object whose property the parent object is. Null for the
* context of the root object.
*
* @constructor
*/
function JsExprContext(opt_data, opt_parent) {
var me = this;
/**
* The local context of the input data in which the jstemplate
* expressions are evaluated. Notice that this is usually an Object,
* but it can also be a scalar value (and then still the expression
* $this can be used to refer to it). Notice this can be a scalar
* value, including undefined.
*
* @type {Object}
*/
me.data_ = opt_data;
/**
* The context for variable definitions in which the jstemplate
* expressions are evaluated. Other than for the local context,
* which replaces the parent context, variable definitions of the
* parent are inherited. The special variable $this points to data_.
*
* @type {Object}
*/
me.vars_ = {};
if (opt_parent) {
copyProperties(me.vars_, opt_parent.vars_);
}
this.vars_[VAR_this] = me.data_;
}
/**
* Evaluates the given expression in the context of the current
* context object and the current local variables.
*
* @param {String} expr A javascript expression.
*
* @param {Element} template DOM node of the template.
*
* @return The value of that expression.
*/
JsExprContext.prototype.jseval = function(expr, template) {
with (this.vars_) {
with (this.data_) {
try {
return (function() {
return eval('[' + expr + '][0]');
}).call(template);
} catch (e) {
return null;
}
}
}
}
/**
* Clones the current context for a new context object. The cloned
* context has the data object as its context object and the current
* context as its parent context. It also sets the $index variable to
* the given value. This value usually is the position of the data
* object in a list for which a template is instantiated multiply.
*
* @param {Object} data The new context object.
*
* @param {Number} index Position of the new context when multiply
* instantiated. (See implementation of jstSelect().)
*
* @return {JsExprContext}
*/
JsExprContext.prototype.clone = function(data, index) {
var ret = new JsExprContext(data, this);
ret.setVariable(VAR_index, index);
if (this.resolver_) {
ret.setSubTemplateResolver(this.resolver_);
}
return ret;
}
/**
* Binds a local variable to the given value. If set from jstemplate
* jsvalue expressions, variable names must start with $, but in the
* API they only have to be valid javascript identifier.
*
* @param {String} name
*
* @param {Object} value
*/
JsExprContext.prototype.setVariable = function(name, value) {
this.vars_[name] = value;
}
/**
* Sets the function used to resolve the values of the transclude
* attribute into DOM nodes. By default, this is jstGetTemplate(). The
* value set here is inherited by clones of this context.
*
* @param {Function} resolver The function used to resolve transclude
* ids into a DOM node of a subtemplate. The DOM node returned by this
* function will be inserted into the template instance being
* processed. Thus, the resolver function must instantiate the
* subtemplate as necessary.
*/
JsExprContext.prototype.setSubTemplateResolver = function(resolver) {
this.resolver_ = resolver;
}
/**
* Resolves a sub template from an id. Used to process the transclude
* attribute. If a resolver function was set using
* setSubTemplateResolver(), it will be used, otherwise
* jstGetTemplate().
*
* @param {String} id The id of the sub template.
*
* @return {Node} The root DOM node of the sub template, for direct
* insertion into the currently processed template instance.
*/
JsExprContext.prototype.getSubTemplate = function(id) {
return (this.resolver_ || jstGetTemplate).call(this, id);
}
/**
* HTML template processor. Data values are bound to HTML templates
* using the attributes transclude, jsselect, jsdisplay, jscontent,
* jsvalues. The template is modifed in place. The values of those
* attributes are JavaScript expressions that are evaluated in the
* context of the data object fragment.
*
* @param {JsExprContext} context Context created from the input data
* object.
*
* @param {Element} template DOM node of the template. This will be
* processed in place. After processing, it will still be a valid
* template that, if processed again with the same data, will remain
* unchanged.
*/
function jstProcess(context, template) {
var processor = new JstProcessor();
processor.run_([ processor, processor.jstProcess_, context, template ]);
}
/**
* Internal class used by jstemplates to maintain context.
* NOTE: This is necessary to process deep templates in Safari
* which has a relatively shallow stack.
* @class
*/
function JstProcessor() {
}
/**
* Runs the state machine, beginning with function "start".
*
* @param {Array} start The first function to run, in the form
* [object, method, args ...]
*/
JstProcessor.prototype.run_ = function(start) {
var me = this;
me.queue_ = [ start ];
while (jsLength(me.queue_)) {
var f = me.queue_.shift();
f[1].apply(f[0], f.slice(2));
}
}
/**
* Appends a function to be called later.
* Analogous to calling that function on a subsequent line, or a subsequent
* iteration of a loop.
*
* @param {Array} f A function in the form [object, method, args ...]
*/
JstProcessor.prototype.enqueue_ = function(f) {
this.queue_.push(f);
}
/**
* Implements internals of jstProcess.
*
* @param {JsExprContext} context
*
* @param {Element} template
*/
JstProcessor.prototype.jstProcess_ = function(context, template) {
var me = this;
var transclude = domGetAttribute(template, ATT_transclude);
if (transclude) {
var tr = context.getSubTemplate(transclude);
if (tr) {
domReplaceChild(tr, template);
me.enqueue_([ me, me.jstProcess_, context, tr ]);
} else {
domRemoveNode(template);
}
return;
}
var select = domGetAttribute(template, ATT_select);
if (select) {
me.jstSelect_(context, template, select);
return;
}
var display = domGetAttribute(template, ATT_display);
if (display) {
if (!context.jseval(display, template)) {
displayNone(template);
return;
}
displayDefault(template);
}
var values = domGetAttribute(template, ATT_values);
if (values) {
me.jstValues_(context, template, values);
}
var expressions = domGetAttribute(template, ATT_eval);
if (expressions) {
foreach(expressions.split(/\s*;\s*/), function(expression) {
expression = stringTrim(expression);
if (jsLength(expression)) {
context.jseval(expression, template);
}
});
}
var content = domGetAttribute(template, ATT_content);
if (content) {
me.jstContent_(context, template, content);
} else {
var childnodes = [];
for (var i = 0; i < jsLength(template.childNodes); ++i) {
if (template.childNodes[i].nodeType == DOM_ELEMENT_NODE) {
me.enqueue_(
[ me, me.jstProcess_, context, template.childNodes[i] ]);
}
}
}
}
/**
* Implements the jsselect attribute: evalutes the value of the
* jsselect attribute in the current context, with the current
* variable bindings (see JsExprContext.jseval()). If the value is an
* array, the current template node is multiplied once for every
* element in the array, with the array element being the context
* object. If the array is empty, or the value is undefined, then the
* current template node is dropped. If the value is not an array,
* then it is just made the context object.
*
* @param {JsExprContext} context The current evaluation context.
*
* @param {Element} template The currently processed node of the template.
*
* @param {String} select The javascript expression to evaluate.
*
* @param {Function} process The function to continue processing with.
*/
JstProcessor.prototype.jstSelect_ = function(context, template, select) {
var me = this;
var value = context.jseval(select, template);
domRemoveAttribute(template, ATT_select);
var instance = domGetAttribute(template, ATT_instance);
var instance_last = false;
if (instance) {
if (instance.charAt(0) == '*') {
instance = parseInt10(instance.substr(1));
instance_last = true;
} else {
instance = parseInt10(instance);
}
}
var multiple = (value !== null &&
typeof value == 'object' &&
typeof value.length == 'number');
var multiple_empty = (multiple && value.length == 0);
if (multiple) {
if (multiple_empty) {
if (!instance) {
domSetAttribute(template, ATT_select, select);
domSetAttribute(template, ATT_instance, '*0');
displayNone(template);
} else {
domRemoveNode(template);
}
} else {
displayDefault(template);
if (instance === null || instance === "" || instance === undefined ||