-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdom-comparator.js
3620 lines (3023 loc) · 101 KB
/
dom-comparator.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
/*!
The MIT License (MIT)
http://opensource.org/licenses/MIT
Copyright (c) 2014 Wingify Software Pvt. Ltd.
http://wingify.com
*/
var VWO = window.VWO || {};
(function(){
var $ = window.vwo_$ || window.$;
var _ = window.vwo__ || window._;
var VWO = window.VWOInjected || window.VWO || {};
/**
* Compare two DOM trees and computes the final result
* of changes.
*
* @class
* @memberOf VWO
*/
VWO.DOMComparator = function (params) {
// clear the DOMNodePool before we begin
VWO.DOMNodePool.clear();
$.extend(true, this, params);
if (this.nodeA) {
this.elA = this.nodeA.el;
}
if (this.nodeB) {
this.elB = this.nodeB.el;
}
/*
Removing extra #text coming as inputs via textarea .
*/
var h, leA = this.elA.length,
leB = this.elB.length;
for (h = 0; h < leA; h++) {
if (this.elA[h].data) {
this.elA.splice(h, 1);
h--;
leA--;
}
}
for (h = 0; h < leB; h++) {
if (this.elB[h].data) {
this.elB.splice(h, 1);
h--;
leB--;
}
}
/*
Striping the nodes and removing extra spaces and line breaks
taking care of all escape characters
*/
var outA = stripNodes($(this.elA).outerHTML());
var outB = stripNodes($(this.elB).outerHTML());
/*
Wrapper method is set to "<him *** </him>"
Because it is not standard as <div> *** </div>
*/
this.nodeA = VWO.DOMNodePool.create({
el: $("<him id='DOMComparisonResult'>" + outA + "</him>").get(0)
});
this.nodeB = VWO.DOMNodePool.create({
el: $("<him id='DOMComparisonResult'>" + outB + "</him>").get(0)
});
this.elAClone = $("<him id='DOMComparisonResult'>" + outA + "</him>").get(0);
};
function stripNodes(node) {
var ans = node.replace(/(?:\r\n|\r|\n)/g, '');
ans = ans.replace(/\>\s+\</g, '><')
var out = '',
l = ans.length,
i = 0;
while (i < l) {
if (ans[i] == '"') {
out += ans[i];
while (1) {
i = i + 1;
if (ans[i] == '"') {
out += ans[i];
break;
}
if ((i + 1) < l && ans[i + 1] == "'") {
out += ans[i];
out += '\\';
continue;
}
if (ans[i] == ' ') {
if (ans[i + 1].search(/[^A-Z0-9a-z\s]/) == -1) {
out += ans[i];
continue;
} else
continue;
}
out += ans[i];
}
} else {
if ((i + 1) < l && ans[i + 1] == "'") {
out += ans[i];
out += '\\';
} else
out += ans[i];
}
i = i + 1;
}
return out;
};
VWO.DOMComparator.create = function (params) {
return new VWO.DOMComparator(params);
};
VWO.DOMComparator.prototype = {
/** @type {Node} First of the two elements to compare. */
elA: null,
/** @type {Node} Second of the two elements to compare. */
elB: null,
/** @type {VWO.DOMNode} First of the two nodes to compare. */
nodeA: null,
/** @type {VWO.DOMNode} Second of the two nodes to compare. */
nodeB: null,
/**
* Uses VWO.DOMMatchFinder to find matches between the two nodes.
* The matches are used to set a 'matchedWith' property on each
* of the nodes in two trees for comparison later. On the nodes in
* the second tree, additional properties 'matchScore' and
* 'matchDifference' are also set.
*/
analyzeMatches: function () {
var matches = VWO.DOMMatchFinder.create({
nodeA: this.nodeA,
nodeB: this.nodeB
}).compare().matches;
var nodeADescendants = this.nodeA.descendants();
var nodeBDescendants = this.nodeB.descendants();
for (var i in matches)
if (matches.hasOwnProperty(i)) {
var j = matches[i];
var comparison = VWO.DOMNodeComparator.create({
nodeA: nodeADescendants[i],
nodeB: nodeBDescendants[j]
});
var matchScore = comparison.finalScore();
if (matchScore) {
nodeADescendants[i].matchedWith = nodeBDescendants[j];
nodeBDescendants[j].matchedWith = nodeADescendants[i];
nodeBDescendants[j].matchScore = comparison.finalScore();
nodeBDescendants[j].matchDifference = comparison.difference();
}
}
},
/**
* Detects newly inserted nodes in the second tree based on nodes
* in the tree that do not have any matches in the initial tree.
*
* @return {Object[][]} A list of inital and final states
* of the operations performed.
*/
detectInserts: function () {
var finalTree = this.nodeB.descendants();
var finalOperationsList = [];
// this loop runs for objects that are newly inserted
_(finalTree).each(function (node, i) {
var adjacentNode, nodeI;
// if node has a match, nothing to do
if (node.matchedWith) return;
// if my parent doesn't have a match, I have already been
// implicitly inserted.
if (node.parent() && !node.parent().matchedWith) return;
// node insertion at node.masterIndex
adjacentNode = null;
// get my match's next sibling who actually has a match
// if found, node = thus found node
// if no such sibling is found,
// node = my match's parent's match
// find
nodeI = node;
while (nodeI = nodeI.nextSibling()) {
if (nodeI.matchedWith) break;
}
// match found
var insertedNode = node.copy();
if (nodeI && nodeI.matchedWith) {
adjacentNode = nodeI.matchedWith;
// adjacentNode.parent().addChildAt(insertedNode, adjacentNode.index());
adjacentNode.parent().addChildAt(insertedNode, node.index());
} else {
adjacentNode = node.parent().matchedWith;
adjacentNode.addChild(insertedNode);
}
var parentSelectorPath = insertedNode.parent().selectorPath();
var indexInParent = insertedNode.index();
// set a flag on the node
insertedNode.isInserted = true;
finalOperationsList.push(({
name: 'insertNode',
// an inserted node does not have a selector path
selectorPath: null,
content: {
html: insertedNode.outerHTML(),
parentSelectorPath: parentSelectorPath,
indexInParent: indexInParent,
existsInDOM: true
}
}));
});
return finalOperationsList;
},
/**
* Detects text nodes that matched in both trees and may have their
* 'textContent' changed.
*
* @return {Object[][]} A list of inital and final states
* of the operations performed.
*/
detectTextNodeChanges: function () {
var initialTree = this.nodeA.descendants();
var finalOperationsList = [];
_(initialTree).each(function (node, i) {
// if node isn't matched, nothing to do (it will be removed in the next iteration)
if (!node.matchedWith) return;
// if node is matched but its a 100% match, nothing to do still,
// rearranges will happen in the next iteration
if (node.matchedWith.matchScore === 1) return;
// finally get the matchDifference (a set of attr and css changes)
var matchDifference = node.matchedWith.matchDifference;
// figure out changes for changeTextBefore, changeCommentBefore
if (node.nodeType() !== Node.ELEMENT_NODE && matchDifference.newInnerText) { // insert new text and comment nodes
var parentSelectorPath = node.parent().selectorPath();
var indexInParent = node.index();
finalOperationsList.push(({
name: 'change' + (node.nodeType() === Node.TEXT_NODE ? 'Text' : 'Comment'),
// a text / comment node does not have a selector path
selectorPath: null,
content: {
text: matchDifference.newInnerText,
parentSelectorPath: parentSelectorPath,
indexInParent: indexInParent
}
}));
node.el.textContent = matchDifference.newInnerText;
}
});
return finalOperationsList;
},
/**
* For all the nodes that matched, detects if the attributes changed.
* If so, returns them.
*
* @return {Object[][]} A list of inital and final states
* of the operations performed.
*/
detectAttributeChanges: function () {
var initialTree = this.nodeA.descendants();
var finalOperationsList = [];
_(initialTree).each(function (node, i) {
// if node isn't matched, nothing to do (it will be removed in the next iteration)
if (!node.matchedWith) return;
// if node is matched but its a 100% match, nothing to do still,
// replacements will happen in the next iteration
if (node.matchedWith.matchScore === 1) return;
// finally get the matchDifference (a set of attr and css changes
var matchDifference = node.matchedWith.matchDifference;
var attr = $.extend({},
matchDifference.addedAttributes,
matchDifference.changedAttributes);
var oldattr;
// Runs over all the attributes for e.g ._(attr).keys() = class
if (_(attr).keys().length) {
oldattr = {};
_(attr).each(function (attr, key) {
oldattr[key] = node.$().attr(key);
});
// node.$().attr('class') = class_name
node.$().attr(attr);
finalOperationsList.push(({
name: 'attr',
selectorPath: node.selectorPath(),
content: attr
}));
}
// Gets the list of all attributes removed
var removedAttr = matchDifference.removedAttributes;
if (_(removedAttr).keys().length) {
oldattr = {};
_(removedAttr).each(function (attr, key) {
oldattr[key] = node.$().attr(key);
node.$().removeAttr(key);
});
finalOperationsList.push(({
name: 'removeAttr',
selectorPath: node.selectorPath(),
content: matchDifference.removedAttributes
}));
}
});
VWO.DOMNodePool.uncacheAll();
VWO.DOMNodePool.cacheAll();
return finalOperationsList;
},
/**
* For all the nodes that matched, detects if the styles changed.
* If so, returns them.
*
* @return {Object[][]} A list of inital and final states
* of the operations performed.
*/
detectStyleChanges: function () {
var initialTree = this.nodeA.descendants();
var finalOperationsList = [];
_(initialTree).each(function (node, i) {
// if node isn't matched, nothing to do (it will be removed in the next iteration)
if (!node.matchedWith) return;
// if node is matched but its a 100% match, nothing to do still,
// replacements will happen in the next iteration
if (node.matchedWith.matchScore === 1) return;
// finally get the matchDifference (a set of attr and css changes
var matchDifference = node.matchedWith.matchDifference;
var css = $.extend({}, matchDifference.addedStyles, matchDifference.changedStyles);
var oldcss;
if (_(css).keys().length) {
oldcss = {};
_(css).each(function (css, key) {
oldcss[key] = node.$().css(key);
});
node.$().css(css);
finalOperationsList.push(({
name: 'css',
selectorPath: node.selectorPath(),
content: css
}));
}
var removedCss = matchDifference.removedStyles;
if (_(removedCss).keys().length) {
oldcss = {};
_(removedCss).each(function (css, key) {
oldcss[key] = node.$().css(key);
node.$().css(key, '');
});
_(removedCss).each(function (css) {
node.$().css(css, '');
});
finalOperationsList.push(({
name: 'removeCss',
selectorPath: node.selectorPath(),
content: removedCss
}));
}
});
VWO.DOMNodePool.uncacheAll();
VWO.DOMNodePool.cacheAll();
return finalOperationsList;
},
/**
* If the position (indexes) of the two matched nodes differs, it is
* detected as a rearrange.
*
* @return {Object[][]} A list of inital and final states
* of the operations performed.
*/
detectRearranges: function () {
var initialTree = this.nodeA.descendants();
var finalOperationsList = [];
_(initialTree).each(function (node, i) {
var adjacentNode, nodeI;
// if the node is not matched with anything, return
// it has already been removed/will be removed in detectRemoves
if (!node.matchedWith) return;
// if my parent has matched with my match's parent, and my index is unchanged, continue.
if (node.parent() && node.parent().matchedWith &&
node.parent().matchedWith === node.matchedWith.parent()) {
if (node.index() === node.matchedWith.index()) return;
// if my index changed, and yet, my next sibling... wtf?
}
// if both me and my match are root nodes without any parent, continue
if (!node.parent() && !node.matchedWith.parent()) return;
// match found, begin replacement
adjacentNode = null;
var oldParentSelectorPath = node.parent().selectorPath();
var oldIndexInParent = node.index();
// get my match's next sibling who actually has a match
// if found, node = thus found node
// if no such sibling is found,
// node = my match's parent's match
nodeI = node.matchedWith;
while (nodeI = nodeI.nextSibling()) {
if (nodeI.matchedWith) break;
}
if (nodeI && nodeI.matchedWith) {
adjacentNode = nodeI.matchedWith;
adjacentNode.parent().addChildAt(node, adjacentNode.index());
} else {
var parentNodeOfMatch = node.matchedWith.parent();
// well what if parent node of match was a new node
// solution: insertion should happen beforehand
// so that new elements in the final dom are actually a part of the
// initial dom.
if (parentNodeOfMatch.matchedWith) {
adjacentNode = parentNodeOfMatch.matchedWith;
} else {
adjacentNode = parentNodeOfMatch;
}
adjacentNode.addChild(node);
}
var parentSelectorPath = node.parent().selectorPath();
var indexInParent = node.index();
finalOperationsList.push(({
name: 'rearrange',
// since text nodes can also be rearranged, selector path
// is always set to null
selectorPath: null,
content: {
parentSelectorPath: parentSelectorPath,
indexInParent: indexInParent,
oldParentSelectorPath: oldParentSelectorPath,
oldIndexInParent: oldIndexInParent,
existsInDOM: true
}
}));
VWO.DOMNodePool.uncacheAll();
VWO.DOMNodePool.cacheAll();
});
return finalOperationsList;
},
/**
* For all nodes in the initial tree that do not have a match in the
* final tree, a remove is detected.
*
* @return {Object[][]} A list of inital and final states
* of the operations performed.
*/
detectRemoves: function () {
var initialTree = this.nodeA.descendants();
var finalOperationsList = [];
_(initialTree).each(function (node, i) {
if (node.matchedWith) return;
// if the node has been just inserted by detectInserts, ignore
if (node.isInserted) return;
// if my parent is removed, i am implicitly removed too.
// removed = !matchedWith
if (node.parent() && !node.parent().matchedWith) return;
var parentSelectorPath = node.parent().selectorPath();
var indexInParent = node.index();
// this node has no match, this should be removed
node.parent().removeChild(node);
finalOperationsList.push(({
name: 'deleteNode',
// a remove operation cannot have a selector path,
// a text node could also be removed
selectorPath: null,
content: {
html: node.outerHTML(),
parentSelectorPath: parentSelectorPath,
indexInParent: indexInParent,
existsInDOM: false
}
}));
});
return finalOperationsList;
},
detectRemovesInB: function () {
var initialTree = this.nodeB.descendants();
var finalOperationsList = [];
_(initialTree).each(function (node, i) {
if (node.matchedWith) return;
// if the node has been just inserted by detectInserts, ignore
if (node.isInserted) return;
// if my parent is removed, i am implicitly removed too.
// removed = !matchedWith
if (node.parent() && !node.parent().matchedWith) return;
var parentSelectorPath = node.parent().selectorPath();
var indexInParent = node.index();
// this node has no match, this should be removed
node.parent().removeChild(node);
finalOperationsList.push(({
name: 'deleteNodeInB',
// a remove operation cannot have a selector path,
// a text node could also be removed
selectorPath: null,
content: {
html: node.outerHTML(),
node: node,
parentSelectorPath: parentSelectorPath,
indexInParent: indexInParent,
existsInDOM: false
}
}));
});
VWO.DOMNodePool.uncacheAll();
VWO.DOMNodePool.cacheAll();
return finalOperationsList;
},
/**
* Finally, verify if the comparison was successful.
* (A console.log message is sent.)
*/
verifyComparison: function () {
console.log('comparison successful: ' + this.nodeA.equals(this.nodeB));
return this.nodeA.equals(this.nodeB);
},
/**
* Assuming the two nodes are set, begin the comparison.
*
* @return {Object[][]} A list of inital and final states
* of the operations performed.
*/
compare: function () {
var self = this;
this.analyzeMatches();
var final_results = [];
var result1 = [
this.detectRemovesInB(),
this.detectRearranges()
];
VWO.DOMNodePool.uncacheAll();
VWO.DOMNodePool.cacheAll();
result1 = _(result1).flatten();
function getActualIndex(parentSelectorPath, indexInParent) {
var parentNode = VWO.DOMNode.create({
el: self.nodeB.el.parentNode.querySelector(parentSelectorPath)
});
if (indexInParent < 0)
return -1;
var childNode = parentNode.children()[indexInParent];
return Array.prototype.slice.apply(parentNode.el.childNodes).indexOf(childNode.el);
};
var output = [],
index, path, html, text, val, attr, css, index1, index2, path1, path2;
var l = result1.length;
for (var i = (l - 1); i >= 0; i--) {
var op = result1[i];
if (op.name == 'deleteNodeInB') {
index = getActualIndex(op.content.parentSelectorPath, op.content.indexInParent - 1);
path = op.content.parentSelectorPath.split('DOMComparisonResult > ')[1];
if (!path)
path = op.content.parentSelectorPath;
html = op.content.html;
if (index == -1)
output[i] = '$(' + JSON.stringify(path) + ').prepend(' + JSON.stringify(html) + ');';
else
output[i] = '$($(' + JSON.stringify(path) + ').get(0).childNodes[' + index + ']).after(' + JSON.stringify(html) + ');';
var ctx = self.nodeB.el;
var $ = function (selector) {
if (selector == "HIM#DOMComparisonResult")
return jQuery(ctx);
return jQuery(selector, ctx);
};
eval(output[i]);
} else
final_results.push(result1[i]);
}
VWO.DOMNodePool.uncacheAll();
VWO.DOMNodePool.cacheAll();
var result = [
this.detectInserts(),
this.detectRemoves(),
this.detectTextNodeChanges(),
this.detectAttributeChanges(),
this.detectStyleChanges(),
];
result = _(result).flatten();
var le = result.length;
for (i = 0; i < le; i++)
final_results.push(result[i]);
console.log(final_results);
this.verifyComparison();
result.toJqueryCode = function toJqueryCode() {
function getActualIndex(parentSelectorPath, indexInParent) {
var parentNode = VWO.DOMNode.create({
el: self.elAClone.parentNode.querySelector(parentSelectorPath)
});
if (indexInParent < 0)
return -1;
var childNode = parentNode.children()[indexInParent];
return Array.prototype.slice.apply(parentNode.el.childNodes).indexOf(childNode.el);
}
var output = [],
index, path, html, text, val, attr, css, index1, index2, path1, path2;
for (var i = 0, l = this.length; i < l; i++) {
var op = this[i];
switch (op.name) {
case 'insertNode':
index = getActualIndex(op.content.parentSelectorPath, op.content.indexInParent - 1);
path = op.content.parentSelectorPath.split('DOMComparisonResult > ')[1];
html = op.content.html;
if (index == -1)
output[i] = '$(' + JSON.stringify(path) + ').append(' + JSON.stringify(html) + ');';
else
output[i] = '$($(' + JSON.stringify(path) + ').get(0).childNodes[' + index + ']).after(' + JSON.stringify(html) + ');';
break;
case 'rearrange':
index1 = getActualIndex(op.content.parentSelectorPath, op.content.indexInParent - 1);
index2 = getActualIndex(op.content.oldParentSelectorPath, op.content.oldIndexInParent);
path1 = op.content.parentSelectorPath.split('DOMComparisonResult > ')[1];
path2 = op.content.oldParentSelectorPath.split('DOMComparisonResult > ')[1];
var node = '$(' + path2 + ').get(0).childNodes[' + index2 + ']';
if (index1 == -1)
output[i] = '$(' + JSON.stringify(path1) + ').append(' + node + ');';
else
output[i] = '$($(' + JSON.stringify(path1) + ').get(0).childNodes[' + index1 + ']).after(' + node + ');';
case 'deleteNode':
index = getActualIndex(op.content.parentSelectorPath, op.content.indexInParent);
path = op.content.parentSelectorPath.split('DOMComparisonResult > ')[1];
html = op.content.html;
output[i] = '$($(' + JSON.stringify(path) + ').get(0).childNodes[' + index + ']).remove();';
break;
case 'changeText':
case 'changeComment':
index = getActualIndex(op.content.parentSelectorPath, op.content.indexInParent);
path = op.content.parentSelectorPath.split('DOMComparisonResult > ')[1];
text = op.content.text;
output[i] = '$($(' + JSON.stringify(path) + ').get(0).childNodes[' + index + ']).remove();';
var ctx = self.elAClone;
var $ = function (selector) {
return jQuery(selector, ctx);
};
eval(output[i]);
output[i] = '$(' + JSON.stringify(path) + ').append(' + JSON.stringify(text) + ');';
break;
case 'attr':
case 'css':
path = op.selectorPath.split('DOMComparisonResult > ')[1];
val = op.content;
output[i] = '$(' + JSON.stringify(path) + ').' + op.name + '(' + JSON.stringify(val) + ');';
break;
case 'removeAttr':
path = op.selectorPath.split('DOMComparisonResult > ')[1];
attr = Object.keys(op.content);
output[i] = '$(' + JSON.stringify(path) + ')' + attr.map(function (attr) {
return '.removeAttr(' + JSON.stringify(attr) + ')';
}).join('') + ';';
break;
case 'removeCss':
path = op.selectorPath.split('DOMComparisonResult > ')[1];
css = Object.keys(op.content);
output[i] = '$(' + JSON.stringify(path) + ')' + css.map(function (css) {
return '.css(' + JSON.stringify(css) + ', "")';
}).join('') + ';';
break;
}
var ctx = self.elAClone;
var $ = function (selector) {
return jQuery(selector, ctx);
};
eval(output[i]);
}
return self.elAClone;
};
return final_results;
}
};
})();
/*!
The MIT License (MIT)
http://opensource.org/licenses/MIT
Copyright (c) 2014 Wingify Software Pvt. Ltd.
http://wingify.com
*/
var VWO = window.VWO || {};
(function(){
var $ = window.vwo_$ || window.$;
var _ = window.vwo__ || window._;
var VWO = window.VWOInjected || window.VWO || {};
/**
* A class to find matches in two DOM trees. After comparison,
* a key-value pair of master indexes in initial and final node
* are set in the 'matches' property.
*
* @class
* @memberOf VWO
*/
VWO.DOMMatchFinder = function (params) {
$.extend(true, this, params);
};
VWO.DOMMatchFinder.create = function (params) {
return new VWO.DOMMatchFinder(params);
};
VWO.DOMMatchFinder.prototype = {
/**
* The inital DOM tree.
* @type {VWO.DOMNode}
*/
nodeA: null,
/**
* The final DOM tree.
* @type {[type]}
*/
nodeB: null,
/**
* Post comparison, this property is populated with
* a key value pair of master indexes in initial tree
* to the respective matches in the final tree.
* @type {Object}
*/
matches: null,
/**
* Returns the outerHTML of the initial DOM tree. Carriage
* returns are removed from the returned strings.
* @return {String}
*/
stringA: function () {
return this.nodeA.outerHTML().replace(/\r\n|\r|\n/gi, "\n");
},
/**
* Returns the outerHTML of the final DOM tree. Carriage
* returns are removed from the returned strings.
* @return {String}
*/
stringB: function () {
return this.nodeB.outerHTML().replace(/\r\n|\r|\n/gi, "\n");
},
/**
* Initiate the comparison process. After the comparison is done,
* the result is set in the 'matches' property on this object.
*
* @return {self}
*/
compare: function () {
var stringA1 = this.stringA();
var stringB1 = this.stringB();
// Storing the original string ...
var stringA_original = this.stringA();
var stringB_original = this.stringB();
var splitOn = '\n';
/*
Manually removing the comments in both strings ..
*/
var i, A_len = stringA1.length,
B_len = stringB1.length;
var start_comment_flag = 0;
var stringA = '',
stringB = '';
for (i = 0; i < A_len; i++) {
if ((i + 3) < A_len && stringA1[i] == '<' && stringA1[i + 1] == '!' && stringA1[i + 2] == '-' && stringA1[i + 3] == '-') {
start_comment_flag = 1;
i = i + 3;
continue;
}
if ((i + 2) < A_len && start_comment_flag == 1 && stringA1[i] == '-' && stringA1[i + 1] == '-' && stringA1[i + 2] == '>') {
start_comment_flag = 0;
i = i + 2;
continue;
}
if (start_comment_flag == 0)
stringA += stringA1[i];
}
start_comment_flag = 0;
for (i = 0; i < B_len; i++) {
if ((i + 3) < B_len && stringB1[i] == '<' && stringB1[i + 1] == '!' && stringB1[i + 2] == '-' && stringB1[i + 3] == '-') {
start_comment_flag = 1;
i = i + 3;
continue;
}
if ((i + 2) < B_len && start_comment_flag == 1 && stringB1[i] == '-' && stringB1[i + 1] == '-' && stringB1[i + 2] == '>') {
start_comment_flag = 0;
i = i + 2;
continue;
}
if (start_comment_flag == 0)
stringB += stringB1[i];
}
var f = function (i) {
return i < 10 ? " " + i : i.toString();
};
var couA = 0,
couB = 0;
var result = VWO.StringComparator.create({
stringA: stringA,
stringB: stringB,
matchA: {},
matchB: {},
couA: 0,
couB: 0,
ignoreA: [],
ignoreB: [],
splitOn: splitOn
}).compare();
var diffUnion = result.diffUnion;
/*
for (var i = 0; i < diffUnion.length; i++) {
var diff = diffUnion[i];
console.log(
diff.indexInA < 0 ? ' +' : f(diff.indexInA),
diff.indexInB < 0 ? ' -' : f(diff.indexInB),
diff.string
);
}
*/
/*
for strings unchanged ... string pointer has been apllied for each unchanged node
*/
var nodeMatches = {};
var stringsInA = result.stringsInA;
var stringsInB = result.stringsInB;
for (i = 0; i < result.stringsUnchanged.length; i++) {
var string = result.stringsUnchanged[i];
var indexInA = string.indexInA;
var indexInB = string.indexInB;
var pointers = VWO.DOMNodeStringPointer.create({
haystack: (indexInA > 0 ? splitOn : '') +
stringsInA[indexInA] +
(indexInA < stringsInA.length - 1 ? splitOn : '')
}).allNodePointers();
var startIndexInA = stringsInA.slice(0, indexInA).join(splitOn).length - 1;
var startIndexInB = stringsInB.slice(0, indexInB).join(splitOn).length - 1;
startIndexInA = startIndexInA.clamp(0);
startIndexInB = startIndexInB.clamp(0);
// Defining num for one letter isssue ... one letter in the text node was not considered as a word ...
var num;
for (var j = 0, jl = pointers.length; j < jl; j++) {
var pointerInA, pointerInB;
if ((j + 1) < jl && (pointers[j + 1].index - pointers[j].index) == 1)
num = 0;
else
num = splitOn.length;
pointerInA = VWO.DOMNodeStringPointer.create({
index: startIndexInA + pointers[j].index + num,
haystack: stringA
});
pointerInB = VWO.DOMNodeStringPointer.create({
index: startIndexInB + pointers[j].index + num,
haystack: stringB
});
nodeMatches[pointerInA.masterIndex()] = pointerInB.masterIndex();
}
}
/*
for all the changed nodes , they are compared using the string comparator ..