-
Notifications
You must be signed in to change notification settings - Fork 80
/
VirtualDom.js
1570 lines (1228 loc) · 31 KB
/
VirtualDom.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
/*
import Basics exposing (identity)
import Elm.Kernel.Debug exposing (crash)
import Elm.Kernel.Json exposing (equality, runHelp, unwrap)
import Elm.Kernel.List exposing (Cons, Nil)
import Elm.Kernel.Utils exposing (Tuple2)
import Elm.Kernel.Platform exposing (export)
import Json.Decode as Json exposing (map, map2, succeed)
import Result exposing (isOk)
import VirtualDom exposing (toHandlerInt)
*/
// HELPERS
var _VirtualDom_divertHrefToApp;
var _VirtualDom_doc = typeof document !== 'undefined' ? document : {};
function _VirtualDom_appendChild(parent, child)
{
parent.appendChild(child);
}
var _VirtualDom_init = F4(function(virtualNode, flagDecoder, debugMetadata, args)
{
// NOTE: this function needs __Platform_export available to work
/**__PROD/
var node = args['node'];
//*/
/**__DEBUG/
var node = args && args['node'] ? args['node'] : __Debug_crash(0);
//*/
node.parentNode.replaceChild(
_VirtualDom_render(virtualNode, function() {}),
node
);
return {};
});
// TEXT
function _VirtualDom_text(string)
{
return {
$: __2_TEXT,
__text: string
};
}
// NODE
var _VirtualDom_nodeNS = F2(function(namespace, tag)
{
return F2(function(factList, kidList)
{
for (var kids = [], descendantsCount = 0; kidList.b; kidList = kidList.b) // WHILE_CONS
{
var kid = kidList.a;
descendantsCount += (kid.__descendantsCount || 0);
kids.push(kid);
}
descendantsCount += kids.length;
return {
$: __2_NODE,
__tag: tag,
__facts: _VirtualDom_organizeFacts(factList),
__kids: kids,
__namespace: namespace,
__descendantsCount: descendantsCount
};
});
});
var _VirtualDom_node = _VirtualDom_nodeNS(undefined);
// KEYED NODE
var _VirtualDom_keyedNodeNS = F2(function(namespace, tag)
{
return F2(function(factList, kidList)
{
for (var kids = [], descendantsCount = 0; kidList.b; kidList = kidList.b) // WHILE_CONS
{
var kid = kidList.a;
descendantsCount += (kid.b.__descendantsCount || 0);
kids.push(kid);
}
descendantsCount += kids.length;
return {
$: __2_KEYED_NODE,
__tag: tag,
__facts: _VirtualDom_organizeFacts(factList),
__kids: kids,
__namespace: namespace,
__descendantsCount: descendantsCount
};
});
});
var _VirtualDom_keyedNode = _VirtualDom_keyedNodeNS(undefined);
// CUSTOM
function _VirtualDom_custom(factList, model, render, diff)
{
return {
$: __2_CUSTOM,
__facts: _VirtualDom_organizeFacts(factList),
__model: model,
__render: render,
__diff: diff
};
}
// MAP
var _VirtualDom_map = F2(function(tagger, node)
{
return {
$: __2_TAGGER,
__tagger: tagger,
__node: node,
__descendantsCount: 1 + (node.__descendantsCount || 0)
};
});
// LAZY
function _VirtualDom_thunk(refs, thunk)
{
return {
$: __2_THUNK,
__refs: refs,
__thunk: thunk,
__node: undefined
};
}
var _VirtualDom_lazy = F2(function(func, a)
{
return _VirtualDom_thunk([func, a], function() {
return func(a);
});
});
var _VirtualDom_lazy2 = F3(function(func, a, b)
{
return _VirtualDom_thunk([func, a, b], function() {
return A2(func, a, b);
});
});
var _VirtualDom_lazy3 = F4(function(func, a, b, c)
{
return _VirtualDom_thunk([func, a, b, c], function() {
return A3(func, a, b, c);
});
});
var _VirtualDom_lazy4 = F5(function(func, a, b, c, d)
{
return _VirtualDom_thunk([func, a, b, c, d], function() {
return A4(func, a, b, c, d);
});
});
var _VirtualDom_lazy5 = F6(function(func, a, b, c, d, e)
{
return _VirtualDom_thunk([func, a, b, c, d, e], function() {
return A5(func, a, b, c, d, e);
});
});
var _VirtualDom_lazy6 = F7(function(func, a, b, c, d, e, f)
{
return _VirtualDom_thunk([func, a, b, c, d, e, f], function() {
return A6(func, a, b, c, d, e, f);
});
});
var _VirtualDom_lazy7 = F8(function(func, a, b, c, d, e, f, g)
{
return _VirtualDom_thunk([func, a, b, c, d, e, f, g], function() {
return A7(func, a, b, c, d, e, f, g);
});
});
var _VirtualDom_lazy8 = F9(function(func, a, b, c, d, e, f, g, h)
{
return _VirtualDom_thunk([func, a, b, c, d, e, f, g, h], function() {
return A8(func, a, b, c, d, e, f, g, h);
});
});
// FACTS
var _VirtualDom_on = F2(function(key, handler)
{
return {
$: 'a__1_EVENT',
__key: key,
__value: handler
};
});
var _VirtualDom_style = F2(function(key, value)
{
return {
$: 'a__1_STYLE',
__key: key,
__value: value
};
});
var _VirtualDom_property = F2(function(key, value)
{
return {
$: 'a__1_PROP',
__key: key,
__value: value
};
});
var _VirtualDom_attribute = F2(function(key, value)
{
return {
$: 'a__1_ATTR',
__key: key,
__value: value
};
});
var _VirtualDom_attributeNS = F3(function(namespace, key, value)
{
return {
$: 'a__1_ATTR_NS',
__key: key,
__value: { __namespace: namespace, __value: value }
};
});
// XSS ATTACK VECTOR CHECKS
function _VirtualDom_noScript(tag)
{
return tag == 'script' ? 'p' : tag;
}
function _VirtualDom_noOnOrFormAction(key)
{
return /^(on|formAction$)/i.test(key) ? 'data-' + key : key;
}
function _VirtualDom_noInnerHtmlOrFormAction(key)
{
return key == 'innerHTML' || key == 'formAction' ? 'data-' + key : key;
}
function _VirtualDom_noJavaScriptUri__PROD(value)
{
return /^javascript:/i.test(value.replace(/\s/g,'')) ? '' : value;
}
function _VirtualDom_noJavaScriptUri__DEBUG(value)
{
return /^javascript:/i.test(value.replace(/\s/g,''))
? 'javascript:alert("This is an XSS vector. Please use ports or web components instead.")'
: value;
}
function _VirtualDom_noJavaScriptOrHtmlUri__PROD(value)
{
return /^\s*(javascript:|data:text\/html)/i.test(value) ? '' : value;
}
function _VirtualDom_noJavaScriptOrHtmlUri__DEBUG(value)
{
return /^\s*(javascript:|data:text\/html)/i.test(value)
? 'javascript:alert("This is an XSS vector. Please use ports or web components instead.")'
: value;
}
// MAP FACTS
var _VirtualDom_mapAttribute = F2(function(func, attr)
{
return (attr.$ === 'a__1_EVENT')
? A2(_VirtualDom_on, attr.__key, _VirtualDom_mapHandler(func, attr.__value))
: attr;
});
function _VirtualDom_mapHandler(func, handler)
{
var tag = __VirtualDom_toHandlerInt(handler);
// 0 = Normal
// 1 = MayStopPropagation
// 2 = MayPreventDefault
// 3 = Custom
return {
$: handler.$,
a:
!tag
? A2(__Json_map, func, handler.a)
:
A3(__Json_map2,
tag < 3
? _VirtualDom_mapEventTuple
: _VirtualDom_mapEventRecord,
__Json_succeed(func),
handler.a
)
};
}
var _VirtualDom_mapEventTuple = F2(function(func, tuple)
{
return __Utils_Tuple2(func(tuple.a), tuple.b);
});
var _VirtualDom_mapEventRecord = F2(function(func, record)
{
return {
__$message: func(record.__$message),
__$stopPropagation: record.__$stopPropagation,
__$preventDefault: record.__$preventDefault
}
});
// ORGANIZE FACTS
function _VirtualDom_organizeFacts(factList)
{
for (var facts = {}; factList.b; factList = factList.b) // WHILE_CONS
{
var entry = factList.a;
var tag = entry.$;
var key = entry.__key;
var value = entry.__value;
if (tag === 'a__1_PROP')
{
(key === 'className')
? _VirtualDom_addClass(facts, key, __Json_unwrap(value))
: facts[key] = __Json_unwrap(value);
continue;
}
var subFacts = facts[tag] || (facts[tag] = {});
(tag === 'a__1_ATTR' && key === 'class')
? _VirtualDom_addClass(subFacts, key, value)
: subFacts[key] = value;
}
return facts;
}
function _VirtualDom_addClass(object, key, newClass)
{
var classes = object[key];
object[key] = classes ? classes + ' ' + newClass : newClass;
}
// RENDER
function _VirtualDom_render(vNode, eventNode)
{
var tag = vNode.$;
if (tag === __2_THUNK)
{
return _VirtualDom_render(vNode.__node || (vNode.__node = vNode.__thunk()), eventNode);
}
if (tag === __2_TEXT)
{
return _VirtualDom_doc.createTextNode(vNode.__text);
}
if (tag === __2_TAGGER)
{
var subNode = vNode.__node;
var tagger = vNode.__tagger;
while (subNode.$ === __2_TAGGER)
{
typeof tagger !== 'object'
? tagger = [tagger, subNode.__tagger]
: tagger.push(subNode.__tagger);
subNode = subNode.__node;
}
var subEventRoot = { __tagger: tagger, __parent: eventNode };
var domNode = _VirtualDom_render(subNode, subEventRoot);
domNode.elm_event_node_ref = subEventRoot;
return domNode;
}
if (tag === __2_CUSTOM)
{
var domNode = vNode.__render(vNode.__model);
_VirtualDom_applyFacts(domNode, eventNode, vNode.__facts);
return domNode;
}
// at this point `tag` must be __2_NODE or __2_KEYED_NODE
var domNode = vNode.__namespace
? _VirtualDom_doc.createElementNS(vNode.__namespace, vNode.__tag)
: _VirtualDom_doc.createElement(vNode.__tag);
if (_VirtualDom_divertHrefToApp && vNode.__tag == 'a')
{
domNode.addEventListener('click', _VirtualDom_divertHrefToApp(domNode));
}
_VirtualDom_applyFacts(domNode, eventNode, vNode.__facts);
for (var kids = vNode.__kids, i = 0; i < kids.length; i++)
{
_VirtualDom_appendChild(domNode, _VirtualDom_render(tag === __2_NODE ? kids[i] : kids[i].b, eventNode));
}
return domNode;
}
// APPLY FACTS
function _VirtualDom_applyFacts(domNode, eventNode, facts)
{
for (var key in facts)
{
var value = facts[key];
key === 'a__1_STYLE'
? _VirtualDom_applyStyles(domNode, value)
:
key === 'a__1_EVENT'
? _VirtualDom_applyEvents(domNode, eventNode, value)
:
key === 'a__1_ATTR'
? _VirtualDom_applyAttrs(domNode, value)
:
key === 'a__1_ATTR_NS'
? _VirtualDom_applyAttrsNS(domNode, value)
:
((key !== 'value' && key !== 'checked') || domNode[key] !== value) && (domNode[key] = value);
}
}
// APPLY STYLES
function _VirtualDom_applyStyles(domNode, styles)
{
var domNodeStyle = domNode.style;
for (var key in styles)
{
domNodeStyle[key] = styles[key];
}
}
// APPLY ATTRS
function _VirtualDom_applyAttrs(domNode, attrs)
{
for (var key in attrs)
{
var value = attrs[key];
typeof value !== 'undefined'
? domNode.setAttribute(key, value)
: domNode.removeAttribute(key);
}
}
// APPLY NAMESPACED ATTRS
function _VirtualDom_applyAttrsNS(domNode, nsAttrs)
{
for (var key in nsAttrs)
{
var pair = nsAttrs[key];
var namespace = pair.__namespace;
var value = pair.__value;
typeof value !== 'undefined'
? domNode.setAttributeNS(namespace, key, value)
: domNode.removeAttributeNS(namespace, key);
}
}
// APPLY EVENTS
function _VirtualDom_applyEvents(domNode, eventNode, events)
{
var allCallbacks = domNode.elmFs || (domNode.elmFs = {});
for (var key in events)
{
var newHandler = events[key];
var oldCallback = allCallbacks[key];
if (!newHandler)
{
domNode.removeEventListener(key, oldCallback);
allCallbacks[key] = undefined;
continue;
}
if (oldCallback)
{
var oldHandler = oldCallback.__handler;
if (oldHandler.$ === newHandler.$)
{
oldCallback.__handler = newHandler;
continue;
}
domNode.removeEventListener(key, oldCallback);
}
oldCallback = _VirtualDom_makeCallback(eventNode, newHandler);
domNode.addEventListener(key, oldCallback,
_VirtualDom_passiveSupported
&& { passive: __VirtualDom_toHandlerInt(newHandler) < 2 }
);
allCallbacks[key] = oldCallback;
}
}
// PASSIVE EVENTS
var _VirtualDom_passiveSupported;
try
{
window.addEventListener('t', null, Object.defineProperty({}, 'passive', {
get: function() { _VirtualDom_passiveSupported = true; }
}));
}
catch(e) {}
// EVENT HANDLERS
function _VirtualDom_makeCallback(eventNode, initialHandler)
{
function callback(event)
{
var handler = callback.__handler;
var result = __Json_runHelp(handler.a, event);
if (!__Result_isOk(result))
{
return;
}
var tag = __VirtualDom_toHandlerInt(handler);
// 0 = Normal
// 1 = MayStopPropagation
// 2 = MayPreventDefault
// 3 = Custom
var value = result.a;
var message = !tag ? value : tag < 3 ? value.a : value.__$message;
var stopPropagation = tag == 1 ? value.b : tag == 3 && value.__$stopPropagation;
var currentEventNode = (
stopPropagation && event.stopPropagation(),
(tag == 2 ? value.b : tag == 3 && value.__$preventDefault) && event.preventDefault(),
eventNode
);
var tagger;
var i;
while (tagger = currentEventNode.__tagger)
{
if (typeof tagger == 'function')
{
message = tagger(message);
}
else
{
for (var i = tagger.length; i--; )
{
message = tagger[i](message);
}
}
currentEventNode = currentEventNode.__parent;
}
currentEventNode(message, stopPropagation); // stopPropagation implies isSync
}
callback.__handler = initialHandler;
return callback;
}
function _VirtualDom_equalEvents(x, y)
{
return x.$ == y.$ && __Json_equality(x.a, y.a);
}
// DIFF
// TODO: Should we do patches like in iOS?
//
// type Patch
// = At Int Patch
// | Batch (List Patch)
// | Change ...
//
// How could it not be better?
//
function _VirtualDom_diff(x, y)
{
var patches = [];
_VirtualDom_diffHelp(x, y, patches, 0);
return patches;
}
function _VirtualDom_pushPatch(patches, type, index, data)
{
var patch = {
$: type,
__index: index,
__data: data,
__domNode: undefined,
__eventNode: undefined
};
patches.push(patch);
return patch;
}
function _VirtualDom_diffHelp(x, y, patches, index)
{
if (x === y)
{
return;
}
var xType = x.$;
var yType = y.$;
// Bail if you run into different types of nodes. Implies that the
// structure has changed significantly and it's not worth a diff.
if (xType !== yType)
{
if (xType === __2_NODE && yType === __2_KEYED_NODE)
{
y = _VirtualDom_dekey(y);
yType = __2_NODE;
}
else
{
_VirtualDom_pushPatch(patches, __3_REDRAW, index, y);
return;
}
}
// Now we know that both nodes are the same $.
switch (yType)
{
case __2_THUNK:
var xRefs = x.__refs;
var yRefs = y.__refs;
var i = xRefs.length;
var same = i === yRefs.length;
while (same && i--)
{
same = xRefs[i] === yRefs[i];
}
if (same)
{
y.__node = x.__node;
return;
}
y.__node = y.__thunk();
var subPatches = [];
_VirtualDom_diffHelp(x.__node, y.__node, subPatches, 0);
subPatches.length > 0 && _VirtualDom_pushPatch(patches, __3_THUNK, index, subPatches);
return;
case __2_TAGGER:
// gather nested taggers
var xTaggers = x.__tagger;
var yTaggers = y.__tagger;
var nesting = false;
var xSubNode = x.__node;
while (xSubNode.$ === __2_TAGGER)
{
nesting = true;
typeof xTaggers !== 'object'
? xTaggers = [xTaggers, xSubNode.__tagger]
: xTaggers.push(xSubNode.__tagger);
xSubNode = xSubNode.__node;
}
var ySubNode = y.__node;
while (ySubNode.$ === __2_TAGGER)
{
nesting = true;
typeof yTaggers !== 'object'
? yTaggers = [yTaggers, ySubNode.__tagger]
: yTaggers.push(ySubNode.__tagger);
ySubNode = ySubNode.__node;
}
// Just bail if different numbers of taggers. This implies the
// structure of the virtual DOM has changed.
if (nesting && xTaggers.length !== yTaggers.length)
{
_VirtualDom_pushPatch(patches, __3_REDRAW, index, y);
return;
}
// check if taggers are "the same"
if (nesting ? !_VirtualDom_pairwiseRefEqual(xTaggers, yTaggers) : xTaggers !== yTaggers)
{
_VirtualDom_pushPatch(patches, __3_TAGGER, index, yTaggers);
}
// diff everything below the taggers
_VirtualDom_diffHelp(xSubNode, ySubNode, patches, index + 1);
return;
case __2_TEXT:
if (x.__text !== y.__text)
{
_VirtualDom_pushPatch(patches, __3_TEXT, index, y.__text);
}
return;
case __2_NODE:
_VirtualDom_diffNodes(x, y, patches, index, _VirtualDom_diffKids);
return;
case __2_KEYED_NODE:
_VirtualDom_diffNodes(x, y, patches, index, _VirtualDom_diffKeyedKids);
return;
case __2_CUSTOM:
if (x.__render !== y.__render)
{
_VirtualDom_pushPatch(patches, __3_REDRAW, index, y);
return;
}
var factsDiff = _VirtualDom_diffFacts(x.__facts, y.__facts);
factsDiff && _VirtualDom_pushPatch(patches, __3_FACTS, index, factsDiff);
var patch = y.__diff(x.__model, y.__model);
patch && _VirtualDom_pushPatch(patches, __3_CUSTOM, index, patch);
return;
}
}
// assumes the incoming arrays are the same length
function _VirtualDom_pairwiseRefEqual(as, bs)
{
for (var i = 0; i < as.length; i++)
{
if (as[i] !== bs[i])
{
return false;
}
}
return true;
}
function _VirtualDom_diffNodes(x, y, patches, index, diffKids)
{
// Bail if obvious indicators have changed. Implies more serious
// structural changes such that it's not worth it to diff.
if (x.__tag !== y.__tag || x.__namespace !== y.__namespace)
{
_VirtualDom_pushPatch(patches, __3_REDRAW, index, y);
return;
}
var factsDiff = _VirtualDom_diffFacts(x.__facts, y.__facts);
factsDiff && _VirtualDom_pushPatch(patches, __3_FACTS, index, factsDiff);
diffKids(x, y, patches, index);
}
// DIFF FACTS
// TODO Instead of creating a new diff object, it's possible to just test if
// there *is* a diff. During the actual patch, do the diff again and make the
// modifications directly. This way, there's no new allocations. Worth it?
function _VirtualDom_diffFacts(x, y, category)
{
var diff;
// look for changes and removals
for (var xKey in x)
{
if (xKey === 'a__1_STYLE' || xKey === 'a__1_EVENT' || xKey === 'a__1_ATTR' || xKey === 'a__1_ATTR_NS')
{
var subDiff = _VirtualDom_diffFacts(x[xKey], y[xKey] || {}, xKey);
if (subDiff)
{
diff = diff || {};
diff[xKey] = subDiff;
}
continue;
}
// remove if not in the new facts
if (!(xKey in y))
{
diff = diff || {};
diff[xKey] =
!category
? (typeof x[xKey] === 'string' ? '' : null)
:
(category === 'a__1_STYLE')
? ''
:
(category === 'a__1_EVENT' || category === 'a__1_ATTR')
? undefined
:
{ __namespace: x[xKey].__namespace, __value: undefined };
continue;
}
var xValue = x[xKey];
var yValue = y[xKey];
// reference equal, so don't worry about it
if (xValue === yValue && xKey !== 'value' && xKey !== 'checked'
|| category === 'a__1_EVENT' && _VirtualDom_equalEvents(xValue, yValue))
{
continue;
}
diff = diff || {};
diff[xKey] = yValue;
}
// add new stuff
for (var yKey in y)
{
if (!(yKey in x))
{
diff = diff || {};
diff[yKey] = y[yKey];
}
}
return diff;
}
// DIFF KIDS
function _VirtualDom_diffKids(xParent, yParent, patches, index)
{
var xKids = xParent.__kids;
var yKids = yParent.__kids;
var xLen = xKids.length;
var yLen = yKids.length;
// FIGURE OUT IF THERE ARE INSERTS OR REMOVALS
if (xLen > yLen)
{
_VirtualDom_pushPatch(patches, __3_REMOVE_LAST, index, {
__length: yLen,
__diff: xLen - yLen
});
}
else if (xLen < yLen)
{
_VirtualDom_pushPatch(patches, __3_APPEND, index, {
__length: xLen,
__kids: yKids
});
}
// PAIRWISE DIFF EVERYTHING ELSE
for (var minLen = xLen < yLen ? xLen : yLen, i = 0; i < minLen; i++)
{
var xKid = xKids[i];
_VirtualDom_diffHelp(xKid, yKids[i], patches, ++index);
index += xKid.__descendantsCount || 0;
}
}
// KEYED DIFF
function _VirtualDom_diffKeyedKids(xParent, yParent, patches, rootIndex)
{
var localPatches = [];
var changes = {}; // Dict String Entry
var inserts = []; // Array { index : Int, entry : Entry }
// type Entry = { tag : String, vnode : VNode, index : Int, data : _ }
var xKids = xParent.__kids;
var yKids = yParent.__kids;
var xLen = xKids.length;
var yLen = yKids.length;
var xIndex = 0;
var yIndex = 0;
var index = rootIndex;
while (xIndex < xLen && yIndex < yLen)
{
var x = xKids[xIndex];
var y = yKids[yIndex];