-
Notifications
You must be signed in to change notification settings - Fork 3
/
doTA.js
1850 lines (1594 loc) · 58.5 KB
/
doTA.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
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = factory(global, global.document);
} else {
factory(global, global.document);
}
}(typeof window !== 'undefined' ? window : this, function(window, document) {
/* for ie8 */
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,'');
};
}
/* no, thanks! firefox */
if (Object.prototype.watch) {
delete Object.prototype.watch;
delete Object.prototype.unwatch;
}
//less memory usage with this, javascript is single threaded anyway
var newNode = document && document.createElement('div');
var doTA = {
diff: diff1,
diff2: diff2,
diff3: diff3,
getId: getUniqueId,
initCH: initCompileHash,
compile: compileHTML,
// PS: parseStyle,
C: {}, //Cached compiled functions
D: {}, //Cached DOM to be used by ngDoTA, needed here to prevent unneccessary rendering
H: {}, //HashMap for TextDiff
W: {}, //Watched Functions
U: {},
N: newNode,
X: 0
};
// pretty indent for debugging
function indent(n, x) {
var ret = new Array(n + 2).join(' '); //4 spaces
return x ? ret.slice(0, -2 * x) : ret; //-2 spaces
}
// decode html entities
function decodeEntities(text) {
return text.indexOf('&') < 0 ? text : text
.replace(/>/g, '>').replace(/</g, '<')
.replace(/&/g, '&').replace(/"/g, '"');
}
// parse attributes from html open tag and make dict object
function parseAttr(chunk, func) {
var attr = {}, tagName;
var pos = chunk.indexOf(' ');
var spPos;
var len, attrName, attrVal;
var valStart, valEndPos;
if (pos !== -1) {
tagName = chunk.slice(0, pos);
len = chunk.length;
//console.log(222, [pos, chunk]);
while (++pos < len) {
var eqPos = chunk.indexOf('=', pos);
// console.log('chunk', [chunk, pos, eqPos, chunk.slice(pos)]);
// ** attribute without value (last attribute) **
if (eqPos === -1) {
do {
spPos = chunk.indexOf(' ', pos);
if (spPos > 0) {
attrName = chunk.slice(pos, spPos);
} else {
attrName = chunk.slice(pos);
}
// console.log('eqPos === -1', [attrName, pos, chunk])
if (attrName !== '/') {
attr[attrName] = '';
}
pos = spPos + 1;
} while (spPos > 0);
//attr required will be required="", while is valid syntax
//http://www.w3.org/TR/html-markup/syntax.html#syntax-attr-empty
break;
}
spPos = chunk.indexOf(' ', pos);
// console.log('chunk', [chunk, eqPos, pos, spPos, chunk.slice(pos, spPos)]);
if (spPos > 0 && spPos < eqPos) {
attr[chunk.slice(pos, spPos)] = "";
pos = spPos;
continue;
}
//console.log(33, [eqPos]);
attrName = chunk.slice(pos, eqPos);
//console.log(331, [attrName]);
valStart = chunk[eqPos + 1];
//console.log(332, [valStart]);
//if attribute value is start with quote
if (valStart === '"' || valStart === "'") {
valEndPos = chunk.indexOf(valStart, eqPos + 2);
if (valEndPos < 0) { throw 'ERR:Invalid HTML: [' + chunk + ']'; }
attrVal = chunk.slice(eqPos + 2, valEndPos);
attr[attrName] = decodeEntities(attrVal);
pos = valEndPos + 1;
//console.log(311, [valEndPos, attrName, attrVal]);
} else {
valEndPos = chunk.indexOf(' ', eqPos + 2);
//when no more attributes
if (valEndPos < 0) {
attrVal = chunk.slice(eqPos + 1);
attr[attrName] = decodeEntities(attrVal);
//console.log(442, [attrVal]);
break;
} else {
attrVal = chunk.slice(eqPos + 1, valEndPos);
attr[attrName] = decodeEntities(attrVal);
//console.log(313, [eqPos, valEndPos, attrVal]);
pos = valEndPos;
}
}
}
var tagNameL = tagName.toLowerCase();
if (tagNameL === 'input' || tagNameL === 'img' || tagNameL === 'hr') {
//http://www.w3.org/TR/html-markup/syntax.html
//area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr
func.openTag(tagName, attr, 1);
func.voidTag();
} else if (attrName === '/') {
func.openTag(tagName, attr);
func.closeTag(tagName);
} else {
func.openTag(tagName, attr);
}
// no attributes
} else {
// self closing, explicit
if (chunk[chunk.length - 1] === '/') {
tagName = chunk.slice(0, -1).toLowerCase();
if (tagName === 'br' || tagName === 'hr') {
func.openTag(tagName, attr, 1);
func.voidTag();
} else {
func.openTag(tagName, attr);
func.closeTag(tagName);
}
} else {
tagName = chunk.toLowerCase();
// self closing, implicit
if (tagName === 'br' || tagName === 'hr') {
func.openTag(tagName, attr, 1);
func.voidTag();
} else {
func.openTag(tagName, attr);
}
}
}
}
//http://jsperf.com/object-key-vs-array-indexof-lookup/6
//var events = ' scroll change click dblclick mousedown mouseup mouseover mouseout mousemove mouseenter mouseleave keydown keyup keypress submit focus blur copy cut paste '
var EVENTS = {
scroll: 1, change: 1, input: 1, click: 1, dblclick: 1,
mousedown: 1, mouseup: 1, mouseover: 1, mouseout: 1, mousemove: 1, mouseenter: 1, mouseleave: 1,
keydown: 1, keyup: 1, keypress: 1,
submit: 1, focus: 1, blur: 1,
copy: 1, cut: 1, paste: 1
};
//no unicode supportedgit
// var valid_chr = '_$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
// var VALID_CHARS = {"_":1,"$":1,"a":1,"b":1,"c":1,"d":1,"e":1,"f":1,"g":1,"h":1,"i":1,"j":1,"k":1,"l":1,"m":1,"n":1,"o":1,"p":1,"q":1,"r":1,"s":1,"t":1,"u":1,"v":1,"w":1,"x":1,"y":1,"z":1,"A":1,"B":1,"C":1,"D":1,"E":1,"F":1,"G":1,"H":1,"I":1,"J":1,"K":1,"L":1,"M":1,"N":1,"O":1,"P":1,"Q":1,"R":1,"S":1,"T":1,"U":1,"V":1,"W":1,"X":1,"Y":1,"Z":1};
// minimal stripped down html parser
function parseHTML(html, func) {
if (!html) { return; }
var prevPos = 0;
if (html[1] === '?' && html[0] === '<') {
prevPos = html.indexOf('?', 2);
if (prevPos >= 1) {
prevPos += 2;
func.text(html.substring(0, prevPos));
} else {
prevPos = 0;
}
}
var pos = html.indexOf('<', prevPos);
do {
if (html[pos] === '<') {
pos++;
if (html[pos] === '/') {
prevPos = ++pos;
pos = html.indexOf('>', prevPos);
//close tag must be like </div>, but not <div />
// console.log(['closetag', prevPos, pos, html.substring(prevPos, pos)])
func.closeTag(html.substring(prevPos, pos));
} else if (html[pos] === '!') {
prevPos = pos;
pos = html.indexOf('-->', prevPos);
//console.log(['comment', prevPos, pos, html.substring(prevPos, pos + 2)]);
func.comment(html.substring(prevPos, pos + 2));
pos += 2;
} else {
prevPos = pos;
pos = html.indexOf('>', prevPos);
// console.log(['opentag', prevPos, pos, html.substring(prevPos, pos), parseAttr(html.substring(prevPos, pos))])
parseAttr(html.substring(prevPos, pos), func);
}
} else if (html[pos] === '>') { //&& html[pos + 1] !== '<'
prevPos = ++pos;
pos = html.indexOf('<', prevPos);
if (pos > prevPos) {
// console.log(['text', prevPos, pos, html.substring(prevPos, pos)])
func.text(html.substring(prevPos, pos));
}
} else {
if (prevPos === 0 && pos < 0 && html) {
func.text(html);
} else {
/** */console.error('Parse ERR?', [prevPos, pos, html, html.substring(prevPos, pos), html.slice(pos)]);
}
break;
}
} while (pos > 0);
}
//diff and patch dom with exact same structure
function diff1(prevKey, html2) {
var html1 = doTA.H[prevKey];
var prevPos1 = 0, pos1 = html1.indexOf('<');
var prevPos2 = 0, pos2 = html2.indexOf('<');
var tagId = '', elem, part1, part2;
var posx, endPosx;
do {
if (html1[pos1] === "<") {
pos1++;
pos2++;
if (html1[pos1] === "/" || html1[pos1] === "!") {
//don't patch comment node and close tag.
pos1 = html1.indexOf('>', pos1);
pos2 = html2.indexOf('>', pos2);
} else {
prevPos1 = pos1;
prevPos2 = pos2;
pos1 = html1.indexOf('>', prevPos1);
pos2 = html2.indexOf('>', prevPos2);
part1 = html1.substring(prevPos1, pos1);
part2 = html2.substring(prevPos2, pos2);
//attributes
if (part1 !== part2) {
// console.log('openTag', [part1, part2])
tagId = parsePatchAttr(part1, part2);
} else {
//record id
//tagId = getTagId(part1);
posx = part1.indexOf(' id="');
0 <= posx && (posx += 5, endPosx = part1.indexOf('"', posx), tagId = part1.substring(posx, endPosx)); //jshint ignore: line
}
}
//text node
} else if (html1[pos1] === '>') {
prevPos1 = ++pos1;
prevPos2 = ++pos2;
pos1 = html1.indexOf('<', prevPos1);
pos2 = html2.indexOf('<', prevPos2);
//textNode, only support firstChild here
if (pos2 > prevPos2) {
var text1 = html1.substring(prevPos1, pos1);
var text2 = html2.substring(prevPos2, pos2);
if (text1 !== text2) {
elem = document.getElementById(tagId);
if (elem) {
if (elem.firstChild && elem.firstChild.nodeType === 3) {
// console.log('textApplied', [text1, text2]);
elem.firstChild.nodeValue = text2;
} //else to log something?
} else {
console.log('tag not found', [tagId]);
}
}
}
}
} while(pos1 > 0);
}
// find position of outerHTML end
// this function will be inline during building
function getOuterHTMLEnd(HTML, START_POS) {
var LVL = 1, POS = START_POS;
for(;;) {
POS = HTML.indexOf('>', POS);
if (HTML[POS - 1] === '/') { //self closing
LVL--;
if (LVL <= 0) break;
}
POS = HTML.indexOf('<', POS);
if (HTML[POS + 1] === '/') {
LVL--;
if (LVL <= 0) {
POS = HTML.indexOf('>', POS + 2);
break;
}
} else if (HTML[POS + 1] !== '!') {
LVL++;
}
// console.log('LVL', LVL);
}
// console.log('getOutHTML', tagName, [tagName, pos2, pos2, ])
return ++POS;
}
// FlatDOM: diff html as text and patch dom nodes
function diff2(prevKey, html2) {
var html1 = doTA.H[prevKey];
var prevPos1 = 0, pos1 = html1.indexOf('<');
var prevPos2 = 0, pos2 = html2.indexOf('<');
var tagId1, tagId2, elem1, part1, part2;
// var tagNo1 = 0, tagNo2 = 0;
var tagStartPos1, tagStartPos2;
var LVL; //this is needed for fnInline
// console.log(html1);
// console.log(html2);
for (;;) {
// console.log('before', [dirty1, dirty2], [tagId1, tagId2], [html1.substr(pos1, 20), html2.substr(pos2, 20)]);
if (pos1 >= 0) {
pos1 = html1.indexOf(' id="', pos1);
if (pos1 > 0) {
prevPos1 = pos1 + 5;
pos1 = html1.indexOf('"', prevPos1);
tagId1 = html1.substring(prevPos1, pos1);
// tagNo1 = tagId1^0;
}
}
// console.log('middle', [tagId1, tagId2], [html1.substr(pos1, 20), html2.substr(pos2, 20)]);
if (pos2 >= 0) {
pos2 = html2.indexOf(' id="', pos2);
if (pos2 > 0) {
prevPos2 = pos2 + 5;
pos2 = html2.indexOf('"', prevPos2);
tagId2 = html2.substring(prevPos2, pos2);
// tagNo2 = tagId2^0;
}
}
// console.log('after', [dirty1, dirty2], [tagId1, tagId2],
// [pos1, pos2], [html1.substr(pos1, 20), html2.substr(pos2, 20)]);
//exist inifite loop
if (pos1 < 0 || pos2 < 0) break;
//same node
if (tagId1 === tagId2) {
tagStartPos1 = ++pos1;
pos1 = html1.indexOf('>', pos1);
part1 = html1.substring(tagStartPos1, pos1);
tagStartPos2 = ++pos2;
pos2 = html2.indexOf('>', pos2);
part2 = html2.substring(tagStartPos2, pos2);
// console.log('same node', [part1, part2]);
//attr really different
if (part1 !== part2) {
elem1 = document.getElementById(tagId1);
if (elem1) {
//nodes to be inserted or deleted
if ((part1.substr(1, 6) === 'hidden') !== (part2.substr(1, 6) === 'hidden')) {
tagStartPos2 = html2.lastIndexOf('<', pos2 - 6);
pos2 = getOuterHTMLEnd(html2, tagStartPos2);
newNode.innerHTML = html2.substring(tagStartPos2, pos2);
// tagStartPos1 = html1.lastIndexOf('<', pos1 - 6);
// console.warn('replaceChild', [tagId2, tagId1], [
// html2.substring(tagStartPos2, getOuterHTMLEnd(html2, tagStartPos2)),
// html1.substring(tagStartPos1, getOuterHTMLEnd(html1, pos1))]);
elem1.parentNode.replaceChild(newNode.firstChild, elem1);
pos1 = getOuterHTMLEnd(html1, pos1);
//only attribute changes
} else {
parsePatchAttr(part1, part2, elem1);
// console.warn('patch node', [tagId1, tagId2], [pos1, pos2], [tagStartPos1, tagStartPos2], [part1, part2])
}
} else {
console.error('elem not found', [tagId1, tagId2], [part1, part2]);
return;
}
} else {
//clear node for textNode
elem1 = void 0;
}
//if blank text node, skip early
if (html1[pos1 + 1] === '<' && html2[pos2 + 1] === '<') {
pos1++;
pos2++;
continue;
}
prevPos1 = pos1;
pos1 = html1.indexOf('<', prevPos1);
part1 = html1.substring(prevPos1 + 1, pos1);
prevPos2 = pos2;
pos2 = html2.indexOf('<', prevPos2);
part2 = html2.substring(prevPos2 + 1, pos2);
//for text node really diff
if (part1 !== part2) {
// console.log('text diff', [tagId1, tagId2], [part1, part2]);
if (!elem1) {
elem1 = document.getElementById(tagId1);
if (!elem1) {
console.error('node not found', [tagId1, tagId2], [part1, part2], [html1.substr(pos1, 15), html2.substr(pos2, 15)], [html1, html2]);
return;
}
}
// console.log('part1,2', [part1, part2]);
if (elem1.firstChild) {
//overwrite textNode value
if (elem1.firstChild.nodeType === 3) {
elem1.firstChild.nodeValue = part2;
// console.warn('textNode overwritten', elem1, elem1.firstChild)
//not textNode, so, insertBefore
} else {
elem1.insertBefore(document.createTextNode(part2), elem1.firstChild);
// console.warn('textNode inserted', elem1, elem1.firstChild)
}
//no childNodes, so append one
} else {
elem1.appendChild(document.createTextNode(part2));
}
}
} else {
console.error("different Id - not supported for now!", [tagId1, tagId2]);
return;
}
} //infinite loop
}
// diff3: no place holder nodes
function diff3(prevKey, html2) {
var html1 = doTA.H[prevKey];
var part1, part2, elem1;
var prevPos1, lastPos1, pos1 = html1.indexOf('<');
var prevPos2, lastPos2, pos2 = html2.indexOf('<');
var tagId1, tagId2, prevTagId1, prevTagId2;
var tagNo1, tagNo2, subNo1, subNo2, dotPos1, dotPos2;
var tagStartPos1, tagStartPos2;
var LVL; // jshint ignore:line
// console.log(html1);
// console.log(html2);
// var logger = [];
for (;;) {
// console.log('before', [dirty1, dirty2], [tagId1, tagId2], [html1.substr(pos1, 20), html2.substr(pos2, 20)]);
if (pos1 >= 0) {
lastPos1 = pos1;
pos1 = html1.indexOf(' id="', pos1);
if (pos1 > 0) {
prevPos1 = pos1 + 5;
pos1 = html1.indexOf('"', prevPos1);
prevTagId1 = tagId1;
tagId1 = html1.substring(prevPos1, pos1);
}
}
// console.log('middle', [tagId1, tagId2], [html1.substr(pos1, 20), html2.substr(pos2, 20)]);
if (pos2 >= 0) {
lastPos2 = pos2;
pos2 = html2.indexOf(' id="', pos2);
if (pos2 > 0) {
prevPos2 = pos2 + 5;
pos2 = html2.indexOf('"', prevPos2);
prevTagId2 = tagId2;
tagId2 = html2.substring(prevPos2, pos2);
}
}
// console.log('after', [dirty1, dirty2], [tagId1, tagId2],
// [pos1, pos2], [html1.substr(pos1, 20), html2.substr(pos2, 20)]);
//exist infinite loop
if (pos1 < 0 && pos2 < 0) break;
//same node
if (tagId1 === tagId2) {
tagStartPos1 = ++pos1;
pos1 = html1.indexOf('>', pos1);
part1 = html1.substring(tagStartPos1, pos1);
tagStartPos2 = ++pos2;
pos2 = html2.indexOf('>', pos2);
part2 = html2.substring(tagStartPos2, pos2);
// console.log('same node', [part1, part2]);
//attr really different
if (part1 !== part2) {
elem1 = document.getElementById(tagId1);
if (elem1) {
//nodes to be inserted or deleted
if ((part1.substr(1, 6) === 'hidden') !== (part2.substr(1, 6) === 'hidden')) {
tagStartPos2 = html2.lastIndexOf('<', pos2 - 6);
pos2 = getOuterHTMLEnd(html2, tagStartPos2);
newNode.innerHTML = html2.substring(tagStartPos2, pos2);
// tagStartPos1 = html1.lastIndexOf('<', pos1 - 6);
// console.warn('replaceChild', [tagId2, tagId1], [
// html2.substring(tagStartPos2, getOuterHTMLEnd(html2, tagStartPos2)),
// html1.substring(tagStartPos1, getOuterHTMLEnd(html1, pos1))]);
elem1.parentNode.replaceChild(newNode.firstChild, elem1);
pos1 = getOuterHTMLEnd(html1, pos1);
//only attribute changes
} else {
parsePatchAttr(part1, part2, elem1);
// console.warn('patch node', [tagId1, tagId2], [pos1, pos2], [tagStartPos1, tagStartPos2], [part1, part2])
}
} else {
console.error('elem not found', [tagId1, tagId2], [part1, part2]);
return;
}
} else {
//clear node for textNode
elem1 = void 0;
}
//if blank text node, skip early
if (html1[pos1 + 1] === '<' && html2[pos2 + 1] === '<') {
pos1++;
pos2++;
continue;
}
prevPos1 = pos1;
pos1 = html1.indexOf('<', prevPos1);
part1 = html1.substring(prevPos1 + 1, pos1);
prevPos2 = pos2;
pos2 = html2.indexOf('<', prevPos2);
part2 = html2.substring(prevPos2 + 1, pos2);
//for text node really diff
if (part1 !== part2) {
// console.log('text diff', [tagId1, tagId2], [part1, part2]);
if (!elem1) {
elem1 = document.getElementById(tagId1);
if (!elem1) {
console.error('node not found', [tagId1, tagId2], [part1, part2], [html1.substr(pos1, 15), html2.substr(pos2, 15)], [html1, html2]);
return;
}
}
// console.log('part1,2', [part1, part2]);
if (elem1.firstChild) {
//overwrite textNode value
if (elem1.firstChild.nodeType === 3) {
elem1.firstChild.nodeValue = part2;
// console.warn('textNode overwritten', elem1, elem1.firstChild)
//not textNode, so, insertBefore
} else {
elem1.insertBefore(document.createTextNode(part2), elem1.firstChild);
// console.warn('textNode inserted', elem1, elem1.firstChild)
}
//no childNodes, so append one
} else {
elem1.appendChild(document.createTextNode(part2));
}
}
//when id is different
} else {
dotPos1 = tagId1.indexOf('.');
tagNo1 = tagId1.slice(0, dotPos1) ^ 0;
dotPos2 = tagId1.indexOf('.', ++dotPos1);
subNo1 = dotPos2 > 0 && tagId1.slice(dotPos1, dotPos2) ^ 0;
dotPos2 = tagId2.indexOf('.');
tagNo2 = tagId2.slice(0, dotPos2) ^ 0;
dotPos1 = tagId2.indexOf('.', ++dotPos2);
subNo2 = dotPos1 > 0 && tagId2.slice(dotPos2, dotPos1) ^ 0;
if (prevTagId1 === prevTagId2 && pos1 > 0 && pos2 > 0) {
//["6.1", "5.7.1"] ["5.6.1", "5.6.1"]
//horizontal scroll left to right, appending nodes
if (tagNo1 > tagNo2) {
//removeChild bottom down 111.1 ["111.1", "16.1"] ["15.6.1", "15.6.1"] [10070, 2106]
if (subNo2) {
//["146.1", "145.12681.1"] ["145.1", "145.1"] ["<div id="145.12681.1" class="cell" style="width:125px" col="2536">0.7734284962061793</div>"]
//["116.1", "115.12.1"] ["115.11.1", "115.11.1"] ["<div id="115.12.1" class="cell" style="width:125px;"></div>"]
elem1 = document.getElementById(tagNo2 + '.' + prevKey);
if (!elem1) {
//resize bottom down - bigger
//[111, 15] [false, false] ["111.1", "15.1"] ["14.6.1", "14.6.1"] [1896, 1871]
console.error("no existing node 5", [tagId1, tagId2], [prevTagId1, prevTagId2], [subNo1, subNo2], [pos1, pos2]);
} else {
tagStartPos2 = html2.lastIndexOf('<', pos2 - 6);
pos2 = getOuterHTMLEnd(html2, tagStartPos2);
newNode.innerHTML = html2.substring(tagStartPos2, pos2);
elem1.appendChild(newNode.firstChild);
// console.info("prevTagId2.appendChild", [tagId1, tagId2], [prevTagId1, prevTagId2], [html2.substring(tagStartPos2, pos2)]);
pos1 = lastPos1;
tagId1 = prevTagId1;
tagId2 = prevTagId2;
continue;
}
//not impl ["111.1", "15.1"] ["14.6.1", "14.6.1"] [1896, 1871]
// ["36.1", "111.1"] ["35.6.1", "35.6.1"] [6872, 6897]
} else if (!subNo1) { //&& !subNo2
tagStartPos2 = html2.lastIndexOf('<', pos2 - 6);
pos2 = getOuterHTMLEnd(html2, tagStartPos2);
newNode.innerHTML = html2.substring(tagStartPos2, pos2);
elem1 = document.getElementById(prevTagId2.slice(0, prevTagId2.indexOf('.') + 1) + prevKey);
elem1.parentNode.appendChild(newNode.firstChild);
// console.info("prevTagId2.parentNode.appendChild", [tagId1, tagId2], [prevTagId1, prevTagId2], [subNo1, subNo2], [html2.substring(tagStartPos2, pos2)]);
pos1 = lastPos1;
tagId1 = prevTagId1;
tagId2 = prevTagId2;
continue;
} else {
console.warn("not impl", [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]);
}
// [5, 6] [24, false] ["5.24.1", "6.1"] ["5.23.1", "5.23.1"] [673, 770]
} else if (tagNo1 < tagNo2) {
// [5, 6] [24, false] ["5.24.1", "6.1"] ["5.23.1", "5.23.1"] [673, 770]
// ["35.1", "111.1"] ["34.6.1", "34.6.1"] [6961, 6986]
if (!subNo2) {
elem1 = document.getElementById(tagId1);
if (!elem1) {
console.warn("no existing node 4", [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]);
} else {
elem1.parentNode.removeChild(elem1);
if (pos1 > 0) {
pos1 = getOuterHTMLEnd(html1, pos1);
}
// console.warn("removeChild bottom down", tagId1, [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]);
// logger.push(["removeChild[first]", tagId1, [tagId1, tagId2], [prevTagId1, prevTagId2], [html2.substring(tagStartPos2, pos2)]]);
pos2 = lastPos2;
tagId1 = prevTagId1;
tagId2 = prevTagId2;
continue;
}
} else {
console.warn("not impl", [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]);
}
//same level
} else if (tagNo1 === tagNo2) {
// [5, 5] [1, 2] ["5.1.1", "5.2.1"] ["5.1", "5.1"] [229, 227]
if ( subNo1 < subNo2 ) {
elem1 = document.getElementById(tagId1);
if (!elem1) {
console.warn(["no existing node 4", [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]]);
} else {
elem1.parentNode.removeChild(elem1);
if (pos1 > 0) {
pos1 = getOuterHTMLEnd(html1, pos1);
}
// console.warn("removeChild bottom down", tagId1, [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]);
// logger.push(["removeChild[backward.last]", tagId1, [tagId1, tagId2], [prevTagId1, prevTagId2], [html2.substring(tagStartPos2, pos2)]]);
pos2 = lastPos2;
tagId1 = prevTagId1;
tagId2 = prevTagId2;
continue;
}
//[5, 5] [19, 18] ["5.19.1", "5.18.1"] ["5.1", "5.1"] [229, 231]
} else if ( subNo1 > subNo2 ) {
elem1 = document.getElementById(tagId1);
tagStartPos2 = html2.lastIndexOf('<', pos2 - 6);
pos2 = getOuterHTMLEnd(html2, tagStartPos2);
newNode.innerHTML = html2.substring(tagStartPos2, pos2);
elem1.parentNode.insertBefore(newNode.firstChild, elem1);
// logger.push(["this.insertBefore[elem1]", [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2], [html2.substring(tagStartPos2, pos2)]]);
pos1 = lastPos1;
tagId1 = prevTagId1;
tagId2 = prevTagId2; //check later
continue;
} else {
console.warn('not impl', [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]);
}
} //same parent
else {
console.warn('not impl', [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]);
}
} //prevTagId1 === prevTagId2
//end of previous html
else if (pos1 < 0) {
// ######## ["28.9.1", "28.10.1"] ["28.8.1", "28.9.1"] [-1, 16911]
if ( tagNo1 === tagNo2) {
// ["28.9.1", "28.10.1"] ["28.8.1", "28.9.1"] [-1, 16911]
if (subNo1 < subNo2 ) {
elem1 = document.getElementById(tagNo2 + '.' + prevKey);
tagStartPos2 = html2.lastIndexOf('<', pos2 - 6);
pos2 = getOuterHTMLEnd(html2, tagStartPos2);
newNode.innerHTML = html2.substring(tagStartPos2, pos2);
elem1.appendChild(newNode.firstChild);
// console.info("this.appendChild", [tagId1, tagId2], [prevTagId1, prevTagId2], [html2.substring(tagStartPos2, pos2)]);
tagId1 = prevTagId1;
tagId2 = prevTagId2;
continue;
} else {
console.warn('not impl', [tagNo1, tagNo2], [subNo1, subNo2], [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2], [html2.substring(tagStartPos2, pos2)]);
}
} else if (tagNo1 < tagNo2) {
//prevTagId2 is a children
// [126, 127] [66, false] ["126.66.1", "127.1"] ["126.61.1", "126.66.1"] [-1, 13065]
if (prevTagId2.indexOf('.') !== prevTagId2.lastIndexOf('.')) {
//not impl1 pos1 < 0 [14, 15] [6, false] ["14.6.1", "15.1"] ["14.6.1", "14.6.1"] [-1, 1950]
elem1 = document.getElementById(prevTagId2.slice(0, prevTagId2.indexOf('.') + 1) + prevKey);
tagStartPos2 = html2.lastIndexOf('<', pos2 - 6);
pos2 = getOuterHTMLEnd(html2, tagStartPos2);
newNode.innerHTML = html2.substring(tagStartPos2, pos2);
// console.info("prevTagId2.parent.appendChild", [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2], [html2.substring(tagStartPos2, pos2)]);
elem1.parentNode.appendChild(newNode.firstChild);
tagId1 = prevTagId1;
tagId2 = prevTagId2;
continue;
//this is children but prevTagId2 is parent
//not impl [143, 144] [false, 499946] ["143.1", "144.499946.1"] ["143.1", "144.1"] [-1, 37542]
} else if (subNo2) {
elem1 = document.getElementById(prevTagId2);
tagStartPos2 = html2.lastIndexOf('<', pos2 - 6);
pos2 = getOuterHTMLEnd(html2, tagStartPos2);
newNode.innerHTML = html2.substring(tagStartPos2, pos2);
// console.info("prevTagId2.appendChild", [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2], [html2.substring(tagStartPos2, pos2)]);
elem1.appendChild(newNode.firstChild);
tagId1 = prevTagId1;
tagId2 = prevTagId2;
continue;
//this and prevTagId2 is not children
//not impl [125, 126] [false, false] ["125.1", "126.1"] ["125.1", "125.1"] [-1, 3444] [" class="row odd blue" row="5"></div><div id="126.1"]
} else {
//////////// EXCEPTIONAL CASE : this is ambiguous, can't figure out new element a child or sibling of previousNode with current design :(
//120 ["119.1", "120.1"] ["118.1", "119.1"] [119, 120] [-1, 3695]
elem1 = document.getElementById(prevTagId2);
tagStartPos2 = html2.lastIndexOf('<', pos2 - 6);
pos2 = getOuterHTMLEnd(html2, tagStartPos2);
newNode.innerHTML = html2.substring(tagStartPos2, pos2);
// console.info("prevTagId2.parent.appendChild", [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2], [html2.substring(tagStartPos2, pos2)]);
elem1.parentNode.appendChild(newNode.firstChild);
tagId1 = prevTagId1;
tagId2 = prevTagId2;
continue;
// console.warn('not impl', [tagNo1, tagNo2], [subNo1, subNo2], [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2], [html2.substring(tagStartPos2, pos2)]);
}
} else {
console.warn('not impl', [tagNo1, tagNo2], [subNo1, subNo2], [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2], [html2.substring(tagStartPos2, pos2)]);
}
}
//end of new html
else if (pos2 < 0) {
//same base id
if (tagNo1 === tagNo2) {
// [28, 28] [undefined, 13] ["28.1", "28.13.1"] ["28.1", "28.1"] [-1, 12127]
if (subNo1 < subNo2) {
elem1 = document.getElementById(prevTagId2);
tagStartPos2 = html2.lastIndexOf('<', pos2 - 6);
pos2 = getOuterHTMLEnd(html2, tagStartPos2);
newNode.innerHTML = html2.substring(tagStartPos2, pos2);
elem1.appendChild(newNode.firstChild);
// logger.push(["appendChild[parent.last]2<0", [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2], [html2.substring(tagStartPos2, pos2)]]);
tagId1 = prevTagId1;
tagId2 = prevTagId2;
continue;
//[28, 28] [27, 26] ["28.27.1", "28.26.1"] ["28.26.1", "28.25.1"] [12548, -1]
} else if (subNo1 > subNo2) {
elem1 = document.getElementById(tagId1);
if (!elem1) {
console.warn(["no existing node 4", [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]]);
} else {
elem1.parentNode.removeChild(elem1);
if (pos1 > 0) {
pos1 = getOuterHTMLEnd(html1, pos1);
}
// console.warn("removeChild bottom down", tagId1, [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]);
tagId1 = prevTagId1;
tagId2 = prevTagId2;
continue;
}
} else {
console.warn('not impl3 pos2 < 0', [tagNo1, tagNo2], [subNo1, subNo2], [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2], [html2.substring(tagStartPos2, pos2)]);
}
//not impl ["146.1", "145.66.1"] ["145.66.1", "145.61.1"] [39591, -1]
//not impl ["145.1", "144.66.1"] ["144.66.1", "144.61.1"] [38200, -1]
//not impl ["144.1", "143.66.1"] ["143.66.1", "143.61.1"] [36747, -1]
//resize bottom up - make smaller
} else if (tagNo1 > tagNo2) {
elem1 = document.getElementById(tagId1);
elem1.parentNode.removeChild(elem1);
if (pos1 > 0) {
pos1 = getOuterHTMLEnd(html1, pos1);
}
// console.warn("removeChild bottom down", tagId1, [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]);
// logger.push(["removeChild[backward.last]", tagId1, [tagId1, tagId2], [prevTagId1, prevTagId2], [html2.substring(tagStartPos2, pos2)]]);
tagId1 = prevTagId1;
tagId2 = prevTagId2;
continue;
} else {
console.warn('not impl', [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]);
}
}
////////////// THIS SHOULD NEVER REACH ///////////////
// console.log('====')
// logger.slice(-10).forEach(function(item){
// console.info.apply(console, item)
// })
console.error("different Id - not supported for now!", [tagNo1, tagNo2], [subNo1, subNo2], [tagId1, tagId2], [prevTagId1, prevTagId2], [pos1, pos2]);
console.log([html1.substr(pos1 - 100, 100), html1.substr(pos1, 20)]);
console.log([html2.substr(pos2 - 100, 100), html2.substr(pos2, 20)]);
// console.log(html1);
// console.log(html2)
return;
}
} //infinite loop
}
var camelCaseRE = /-(.)/g;
function camelCase(str) {
return str.replace(camelCaseRE, function($0, $1){ return $1.toUpperCase(); });
}
// parse attributes from html open tag and patch DOM when different
function parsePatchAttr(chunkA, chunkB, elem) {
var tagId;
var posA1, posA2, posB1, posB2;
var posDiff = 0;
var attrName, attrVal1, attrVal2;
// var len1 = chunk1.length;
// console.log('chunks', [chunkA, chunkB]);
//extract id to tag, if no elem specified
if (!elem) {
posA1 = chunkA.indexOf(' id="', posA1);
if (posA1 >= 0) {
posA1 += 5;
posA2 = chunkA.indexOf('"', posA1);
tagId = chunkA.slice(posA1, posA2);
elem = document.getElementById(tagId);
if (!elem) {
throw console.error('tag not found', [posA1, posA2, tagId, elem, chunkA, chunkB]);
}
posA2 += 2;
} else {
throw console.error('id not found', [posA1, posA1, chunkA, chunkB]);
}
} else {
//first char is always space
posA2 = 1;
}
for(;;) {
//attr name
posA1 = chunkA.indexOf('="', posA2);
if (posA1 < 0) break;
attrName = chunkA.slice(posA2, posA1);
//attr values
posA2 = chunkA.indexOf('"', posA1 + 2);
attrVal1 = chunkA.slice(posA1 + 2, posA2);
posB1 = posA1 + posDiff;
posB2 = chunkB.indexOf('"', posB1 + 2);
attrVal2 = chunkB.slice(posB1 + 2, posB2);
if (attrVal1 !== attrVal2) {
// value as property
if (attrName === 'value') {
elem[attrName] = attrVal2;
// prefix with - as property, like -scrollLeft
} else if (attrName[0] === '-') {
//only if old value is same is element property to prevent loop (firefox)
if (elem[attrName.substr(1)] == attrVal1) {
elem[attrName.substr(1)] = attrVal2;
}
//console.log('prop-', attrName.substr(1), attrVal1, attrVal2, elem[attrName.substr(1)]);
} else {
elem.setAttribute(attrName, attrVal2);
}
posDiff = posB2 - posA2;
}
posA2 += 2;
}
return tagId;
}
// extract value of id from part of html open tag
// only id="xxx" supported, this is internal use, so it's always double-quotes