This repository has been archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
diff.gs
1143 lines (955 loc) · 33.1 KB
/
diff.gs
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
// <source lang="JavaScript">
/*
Name: wDiff.js
Version: 0.9.9 (October 10, 2010)
Info: http://en.wikipedia.org/wiki/User:Cacycle/diff
Code: http://en.wikipedia.org/wiki/User:Cacycle/diff.js
JavaScript diff algorithm by [[en:User:Cacycle]] (http://en.wikipedia.org/wiki/User_talk:Cacycle).
Outputs html/css-formatted new text with highlighted deletions, inserts, and block moves.
For newline highlighting the following style rules have to be added to the document:
.wDiffParagraph:before { content: "¶"; };
The program uses cross-browser code and should work with all modern browsers. It has been tested with:
* Mozilla Firefox 1.5.0.1
* Mozilla SeaMonkey 1.0
* Opera 8.53
* Internet Explorer 6.0.2900.2180
* Internet Explorer 7.0.5730.11
This program is also compatible with Greasemonkey
An implementation of the word-based algorithm from:
Communications of the ACM 21(4):264 (1978)
http://doi.acm.org/10.1145/359460.359467
With the following additional feature:
* Word types have been optimized for MediaWiki source texts
* Additional post-pass 5 code for resolving islands caused by adding
two common words at the end of sequences of common words
* Additional detection of block borders and color coding of moved blocks and their original position
* Optional "intelligent" omission of unchanged parts from the output
This code is used by the MediaWiki in-browser text editors [[en:User:Cacycle/editor]] and [[en:User:Cacycle/wikEd]]
and the enhanced diff view tool wikEdDiff [[en:User:Cacycle/wikEd]].
Usage: var htmlText = WDiffString(oldText, newText);
This code has been released into the public domain.
Datastructures (abbreviations from publication):
text: an object that holds all text related datastructures
.newWords: consecutive words of the new text (N)
.oldWords: consecutive words of the old text (O)
.newToOld: array pointing to corresponding word number in old text (NA)
.oldToNew: array pointing to corresponding word number in new text (OA)
.message: output message for testing purposes
symbol table:
symbols[word]: associative array (object) of detected words for passes 1 - 3, points to symbol[i]
symbol[i]: array of objects that hold word counters and pointers:
.newCtr: new word occurences counter (NC)
.oldCtr: old word occurences counter (OC)
.toNew: first word occurrence in new text, points to text.newWords[i]
.toOld: last word occurrence in old text, points to text.oldWords[i]
block: an object that holds block move information
blocks indexed after new text:
.newStart: new text word number of start of this block
.newLength: element number of this block including non-words
.newWords: true word number of this block
.newNumber: corresponding block index in old text
.newBlock: moved-block-number of a block that has been moved here
.newLeft: moved-block-number of a block that has been moved from this border leftwards
.newRight: moved-block-number of a block that has been moved from this border rightwards
.newLeftIndex: index number of a block that has been moved from this border leftwards
.newRightIndex: index number of a block that has been moved from this border rightwards
blocks indexed after old text:
.oldStart: word number of start of this block
.oldToNew: corresponding new text word number of start
.oldLength: element number of this block including non-words
.oldWords: true word number of this block
*/
// css for change indicators
if (typeof(wDiffStyleDelete) == 'undefined') { wDiffStyleDelete = 'font-weight: normal; text-decoration: none; color: #fff; background-color: #990033;'; }
if (typeof(wDiffStyleInsert) == 'undefined') { wDiffStyleInsert = 'font-weight: normal; text-decoration: none; color: #fff; background-color: #009933;'; }
if (typeof(wDiffStyleMoved) == 'undefined') { wDiffStyleMoved = 'font-weight: bold; color: #000; vertical-align: text-bottom; font-size: xx-small; padding: 0; border: solid 1px;'; }
if (typeof(wDiffStyleBlock) == 'undefined') { wDiffStyleBlock = [
'color: #000; background-color: #ffff80;',
'color: #000; background-color: #c0ffff;',
'color: #000; background-color: #ffd0f0;',
'color: #000; background-color: #ffe080;',
'color: #000; background-color: #aaddff;',
'color: #000; background-color: #ddaaff;',
'color: #000; background-color: #ffbbbb;',
'color: #000; background-color: #d8ffa0;',
'color: #000; background-color: #d0d0d0;'
]; }
// html for change indicators, {number} is replaced by the block number
// {block} is replaced by the block style, class and html comments are important for shortening the output
if (typeof(wDiffHtmlMovedRight) == 'undefined') { wDiffHtmlMovedRight = '<input class="wDiffHtmlMovedRight" type="button" value=">" style="' + wDiffStyleMoved + ' {block}"><!--wDiffHtmlMovedRight-->'; }
if (typeof(wDiffHtmlMovedLeft) == 'undefined') { wDiffHtmlMovedLeft = '<input class="wDiffHtmlMovedLeft" type="button" value="<" style="' + wDiffStyleMoved + ' {block}"><!--wDiffHtmlMovedLeft-->'; }
if (typeof(wDiffHtmlBlockStart) == 'undefined') { wDiffHtmlBlockStart = '<span class="wDiffHtmlBlock" style="{block}">'; }
if (typeof(wDiffHtmlBlockEnd) == 'undefined') { wDiffHtmlBlockEnd = '</span><!--wDiffHtmlBlock-->'; }
if (typeof(wDiffHtmlDeleteStart) == 'undefined') { wDiffHtmlDeleteStart = '<span class="wDiffHtmlDelete" style="' + wDiffStyleDelete + '">'; }
if (typeof(wDiffHtmlDeleteEnd) == 'undefined') { wDiffHtmlDeleteEnd = '</span><!--wDiffHtmlDelete-->'; }
if (typeof(wDiffHtmlInsertStart) == 'undefined') { wDiffHtmlInsertStart = '<span class="wDiffHtmlInsert" style="' + wDiffStyleInsert + '">'; }
if (typeof(wDiffHtmlInsertEnd) == 'undefined') { wDiffHtmlInsertEnd = '</span><!--wDiffHtmlInsert-->'; }
// minimal number of real words for a moved block (0 for always displaying block move indicators)
if (typeof(wDiffBlockMinLength) == 'undefined') { wDiffBlockMinLength = 3; }
// exclude identical sequence starts and endings from change marking
if (typeof(wDiffWordDiff) == 'undefined') { wDiffWordDiff = true; }
// enable recursive diff to resolve problematic sequences
if (typeof(wDiffRecursiveDiff) == 'undefined') { wDiffRecursiveDiff = true; }
// enable block move display
if (typeof(wDiffShowBlockMoves) == 'undefined') { wDiffShowBlockMoves = true; }
// remove unchanged parts from final output
// characters before diff tag to search for previous heading, paragraph, line break, cut characters
if (typeof(wDiffHeadingBefore) == 'undefined') { wDiffHeadingBefore = 1500; }
if (typeof(wDiffParagraphBefore) == 'undefined') { wDiffParagraphBefore = 1500; }
if (typeof(wDiffLineBeforeMax) == 'undefined') { wDiffLineBeforeMax = 1000; }
if (typeof(wDiffLineBeforeMin) == 'undefined') { wDiffLineBeforeMin = 500; }
if (typeof(wDiffBlankBeforeMax) == 'undefined') { wDiffBlankBeforeMax = 1000; }
if (typeof(wDiffBlankBeforeMin) == 'undefined') { wDiffBlankBeforeMin = 500; }
if (typeof(wDiffCharsBefore) == 'undefined') { wDiffCharsBefore = 500; }
// characters after diff tag to search for next heading, paragraph, line break, or characters
if (typeof(wDiffHeadingAfter) == 'undefined') { wDiffHeadingAfter = 1500; }
if (typeof(wDiffParagraphAfter) == 'undefined') { wDiffParagraphAfter = 1500; }
if (typeof(wDiffLineAfterMax) == 'undefined') { wDiffLineAfterMax = 1000; }
if (typeof(wDiffLineAfterMin) == 'undefined') { wDiffLineAfterMin = 500; }
if (typeof(wDiffBlankAfterMax) == 'undefined') { wDiffBlankAfterMax = 1000; }
if (typeof(wDiffBlankAfterMin) == 'undefined') { wDiffBlankAfterMin = 500; }
if (typeof(wDiffCharsAfter) == 'undefined') { wDiffCharsAfter = 500; }
// maximal fragment distance to join close fragments
if (typeof(wDiffFragmentJoin) == 'undefined') { wDiffFragmentJoin = 1000; }
if (typeof(wDiffOmittedChars) == 'undefined') { wDiffOmittedChars = '…'; }
if (typeof(wDiffOmittedLines) == 'undefined') { wDiffOmittedLines = '<hr style="height: 2px; margin: 1em 10%;">'; }
if (typeof(wDiffNoChange) == 'undefined') { wDiffNoChange = '<hr style="height: 2px; margin: 1em 20%;">'; }
// compatibility fix for old name of main function
//StringDiff = WDiffString;
// WDiffString: main program
// input: oldText, newText, strings containing the texts
// returns: html diff
WDiffString = function(oldText, newText) {
// IE / Mac fix
oldText = oldText.replace(/\r\n?/g, '\n');
newText = newText.replace(/\r\n?/g, '\n');
var text = {};
text.newWords = [];
text.oldWords = [];
text.newToOld = [];
text.oldToNew = [];
text.message = '';
var block = {};
var outText = '';
// trap trivial changes: no change
if (oldText == newText) {
outText = newText;
//outText = WDiffEscape(outText);
//outText = WDiffHtmlFormat(outText);
return(outText);
}
// trap trivial changes: old text deleted
if ( (oldText == null) || (oldText.length == 0) ) {
outText = newText;
outText = WDiffEscape(outText);
outText = WDiffHtmlFormat(outText);
outText = wDiffHtmlInsertStart + outText + wDiffHtmlInsertEnd;
return(outText);
}
// trap trivial changes: new text deleted
if ( (newText == null) || (newText.length == 0) ) {
outText = oldText;
outText = WDiffEscape(outText);
outText = WDiffHtmlFormat(outText);
outText = wDiffHtmlDeleteStart + outText + wDiffHtmlDeleteEnd;
return(outText);
}
// split new and old text into words
WDiffSplitText(oldText, newText, text);
// calculate diff information
WDiffText(text);
//detect block borders and moved blocks
WDiffDetectBlocks(text, block);
// process diff data into formatted html text
outText = WDiffToHtml(text, block);
// IE fix
outText = outText.replace(/> ( *)</g, '> $1<');
return(outText);
};
// WDiffSplitText: split new and old text into words
// input: oldText, newText, strings containing the texts
// changes: text.newWords and text.oldWords, arrays containing the texts in arrays of words
WDiffSplitText = function(oldText, newText, text) {
// convert strange spaces
oldText = oldText.replace(/[\t\u000b\u00a0\u2028\u2029]+/g, ' ');
newText = newText.replace(/[\t\u000b\u00a0\u2028\u2029]+/g, ' ');
// split old text into words
// / | | | | | | | | | | | | | | /
var pattern = /[\w]+|\[\[|\]\]|\{\{|\}\}|\n+| +|&\w+;|'''|''|=+|\{\||\|\}|\|\-|./g;
var result;
do {
result = pattern.exec(oldText);
if (result != null) {
text.oldWords.push(result[0]);
}
} while (result != null);
// split new text into words
do {
result = pattern.exec(newText);
if (result != null) {
text.newWords.push(result[0]);
}
} while (result != null);
return;
};
// WDiffText: calculate diff information
// input: text.newWords and text.oldWords, arrays containing the texts as arrays of words
// optionally for recursive calls: newStart, newEnd, oldStart, oldEnd, recursionLevel
// changes: text.newToOld and text.oldToNew, arrays pointing to corresponding words
WDiffText = function(text, newStart, newEnd, oldStart, oldEnd, recursionLevel) {
var symbol = [];
var symbols = {};
// set defaults
if (typeof(newStart) == 'undefined') { newStart = 0; }
if (typeof(newEnd) == 'undefined') { newEnd = text.newWords.length; }
if (typeof(oldStart) == 'undefined') { oldStart = 0; }
if (typeof(oldEnd) == 'undefined') { oldEnd = text.oldWords.length; }
if (typeof(recursionLevel) == 'undefined') { recursionLevel = 0; }
// limit recursion depth
if (recursionLevel > 10) {
return;
}
//
// pass 1: Parse new text into symbol table
//
for (var i = newStart; i < newEnd; i ++) {
var word = text.newWords[i];
// preserve the native method
if (word.indexOf('hasOwnProperty') == 0) {
word = word.replace(/^(hasOwnProperty_*)$/, '$1_');
}
// add new entry to symbol table
if (symbols.hasOwnProperty(word) == false) {
var last = symbol.length;
symbols[word] = last;
symbol[last] = { newCtr: 1, oldCtr: 0, toNew: i, toOld: null };
}
// or update existing entry
else {
// increment word counter for new text
var hashToArray = symbols[word];
symbol[hashToArray].newCtr ++;
}
}
//
// pass 2: parse old text into symbol table
//
for (var i = oldStart; i < oldEnd; i ++) {
var word = text.oldWords[i];
// preserve the native method
if (word.indexOf('hasOwnProperty') == 0) {
word = word.replace(/^(hasOwnProperty_*)$/, '$1_');
}
// add new entry to symbol table
if (symbols.hasOwnProperty(word) == false) {
var last = symbol.length;
symbols[word] = last;
symbol[last] = { newCtr: 0, oldCtr: 1, toNew: null, toOld: i };
}
// or update existing entry
else {
// increment word counter for old text
var hashToArray = symbols[word];
symbol[hashToArray].oldCtr ++;
// add word number for old text
symbol[hashToArray].toOld = i;
}
}
//
// pass 3: connect unique words
//
for (var i = 0; i < symbol.length; i ++) {
// find words in the symbol table that occur only once in both versions
if ( (symbol[i].newCtr == 1) && (symbol[i].oldCtr == 1) ) {
var toNew = symbol[i].toNew;
var toOld = symbol[i].toOld;
// do not use spaces as unique markers
if (/^\s+$/.test(text.newWords[toNew]) == false) {
// connect from new to old and from old to new
text.newToOld[toNew] = toOld;
text.oldToNew[toOld] = toNew;
}
}
}
//
// pass 4: connect adjacent identical words downwards
//
for (var i = newStart; i < newEnd - 1; i ++) {
// find already connected pairs
if (text.newToOld[i] != null) {
var j = text.newToOld[i];
// check if the following words are not yet connected
if ( (text.newToOld[i + 1] == null) && (text.oldToNew[j + 1] == null) ) {
// connect if the following words are the same
if (text.newWords[i + 1] == text.oldWords[j + 1]) {
text.newToOld[i + 1] = j + 1;
text.oldToNew[j + 1] = i + 1;
}
}
}
}
//
// pass 5: connect adjacent identical words upwards
//
for (var i = newEnd - 1; i > newStart; i --) {
// find already connected pairs
if (text.newToOld[i] != null) {
var j = text.newToOld[i];
// check if the preceeding words are not yet connected
if ( (text.newToOld[i - 1] == null) && (text.oldToNew[j - 1] == null) ) {
// connect if the preceeding words are the same
if ( text.newWords[i - 1] == text.oldWords[j - 1] ) {
text.newToOld[i - 1] = j - 1;
text.oldToNew[j - 1] = i - 1;
}
}
}
}
//
// "pass" 6: recursively diff still unresolved regions downwards
//
if (wDiffRecursiveDiff == true) {
var i = newStart;
var j = oldStart;
while (i < newEnd) {
if (text.newToOld[i - 1] != null) {
j = text.newToOld[i - 1] + 1;
}
// check for the start of an unresolved sequence
if ( (text.newToOld[i] == null) && (text.oldToNew[j] == null) ) {
// determine the ends of the sequences
var iStart = i;
var iEnd = i;
while ( (text.newToOld[iEnd] == null) && (iEnd < newEnd) ) {
iEnd ++;
}
var iLength = iEnd - iStart;
var jStart = j;
var jEnd = j;
while ( (text.oldToNew[jEnd] == null) && (jEnd < oldEnd) ) {
jEnd ++;
}
var jLength = jEnd - jStart;
// recursively diff the unresolved sequence
if ( (iLength > 0) && (jLength > 0) ) {
if ( (iLength > 1) || (jLength > 1) ) {
if ( (iStart != newStart) || (iEnd != newEnd) || (jStart != oldStart) || (jEnd != oldEnd) ) {
WDiffText(text, iStart, iEnd, jStart, jEnd, recursionLevel + 1);
}
}
}
i = iEnd;
}
else {
i ++;
}
}
}
//
// "pass" 7: recursively diff still unresolved regions upwards
//
if (wDiffRecursiveDiff == true) {
var i = newEnd - 1;
var j = oldEnd - 1;
while (i >= newStart) {
if (text.newToOld[i + 1] != null) {
j = text.newToOld[i + 1] - 1;
}
// check for the start of an unresolved sequence
if ( (text.newToOld[i] == null) && (text.oldToNew[j] == null) ) {
// determine the ends of the sequences
var iStart = i;
var iEnd = i + 1;
while ( (text.newToOld[iStart - 1] == null) && (iStart >= newStart) ) {
iStart --;
}
if (iStart < 0) {
iStart = 0;
}
var iLength = iEnd - iStart;
var jStart = j;
var jEnd = j + 1;
while ( (text.oldToNew[jStart - 1] == null) && (jStart >= oldStart) ) {
jStart --;
}
if (jStart < 0) {
jStart = 0;
}
var jLength = jEnd - jStart;
// recursively diff the unresolved sequence
if ( (iLength > 0) && (jLength > 0) ) {
if ( (iLength > 1) || (jLength > 1) ) {
if ( (iStart != newStart) || (iEnd != newEnd) || (jStart != oldStart) || (jEnd != oldEnd) ) {
WDiffText(text, iStart, iEnd, jStart, jEnd, recursionLevel + 1);
}
}
}
i = iStart - 1;
}
else {
i --;
}
}
}
return;
};
// WDiffToHtml: process diff data into formatted html text
// input: text.newWords and text.oldWords, arrays containing the texts in arrays of words
// text.newToOld and text.oldToNew, arrays pointing to corresponding words
// block data structure
// returns: outText, a html string
WDiffToHtml = function(text, block) {
var outText = text.message;
var blockNumber = 0;
var i = 0;
var j = 0;
var movedAsInsertion;
// cycle through the new text
do {
var movedIndex = [];
var movedBlock = [];
var movedLeft = [];
var blockText = '';
var identText = '';
var delText = '';
var insText = '';
var identStart = '';
// check if a block ends here and finish previous block
if (movedAsInsertion != null) {
if (movedAsInsertion == false) {
identStart += wDiffHtmlBlockEnd;
}
else {
identStart += wDiffHtmlInsertEnd;
}
movedAsInsertion = null;
}
// detect block boundary
if ( (text.newToOld[i] != j) || (blockNumber == 0 ) ) {
if ( ( (text.newToOld[i] != null) || (i >= text.newWords.length) ) && ( (text.oldToNew[j] != null) || (j >= text.oldWords.length) ) ) {
// block moved right
var moved = block.newRight[blockNumber];
if (moved > 0) {
var index = block.newRightIndex[blockNumber];
movedIndex.push(index);
movedBlock.push(moved);
movedLeft.push(false);
}
// block moved left
moved = block.newLeft[blockNumber];
if (moved > 0) {
var index = block.newLeftIndex[blockNumber];
movedIndex.push(index);
movedBlock.push(moved);
movedLeft.push(true);
}
// check if a block starts here
moved = block.newBlock[blockNumber];
if (moved > 0) {
// mark block as inserted text
if (block.newWords[blockNumber] < wDiffBlockMinLength) {
identStart += wDiffHtmlInsertStart;
movedAsInsertion = true;
}
// mark block by color
else {
if (moved > wDiffStyleBlock.length) {
moved = wDiffStyleBlock.length;
}
identStart += WDiffHtmlCustomize(wDiffHtmlBlockStart, moved - 1);
movedAsInsertion = false;
}
}
if (i >= text.newWords.length) {
i ++;
}
else {
j = text.newToOld[i];
blockNumber ++;
}
}
}
// get the correct order if moved to the left as well as to the right from here
if (movedIndex.length == 2) {
if (movedIndex[0] > movedIndex[1]) {
movedIndex.reverse();
movedBlock.reverse();
movedLeft.reverse();
}
}
// handle left and right block moves from this position
for (var m = 0; m < movedIndex.length; m ++) {
// insert the block as deleted text
if (block.newWords[ movedIndex[m] ] < wDiffBlockMinLength) {
var movedStart = block.newStart[ movedIndex[m] ];
var movedLength = block.newLength[ movedIndex[m] ];
var str = '';
for (var n = movedStart; n < movedStart + movedLength; n ++) {
str += text.newWords[n];
}
str = WDiffEscape(str);
str = str.replace(/\n/g, '<span class="wDiffParagraph"></span><br>');
blockText += wDiffHtmlDeleteStart + str + wDiffHtmlDeleteEnd;
}
// add a placeholder / move direction indicator
else {
if (movedBlock[m] > wDiffStyleBlock.length) {
movedBlock[m] = wDiffStyleBlock.length;
}
if (movedLeft[m]) {
blockText += WDiffHtmlCustomize(wDiffHtmlMovedLeft, movedBlock[m] - 1);
}
else {
blockText += WDiffHtmlCustomize(wDiffHtmlMovedRight, movedBlock[m] - 1);
}
}
}
// collect consecutive identical text
while ( (i < text.newWords.length) && (j < text.oldWords.length) ) {
if ( (text.newToOld[i] == null) || (text.oldToNew[j] == null) ) {
break;
}
if (text.newToOld[i] != j) {
break;
}
identText += text.newWords[i];
i ++;
j ++;
}
// collect consecutive deletions
while ( (text.oldToNew[j] == null) && (j < text.oldWords.length) ) {
delText += text.oldWords[j];
j ++;
}
// collect consecutive inserts
while ( (text.newToOld[i] == null) && (i < text.newWords.length) ) {
insText += text.newWords[i];
i ++;
}
// remove leading and trailing similarities between delText and ins from highlighting
var preText = '';
var postText = '';
if (wDiffWordDiff) {
if ( (delText != '') && (insText != '') ) {
// remove leading similarities
while ( delText.charAt(0) == insText.charAt(0) && (delText != '') && (insText != '') ) {
preText = preText + delText.charAt(0);
delText = delText.substr(1);
insText = insText.substr(1);
}
// remove trailing similarities
while ( delText.charAt(delText.length - 1) == insText.charAt(insText.length - 1) && (delText != '') && (insText != '') ) {
postText = delText.charAt(delText.length - 1) + postText;
delText = delText.substr(0, delText.length - 1);
insText = insText.substr(0, insText.length - 1);
}
}
}
// output the identical text, deletions and inserts
// moved from here indicator
if (blockText != '') {
outText += blockText;
}
// identical text
if (identText != '') {
outText += identStart + WDiffEscape(identText);
}
outText += preText;
// deleted text
if (delText != '') {
delText = wDiffHtmlDeleteStart + WDiffEscape(delText) + wDiffHtmlDeleteEnd;
delText = delText.replace(/\n/g, '<span class="wDiffParagraph"></span><br><br>');
outText += delText;
}
// inserted text
if (insText != '') {
insText = wDiffHtmlInsertStart + WDiffEscape(insText) + wDiffHtmlInsertEnd;
insText = insText.replace(/\n/g, '<span class="wDiffParagraph"></span><br><br>');
outText += insText;
}
outText += postText;
} while (i <= text.newWords.length);
outText += '\n';
outText = WDiffHtmlFormat(outText);
return(outText);
};
// WDiffEscape: replaces html-sensitive characters in output text with character entities
WDiffEscape = function(text) {
text = text.replace(/&/g, '&');
text = text.replace(/</g, '<');
text = text.replace(/>/g, '>');
text = text.replace(/"/g, '"');
return(text);
};
// HtmlCustomize: customize indicator html: replace {number} with the block number, {block} with the block style
WDiffHtmlCustomize = function(text, block) {
text = text.replace(/\{number\}/, block);
text = text.replace(/\{block\}/, wDiffStyleBlock[block]);
return(text);
};
// HtmlFormat: replaces newlines and multiple spaces in text with html code
WDiffHtmlFormat = function(text) {
text = text.replace(/ {2}/g, ' ');
text = text.replace(/\n/g, '<br><br>');
return(text);
};
// WDiffDetectBlocks: detect block borders and moved blocks
// input: text object, block object
WDiffDetectBlocks = function(text, block) {
block.oldStart = [];
block.oldToNew = [];
block.oldLength = [];
block.oldWords = [];
block.newStart = [];
block.newLength = [];
block.newWords = [];
block.newNumber = [];
block.newBlock = [];
block.newLeft = [];
block.newRight = [];
block.newLeftIndex = [];
block.newRightIndex = [];
var blockNumber = 0;
var wordCounter = 0;
var realWordCounter = 0;
// get old text block order
if (wDiffShowBlockMoves) {
var j = 0;
var i = 0;
do {
// detect block boundaries on old text
if ( (text.oldToNew[j] != i) || (blockNumber == 0 ) ) {
if ( ( (text.oldToNew[j] != null) || (j >= text.oldWords.length) ) && ( (text.newToOld[i] != null) || (i >= text.newWords.length) ) ) {
if (blockNumber > 0) {
block.oldLength[blockNumber - 1] = wordCounter;
block.oldWords[blockNumber - 1] = realWordCounter;
wordCounter = 0;
realWordCounter = 0;
}
if (j >= text.oldWords.length) {
j ++;
}
else {
i = text.oldToNew[j];
block.oldStart[blockNumber] = j;
block.oldToNew[blockNumber] = text.oldToNew[j];
blockNumber ++;
}
}
}
// jump over identical pairs
while ( (i < text.newWords.length) && (j < text.oldWords.length) ) {
if ( (text.newToOld[i] == null) || (text.oldToNew[j] == null) ) {
break;
}
if (text.oldToNew[j] != i) {
break;
}
i ++;
j ++;
wordCounter ++;
if ( /\w/.test( text.newWords[i] ) ) {
realWordCounter ++;
}
}
// jump over consecutive deletions
while ( (text.oldToNew[j] == null) && (j < text.oldWords.length) ) {
j ++;
}
// jump over consecutive inserts
while ( (text.newToOld[i] == null) && (i < text.newWords.length) ) {
i ++;
}
} while (j <= text.oldWords.length);
// get the block order in the new text
var lastMin;
var currMinIndex;
lastMin = null;
// sort the data by increasing start numbers into new text block info
for (var i = 0; i < blockNumber; i ++) {
currMin = null;
for (var j = 0; j < blockNumber; j ++) {
curr = block.oldToNew[j];
if ( (curr > lastMin) || (lastMin == null) ) {
if ( (curr < currMin) || (currMin == null) ) {
currMin = curr;
currMinIndex = j;
}
}
}
block.newStart[i] = block.oldToNew[currMinIndex];
block.newLength[i] = block.oldLength[currMinIndex];
block.newWords[i] = block.oldWords[currMinIndex];
block.newNumber[i] = currMinIndex;
lastMin = currMin;
}
// detect not moved blocks
for (var i = 0; i < blockNumber; i ++) {
if (block.newBlock[i] == null) {
if (block.newNumber[i] == i) {
block.newBlock[i] = 0;
}
}
}
// detect switches of neighbouring blocks
for (var i = 0; i < blockNumber - 1; i ++) {
if ( (block.newBlock[i] == null) && (block.newBlock[i + 1] == null) ) {
if (block.newNumber[i] - block.newNumber[i + 1] == 1) {
if ( (block.newNumber[i + 1] - block.newNumber[i + 2] != 1) || (i + 2 >= blockNumber) ) {
// the shorter one is declared the moved one
if (block.newLength[i] < block.newLength[i + 1]) {
block.newBlock[i] = 1;
block.newBlock[i + 1] = 0;
}
else {
block.newBlock[i] = 0;
block.newBlock[i + 1] = 1;
}
}
}
}
}
// mark all others as moved and number the moved blocks
j = 1;
for (var i = 0; i < blockNumber; i ++) {
if ( (block.newBlock[i] == null) || (block.newBlock[i] == 1) ) {
block.newBlock[i] = j++;
}
}
// check if a block has been moved from this block border
for (var i = 0; i < blockNumber; i ++) {
for (var j = 0; j < blockNumber; j ++) {
if (block.newNumber[j] == i) {
if (block.newBlock[j] > 0) {
// block moved right
if (block.newNumber[j] < j) {
block.newRight[i] = block.newBlock[j];
block.newRightIndex[i] = j;
}
// block moved left
else {
block.newLeft[i + 1] = block.newBlock[j];
block.newLeftIndex[i + 1] = j;
}
}
}
}
}
}
return;
};
// WDiffShortenOutput: remove unchanged parts from final output
// input: the output of WDiffString
// returns: the text with removed unchanged passages indicated by (...)
WDiffShortenOutput = function(diffText) {
// html <br/> to newlines
diffText = diffText.replace(/<br[^>]*>/g, '\n');
// scan for diff html tags
var regExpDiff = /<\w+ class="(\w+)"[^>]*>(.|\n)*?<!--\1-->/g;
var tagStart = [];
var tagEnd = [];
var i = 0;
var found;
while ( (found = regExpDiff.exec(diffText)) != null ) {
// combine consecutive diff tags
if ( (i > 0) && (tagEnd[i - 1] == found.index) ) {
tagEnd[i - 1] = found.index + found[0].length;
}
else {
tagStart[i] = found.index;
tagEnd[i] = found.index + found[0].length;
i ++;
}
}
// no diff tags detected
if (tagStart.length == 0) {
return(wDiffNoChange);
}
// define regexps
var regExpHeading = /\n=+.+?=+ *\n|\n\{\||\n\|\}/g;
var regExpParagraph = /\n\n+/g;
var regExpLine = /\n+/g;
var regExpBlank = /(<[^>]+>)*\s+/g;
// determine fragment border positions around diff tags
var rangeStart = [];
var rangeEnd = [];
var rangeStartType = [];
var rangeEndType = [];
for (var i = 0; i < tagStart.length; i ++) {
var found;
// find last heading before diff tag
var lastPos = tagStart[i] - wDiffHeadingBefore;
if (lastPos < 0) {
lastPos = 0;
}
regExpHeading.lastIndex = lastPos;
while ( (found = regExpHeading.exec(diffText)) != null ) {
if (found.index > tagStart[i]) {
break;
}
rangeStart[i] = found.index;
rangeStartType[i] = 'heading';
}
// find last paragraph before diff tag
if (rangeStart[i] == null) {
lastPos = tagStart[i] - wDiffParagraphBefore;
if (lastPos < 0) {
lastPos = 0;
}
regExpParagraph.lastIndex = lastPos;
while ( (found = regExpParagraph.exec(diffText)) != null ) {
if (found.index > tagStart[i]) {
break;
}
rangeStart[i] = found.index;
rangeStartType[i] = 'paragraph';
}
}
// find line break before diff tag
if (rangeStart[i] == null) {
lastPos = tagStart[i] - wDiffLineBeforeMax;
if (lastPos < 0) {
lastPos = 0;
}
regExpLine.lastIndex = lastPos;
while ( (found = regExpLine.exec(diffText)) != null ) {
if (found.index > tagStart[i] - wDiffLineBeforeMin) {
break;
}
rangeStart[i] = found.index;
rangeStartType[i] = 'line';
}
}
// find blank before diff tag
if (rangeStart[i] == null) {
lastPos = tagStart[i] - wDiffBlankBeforeMax;
if (lastPos < 0) {
lastPos = 0;
}