-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
1351 lines (1287 loc) · 45.2 KB
/
index.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
if (typeof require === 'function') {
var serializeObject = require('serialize-object');
}
// UPDATE_PROPERTIES map HTML attribute names to an Element DOM property that
// should be used for setting on bindings updates instead of setAttribute.
//
// https://github.com/jquery/jquery/blob/1.x-master/src/attributes/prop.js
// https://github.com/jquery/jquery/blob/master/src/attributes/prop.js
// http://webbugtrack.blogspot.com/2007/08/bug-242-setattribute-doesnt-always-work.html
var BOOLEAN_PROPERTIES = {
checked: 'checked'
, disabled: 'disabled'
, indeterminate: 'indeterminate'
, readonly: 'readOnly'
, selected: 'selected'
};
var INTEGER_PROPERTIES = {
colspan: 'colSpan'
, maxlength: 'maxLength'
, rowspan: 'rowSpan'
, tabindex: 'tabIndex'
};
var STRING_PROPERTIES = {
cellpadding: 'cellPadding'
, cellspacing: 'cellSpacing'
, 'class': 'className'
, contenteditable: 'contentEditable'
, enctype: 'encoding'
, 'for': 'htmlFor'
, frameborder: 'frameBorder'
, id: 'id'
, title: 'title'
, type: 'type'
, usemap: 'useMap'
, value: 'value'
};
var UPDATE_PROPERTIES = {};
mergeInto(BOOLEAN_PROPERTIES, UPDATE_PROPERTIES);
mergeInto(INTEGER_PROPERTIES, UPDATE_PROPERTIES);
mergeInto(STRING_PROPERTIES, UPDATE_PROPERTIES);
// CREATE_PROPERTIES map HTML attribute names to an Element DOM property that
// should be used for setting on Element rendering instead of setAttribute.
// input.defaultChecked and input.defaultValue affect the attribute, so we want
// to use these for initial dynamic rendering. For binding updates,
// input.checked and input.value are modified.
var CREATE_PROPERTIES = {};
mergeInto(UPDATE_PROPERTIES, CREATE_PROPERTIES);
CREATE_PROPERTIES.checked = 'defaultChecked';
CREATE_PROPERTIES.value = 'defaultValue';
// http://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements
var VOID_ELEMENTS = {
area: true
, base: true
, br: true
, col: true
, embed: true
, hr: true
, img: true
, input: true
, keygen: true
, link: true
, menuitem: true
, meta: true
, param: true
, source: true
, track: true
, wbr: true
};
var NAMESPACE_URIS = {
svg: 'http://www.w3.org/2000/svg'
, xlink: 'http://www.w3.org/1999/xlink'
, xmlns: 'http://www.w3.org/2000/xmlns/'
};
exports.CREATE_PROPERTIES = CREATE_PROPERTIES;
exports.BOOLEAN_PROPERTIES = BOOLEAN_PROPERTIES;
exports.INTEGER_PROPERTIES = INTEGER_PROPERTIES;
exports.STRING_PROPERTIES = STRING_PROPERTIES;
exports.UPDATE_PROPERTIES = UPDATE_PROPERTIES;
exports.VOID_ELEMENTS = VOID_ELEMENTS;
exports.NAMESPACE_URIS = NAMESPACE_URIS;
// Template Classes
exports.Template = Template;
exports.Doctype = Doctype;
exports.Text = Text;
exports.DynamicText = DynamicText;
exports.Comment = Comment;
exports.DynamicComment = DynamicComment;
exports.Html = Html;
exports.DynamicHtml = DynamicHtml;
exports.Element = Element;
exports.DynamicElement = DynamicElement;
exports.Block = Block;
exports.ConditionalBlock = ConditionalBlock;
exports.EachBlock = EachBlock;
exports.Attribute = Attribute;
exports.DynamicAttribute = DynamicAttribute;
// Binding Classes
exports.Binding = Binding;
exports.NodeBinding = NodeBinding;
exports.AttributeBinding = AttributeBinding;
exports.RangeBinding = RangeBinding;
function Template(content, source) {
this.content = content;
this.source = source;
}
Template.prototype.toString = function() {
return this.source;
};
Template.prototype.get = function(context, unescaped) {
return contentHtml(this.content, context, unescaped);
};
Template.prototype.getFragment = function(context, binding) {
var fragment = document.createDocumentFragment();
this.appendTo(fragment, context, binding);
return fragment;
};
Template.prototype.appendTo = function(parent, context) {
context.pause();
appendContent(parent, this.content, context);
context.unpause();
};
Template.prototype.attachTo = function(parent, node, context) {
context.pause();
var node = attachContent(parent, node, this.content, context);
context.unpause();
return node;
};
Template.prototype.update = function() {};
Template.prototype.stringify = function(value) {
return (value == null) ? '' : value + '';
};
Template.prototype.equals = function(other) {
return this === other;
};
Template.prototype.module = 'templates';
Template.prototype.type = 'Template';
Template.prototype.serialize = function() {
return serializeObject.instance(this, this.content, this.source);
};
function Doctype(name, publicId, systemId) {
this.name = name;
this.publicId = publicId;
this.systemId = systemId;
}
Doctype.prototype = Object.create(Template.prototype);
Doctype.prototype.constructor = Doctype;
Doctype.prototype.get = function() {
var publicText = (this.publicId) ?
' PUBLIC "' + this.publicId + '"' :
'';
var systemText = (this.systemId) ?
(this.publicId) ?
' "' + this.systemId + '"' :
' SYSTEM "' + this.systemId + '"' :
'';
return '<!DOCTYPE ' + this.name + publicText + systemText + '>';
};
Doctype.prototype.appendTo = function() {
// Doctype could be created via:
// document.implementation.createDocumentType(this.name, this.publicId, this.systemId)
// However, it does not appear possible or useful to append it to the
// document fragment. Therefore, just don't render it in the browser
};
Doctype.prototype.attachTo = function(parent, node) {
if (!node || node.nodeType !== 10) {
throw attachError(parent, node);
}
return node.nextSibling;
};
Doctype.prototype.type = 'Doctype';
Doctype.prototype.serialize = function() {
return serializeObject.instance(this, this.name, this.publicId, this.systemId);
};
function Text(data) {
this.data = data;
this.escaped = escapeHtml(data);
}
Text.prototype = Object.create(Template.prototype);
Text.prototype.constructor = Text;
Text.prototype.get = function(context, unescaped) {
return (unescaped) ? this.data : this.escaped;
};
Text.prototype.appendTo = function(parent) {
var node = document.createTextNode(this.data);
parent.appendChild(node);
};
Text.prototype.attachTo = function(parent, node) {
return attachText(parent, node, this.data, this);
};
Text.prototype.type = 'Text';
Text.prototype.serialize = function() {
return serializeObject.instance(this, this.data);
};
// DynamicText might be more accurately named DynamicContent. When its
// expression returns a template, it acts similar to a Block, and it renders
// the template surrounded by comment markers for range replacement. When its
// expression returns any other type, it renders a DOM Text node with no
// markers. Text nodes are bound by updating their data property dynamically.
// The update method must take care to switch between these types of bindings
// in case the expression return type changes dynamically.
function DynamicText(expression) {
this.expression = expression;
this.unbound = false;
}
DynamicText.prototype = Object.create(Template.prototype);
DynamicText.prototype.constructor = DynamicText;
DynamicText.prototype.get = function(context, unescaped) {
var value = this.expression.get(context);
if (value instanceof Template) {
do {
value = value.get(context, unescaped);
} while (value instanceof Template);
return value;
}
var data = this.stringify(value);
return (unescaped) ? data : escapeHtml(data);
};
DynamicText.prototype.appendTo = function(parent, context, binding) {
var value = this.expression.get(context);
if (value instanceof Template) {
var start = document.createComment(this.expression);
var end = document.createComment('/' + this.expression);
var condition = this.getCondition(context);
parent.appendChild(start);
value.appendTo(parent, context);
parent.appendChild(end);
updateRange(context, binding, this, start, end, null, condition);
return;
}
var data = this.stringify(value);
var node = document.createTextNode(data);
parent.appendChild(node);
addNodeBinding(this, context, node);
};
DynamicText.prototype.attachTo = function(parent, node, context) {
var value = this.expression.get(context);
if (value instanceof Template) {
var start = document.createComment(this.expression);
var end = document.createComment('/' + this.expression);
var condition = this.getCondition(context);
parent.insertBefore(start, node || null);
node = value.attachTo(parent, node, context);
parent.insertBefore(end, node || null);
updateRange(context, null, this, start, end, null, condition);
return node;
}
var data = this.stringify(value);
return attachText(parent, node, data, this, context);
};
DynamicText.prototype.update = function(context, binding) {
if (binding instanceof RangeBinding) {
this._blockUpdate(context, binding);
return;
}
var value = this.expression.get(context);
if (value instanceof Template) {
var start = binding.node;
if (!start.parentNode) return;
var end = start;
var fragment = this.getFragment(context);
replaceRange(context, start, end, fragment, binding);
return;
}
binding.node.data = this.stringify(value);
};
DynamicText.prototype.getCondition = function(context) {
return this.expression.get(context);
};
DynamicText.prototype.type = 'DynamicText';
DynamicText.prototype.serialize = function() {
return serializeObject.instance(this, this.expression);
};
function attachText(parent, node, data, template, context) {
if (!node) {
var newNode = document.createTextNode(data);
parent.appendChild(newNode);
addNodeBinding(template, context, newNode);
return;
}
if (node.nodeType === 3) {
// Proceed if nodes already match
if (node.data === data) {
addNodeBinding(template, context, node);
return node.nextSibling;
}
data = normalizeLineBreaks(data);
// Split adjacent text nodes that would have been merged together in HTML
var nextNode = splitData(node, data.length);
if (node.data !== data) {
throw attachError(parent, node);
}
addNodeBinding(template, context, node);
return nextNode;
}
// An empty text node might not be created at the end of some text
if (data === '') {
var newNode = document.createTextNode('');
parent.insertBefore(newNode, node || null);
addNodeBinding(template, context, newNode);
return node;
}
throw attachError(parent, node);
}
function Comment(data, hooks) {
this.data = data;
this.hooks = hooks;
}
Comment.prototype = Object.create(Template.prototype);
Comment.prototype.constructor = Comment;
Comment.prototype.get = function() {
return '<!--' + this.data + '-->';
};
Comment.prototype.appendTo = function(parent, context) {
var node = document.createComment(this.data);
parent.appendChild(node);
emitHooks(this.hooks, context, node);
};
Comment.prototype.attachTo = function(parent, node, context) {
return attachComment(parent, node, this.data, this, context);
};
Comment.prototype.type = 'Comment';
Comment.prototype.serialize = function() {
return serializeObject.instance(this, this.data, this.hooks);
}
function DynamicComment(expression, hooks) {
this.expression = expression;
this.hooks = hooks;
}
DynamicComment.prototype = Object.create(Template.prototype);
DynamicComment.prototype.constructor = DynamicComment;
DynamicComment.prototype.get = function(context) {
var value = getUnescapedValue(this.expression, context);
var data = this.stringify(value);
return '<!--' + data + '-->';
};
DynamicComment.prototype.appendTo = function(parent, context) {
var value = getUnescapedValue(this.expression, context);
var data = this.stringify(value);
var node = document.createComment(data);
parent.appendChild(node);
addNodeBinding(this, context, node);
};
DynamicComment.prototype.attachTo = function(parent, node, context) {
var value = getUnescapedValue(this.expression, context);
var data = this.stringify(value);
return attachComment(parent, node, data, this, context);
};
DynamicComment.prototype.update = function(context, binding) {
var value = getUnescapedValue(this.expression, context);
binding.node.data = this.stringify(value);
};
DynamicComment.prototype.type = 'DynamicComment';
DynamicComment.prototype.serialize = function() {
return serializeObject.instance(this, this.expression, this.hooks);
}
function attachComment(parent, node, data, template, context) {
// Sometimes IE fails to create Comment nodes from HTML or innerHTML.
// This is an issue inside of <select> elements, for example.
if (!node || node.nodeType !== 8) {
var newNode = document.createComment(data);
parent.insertBefore(newNode, node || null);
addNodeBinding(template, context, newNode);
return node;
}
// Proceed if nodes already match
if (node.data === data) {
addNodeBinding(template, context, node);
return node.nextSibling;
}
throw attachError(parent, node);
}
function addNodeBinding(template, context, node) {
if (template.expression && !template.unbound) {
context.addBinding(new NodeBinding(template, context, node));
}
emitHooks(template.hooks, context, node);
}
function Html(data) {
this.data = data;
}
Html.prototype = Object.create(Template.prototype);
Html.prototype.constructor = Html;
Html.prototype.get = function() {
return this.data;
};
Html.prototype.appendTo = function(parent) {
var fragment = createHtmlFragment(parent, this.data);
parent.appendChild(fragment);
};
Html.prototype.attachTo = function(parent, node) {
return attachHtml(parent, node, this.data);
};
Html.prototype.type = "Html";
Html.prototype.serialize = function() {
return serializeObject.instance(this, this.data);
};
function DynamicHtml(expression) {
this.expression = expression;
this.ending = '/' + expression;
}
DynamicHtml.prototype = Object.create(Template.prototype);
DynamicHtml.prototype.constructor = DynamicHtml;
DynamicHtml.prototype.get = function(context) {
var value = getUnescapedValue(this.expression, context);
return this.stringify(value);
};
DynamicHtml.prototype.appendTo = function(parent, context, binding) {
var start = document.createComment(this.expression);
var end = document.createComment(this.ending);
var value = getUnescapedValue(this.expression, context);
var html = this.stringify(value);
var fragment = createHtmlFragment(parent, html);
parent.appendChild(start);
parent.appendChild(fragment);
parent.appendChild(end);
updateRange(context, binding, this, start, end);
};
DynamicHtml.prototype.attachTo = function(parent, node, context) {
var start = document.createComment(this.expression);
var end = document.createComment(this.ending);
var value = getUnescapedValue(this.expression, context);
var html = this.stringify(value);
parent.insertBefore(start, node || null);
node = attachHtml(parent, node, html);
parent.insertBefore(end, node || null);
updateRange(context, null, this, start, end);
return node;
};
DynamicHtml.prototype.update = function(context, binding) {
var parent = binding.start.parentNode;
if (!parent) return;
// Get start and end in advance, since binding is mutated in getFragment
var start = binding.start;
var end = binding.end;
var value = getUnescapedValue(this.expression, context);
var html = this.stringify(value);
var fragment = createHtmlFragment(parent, html);
var innerOnly = true;
replaceRange(context, start, end, fragment, binding, innerOnly);
};
DynamicHtml.prototype.type = 'DynamicHtml';
DynamicHtml.prototype.serialize = function() {
return serializeObject.instance(this, this.expression);
};
function createHtmlFragment(parent, html) {
if (parent && parent.nodeType === 1) {
var range = document.createRange();
range.selectNodeContents(parent);
return range.createContextualFragment(html);
}
var div = document.createElement('div');
var range = document.createRange();
div.innerHTML = html;
range.selectNodeContents(div);
return range.extractContents();
}
function attachHtml(parent, node, html) {
var fragment = createHtmlFragment(parent, html);
for (var i = 0, len = fragment.childNodes.length; i < len; i++) {
if (!node) throw attachError(parent, node);
node = node.nextSibling;
}
return node;
}
function Attribute(data, ns) {
this.data = data;
this.ns = ns;
}
Attribute.prototype = Object.create(Template.prototype);
Attribute.prototype.constructor = Attribute;
Attribute.prototype.get = Attribute.prototype.getBound = function(context) {
return this.data;
};
Attribute.prototype.type = 'Attribute';
Attribute.prototype.serialize = function() {
return serializeObject.instance(this, this.data, this.ns);
};
function DynamicAttribute(expression, ns) {
// In attributes, expression may be an instance of Template or Expression
this.expression = expression;
this.ns = ns;
this.elementNs = null;
}
DynamicAttribute.prototype = Object.create(Attribute.prototype);
DynamicAttribute.prototype.constructor = DynamicAttribute;
DynamicAttribute.prototype.get = function(context) {
return getUnescapedValue(this.expression, context);
};
DynamicAttribute.prototype.getBound = function(context, element, name, elementNs) {
this.elementNs = elementNs;
context.addBinding(new AttributeBinding(this, context, element, name));
return getUnescapedValue(this.expression, context);
};
DynamicAttribute.prototype.update = function(context, binding) {
var value = getUnescapedValue(this.expression, context);
var element = binding.element;
var propertyName = !this.elementNs && UPDATE_PROPERTIES[binding.name];
if (propertyName) {
// Update via DOM property, short-circuiting if no update is needed.
// Certain properties must be strings, so for those properties, the value gets stringified.
//
// There is one special case, when updating the string `input.value` property with a number.
// If a user tries to type "1.01" in an `<input type="number">, then once they've typed "1.0",
// the context value is set to `1`, triggering this update function to set the input value to
// "1". That means typing "1.01" would be impossible without special handling to avoid
// overwriting an existing input value of "1.0" with a new value of "1".
if (element.tagName === 'INPUT' && propertyName === 'value' && typeof value === 'number') {
if (parseFloat(element.value) === value) {
return;
}
}
var propertyValue = (STRING_PROPERTIES[binding.name]) ?
this.stringify(value) : value;
if (element[propertyName] === propertyValue) return;
element[propertyName] = propertyValue;
return;
}
if (value === false || value == null) {
if (this.ns) {
element.removeAttributeNS(this.ns, binding.name);
} else {
element.removeAttribute(binding.name);
}
return;
}
if (value === true) value = binding.name;
if (this.ns) {
element.setAttributeNS(this.ns, binding.name, value);
} else {
element.setAttribute(binding.name, value);
}
};
DynamicAttribute.prototype.type = 'DynamicAttribute';
DynamicAttribute.prototype.serialize = function() {
return serializeObject.instance(this, this.expression, this.ns);
};
function getUnescapedValue(expression, context) {
var unescaped = true;
var value = expression.get(context, unescaped);
while (value instanceof Template) {
value = value.get(context, unescaped);
}
return value;
}
function Element(tagName, attributes, content, hooks, selfClosing, notClosed, ns) {
this.tagName = tagName;
this.attributes = attributes;
this.content = content;
this.hooks = hooks;
this.selfClosing = selfClosing;
this.notClosed = notClosed;
this.ns = ns;
this.endTag = getEndTag(tagName, selfClosing, notClosed);
this.startClose = getStartClose(selfClosing);
var lowerTagName = tagName && tagName.toLowerCase();
this.unescapedContent = (lowerTagName === 'script' || lowerTagName === 'style');
this.bindContentToValue = (lowerTagName === 'textarea');
}
Element.prototype = Object.create(Template.prototype);
Element.prototype.constructor = Element;
Element.prototype.getTagName = function() {
return this.tagName;
};
Element.prototype.getEndTag = function() {
return this.endTag;
};
Element.prototype.get = function(context) {
var tagName = this.getTagName(context);
var endTag = this.getEndTag(tagName);
var tagItems = [tagName];
for (var key in this.attributes) {
var value = this.attributes[key].get(context);
if (value === true) {
tagItems.push(key);
} else if (value !== false && value != null) {
tagItems.push(key + '="' + escapeAttribute(value) + '"');
}
}
var startTag = '<' + tagItems.join(' ') + this.startClose;
if (this.content) {
var inner = contentHtml(this.content, context, this.unescapedContent);
return startTag + inner + endTag;
}
return startTag + endTag;
};
Element.prototype.appendTo = function(parent, context) {
var tagName = this.getTagName(context);
var element = (this.ns) ?
document.createElementNS(this.ns, tagName) :
document.createElement(tagName);
for (var key in this.attributes) {
var attribute = this.attributes[key];
var value = attribute.getBound(context, element, key, this.ns);
if (value === false || value == null) continue;
var propertyName = !this.ns && CREATE_PROPERTIES[key];
if (propertyName) {
element[propertyName] = value;
continue;
}
if (value === true) value = key;
if (attribute.ns) {
element.setAttributeNS(attribute.ns, key, value);
} else {
element.setAttribute(key, value);
}
}
if (this.content) {
this._bindContent(context, element);
appendContent(element, this.content, context);
}
parent.appendChild(element);
emitHooks(this.hooks, context, element);
};
Element.prototype.attachTo = function(parent, node, context) {
var tagName = this.getTagName(context);
if (
!node ||
node.nodeType !== 1 ||
node.tagName.toLowerCase() !== tagName.toLowerCase()
) {
throw attachError(parent, node);
}
for (var key in this.attributes) {
// Get each attribute to create bindings
this.attributes[key].getBound(context, node, key, this.ns);
// TODO: Ideally, this would also check that the node's current attributes
// are equivalent, but there are some tricky edge cases
}
if (this.content) {
this._bindContent(context, node);
attachContent(node, node.firstChild, this.content, context);
}
emitHooks(this.hooks, context, node);
return node.nextSibling;
};
Element.prototype._bindContent = function(context, element) {
// For textareas with dynamic text content, bind to the value property
var child = this.bindContentToValue &&
this.content.length === 1 &&
this.content[0];
if (child instanceof DynamicText) {
child.unbound = true;
var template = new DynamicAttribute(child.expression);
context.addBinding(new AttributeBinding(template, context, element, 'value'));
}
};
Element.prototype.type = 'Element';
Element.prototype.serialize = function() {
return serializeObject.instance(
this
, this.tagName
, this.attributes
, this.content
, this.hooks
, this.selfClosing
, this.notClosed
, this.ns
);
};
function DynamicElement(tagName, attributes, content, hooks, selfClosing, notClosed, ns) {
this.tagName = tagName;
this.attributes = attributes;
this.content = content;
this.hooks = hooks;
this.selfClosing = selfClosing;
this.notClosed = notClosed;
this.ns = ns;
this.startClose = getStartClose(selfClosing);
this.unescapedContent = false;
}
DynamicElement.prototype = Object.create(Element.prototype);
DynamicElement.prototype.constructor = DynamicElement;
DynamicElement.prototype.getTagName = function(context) {
return getUnescapedValue(this.tagName, context);
};
DynamicElement.prototype.getEndTag = function(tagName) {
return getEndTag(tagName, this.selfClosing, this.notClosed);
};
DynamicElement.prototype.type = 'DynamicElement';
function getStartClose(selfClosing) {
return (selfClosing) ? ' />' : '>';
}
function getEndTag(tagName, selfClosing, notClosed) {
var lowerTagName = tagName && tagName.toLowerCase();
var isVoid = VOID_ELEMENTS[lowerTagName];
return (isVoid || selfClosing || notClosed) ? '' : '</' + tagName + '>';
}
function getAttributeValue(element, name) {
var propertyName = UPDATE_PROPERTIES[name];
return (propertyName) ? element[propertyName] : element.getAttribute(name);
}
function emitHooks(hooks, context, value) {
if (!hooks) return;
context.queue(function queuedHooks() {
for (var i = 0, len = hooks.length; i < len; i++) {
hooks[i].emit(context, value);
}
});
}
function Block(expression, content) {
this.expression = expression;
this.ending = '/' + expression;
this.content = content;
}
Block.prototype = Object.create(Template.prototype);
Block.prototype.constructor = Block;
Block.prototype.get = function(context, unescaped) {
var blockContext = context.child(this.expression);
return contentHtml(this.content, blockContext, unescaped);
};
Block.prototype.appendTo = function(parent, context, binding) {
var blockContext = context.child(this.expression);
var start = document.createComment(this.expression);
var end = document.createComment(this.ending);
var condition = this.getCondition(context);
parent.appendChild(start);
appendContent(parent, this.content, blockContext);
parent.appendChild(end);
updateRange(context, binding, this, start, end, null, condition);
};
Block.prototype.attachTo = function(parent, node, context) {
var blockContext = context.child(this.expression);
var start = document.createComment(this.expression);
var end = document.createComment(this.ending);
var condition = this.getCondition(context);
parent.insertBefore(start, node || null);
node = attachContent(parent, node, this.content, blockContext);
parent.insertBefore(end, node || null);
updateRange(context, null, this, start, end, null, condition);
return node;
};
Block.prototype.type = 'Block';
Block.prototype.serialize = function() {
return serializeObject.instance(this, this.expression, this.content);
};
Block.prototype.update = function(context, binding) {
if (!binding.start.parentNode) return;
var condition = this.getCondition(context);
// Cancel update if prior condition is equivalent to current value
if (equalConditions(condition, binding.condition)) return;
binding.condition = condition;
// Get start and end in advance, since binding is mutated in getFragment
var start = binding.start;
var end = binding.end;
var fragment = this.getFragment(context, binding);
replaceRange(context, start, end, fragment, binding);
};
Block.prototype.getCondition = function(context) {
// We do an identity check to see if the value has changed before updating.
// With objects, the object would still be the same, so this identity check
// would fail to update enough. Thus, return NaN, which never equals anything
// including itself, so that we always update on objects.
//
// We could also JSON stringify or use some other hashing approach. However,
// that could be really expensive on gets of things that never change, and
// is probably not a good tradeoff. Perhaps there should be a separate block
// type that is only used in the case of dynamic updates
var value = this.expression.get(context);
return (typeof value === 'object') ? NaN : value;
};
DynamicText.prototype._blockUpdate = Block.prototype.update;
function ConditionalBlock(expressions, contents) {
this.expressions = expressions;
this.beginning = expressions.join('; ');
this.ending = '/' + this.beginning;
this.contents = contents;
}
ConditionalBlock.prototype = Object.create(Block.prototype);
ConditionalBlock.prototype.constructor = ConditionalBlock;
ConditionalBlock.prototype.get = function(context, unescaped) {
var condition = this.getCondition(context);
if (condition == null) return '';
var expression = this.expressions[condition];
var blockContext = context.child(expression);
return contentHtml(this.contents[condition], blockContext, unescaped);
};
ConditionalBlock.prototype.appendTo = function(parent, context, binding) {
var start = document.createComment(this.beginning);
var end = document.createComment(this.ending);
parent.appendChild(start);
var condition = this.getCondition(context);
if (condition != null) {
var expression = this.expressions[condition];
var blockContext = context.child(expression);
appendContent(parent, this.contents[condition], blockContext);
}
parent.appendChild(end);
updateRange(context, binding, this, start, end, null, condition);
};
ConditionalBlock.prototype.attachTo = function(parent, node, context) {
var start = document.createComment(this.beginning);
var end = document.createComment(this.ending);
parent.insertBefore(start, node || null);
var condition = this.getCondition(context);
if (condition != null) {
var expression = this.expressions[condition];
var blockContext = context.child(expression);
node = attachContent(parent, node, this.contents[condition], blockContext);
}
parent.insertBefore(end, node || null);
updateRange(context, null, this, start, end, null, condition);
return node;
};
ConditionalBlock.prototype.type = 'ConditionalBlock';
ConditionalBlock.prototype.serialize = function() {
return serializeObject.instance(this, this.expressions, this.contents);
};
ConditionalBlock.prototype.update = function(context, binding) {
if (!binding.start.parentNode) return;
var condition = this.getCondition(context);
// Cancel update if prior condition is equivalent to current value
if (equalConditions(condition, binding.condition)) return;
binding.condition = condition;
// Get start and end in advance, since binding is mutated in getFragment
var start = binding.start;
var end = binding.end;
var fragment = this.getFragment(context, binding);
replaceRange(context, start, end, fragment, binding);
};
ConditionalBlock.prototype.getCondition = function(context) {
for (var i = 0, len = this.expressions.length; i < len; i++) {
if (this.expressions[i].truthy(context)) {
return i;
}
}
};
function EachBlock(expression, content, elseContent) {
this.expression = expression;
this.ending = '/' + expression;
this.content = content;
this.elseContent = elseContent;
}
EachBlock.prototype = Object.create(Block.prototype);
EachBlock.prototype.constructor = EachBlock;
EachBlock.prototype.get = function(context, unescaped) {
var items = this.expression.get(context);
if (items && items.length) {
var html = '';
for (var i = 0, len = items.length; i < len; i++) {
var itemContext = context.eachChild(this.expression, i);
html += contentHtml(this.content, itemContext, unescaped);
}
return html;
} else if (this.elseContent) {
return contentHtml(this.elseContent, context, unescaped);
}
return '';
};
EachBlock.prototype.appendTo = function(parent, context, binding) {
var items = this.expression.get(context);
var start = document.createComment(this.expression);
var end = document.createComment(this.ending);
parent.appendChild(start);
if (items && items.length) {
for (var i = 0, len = items.length; i < len; i++) {
var itemContext = context.eachChild(this.expression, i);
this.appendItemTo(parent, itemContext, start);
}
} else if (this.elseContent) {
appendContent(parent, this.elseContent, context);
}
parent.appendChild(end);
updateRange(context, binding, this, start, end);
};
EachBlock.prototype.appendItemTo = function(parent, context, itemFor, binding) {
var before = parent.lastChild;
var start, end;
appendContent(parent, this.content, context);
if (before === parent.lastChild) {
start = end = document.createComment('empty');
parent.appendChild(start);
} else {
start = (before && before.nextSibling) || parent.firstChild;
end = parent.lastChild;
}
updateRange(context, binding, this, start, end, itemFor);
};
EachBlock.prototype.attachTo = function(parent, node, context) {
var items = this.expression.get(context);
var start = document.createComment(this.expression);
var end = document.createComment(this.ending);
parent.insertBefore(start, node || null);
if (items && items.length) {
for (var i = 0, len = items.length; i < len; i++) {
var itemContext = context.eachChild(this.expression, i);
node = this.attachItemTo(parent, node, itemContext, start);
}
} else if (this.elseContent) {
node = attachContent(parent, node, this.elseContent, context);
}
parent.insertBefore(end, node || null);
updateRange(context, null, this, start, end);
return node;
};
EachBlock.prototype.attachItemTo = function(parent, node, context, itemFor) {
var start, end;
var oldPrevious = node && node.previousSibling;
var nextNode = attachContent(parent, node, this.content, context);
if (nextNode === node) {
start = end = document.createComment('empty');
parent.insertBefore(start, node || null);
} else {
start = (oldPrevious && oldPrevious.nextSibling) || parent.firstChild;
end = (nextNode && nextNode.previousSibling) || parent.lastChild;
}
updateRange(context, null, this, start, end, itemFor);
return nextNode;
};
EachBlock.prototype.update = function(context, binding) {
if (!binding.start.parentNode) return;
var start = binding.start;
var end = binding.end;
if (binding.itemFor) {
var fragment = document.createDocumentFragment();
this.appendItemTo(fragment, context, binding.itemFor, binding);
} else {
var fragment = this.getFragment(context, binding);
}
replaceRange(context, start, end, fragment, binding);
};
EachBlock.prototype.insert = function(context, binding, index, howMany) {
var parent = binding.start.parentNode;
if (!parent) return;
// In case we are inserting all of the items, update instead. This is needed
// when we were previously rendering elseContent so that it is replaced
if (index === 0 && this.expression.get(context).length === howMany) {
return this.update(context, binding);
}
var node = indexStartNode(binding, index);
var fragment = document.createDocumentFragment();
for (var i = index, len = index + howMany; i < len; i++) {
var itemContext = context.eachChild(this.expression, i);
this.appendItemTo(fragment, itemContext, binding.start);
}
parent.insertBefore(fragment, node || null);
};
EachBlock.prototype.remove = function(context, binding, index, howMany) {
var parent = binding.start.parentNode;
if (!parent) return;
// In case we are removing all of the items, update instead. This is needed
// when elseContent should be rendered
if (index === 0 && this.expression.get(context).length === 0) {
return this.update(context, binding);
}
var node = indexStartNode(binding, index);
var i = 0;
while (node) {
if (node === binding.end) return;
if (node.$bindItemStart && node.$bindItemStart.itemFor === binding.start) {
if (howMany === i++) return;
}
var nextNode = node.nextSibling;
parent.removeChild(node);
emitRemoved(context, node, binding);
node = nextNode;
}
};
EachBlock.prototype.move = function(context, binding, from, to, howMany) {
var parent = binding.start.parentNode;
if (!parent) return;
var node = indexStartNode(binding, from);
var fragment = document.createDocumentFragment();
var i = 0;
while (node) {
if (node === binding.end) break;